Search in sources :

Example 1 with MultiParentClassLoader

use of org.knime.core.data.convert.util.MultiParentClassLoader in project knime-core by knime.

the class JavaSnippet method createSnippetClass.

/**
 * Create the class file of the snippet.
 *
 * Creates a URLClassLoader which may open jar files referenced by the Java Snippet class. Make sure to match every
 * call to this method with a call to {@link #close()} in order for KNIME to be able to clean up .jar files that
 * were downloaded from URLs.
 *
 * @return the compiled snippet
 */
@SuppressWarnings("unchecked")
private Class<? extends AbstractJSnippet> createSnippetClass() {
    JavaSnippetCompiler compiler = new JavaSnippetCompiler(this);
    /* Recompile/Reload either if the code changed or the class loader has been closed since */
    if (m_classLoader != null && m_snippetCache.isValid(getDocument())) {
        if (!m_snippetCache.hasCustomFields()) {
            return m_snippetCache.getSnippetClass();
        }
    } else {
        // recompile
        m_snippetCache.invalidate();
        StringWriter log = new StringWriter();
        DiagnosticCollector<JavaFileObject> digsCollector = new DiagnosticCollector<>();
        CompilationTask compileTask = null;
        try {
            compileTask = compiler.getTask(log, digsCollector);
        } catch (IOException e) {
            throw new IllegalStateException("Compile with errors: " + e.getMessage(), e);
        }
        boolean success = compileTask.call();
        if (!success) {
            StringBuilder msg = new StringBuilder();
            msg.append("Compile with errors:\n");
            for (Diagnostic<? extends JavaFileObject> d : digsCollector.getDiagnostics()) {
                boolean isSnippet = this.isSnippetSource(d.getSource());
                if (isSnippet && d.getKind().equals(javax.tools.Diagnostic.Kind.ERROR)) {
                    long line = d.getLineNumber();
                    if (line != Diagnostic.NOPOS) {
                        msg.append("Error in line " + line + ": ");
                    } else {
                        msg.append("Error: ");
                    }
                    msg.append(d.getMessage(Locale.US));
                    msg.append('\n');
                }
            }
            throw new IllegalStateException(msg.toString());
        }
    }
    try {
        close();
        final LinkedHashSet<ClassLoader> customTypeClassLoaders = new LinkedHashSet<>();
        customTypeClassLoaders.add(JavaSnippet.class.getClassLoader());
        for (final InCol col : m_fields.getInColFields()) {
            customTypeClassLoaders.addAll(getClassLoadersFor(col.getConverterFactoryId()));
        }
        for (final OutCol col : m_fields.getOutColFields()) {
            customTypeClassLoaders.addAll(getClassLoadersFor(col.getConverterFactoryId()));
        }
        /* Add class loaders of additional bundles */
        customTypeClassLoaders.addAll(getAdditionalBundlesClassLoaders());
        // remove core class loader:
        // (a) it's referenced via JavaSnippet.class classloader and
        // (b) it would collect buddies when used directly (see support ticket #1943)
        customTypeClassLoaders.remove(DataCellToJavaConverterRegistry.class.getClassLoader());
        final MultiParentClassLoader customTypeLoader = new MultiParentClassLoader(customTypeClassLoaders.stream().toArray(ClassLoader[]::new));
        // TODO (Next version bump) change return value of createClassLoader instead of cast
        m_classLoader = (URLClassLoader) compiler.createClassLoader(customTypeLoader);
        Class<? extends AbstractJSnippet> snippetClass = (Class<? extends AbstractJSnippet>) m_classLoader.loadClass("JSnippet");
        m_snippetCache.update(getDocument(), snippetClass, m_settings);
        return snippetClass;
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Could not load class file.", e);
    } catch (IOException e) {
        throw new IllegalStateException("Could not load jar files.", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataCellToJavaConverterRegistry(org.knime.core.data.convert.java.DataCellToJavaConverterRegistry) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) MultiParentClassLoader(org.knime.core.data.convert.util.MultiParentClassLoader) IOException(java.io.IOException) InCol(org.knime.base.node.jsnippet.util.field.InCol) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet) URLClassLoader(java.net.URLClassLoader) MultiParentClassLoader(org.knime.core.data.convert.util.MultiParentClassLoader) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) DiagnosticCollector(javax.tools.DiagnosticCollector) JavaSnippetCompiler(org.knime.base.node.jsnippet.util.JavaSnippetCompiler)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 URLClassLoader (java.net.URLClassLoader)1 LinkedHashSet (java.util.LinkedHashSet)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 CompilationTask (javax.tools.JavaCompiler.CompilationTask)1 JavaFileObject (javax.tools.JavaFileObject)1 ModuleClassLoader (org.eclipse.osgi.internal.loader.ModuleClassLoader)1 AbstractJSnippet (org.knime.base.node.jsnippet.expression.AbstractJSnippet)1 JavaSnippetCompiler (org.knime.base.node.jsnippet.util.JavaSnippetCompiler)1 InCol (org.knime.base.node.jsnippet.util.field.InCol)1 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)1 DataCellToJavaConverterRegistry (org.knime.core.data.convert.java.DataCellToJavaConverterRegistry)1 MultiParentClassLoader (org.knime.core.data.convert.util.MultiParentClassLoader)1