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.
-
@Nambari: you can define multiple classes in a single java file, but only one can be public.leonbloy– leonbloy2012-10-12 18:06:13 +00:00Commented Oct 12, 2012 at 18:06
-
@Nambari, there can be multiple java classes at the top level in one Java file. The only restriction is, at most one of them can be public and it has to have the same name as the name of the file.Abhinav Sarkar– Abhinav Sarkar2012-10-12 18:06:32 +00:00Commented Oct 12, 2012 at 18:06
-
1It may have changed, but it used to be that javac would compile any .java files it couldn't find matching .class files for when resolving references in the named .java file. There may be a log that javac creates, but otherwise all you can do is look at the change dates of the .class files.Hot Licks– Hot Licks2012-10-12 18:08:15 +00:00Commented Oct 12, 2012 at 18:08
3 Answers
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;
}
};
Comments
javax.tools package shows several ways to manage compilation units.
See JavaFileObject or JavaFileManager
Comments
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