About Reflection (or not)  
Author Message
pek





PostPosted: 2007-10-26 7:52:00 Top

java-programmer, About Reflection (or not) Hello everyone..
Here I am again for another question.. :P
Suppose we have the following class

public class Test {
private Test2 test2 = new Test2();
public void testMethod() {
test2.test2Method();
}
}

Is there a way, using anything (probably reflection) to know that the
method testMethod of this class calls test2Method() method of Test2
class..? Or the other way around.. Is there a way to know what classes
call Test2's method test2Method()..?

Any help/reference/book etc. is appreciated.
Thank you very much.

 
Joshua Cranmer





PostPosted: 2007-10-26 8:01:00 Top

java-programmer >> About Reflection (or not) pek wrote:
> Is there a way, using anything (probably reflection) to know that the
> method testMethod of this class calls test2Method() method of Test2
> class..?

You could look at the bytecode and deduce for yourself. Check out the
Java VM spec if you want to do manual processing.

> Or the other way around.. Is there a way to know what classes
> call Test2's method test2Method()..?

Not without static analysis, if you don't want to actually run the
program and see.

Note that these can be at best heuristics, since anything with a
Method.invoke() can invalidate number one and the second option has its
own problems.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Daniel Pitts





PostPosted: 2007-10-26 8:30:00 Top

java-programmer >> About Reflection (or not) pek wrote:
> Hello everyone..
> Here I am again for another question.. :P
> Suppose we have the following class
>
> public class Test {
> private Test2 test2 = new Test2();
> public void testMethod() {
> test2.test2Method();
> }
> }
>
> Is there a way, using anything (probably reflection) to know that the
> method testMethod of this class calls test2Method() method of Test2
> class..? Or the other way around.. Is there a way to know what classes
> call Test2's method test2Method()..?
>
> Any help/reference/book etc. is appreciated.
> Thank you very much.
>
Reflection wouldn't work. In general, the only way to find this out is
to build an index of the calls to test2Method. This index cannot ever
be complete because a new class that hasn't yet been created could
eventually call that method.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
 
pek





PostPosted: 2007-10-26 8:30:00 Top

java-programmer >> About Reflection (or not) On Oct 26, 3:01 am, Joshua Cranmer <email***@***.com> wrote:
> pek wrote:
> > Is there a way, using anything (probably reflection) to know that the
> > method testMethod of this class calls test2Method() method of Test2
> > class..?
>
> You could look at the bytecode and deduce for yourself. Check out the
> Java VM spec if you want to do manual processing.

Hmmm.. I believe that could be pretty hard.. ;)

>
> > Or the other way around.. Is there a way to know what classes
>
> > call Test2's method test2Method()..?
>
> Not without static analysis, if you don't want to actually run the
> program and see.
>

I have no problem trying out static analysis (I use a couple of them -
FindBugs, TPTP etc.). But I can't find anywhere (actually I don't know
what exactly should I look for) on how to create an app that actually
does static analysis. Is it basically a program that uses the
reflection api? Or is it something more than that?

> Note that these can be at best heuristics, since anything with a
> Method.invoke() can invalidate number one and the second option has its
> own problems.
>

Sorry, but I didn't quite understood that.

> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

Thank you very much.

 
 
Daniel Pitts





PostPosted: 2007-10-26 8:34:00 Top

java-programmer >> About Reflection (or not) Daniel Pitts wrote:
> pek wrote:
>> Hello everyone..
>> Here I am again for another question.. :P
>> Suppose we have the following class
>>
>> public class Test {
>> private Test2 test2 = new Test2();
>> public void testMethod() {
>> test2.test2Method();
>> }
>> }
>>
>> Is there a way, using anything (probably reflection) to know that the
>> method testMethod of this class calls test2Method() method of Test2
>> class..? Or the other way around.. Is there a way to know what classes
>> call Test2's method test2Method()..?
>>
>> Any help/reference/book etc. is appreciated.
>> Thank you very much.
>>
> Reflection wouldn't work. In general, the only way to find this out is
> to build an index of the calls to test2Method. This index cannot ever
> be complete because a new class that hasn't yet been created could
> eventually call that method.
>
>
If you want to see references within one codebase, there are tools to do
that with static analysis. Personally, I use IntellI IDEA, and it has a
"find references" tool. There may be ones that don't come with a
commercial IDE.

Another tool that may help is opengrok. opengrok will build a search
index based on your codebase. Its quite useful in my opinion.

HTH,
Daniel.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
 
pek





PostPosted: 2007-10-26 9:02:00 Top

