How to get all imports defined in a class using java reflection?

PHOTO EMBED

Wed Apr 01 2020 10:09:44 GMT+0000 (UTC)

Saved by @SunLoves #java #java #reflection #dependencies

String fileFullPath = "Your\\java\\ file \\full\\path";
    JavaDocBuilder builder = new JavaDocBuilder();
    builder.addSource(new FileReader( fileFullPath  ));

    JavaSource src = builder.getSources()[0];
    String[] imports = src.getImports();

    for ( String imp : imports )
    {
        System.out.println(imp);
    }
content_copyCOPY

You can use Qdox to get all the imports in a class which is not actually through reflection, but it can serve your purpose.

https://stackoverflow.com/questions/5701305/how-to-get-all-imports-defined-in-a-class-using-java-reflection