use of com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException 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 com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException in project otter by alibaba.
the class JdkCompiler method compile.
public Class compile(JavaSource javaSource) {
try {
final DiagnosticCollector<JavaFileObject> errs = new DiagnosticCollector<JavaFileObject>();
JdkCompileTask compileTask = new JdkCompileTask(new JdkCompilerClassLoader(this.getClass().getClassLoader()), options);
String fullName = javaSource.getPackageName() + "." + javaSource.getClassName();
Class newClass = compileTask.compile(fullName, javaSource.getSource(), errs);
return newClass;
} catch (JdkCompileException ex) {
DiagnosticCollector<JavaFileObject> diagnostics = ex.getDiagnostics();
throw new CompileExprException("compile error, source : \n" + javaSource + ", " + diagnostics.getDiagnostics(), ex);
} catch (Exception ex) {
throw new CompileExprException("compile error, source : \n" + javaSource, ex);
}
}
Aggregations