You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by James CE Johnson <jc...@tragus.org> on 2003/08/05 19:56:56 UTC

New bcel user question

Hi,

I'm trying to use bcel to analyze a class to see what other classes it
references.

Let's say I have a class like this:

class Foo
{
  public void foo()
  {
    Class c = some.packaged.ClassFile;
    ...
  }
}

In my "analyzis" of Foo I need to know that it references
some.packaged.ClassFile. I can't quite figure out how to do that.

Suggestions?

Thanks,
James


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


Re: New bcel user question

Posted by James CE Johnson <jc...@tragus.org>.
> James CE Johnson wrote:
>> I think I'm trying to reinvent what you did :-) I may be able to use
>> your analyzer directly.
>
> Suppose there was the APL at the beginning of the file...
>
>> What I want to do is start at a particular object in
>> our model tier and find all of the classes that use that. Then all of
>> the classes that use those. And so on and so fort. It should
>> eventually terminate at our view tier. If it doesn't then we've
>> managed to orphan something.
>
> Watch out for the traps. The analyzer doesn't do a real flow
> analysis, therefore you could have orphaned classes without
> really noting it (instances created only by unreachable code).
> Furthermore, object instances may be created through reflection.
> I tried to solve this by adding all subclasses but I already
> found that something goes amiss, perhaps because the optimizer
> inlines stuff or omits no-op code.

Yea, I don't expect to have something that'll perfectly find all orphans.
If it'll just give me a handful of starting points I'll be much further
along than I am.

Using your code I've narrowed this:
        Class foo = Synthetic.class;
down to a getstatic opcode.

Re: New bcel user question

Posted by James CE Johnson <jc...@tragus.org>.
> James CE Johnson wrote:
>> I think I'm trying to reinvent what you did :-) I may be able to use
>> your analyzer directly.
>
> Suppose there was the APL at the beginning of the file...
>
>> What I want to do is start at a particular object in
>> our model tier and find all of the classes that use that. Then all of
>> the classes that use those. And so on and so fort. It should
>> eventually terminate at our view tier. If it doesn't then we've
>> managed to orphan something.
>
> Watch out for the traps. The analyzer doesn't do a real flow
> analysis, therefore you could have orphaned classes without
> really noting it (instances created only by unreachable code).
> Furthermore, object instances may be created through reflection.
> I tried to solve this by adding all subclasses but I already
> found that something goes amiss, perhaps because the optimizer
> inlines stuff or omits no-op code.

Yea, I don't expect to have something that'll perfectly find all orphans.
If it'll just give me a handful of starting points I'll be much further
along than I am.

Using your code I've narrowed this:
        Class foo = Synthetic.class;
down to a getstatic opcode.

>From that I've used this code:

ConstantCP ccp =
  (ConstantCP) cp.getConstant((hi << 8) + lo);
String className =
  cp.constantToString(ccp.getClassIndex(),
                      Constants.CONSTANT_Class
                      );

ConstantNameAndType cnt =
 (ConstantNameAndType) cp.getConstant(ccp.getNameAndTypeIndex());

System.err.println("+>"+cnt.getName(cp));
System.err.println("+>"+cnt.getSignature(cp));
System.err.println(">>"+Utility.signatureToString(cnt.getSignature(cp))+"<<");

Which tells me:

+>class$org$apache$bcel$classfile$Synthetic
+>Ljava/lang/Class;
>>Class<<

I'm stumpped at converting 'class$org$apache$bcel$classfile$Synthetic' to
an actual class name. I'm sure there's something magic on Utility that
I've just not found yet. (After all, I'd barely heard of bcel before this
morning...)
>
> J.Pietschmann
>
>


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


Re: New bcel user question

Posted by "J.Pietschmann" <j3...@yahoo.de>.
James CE Johnson wrote:
> I think I'm trying to reinvent what you did :-) I may be able to use your
> analyzer directly.

Suppose there was the APL at the beginning of the file...

> What I want to do is start at a particular object in
> our model tier and find all of the classes that use that. Then all of the
> classes that use those. And so on and so fort. It should eventually
> terminate at our view tier. If it doesn't then we've managed to orphan
> something.

Watch out for the traps. The analyzer doesn't do a real flow
analysis, therefore you could have orphaned classes without
really noting it (instances created only by unreachable code).
Furthermore, object instances may be created through reflection.
I tried to solve this by adding all subclasses but I already
found that something goes amiss, perhaps because the optimizer
inlines stuff or omits no-op code.

J.Pietschmann



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


Re: New bcel user question

