use of javax.tools.JavaCompiler.CompilationTask in project otter by alibaba.
the class JdkCompileTask method compile.
public synchronized Map<String, Class> compile(final Map<String, CharSequence> classes, final DiagnosticCollector<JavaFileObject> diagnosticsList) throws JdkCompileException {
Map<String, Class> compiled = new HashMap<String, Class>();
List<JavaFileObject> sources = new ArrayList<JavaFileObject>();
for (Entry<String, CharSequence> entry : classes.entrySet()) {
String qualifiedClassName = entry.getKey();
CharSequence javaSource = entry.getValue();
if (javaSource != null) {
final int dotPos = qualifiedClassName.lastIndexOf('.');
final String className = dotPos == -1 ? qualifiedClassName : qualifiedClassName.substring(dotPos + 1);
final String packageName = dotPos == -1 ? "" : qualifiedClassName.substring(0, dotPos);
final JavaFileObjectImpl source = new JavaFileObjectImpl(className, javaSource);
sources.add(source);
javaFileManager.putFileForInput(StandardLocation.SOURCE_PATH, packageName, className + JAVA_EXTENSION, source);
}
}
// Get a CompliationTask from the compiler and compile the sources
final CompilationTask task = compiler.getTask(null, javaFileManager, diagnostics, options, null, sources);
final Boolean result = task.call();
if (result == null || !result.booleanValue()) {
throw new JdkCompileException("Compilation failed.", classes.keySet(), diagnostics);
}
try {
// put it in the output map
for (String qualifiedClassName : classes.keySet()) {
final Class<T> newClass = loadClass(qualifiedClassName);
compiled.put(qualifiedClassName, (Class<?>) newClass);
}
return compiled;
} catch (ClassNotFoundException e) {
throw new JdkCompileException(classes.keySet(), e, diagnostics);
} catch (IllegalArgumentException e) {
throw new JdkCompileException(classes.keySet(), e, diagnostics);
} catch (SecurityException e) {
throw new JdkCompileException(classes.keySet(), e, diagnostics);
}
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.
the class JavahTask method run.
public boolean run() throws Util.Exit {
Util util = new Util(log, diagnosticListener);
if (noArgs || help) {
showHelp();
// treat noArgs as an error for purposes of exit code
return help;
}
if (version || fullVersion) {
showVersion(fullVersion);
return true;
}
util.verbose = verbose;
Gen g;
if (llni)
g = new LLNI(doubleAlign, util);
else {
// if (stubs)
// throw new BadArgs("jni.no.stubs");
g = new JNI(util);
}
if (ofile != null) {
if (!(fileManager instanceof StandardJavaFileManager)) {
diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-o"));
return false;
}
Iterable<? extends JavaFileObject> iter = ((StandardJavaFileManager) fileManager).getJavaFileObjectsFromFiles(Collections.singleton(ofile));
JavaFileObject fo = iter.iterator().next();
g.setOutFile(fo);
} else {
if (odir != null) {
if (!(fileManager instanceof StandardJavaFileManager)) {
diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-d"));
return false;
}
if (!odir.exists())
if (!odir.mkdirs())
util.error("cant.create.dir", odir.toString());
try {
((StandardJavaFileManager) fileManager).setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(odir));
} catch (IOException e) {
Object msg = e.getLocalizedMessage();
if (msg == null) {
msg = e;
}
diagnosticListener.report(createDiagnostic("err.ioerror", odir, msg));
return false;
}
}
g.setFileManager(fileManager);
}
/*
* Force set to false will turn off smarts about checking file
* content before writing.
*/
g.setForce(force);
if (fileManager instanceof JavahFileManager)
((JavahFileManager) fileManager).setIgnoreSymbolFile(true);
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
List<String> opts = new ArrayList<String>();
opts.add("-proc:only");
opts.addAll(javac_extras);
CompilationTask t = c.getTask(log, fileManager, diagnosticListener, opts, internalize(classes), null);
JavahProcessor p = new JavahProcessor(g);
t.setProcessors(Collections.singleton(p));
boolean ok = t.call();
if (p.exit != null)
throw new Util.Exit(p.exit);
return ok;
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.
the class TestJavacTask_Multiple method test.
void test(JavaCompiler comp, StandardJavaFileManager fm, TestKind tk) {
System.err.println("test " + tk);
File testSrc = new File(System.getProperty("test.src"));
String thisClassName = TestJavacTask_Multiple.class.getName();
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
List<CompilationTask> tasks = new ArrayList<CompilationTask>();
for (int i = 1; i <= MAX_TASKS; i++) {
File tmpDir = new File(tk + "_" + i);
tmpDir.mkdirs();
List<String> options = Arrays.asList("-d", tmpDir.getPath());
CompilationTask t = comp.getTask(null, fm, null, options, null, files);
((JavacTask) t).setTaskListener(createTaskListener(tk, i));
tasks.add(t);
}
for (CompilationTask t : tasks) count += tk.test(t);
System.err.println();
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.
the class TestJavacTask_Lock method test.
void test(MethodKind first, MethodKind second) {
System.err.println("test: " + first + ", " + second);
File testSrc = new File(System.getProperty("test.src"));
String thisClassName = TestJavacTask_Lock.class.getName();
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
File tmpDir = new File(first + "_" + second);
tmpDir.mkdirs();
List<String> options = Arrays.asList("-d", tmpDir.getPath());
CompilationTask t = comp.getTask(null, fm, null, options, null, files);
try {
first.test(t);
second.test(t);
error("No exception thrown");
} catch (IllegalStateException e) {
System.err.println("Expected exception caught: " + e);
} catch (Exception e) {
error("Unexpected exception caught: " + e);
e.printStackTrace(System.err);
}
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.
the class T7031108 method compile.
void compile(JavaCompiler comp, JavaFileManager fm, DiagnosticListener<JavaFileObject> dl, String processor, JavaFileObject... files) throws Exception {
System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
List<String> opts = new ArrayList<String>();
if (processor != null) {
// opts.add("-verbose");
opts.addAll(Arrays.asList("-processor", processor));
}
CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
boolean ok = task.call();
if (dl == null && !ok)
throw new Exception("compilation failed");
}
Aggregations