I'd like to obtain the list of imports a class has. Since this is removed by the compiler, I suppose via reflection one could scan through the class, it's methods, fields and so on and collect a list of classes which are required in order for the classloader to load the class. Is there some sort of library, tutorial, or article you could point me at so I could better understand how this can be done? (I understand similar questions have been asked, but I could not find a proper answer and I'm quite sure this should be possible). I saw some examples showing how you could do it, if you had the sources, but that would not necessarily be the case in my scenario.
Many thanks in advance!
No, reflection will not help
void test() {
Date date = new Date();
}
you cannot detect that Date is used inside method with reflection. But you can use Javassist https://github.com/jboss-javassist/javassist
ClassPool cp = ClassPool.getDefault();
Collection classes = cp.get("test.Test").getRefClasses();
this code produces a collection of the names of all the classes referenced in test.Test class
I don't think this is remotely possible except by reading the source .java file itself. I'm fairly sure Java just using the list to scan during compiling then throws it away.
Spring does stuff like this however, and there is a google project that can scan packages for annotations and the like (Forget the name--classpath?).
I recommend you look into spring, chances are it does exactly what you want and a lot more and does it in a way that the next programmer will understand automatically (if they know Spring).