Sagewire Logo

About Reflection (or not)

9 Message(s) by 3 Author(s) originally posted in java programmer


From: pek Date:   Thursday, October 25, 2007
Hello everyone..
Here I am again for another question.. :P
Suppose we've 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 app reciated.
Thank you very much.


From: Joshua Cranmer Date:   Thursday, October 25, 2007
wrote in message:
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 do not 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


From: pek Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
> 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 do not 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 not find anywhere (actually I do not 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 did not 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.


From: Daniel Pitts Date:   Thursday, October 25, 2007
wrote in message:
Hello everyone..
Here I am again for another question.. :P
Suppose we've 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 would not 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 has not yet been created could
eventually call that method.--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


From: Daniel Pitts Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
Hello everyone..
Here I am again for another question.. :P
Suppose we've 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 would not 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 has not yet been created could
eventually call that method.


If you want to see references within one codebase, there are tool s to do
that with static analysis. Personally, I use IntellI IDEA, and it has a
"find references" tool. There may be ones that do not 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/>


From: pek Date:   Thursday, October 25, 2007
On Oct 26, 3:33 am, Daniel Pitts
wrote in message:
wrote in message:
wrote in message:
Hello everyone..
Here I am again for another question.. :P
Suppose we've 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 would not 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 has not 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 do not 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 thread ing. For
example, you've a method that you know it creates a thread, so you
annotate it with @xxxxxxxxxxx That thread will call another class's
method. You've 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 @xxxxxxxxxxx What I want
basically to do is create a system that'd lookup all the
@xxxxxxxxxxx methods and look inside those methods what other
methods they call. If a method they call is not annotated as
@xxxxxxxxxxx, 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 not
find a way to find out what other methods a method calls. I also can not
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.


From: Daniel Pitts Date:   Thursday, October 25, 2007
wrote in message:
On Oct 26, 3:33 am, Daniel Pitts
wrote in message:
wrote in message:
wrote in message:
Hello everyone..
Here I am again for another question.. :P
Suppose we've 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 would not 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 has not 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 do not 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've a method that you know it creates a thread, so you
annotate it with @xxxxxxxxxxx That thread will call another class's
method. You've 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 @xxxxxxxxxxx What I want
basically to do is create a system that'd lookup all the
@xxxxxxxxxxx methods and look inside those methods what other
methods they call. If a method they call is not annotated as
@xxxxxxxxxxx, 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 not
find a way to find out what other methods a method calls. I also can not
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 will get a lot of false positives.

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


From: pek Date:   Thursday, October 25, 2007
On Oct 26, 4:05 am, Daniel Pitts
wrote in message:
wrote in message:
> On Oct 26, 3:33 am, Daniel Pitts
wrote in message:
wrote in message:
wrote in message:
Hello everyone..
Here I am again for another question.. :P
Suppose we've 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 would not 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 has not 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 do not 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've a method that you know it creates a thread, so you
> annotate it with @xxxxxxxxxxx That thread will call another class's
> method. You've 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 @xxxxxxxxxxx What I want
> basically to do is create a system that'd lookup all the
> @xxxxxxxxxxx methods and look inside those methods what other
> methods they call. If a method they call is not annotated as
> @xxxxxxxxxxx, 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 not
> find a way to find out what other methods a method calls. I also can not
> 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 will 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.


From: Joshua Cranmer Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
wrote in message:
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'd 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 do not 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 not find anywhere (actually I do not 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 does not 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 don't 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 did not 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'd 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



Next Message: Memory leak in JAVA app


Blogs related to About Reflection (or not)

Writing toString Methods & Tech Days
String.valueOf first checks to make sure that the reference is not null. It then calls the toString method for the object. Since the MyPoint class has no toString method, the default one in java.lang.Object is used instead. ...

Applying Java reflection to Ioc and AOP (1)
Today, I am not going to explain what is IOC and what is AOP. I just want to show two examples to present the technology of java reflection plays an important role in IOC and AOP. The first example would be a little bit different from ...

Java Sa
Primitive values in Java are not objects. In order to manipulate these values as objects, the java.lang package provides a wrapper class for each of the primitive data types. All wrapper classes are final. The objects of all wrapper ...

Java Interview Questions
This driver includes its own implementation of a TCP/IP version of Oracle’s Net8 written entirely in Java, so it is platform independent, can be downloaded to a browser at runtime, and does not require any Oracle software on the client ...

org.apache.commons.beanutils
populate(java.lang.object bean, java.util.Map properties) is a static method. This method can do some basic type conversions such as from String to int. However, if there are a mismatch with the type, and cannot be converted, ...

Thieves in the Temple: Vandals, Dragons and Warp Guns on the JVM
With Java Security on, Java catches the reflection attempt. Conclusions Did we ultimately steal the Dragon's breath? To be honest, I don't think so: certainly not with the Java Security turned on. In pure Groovy, there are either some ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional