开发者

how can i find the files imported in a java class?

开发者 https://www.devze.com 2023-02-20 22:38 出处:网络
I don\'t get to see the source code but the .class file. C开发者_JS百科an I still find out the files that are imported?Keep in mind that imports are simply a convenience mechanism that lets the Java d

I don't get to see the source code but the .class file. C开发者_JS百科an I still find out the files that are imported?


Keep in mind that imports are simply a convenience mechanism that lets the Java developer refer to a class using it's simple name (Date) rather than it's Fully Qualified Name (FQN - java.util.Date or java.sql.Date).

So if you run the .class file through a decompiler, you'll likely see references using the FQN and possibly no import statements.


Well, if you just want to do it manually, I suggest you take a look at a decompiler such as JD GUI

Otherwise, you need to go the reflection way if you want this information programmatically.


If you need to do this in batch, and do not want to bother with all the other details that a decompiler would offer, you can inspect the constant pool for class references.

Beware that, as previously mentioned, source imports are merely a convenience and do not correspond directly to anything in class files. Scanning the constant pool will not show unused imports from the source file, and it will not show classes used only for compile-time constants (public static final String ... and the like). It will show FQNs even for classes in the same package, and it will show classes referred to using FQN without an import. It will show classes whose signatures are used implicitly:

URL loc = Something.class.getProtectionDomain().getCodeSource().getLocation();

will produce references to ProtectionDomain and CodeSource in bytecode even though the source did not explicitly mention them.

https://hg.netbeans.org/core-main/raw-file/default/nbbuild/antsrc/org/netbeans/nbbuild/VerifyClassLinkage.java is an example of how to do this scan (see the dependencies method).

0

精彩评论

暂无评论...
验证码 换一张
取 消