1

I trying to implement a RemoteClassLoader which copy and load all classes which will be used in runtime. First I need to collect the used Classes, I found a solution:

Find out which classes of a given API are used

but this is not exactly what a need, it collect only the "visible" class usages, just like loading the class and iterating all of declared field and methods, and collecting all types. I have a class which contains only static methods, instance of this method is not used, so it will be never given to a function or will be a filed, and so I can't see that class.

Naturally the bytecode file contains the name of this class:

strings TestClass.class | grep -i "json"

gives: org/json/JSONObject

And yes that class I search and not fond. How can I find it? And the others which I use only in functions.

Community
  • 1
  • 1
Dankó Dávid
  • 302
  • 3
  • 17

1 Answers1

0

The easiest, albeit conservative method is to simply take all of the Class_info entries from the constant pool. In order to call a method or access a field of a class, there must be a constant pool entry for that class (not counting reflection and not counting overriding methods in subclasses).

There are a number of tools out there that will parse a classfile and give you access to this. Reflection of course is much harder, and in general undecideable.

Edit: This won't include type descriptors, which are just Utf8_infos. If you want to find classes used as types as well, there are two approaches. Either you can go through all the Utf8s and include everything that looks like a descriptor (which may have false positives in rare cases), or you can go through the classfile and find all the type descriptor references.

Antimony
  • 37,781
  • 10
  • 100
  • 107