jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hiller, Dean D \(Dean\)" <dhil...@avaya.com>
Subject RE: how to get the type info from this small code snippet?
Date Wed, 26 Jan 2005 19:57:23 GMT
I am a little confused on scanning for this
  LDC "com.foobar.SomeClass"
  INVOKESTATIC "java.lang.Class class$(java.lang.String)"

The closest thing I see is this
         instr=aload_0[42](1)
         instr=invokestatic[184](3) 1

I think I tried to get the type info from the aload_0 and failed.  How
do I get aload_0, type info.  I think I was using the visitor stuff but
might have overloaded the wrong method...not sure.
Thanks,
dean

-----Original Message-----
From: David Hovemeyer [mailto:daveho@cs.umd.edu] 
Sent: Wednesday, January 26, 2005 12:32 PM
To: BCEL Users List
Subject: Re: how to get the type info from this small code snippet?

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



---------------------------------------------------------------------
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