Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

About Reflection (or not)

Thread view: 
pek - 26 Oct 2007 00:51 GMT
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 - 26 Oct 2007 01:01 GMT
> 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.

Signature

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

pek - 26 Oct 2007 01:29 GMT
> > 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.
Joshua Cranmer - 26 Oct 2007 03:26 GMT
>>> Is there a way, using anything (probably reflection) to know that the
>>> method testMethod of this class calls test2Method() method of Test2
[quoted text clipped - 3 lines]
>
> 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
>>
[quoted text clipped - 7 lines]
> 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).

Signature

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

pek - 30 Oct 2007 12:19 GMT
> >>> Is there a way, using anything (probably reflection) to know that the
> >>> method testMethod of this class calls test2Method() method of Test2
[quoted text clipped - 7 lines]
> 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
>
[quoted text clipped - 12 lines]
> 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
[quoted text clipped - 14 lines]
> 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
Daniel Pitts - 26 Oct 2007 01:30 GMT
> Hello everyone..
> Here I am again for another question.. :P
[quoted text clipped - 14 lines]
> 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.

Signature

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

Daniel Pitts - 26 Oct 2007 01:33 GMT
>> Hello everyone..
>> Here I am again for another question.. :P
[quoted text clipped - 19 lines]
> 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.

Signature

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

pek - 26 Oct 2007 02:01 GMT
On Oct 26, 3:33 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> >> Hello everyone..
> >> Here I am again for another question.. :P
[quoted text clipped - 33 lines]
> --
> 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 - 26 Oct 2007 02:05 GMT
> On Oct 26, 3:33 am, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
[quoted text clipped - 59 lines]
>
> 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-concurr
ency-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.

Signature

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

pek - 26 Oct 2007 02:08 GMT
On Oct 26, 4:05 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> > On Oct 26, 3:33 am, Daniel Pitts
> > <newsgroup.spamfil...@virtualinfinity.net> wrote:
[quoted text clipped - 70 lines]
> --
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.