Posted by "J.Pietschmann" <j3...@yahoo.de>.
James CE Johnson wrote:
> I think I'm trying to reinvent what you did :-) I may be able to use your
> analyzer directly.

Suppose there was the APL at the beginning of the file...

> What I want to do is start at a particular object in
> our model tier and find all of the classes that use that. Then all of the
> classes that use those. And so on and so fort. It should eventually
> terminate at our view tier. If it doesn't then we've managed to orphan
> something.

Watch out for the traps. The analyzer doesn't do a real flow
analysis, therefore you could have orphaned classes without
really noting it (instances created only by unreachable code).
Furthermore, object instances may be created through reflection.
I tried to solve this by adding all subclasses but I already
found that something goes amiss, perhaps because the optimizer
inlines stuff or omits no-op code.

J.Pietschmann



Re: New bcel user question

Posted by James CE Johnson <jc...@tragus.org>.
> James CE Johnson wrote:
>> I'm trying to use bcel to analyze a class to see what other classes it
>> references.
> ...
>> In my "analyzis" of Foo I need to know that it references
>> some.packaged.ClassFile. I can't quite figure out how to do that.
>
> It depends on how you define "reference", apart from the example
> you gave.

I think I'm trying to reinvent what you did :-) I may be able to use your
analyzer directly. What I want to do is start at a particular object in
our model tier and find all of the classes that use that. Then all of the
classes that use those. And so on and so fort. It should eventually
terminate at our view tier. If it doesn't then we've managed to orphan
something.

>
> I did it the hard way: get the byte code of a class, iterate and
> note references manually, see the BCEL analyzer from
>   http://cvs.apache.org/~pietsch/
>
> J.Pietschmann
>
>
> --------------------------------------------------------------------- To
> unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: New bcel user question

Posted by James CE Johnson <jc...@tragus.org>.
> James CE Johnson wrote:
>> I'm trying to use bcel to analyze a class to see what other classes it
>> references.
> ...
>> In my "analyzis" of Foo I need to know that it references
>> some.packaged.ClassFile. I can't quite figure out how to do that.
>
> It depends on how you define "reference", apart from the example
> you gave.

I think I'm trying to reinvent what you did :-) I may be able to use your
analyzer directly. What I want to do is start at a particular object in
our model tier and find all of the classes that use that. Then all of the
classes that use those. And so on and so fort. It should eventually
terminate at our view tier. If it doesn't then we've managed to orphan
something.

>
> I did it the hard way: get the byte code of a class, iterate and
> note references manually, see the BCEL analyzer from
>   http://cvs.apache.org/~pietsch/
>
> J.Pietschmann
>
>
> --------------------------------------------------------------------- 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


Re: New bcel user question

Posted by "J.Pietschmann" <j3...@yahoo.de>.
James CE Johnson wrote:
> I'm trying to use bcel to analyze a class to see what other classes it
> references.
...
> In my "analyzis" of Foo I need to know that it references
> some.packaged.ClassFile. I can't quite figure out how to do that.

It depends on how you define "reference", apart from the example
you gave.

I did it the hard way: get the byte code of a class, iterate and
note references manually, see the BCEL analyzer from
  http://cvs.apache.org/~pietsch/

J.Pietschmann


Re: New bcel user question

Posted by "J.Pietschmann" <j3...@yahoo.de>.
James CE Johnson wrote:
> I'm trying to use bcel to analyze a class to see what other classes it
> references.
...
> In my "analyzis" of Foo I need to know that it references
> some.packaged.ClassFile. I can't quite figure out how to do that.

It depends on how you define "reference", apart from the example
you gave.

I did it the hard way: get the byte code of a class, iterate and
note references manually, see the BCEL analyzer from
  http://cvs.apache.org/~pietsch/

J.Pietschmann


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


Re: New bcel user question

Posted by David Hovemeyer <da...@cs.umd.edu>.
On Tue, Aug 05, 2003 at 01:56:56PM -0400, James CE Johnson wrote:
> I'm trying to use bcel to analyze a class to see what other classes it
> references.

You might want to take a look at examples/TransitiveHull.java in
the BCEL source distribution.  I think it does more or less what
you want.

-Dave

Re: New bcel user question

Posted by David Hovemeyer <da...@cs.umd.edu>.
On Tue, Aug 05, 2003 at 01:56:56PM -0400, James CE Johnson wrote:
> I'm trying to use bcel to analyze a class to see what other classes it
> references.

You might want to take a look at examples/TransitiveHull.java in
the BCEL source distribution.  I think it does more or less what
you want.

-Dave

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