JavaCompilerTool can compile and generate '.class' of non '.java' files.
<code>
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.*;
import static javax.tools.StandardJavaFileManager.StandardLocation.*;
import static javax.tools.JavaFileObject.Kind.*;
public class Foo {
private void test(String... args) throws Throwable {
JavaCompilerTool javac = ToolProvider.getSystemJavaCompilerTool();
DiagnosticListener<? super javax.tools.JavaFileObject> dl =
new DiagnosticListener<javax.tools.JavaFileObject>() {
public void report(Diagnostic message) {
System.out.println("\nFile Name : "+new File(
message.getSource().toString()).getName());
}
};
StandardJavaFileManager sjfm = javac.getStandardFileManager(dl);
Iterable<? extends JavaFileObject> iteJFO =
sjfm.getJavaFileObjectsFromFiles(Arrays.asList(new File(args[0])));
List<String> options = new ArrayList<String>();
options.add("-Xlint:all");
JavaCompilerTool.CompilationTask task =
javac.getTask(null,sjfm,null,options,null,iteJFO);
task.run();
}
public static void main(String... arg) throws Throwable {
Foo test = new Foo();
test.test("Bar.other");
}
}
</code>
<Bar.other>
class Bar {
}
</Bar.other>
After running the code, Bar.class file will be created.
When I run Bar.other in the command line, compiler will throw the following error:
error: Class names, 'Bar.other', are only accepted if annotation processing is explicitly requested
1 error
I feel it will be good, if JavaCompilerTool also throws the same exception.
<code>
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.*;
import static javax.tools.StandardJavaFileManager.StandardLocation.*;
import static javax.tools.JavaFileObject.Kind.*;
public class Foo {
private void test(String... args) throws Throwable {
JavaCompilerTool javac = ToolProvider.getSystemJavaCompilerTool();
DiagnosticListener<? super javax.tools.JavaFileObject> dl =
new DiagnosticListener<javax.tools.JavaFileObject>() {
public void report(Diagnostic message) {
System.out.println("\nFile Name : "+new File(
message.getSource().toString()).getName());
}
};
StandardJavaFileManager sjfm = javac.getStandardFileManager(dl);
Iterable<? extends JavaFileObject> iteJFO =
sjfm.getJavaFileObjectsFromFiles(Arrays.asList(new File(args[0])));
List<String> options = new ArrayList<String>();
options.add("-Xlint:all");
JavaCompilerTool.CompilationTask task =
javac.getTask(null,sjfm,null,options,null,iteJFO);
task.run();
}
public static void main(String... arg) throws Throwable {
Foo test = new Foo();
test.test("Bar.other");
}
}
</code>
<Bar.other>
class Bar {
}
</Bar.other>
After running the code, Bar.class file will be created.
When I run Bar.other in the command line, compiler will throw the following error:
error: Class names, 'Bar.other', are only accepted if annotation processing is explicitly requested
1 error
I feel it will be good, if JavaCompilerTool also throws the same exception.
- relates to
-
JDK-6420156 JSR 199: rename JavaCompilerTool to JavaCompiler
-
- Resolved
-