Search in sources :

Example 1 with JavaFileObjectImpl

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);
    }
}
Also used : HashMap(java.util.HashMap) JavaFileObjectImpl(com.alibaba.otter.shared.common.utils.compile.model.JavaFileObjectImpl) ArrayList(java.util.ArrayList) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JdkCompileException(com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException) JavaFileObject(javax.tools.JavaFileObject)

Aggregations

JdkCompileException (com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException)1 JavaFileObjectImpl (com.alibaba.otter.shared.common.utils.compile.model.JavaFileObjectImpl)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CompilationTask (javax.tools.JavaCompiler.CompilationTask)1 JavaFileObject (javax.tools.JavaFileObject)1