java-programmer >> About Reflection (or not) On Oct 26, 3:33 am, Daniel Pitts
<email***@***.com> wrote:
> Daniel Pitts wrote:
> > pek wrote:
> >> Hello everyone..
> >> Here I am again for another question.. :P
> >> Suppose we have the following class
>
> >> public class Test {
> >> private Test2 test2 = new Test2();
> >> public void testMethod() {
> >> test2.test2Method();
> >> }
> >> }
>
> >> Is there a way, using anything (probably reflection) to know that the
> >> method testMethod of this class calls test2Method() method of Test2
> >> class..? Or the other way around.. Is there a way to know what classes
> >> call Test2's method test2Method()..?
>
> >> Any help/reference/book etc. is appreciated.
> >> Thank you very much.
>
> > Reflection wouldn't work. In general, the only way to find this out is
> > to build an index of the calls to test2Method. This index cannot ever
> > be complete because a new class that hasn't yet been created could
> > eventually call that method.
>
> If you want to see references within one codebase, there are tools to do
> that with static analysis. Personally, I use IntellI IDEA, and it has a
> "find references" tool. There may be ones that don't come with a
> commercial IDE.
>
> Another tool that may help is opengrok. opengrok will build a search
> index based on your codebase. Its quite useful in my opinion.
>
> HTH,
> Daniel.
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

OK, let me tell you about my idea.
I basically want to create an annotation system for threading. For
example, you have a method that you know it creates a thread, so you
annotate it with @ThreadStarter. That thread will call another class's
method. You have to make sure that everything the method does is
thread safe in that other class. So eventually you know that method is
thread safe. Now you annotate that method as @ThreadSafe. What I want
basically to do is create a system that would lookup all the
@ThreadStarter methods and look inside those methods what other
methods they call. If a method they call isn't annotated as
@ThreadSafe, then the system will generate an error/warning etc.

I know that Reflection will help me in some way. Annotation will too.
I also found out about apt (Annotation Processing Tool). But I can't
find a way to find out what other methods a method calls. I also can't
find a desent tutorial/documentation/help/examples about the apt.
Eclipse has a wonderful plugin for adding your own apt jar and it
works just like a jvm! It throws warnings/errors/etc. while writing
code. It's pretty incredible and really want to find out how to do
this.

I also heard about a JSR exactly for this reason. JSR-305 (http://
groups.google.com/group/jsr-305). One of the creators is Brian Goetz
(developer of FindBugs and an incredible "researcher" of concurrency).

So, any help? :S

Thanks again.

 
 
Daniel Pitts





PostPosted: 2007-10-26 9:06:00 Top

java-programmer >> About Reflection (or not) pek wrote:
> On Oct 26, 3:33 am, Daniel Pitts
> <email***@***.com> wrote:
>> Daniel Pitts wrote:
>>> pek wrote:
>>>> Hello everyone..
>>>> Here I am again for another question.. :P
>>>> Suppose we have the following class
>>>> public class Test {
>>>> private Test2 test2 = new Test2();
>>>> public void testMethod() {
>>>> test2.test2Method();
>>>> }
>>>> }
>>>> Is there a way, using anything (probably reflection) to know that the
>>>> method testMethod of this class calls test2Method() method of Test2
>>>> class..? Or the other way around.. Is there a way to know what classes
>>>> call Test2's method test2Method()..?
>>>> Any help/reference/book etc. is appreciated.
>>>> Thank you very much.
>>> Reflection wouldn't work. In general, the only way to find this out is
>>> to build an index of the calls to test2Method. This index cannot ever
>>> be complete because a new class that hasn't yet been created could
>>> eventually call that method.
>> If you want to see references within one codebase, there are tools to do
>> that with static analysis. Personally, I use IntellI IDEA, and it has a
>> "find references" tool. There may be ones that don't come with a
>> commercial IDE.
>>
>> Another tool that may help is opengrok. opengrok will build a search
>> index based on your codebase. Its quite useful in my opinion.
>>
>> HTH,
>> Daniel.
>>
>> --
>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
>
> OK, let me tell you about my idea.
> I basically want to create an annotation system for threading. For
> example, you have a method that you know it creates a thread, so you
> annotate it with @ThreadStarter. That thread will call another class's
> method. You have to make sure that everything the method does is
> thread safe in that other class. So eventually you know that method is
> thread safe. Now you annotate that method as @ThreadSafe. What I want
> basically to do is create a system that would lookup all the
> @ThreadStarter methods and look inside those methods what other
> methods they call. If a method they call isn't annotated as
> @ThreadSafe, then the system will generate an error/warning etc.
>
> I know that Reflection will help me in some way. Annotation will too.
> I also found out about apt (Annotation Processing Tool). But I can't
> find a way to find out what other methods a method calls. I also can't
> find a desent tutorial/documentation/help/examples about the apt.
> Eclipse has a wonderful plugin for adding your own apt jar and it
> works just like a jvm! It throws warnings/errors/etc. while writing
> code. It's pretty incredible and really want to find out how to do
> this.
>
> I also heard about a JSR exactly for this reason. JSR-305 (http://
> groups.google.com/group/jsr-305). One of the creators is Brian Goetz
> (developer of FindBugs and an incredible "researcher" of concurrency).
>
> So, any help? :S
>
> Thanks again.
>
Have you read /Java Concurrency in Practice/? They describe they're own
thread safety annotations in that book.
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurrency-in-practice/>

