Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easy way to find references to other classes in class file

The class file format as described in http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html contains all references to other classes in the the constant pool as entries of type CONSTANT_Utf8.

But these entries are not only references to classes but also class literals, names of methods, fields and what not.

In a first attempt I thought it would be sufficient to use the constant pool entries referenced by other constant_pool entries of type CONSTANT_Class, CONSTANT_NameAndType and CONSTANT_MethodType

But these don't seem to include type parameters and annotations. Further reading of the specification seems to suggest that I need to parse things like RuntimeVisibleAnnotations and similar constructs in order to identify the relevant constant pool entries. Which means I have to parse more or less the complete class file.

But the whole idea behind parsing the class file myself was that it would be simpler then using a library like ASM, because I thought it would be sufficient to interpret the constant pool.

My question is: Is there a way to reliable identify all classes referenced in a class file by just interpreting little more than the constant pool?

like image 312
Jens Schauder Avatar asked Dec 18 '14 08:12

Jens Schauder


People also ask

How do I find class references in eclipse?

To get there, we go to the menu under Navigate > Open Type in Hierarchy or we use the shortcut: Ctrl + Shift + H on a PC or Cmd + Shift + H on a Mac. This dialog is similar to the Open Type dialog. Except for this time when we click on a class, then we get the Type Hierarchy view.

Can two classes reference each other Java?

Do you really need both classes to depend on each other? A stack overflow has nothing to do with classes referencing each other. It is perfectly possible and supported by Java to do that. The Exception is caused by a too deep recursin, this can happen with one method/class as well.

Is the reference of class?

A class reference is a special meta type that can be used to refer to classes (not instances of them) within a certain subtree of the class hierarchy. This allows to write code that can work polymorphically with classes – for example dynamically instantiate different subclasses or call virtual static methods.


1 Answers

Annotation types that cannot be loaded by a class loader are ignored by this class loader and will simply appear to be invisible at runtime. I assume that this is the reason that types that are referenced by an annotation are not stored in the constant pool where the resolution of an unknown type would prohibit successful class loading. Annotations are code attributes, i.e. meta data and they should not be linked deeply into the class by avoiding a constant pool entry.

You are therefore required to also introspect RuntimeVisibleAnnotations which live outside of the constant pool. However, if the constant pool does not contain a string RunntimeVisibleAnnotations, your approach is working. ASM has however very little overhead so I would use it nevertheless.

like image 137
Rafael Winterhalter Avatar answered Oct 27 '22 11:10

Rafael Winterhalter