Java – get all classes referenced by the class

Suppose I have a Java. Net at runtime Lang. class instance

At run time, check which classes C uses anyway? For example, if c.foo instantiates B, B is "used" If c.foo calls the static method g.bar (), G

Note: just trying to get statically aware classes - I missed the reflection loaded classes very well

In Python, the same function is found in modulefinder: http://docs.python.org/library/modulefinder.html

Solution

You can use reflection to query everything that happens outside the method body, constructor, and static initializer You can view the field and method signatures and reflect the field to get the actual class of the field value

Using classes in methods is a considerable challenge You can use BCEL or similar tools to analyze class files and extract all hard coded class names from class files But once you program the interface, it will fail:

public List someMethod() {
  return SomeClass.getList();
}

The type of class returned by somemethod is unknown to the class file And there is no lookup table or central registry The JVM will know the type, see if the class is loaded, and if not, try to load it It doesn't have to care which class requires other classes, and it won't record this information Just because it doesn't need exception and doesn't need garbage collection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>