1

I have some (maybe) strange requirements - I wanted to detect definitions of local (method) variables of a given interface name. When finding such a variable I would like to detect which methods (set/get*) will be called on this variable. I tried Javassist without luck, and now I have a deeper look into ASM, but not sure if it is possible what I wanted. The reason for this is that I like to generated a dependency graph with GraphViz of beans that depend on the same data structure. If this thing is possible could somebody please give me a hint on how it could be done? Maybe there are other Frameworks that could do?

01.09.2015

To make things more clear:

The interface is self written - the target of the whole action is to create a dependency graph in the first step automatically - later on a graphical editor should be implemented that is based on the dependencies. I wonder how FindBugs/PMD work, because they also use the byte code and detect for example null pointer calls (variable not initialized and method will be called on it). So I thought that I could implement my idea in the same way. The whole code is Spring based - maybe this opens another solution to the point? Last but not least I could work on a source-jar?

While thinging about the problem - would it be possible via ASM/javassist to detect all available methods from the interface and find calls to them in the other classes?

PowerStat
  • 3,757
  • 8
  • 32
  • 57
  • I doubt that FindBugs et al work on bytecode. Besides that, the task is completely different. Detecting a `null` access means detecting what gets stored and retrieved rather than detecting how something is *declared*. Detecting interface method calls in ASM is easy, but you have to think about how to deal with direct calls into implementation classes (unless your API really enforces interface method calls by having inaccessible implementation classes only). But you shouldn’t extend a question to become an entirely different one. You can always open a new question. – Holger Sep 01 '15 at 10:34
  • I think the question has not been changed - I still wanted to know how I could read the information I wanted from the class files. In the meantime I have written a small dos script that extracts the informations from the source code but thats not very handy. – PowerStat Sep 01 '15 at 11:17
  • Only that you are now asking about a different information. – Holger Sep 01 '15 at 11:20
  • I am asking for how to extract the information from class files - the difference for me is that when I extract them from source, then every spring application developer needs to do so. When I could extract the information from class file - then the editor I would like to write could be used on other peoples code without knowing about their souce ... – PowerStat Sep 01 '15 at 11:42
  • Related: http://stackoverflow.com/questions/26760153/parse-jar-file-and-find-relationships-between-classes It would be good if you could describe (more) precisely in how far the functionalities of the answers from this question are *not* sufficient for solving your task. – Marco13 Sep 02 '15 at 15:14
  • Thanks Marco13 that looks like it could solve my problem - I will try it. – PowerStat Sep 03 '15 at 05:07

2 Answers2

1

I’m afraid, what you want to do is not possible. In compiled Java code, there are no local variables in the form you have in the source code. Methods use stack frames which have memory reserved for local variables, which is addressed by a numerical index. The type is implied by what instructions write to it and may change throughout the method’s code as the memory may get reused for different variables having a disjunct scope. The names on the other hand are completely irrelevant.

When bytecode gets verified, the effect of all instructions to the stack frame will get modeled to infer the type of each stack frame slot at each point of the execution so that the validity of all operations can be checked. Starting with class file version 50, there will be StackMapTable attributes aiding the process by containing explicit type information, but only for code with branches. For sequential code, the type of variables still has to be derived by inference.

These inferred types are not necessarily the declared types. E.g., on the byte code level, there will be no difference between

CharSequence cs="foo";
cs.charAt(0);

and

String s="foo";
((CharSequence)s).charAt(0);

In both cases, there will be a storage of a String constant into a local variable followed by the invocation of an interface method. The inferred type will be String in both cases and the invocation of a CharSequence method considered valid as String implements CharSequence.

This disproves the idea of detecting that there is a local variable declared using the CharSequence (interface) type, as the actual declared type is irrelevant and not stored in the regular byte code.


There are, however, debugging attributes containing information about the local variables, see the LocalVariableTable attribute and libraries like ASM will tell you about the declarations if such information is present. But you can’t rely on these optional information. E.g. Oracle’s JRE libraries are by default shipped without them.

Holger
  • 285,553
  • 42
  • 434
  • 765
-3

I don't sure I understood exacly what you want but .

you can use implement on each object , evry object that have getter you can implement it with class called getable . and then you could do stuff only on object that have the function that you implement from the class getable .

https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

Ori Yampolsky
  • 125
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Moslem Ben Dhaou Sep 07 '15 at 09:32
  • nha .. i dont really care about point and stuff like that , if the one asked the qwestion want an answer i gave it to him .. if he want someone else to write the code for him .. he will wait for someone else .. knowlage worth much more then some codes.. – Ori Yampolsky Sep 07 '15 at 20:29
  • Those are SO rules. That was an automated review comment – Moslem Ben Dhaou Sep 07 '15 at 20:33