In general, its hard to prove something *is* thread safe in a
programatic manor. It is easier to prove that something is *not* thread
safe, but you'll get a lot of false positives.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
 
pek





PostPosted: 2007-10-26 9:08:00 Top

java-programmer >> About Reflection (or not) On Oct 26, 4:05 am, Daniel Pitts
<email***@***.com> wrote:
> pek wrote:
> > On Oct 26, 3:33 am, Daniel Pitts
> > <email***@***.com> wrote:
> >> Daniel Pitts wrote:
> >>> pek wrote:
> >>>> Hello everyone..
> >>>> Here I am again for another question.. :P
> >>>> Suppose we have the following class
> >>>> public class Test {
> >>>> private Test2 test2 = new Test2();
> >>>> public void testMethod() {
> >>>> test2.test2Method();
> >>>> }
> >>>> }
> >>>> Is there a way, using anything (probably reflection) to know that the
> >>>> method testMethod of this class calls test2Method() method of Test2
> >>>> class..? Or the other way around.. Is there a way to know what classes
> >>>> call Test2's method test2Method()..?
> >>>> Any help/reference/book etc. is appreciated.
> >>>> Thank you very much.
> >>> Reflection wouldn't work. In general, the only way to find this out is
> >>> to build an index of the calls to test2Method. This index cannot ever
> >>> be complete because a new class that hasn't yet been created could
> >>> eventually call that method.
> >> If you want to see references within one codebase, there are tools to do
> >> that with static analysis. Personally, I use IntellI IDEA, and it has a
> >> "find references" tool. There may be ones that don't come with a
> >> commercial IDE.
>
> >> Another tool that may help is opengrok. opengrok will build a search
> >> index based on your codebase. Its quite useful in my opinion.
>
> >> HTH,
> >> Daniel.
>
> >> --
> >> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
>
> > OK, let me tell you about my idea.
> > I basically want to create an annotation system for threading. For
> > example, you have a method that you know it creates a thread, so you
> > annotate it with @ThreadStarter. That thread will call another class's
> > method. You have to make sure that everything the method does is
> > thread safe in that other class. So eventually you know that method is
> > thread safe. Now you annotate that method as @ThreadSafe. What I want
> > basically to do is create a system that would lookup all the
> > @ThreadStarter methods and look inside those methods what other
> > methods they call. If a method they call isn't annotated as
> > @ThreadSafe, then the system will generate an error/warning etc.
>
> > I know that Reflection will help me in some way. Annotation will too.
> > I also found out about apt (Annotation Processing Tool). But I can't
> > find a way to find out what other methods a method calls. I also can't
> > find a desent tutorial/documentation/help/examples about the apt.
> > Eclipse has a wonderful plugin for adding your own apt jar and it
> > works just like a jvm! It throws warnings/errors/etc. while writing
> > code. It's pretty incredible and really want to find out how to do
> > this.
>
> > I also heard about a JSR exactly for this reason. JSR-305 (http://
> > groups.google.com/group/jsr-305). One of the creators is Brian Goetz
> > (developer of FindBugs and an incredible "researcher" of concurrency).
>
> > So, any help? :S
>
> > Thanks again.
>
> Have you read /Java Concurrency in Practice/? They describe they're own
> thread safety annotations in that book.
> <http://virtualinfinity.net/wordpress/technical-book-recommendations/j...>
>
> In general, its hard to prove something *is* thread safe in a
> programatic manor. It is easier to prove that something is *not* thread
> safe, but you'll get a lot of false positives.
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

I actually bought the book. ;) But it's more like noting a method not
actually analyzing the method and finding out.

 
 
Joshua Cranmer





PostPosted: 2007-10-26 10:26:00 Top

