mentics
mentics

Reputation: 6999

Using Java Compiler API, what classes were created?

Because a single Java file can compile into any number of class files, is there some way from the compiler API to find out which class files were generated? It's outputting to a directory that may have other files in it, so I can't just look at that.

Upvotes: 1

Views: 213

Answers (3)

mentics
mentics

Reputation: 6999

I figured out something that appears to work. The *FileManager has callbacks to get the locations for things, including things for output. You can wrap it using the ForwardingJavaFileManager, override, and store the values from the calls.

final List<String> classFiles = new ArrayList<>();
StandardJavaFileManager inner = compiler.getStandardFileManager(null, null, null);
JavaFileManager fileManager = new ForwardingJavaFileManager(inner) {
    @Override
    public JavaFileObject getJavaFileForOutput(Location location, String className,
                JavaFileObject.Kind kind, FileObject sibling) throws IOException {
        JavaFileObject o = super.getJavaFileForOutput(location, className, kind, sibling);
        classFiles.add(o.getName());
        return o;
    }
};

Upvotes: 3

Satheesh Cheveri
Satheesh Cheveri

Reputation: 3679

I don't think we can find the non public classes created by Java Compiler API using any of the given references. You would need to parse (or apply reg expression ) on the input java files to identify the available class names. Once you get the name of the classes, you should be able to load them using custom class loaders.

Satheesh

Upvotes: 0

Aubin
Aubin

Reputation: 14873

javax.tools package shows several ways to manage compilation units.

See JavaFileObject or JavaFileManager

Upvotes: 0

Related Questions