1

Is there a way to find out what imports a class has?

In this question: Jon Skeet says that you can't do this using reflection, but

If you want to find all the types used within the compiled code, that's a slightly different matter. You may want to look at BCEL as a way of analyzing bytecode.

This is what I want to know how to do.

Community
  • 1
  • 1
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
  • 1
    See http://stackoverflow.com/questions/5701305/how-to-get-all-imports-defined-in-a-class-using-java-reflection – nolegs Jul 18 '12 at 19:21
  • @nolegs I edited my question to reflect your comment – LanguagesNamedAfterCofee Jul 18 '12 at 19:30
  • Do you mean in code (as in using reflection) or just from the class file (using a decompiler or similar?) – Michael Berry Jul 18 '12 at 19:30
  • 1
    Classes are kept in the .class constant table, typed appropiately. There is one caveat: imported static final constants are copied, so that their class import might fall away. (At least that was so in sun's java.) – Joop Eggen Jul 18 '12 at 19:46
  • See also http://stackoverflow.com/questions/3734825/find-out-which-classes-of-a-given-api-are-used – earcam Apr 23 '17 at 12:23
  • As you already know you can't find the imports, why do your title and your first sentence ask it anyway? Please amended them to agree with what you're actually asking. – user207421 Aug 28 '17 at 12:03

2 Answers2

2

Here is an old tutorial for ASM bytecode manipulation framework explaining how to read class dependencies with ASM.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • Links don't work (which is why answers shouldn't be just links!). Archive: https://web.archive.org/web/20151231173739/http://asm.ow2.org/doc/tutorial-asm-2.0.html – NateS Sep 30 '18 at 15:58
1

You can use a Java Decompiler like this one: Java Decompiler

Baz
  • 36,440
  • 11
  • 68
  • 94