java-programmer >> About Reflection (or not) pek wrote:
> On Oct 26, 3:01 am, Joshua Cranmer <email***@***.com> wrote:
>> pek wrote:
>>> Is there a way, using anything (probably reflection) to know that the
>>> method testMethod of this class calls test2Method() method of Test2
>>> class..?
>> You could look at the bytecode and deduce for yourself. Check out the
>> Java VM spec if you want to do manual processing.
>
> Hmmm.. I believe that could be pretty hard.. ;)

Parsing bytecode is actually relatively easy. I have source code to
handle most of the bytecode parsing, if you would like me to send you a
modified copy that can come close to doing what you want it to do.


>> > Or the other way around.. Is there a way to know what classes
>>
>>> call Test2's method test2Method()..?
>> Not without static analysis, if you don't want to actually run the
>> program and see.
>>
>
> I have no problem trying out static analysis (I use a couple of them -
> FindBugs, TPTP etc.). But I can't find anywhere (actually I don't know
> what exactly should I look for) on how to create an app that actually
> does static analysis. Is it basically a program that uses the
> reflection api? Or is it something more than that?

Reflection doesn't retain enough API: static analysis typically either
uses the bytecode (see above) or the actual source code. The apt tool
might be able to be used as a platform for this, but I do not have
sufficient experience to help you in this regard.


>> Note that these can be at best heuristics, since anything with a
>> Method.invoke() can invalidate number one and the second option has its
>> own problems.
>>
>
> Sorry, but I didn't quite understood that.

Method.invoke() (and maybe the invokedynamic opcode) allows run to
specify a method to be invoked at runtime, possibly without any possible
verification. Therefore, any code which uses Method.invoke() cannot be
fully verified for correctness.

The second method has the problem that it can only check code which you
know to exist: it can guarantee, for example, thread safety from your
code which calls it, but not from anyone else's.

For various reasons, I prefer the first (transformations can also be
applied at source code level here, but I would probably find bytecode
easier to work with).

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
 
pek





PostPosted: 2007-10-30 19:19:00 Top

java-programmer >> About Reflection (or not) On Oct 26, 4:26 am, Joshua Cranmer <email***@***.com> wrote:
> pek wrote:
> > On Oct 26, 3:01 am, Joshua Cranmer <email***@***.com> wrote:
> >> pek wrote:
> >>> Is there a way, using anything (probably reflection) to know that the
> >>> method testMethod of this class calls test2Method() method of Test2
> >>> class..?
> >> You could look at the bytecode and deduce for yourself. Check out the
> >> Java VM spec if you want to do manual processing.
>
> > Hmmm.. I believe that could be pretty hard.. ;)
>
> Parsing bytecode is actually relatively easy. I have source code to
> handle most of the bytecode parsing, if you would like me to send you a
> modified copy that can come close to doing what you want it to do.
>

If reading what I want and believing that one way to solve my problem
is using this, then please, do so. And thank you also.

> >> > Or the other way around.. Is there a way to know what classes
>
> >>> call Test2's method test2Method()..?
> >> Not without static analysis, if you don't want to actually run the
> >> program and see.
>
> > I have no problem trying out static analysis (I use a couple of them -
> > FindBugs, TPTP etc.). But I can't find anywhere (actually I don't know
> > what exactly should I look for) on how to create an app that actually
> > does static analysis. Is it basically a program that uses the
> > reflection api? Or is it something more than that?
>
> Reflection doesn't retain enough API: static analysis typically either
> uses the bytecode (see above) or the actual source code. The apt tool
> might be able to be used as a platform for this, but I do not have
> sufficient experience to help you in this regard.
>

Nor do I. Even after spending so much time, I couldn't find any good
documentation.

> >> Note that these can be at best heuristics, since anything with a
> >> Method.invoke() can invalidate number one and the second option has its
> >> own problems.
>
> > Sorry, but I didn't quite understood that.
>
> Method.invoke() (and maybe the invokedynamic opcode) allows run to
> specify a method to be invoked at runtime, possibly without any possible
> verification. Therefore, any code which uses Method.invoke() cannot be
> fully verified for correctness.
>
> The second method has the problem that it can only check code which you
> know to exist: it can guarantee, for example, thread safety from your
> code which calls it, but not from anyone else's.
>
> For various reasons, I prefer the first (transformations can also be
> applied at source code level here, but I would probably find bytecode
> easier to work with).
>

So, concluding, I should ask you for the code you mentioned earlier.
Correct?
If so, can you please explain to me (or refer a site) about what
should I do (or look for).

> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

Thank you very much for your help.
-pek