Search in sources :

Example 1 with JdkCompileException

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);
    }
}
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)

Example 2 with JdkCompileException

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);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JdkCompilerClassLoader(com.alibaba.otter.shared.common.utils.compile.model.JdkCompilerClassLoader) DiagnosticCollector(javax.tools.DiagnosticCollector) CompileExprException(com.alibaba.otter.shared.common.utils.compile.exception.CompileExprException) JdkCompileException(com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException) CompileExprException(com.alibaba.otter.shared.common.utils.compile.exception.CompileExprException) JdkCompileException(com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException)

Aggregations

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