jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Hovemeyer <dav...@cs.umd.edu>
Subject Re: how to get the type info from this small code snippet?
Date Wed, 26 Jan 2005 19:31:34 GMT
On Wed, Jan 26, 2005 at 12:12:32PM -0700, Hiller, Dean D (Dean) wrote:
>  
> 
> package mod.fieldrefdepend;
> import mod.dummy.DummyClass;
> public class ClassDependsOnReferenceInFieldDeclaration {
>             private Class c = DummyClass.class; 
> }
> 
> Anyone know how to process the ConstantPool or the instruction code to
> find out DummyClass.class is used in this context(ie. Class c =
> DummyClass.class).  I don't care about other contexts.

If the code was generated by javac from JDK 1.5, just look
for a LDC instruction that refers to a Constant_Class.

I believe that for javac from JDK 1.4 and earlier, SomeClass.class
expressions are compiled to

 - check a synthetic static field which caches the Class
   object

 - if the field contains null, call a synthetic static method
   (called "class$", judging from the code you posted),
   passing the name of the class as a param.  The return value
   of this method is the Class object.

So, I think you can find out what classes are involved in
SomeClass.class expressions by scanning method bytecode for sequences of

  LDC "com.foobar.SomeClass"
  INVOKESTATIC "java.lang.Class class$(java.lang.String)"

You will probably want to use ConstantPoolGen/ClassGen/MethodGen
to produce an InstructionList for each method, unless you want
to write code to disassemble the actual bytecodes.  As a side
benefit, these classes make it easier to extract constants from
the constant pool (e.g., symbolic information for particular
instructions).

Other compilers (e.g., jikes, Eclipse) may generate different
bytecode.

Hope that helps,

-Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Mime
View raw message