Class: BiteScript::ASM::ClassMirror

Inherits:
Object
  • Object
show all
Includes:
Annotated, Generics, Modifiers
Defined in:
lib/bitescript/mirror.rb,
lib/bitescript/asm3/mirror.rb

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Attributes included from Modifiers

#flags

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Generics

#inspect_generic, #inspect_type

Methods included from Modifiers

add_modifier

Methods included from Annotated

#addAnnotation, #annotations, #declaredAnnotations, #getDeclaredAnnotation, #inspect_annotations

Constructor Details

#initialize(type, flags) ⇒ ClassMirror

Returns a new instance of ClassMirror.



189
190
191
192
193
194
195
196
197
# File 'lib/bitescript/mirror.rb', line 189

def initialize(type, flags)
  super()
  @type = type
  @flags = flags
  @methods = Hash.new {|h, k| h[k] = {}}
  @constructors = {}
  @fields = {}
  @interfaces = []
end

Instance Attribute Details

#interfacesObject (readonly)

Returns the value of attribute interfaces.



186
187
188
# File 'lib/bitescript/mirror.rb', line 186

def interfaces
  @interfaces
end

#signatureObject

Returns the value of attribute signature.



187
188
189
# File 'lib/bitescript/mirror.rb', line 187

def signature
  @signature
end

#superclassObject

Returns the value of attribute superclass.



187
188
189
# File 'lib/bitescript/mirror.rb', line 187

def superclass
  @superclass
end

#typeObject (readonly)

Returns the value of attribute type.



186
187
188
# File 'lib/bitescript/mirror.rb', line 186

def type
  @type
end

Class Method Details

.for_name(name) ⇒ Object



212
213
214
# File 'lib/bitescript/mirror.rb', line 212

def self.for_name(name)
  load(name)
end

.load(name_or_bytes) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/bitescript/mirror.rb', line 199

def self.load(name_or_bytes)
  builder = BiteScript::ASM::ClassMirror::Builder.new
  if name_or_bytes.kind_of?(String)
    classname = name_or_bytes.tr('.', '/') + ".class"
    stream = JRuby.runtime.jruby_class_loader.getResourceAsStream(
        classname)
    raise NameError, "Class '#{name_or_bytes}' not found." unless stream
    name_or_bytes = stream
  end
  BiteScript::ASM::ClassReader.new(name_or_bytes).accept(builder, 3)
  builder.mirror
end

Instance Method Details

#addConstructor(constructor) ⇒ Object



224
225
226
# File 'lib/bitescript/mirror.rb', line 224

def addConstructor(constructor)
  @constructors[constructor.parameters] = constructor
end

#addField(field) ⇒ Object



284
285
286
# File 'lib/bitescript/mirror.rb', line 284

def addField(field)
  @fields[field.name] = field
end

#addMethod(method) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/bitescript/mirror.rb', line 243

def addMethod(method)
  # TODO this is a hack to fix resolution of covariant returns.
  # We should properly support methods that only differ by return type.
  return if method.synthetic?
  type_names = method.argument_types.map {|type| type.descriptor}
  if method.name == '<init>'
    @constructors[type_names] = method
  else
    @methods[method.name][type_names] = method
  end
end

#generic_interfacesObject



296
297
298
# File 'lib/bitescript/mirror.rb', line 296

def generic_interfaces
  signature.interfaces if signature
end

#generic_superclassObject



292
293
294
# File 'lib/bitescript/mirror.rb', line 292

def generic_superclass
  signature.superclass if signature
end

#getConstructor(*arg_types) ⇒ Object



216
217
218
# File 'lib/bitescript/mirror.rb', line 216

def getConstructor(*arg_types)
  @constructors[arg_types]
end

#getConstructorsObject



220
221
222
# File 'lib/bitescript/mirror.rb', line 220

def getConstructors
  @constructors.values
end

#getDeclaredFieldsObject



280
281
282
# File 'lib/bitescript/mirror.rb', line 280

def getDeclaredFields
  @fields.values
end

#getDeclaredMethod(name, *args) ⇒ Object



228
229
230
231
232
233
# File 'lib/bitescript/mirror.rb', line 228

def getDeclaredMethod(name, *args)
  if args[0].kind_of?(Array)
    args = args[0]
  end
  @methods[name][args]
end

#getDeclaredMethods(name = nil) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/bitescript/mirror.rb', line 235

def getDeclaredMethods(name=nil)
  if name
    @methods[name].values
  else
    @methods.values.map {|m| m.values}.flatten
  end
end

#getField(name) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bitescript/mirror.rb', line 255

def getField(name)
  field   = @fields[name]
  field ||= begin 
              ifaces = interfaces.map do |i|
                wrap_it i
              end
              ifaces.map{|i| i.getField(name) }.compact.first
            end
  field ||= begin
              s = wrap_it superclass
              f, s = [s.getField(name), wrap_it(s.superclass)] until f || s.nil?
              f
            end
  field
end

#inspectObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/bitescript/mirror.rb', line 300

def inspect
  if annotation?
    kind = "@interface"
  elsif interface?
    kind = "interface"
  elsif enum?
    kind = "enum"
  else
    kind = "class"
  end
  if superclass && !enum? && !interface?
    extends = "extends #{inspect_generic(superclass, generic_superclass)} "
  end
  if self.interfaces && !self.interfaces.empty?
    interfaces = (self.generic_interfaces || self.interfaces).map do |i|
      inspect_type(i)
    end.join(', ')
    if interface?
      extends = "extends #{interfaces} "
    else
      implements = "implements #{interfaces} "
    end
  end
end

#type_parametersObject



288
289
290
# File 'lib/bitescript/mirror.rb', line 288

def type_parameters
  signature.type_parameters if signature
end

#wrap_it(i) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/bitescript/mirror.rb', line 271

def wrap_it i
  return unless i
  if i.respond_to?(:getField)
    i
  else
    ClassMirror.for_name i.class_name
  end
end