Re: About Reflection (or not)
pek wrote:
On Oct 26, 3:01 am, Joshua Cranmer <Pidgeo...@verizon.invalid> 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