use of com.alibaba.otter.shared.common.utils.compile.model.JavaFileObjectImpl 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);
}
}
Aggregations