Search in sources :

Example 1 with AbstractJSnippet

use of org.knime.base.node.jsnippet.expression.AbstractJSnippet in project knime-core by knime.

the class JavaSnippetTest method testSimpleSnippet.

/**
 * Test execution of a simple java snippet.
 * @throws Exception
 */
@Test
public void testSimpleSnippet() throws Exception {
    final JavaSnippetSettings settings = new JavaSnippetSettings("throw new Abort(\"success\");");
    snippet.setSettings(settings);
    final AbstractJSnippet s = snippet.createSnippetInstance();
    assertNotNull(s);
    try {
        s.snippet();
        fail("Expected exception to be thrown by snippet");
    } catch (Abort e) {
        if (!e.getMessage().equals("success")) {
            throw e;
        }
    }
}
Also used : Abort(org.knime.base.node.jsnippet.expression.Abort) AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet) JavaSnippetSettings(org.knime.base.node.jsnippet.util.JavaSnippetSettings) Test(org.junit.Test)

Example 2 with AbstractJSnippet

use of org.knime.base.node.jsnippet.expression.AbstractJSnippet in project knime-core by knime.

the class JavaSnippet method createSnippetInstance.

/**
 * Create an instance of the snippet.
 *
 * @return a snippet instance
 */
public AbstractJSnippet createSnippetInstance() {
    Class<? extends AbstractJSnippet> jsnippetClass = createSnippetClass();
    AbstractJSnippet instance;
    try {
        instance = jsnippetClass.newInstance();
    } catch (InstantiationException e) {
        // cannot happen, but rethrow and close resources just in case
        close();
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        // cannot happen, but rethrow and close resources just in case
        close();
        throw new RuntimeException(e);
    }
    if (m_logger != null) {
        instance.attachLogger(m_logger);
    }
    return instance;
}
Also used : AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet)

Example 3 with AbstractJSnippet

use of org.knime.base.node.jsnippet.expression.AbstractJSnippet 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)

Example 4 with AbstractJSnippet

use of org.knime.base.node.jsnippet.expression.AbstractJSnippet in project knime-core by knime.

the class JavaSnippetTest method testAdditionalBundles.

/**
 * Test compiling with additional eclipse/osgi bundles.
 * @throws Exception
 */
@Test
public void testAdditionalBundles() throws Exception {
    final JavaSnippetSettings settings = new JavaSnippetSettings("new Complex(1.0, 1.0);");
    settings.setBundles(new String[] { "org.apache.commons.math3" });
    settings.setScriptImports("import org.apache.commons.math3.complex.Complex;");
    snippet.setSettings(settings);
    final AbstractJSnippet s = snippet.createSnippetInstance();
    assertNotNull(s);
    s.snippet();
}
Also used : AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet) JavaSnippetSettings(org.knime.base.node.jsnippet.util.JavaSnippetSettings) Test(org.junit.Test)

Example 5 with AbstractJSnippet

use of org.knime.base.node.jsnippet.expression.AbstractJSnippet in project knime-core by knime.

the class JavaSnippetTest method testEncoding.

/**
 * Test encoding.
 * @throws Exception
 */
@Test
public void testEncoding() throws Exception {
    final JavaSnippetSettings settings = new JavaSnippetSettings("outString = \"���\";");
    final OutCol outCol = new OutCol();
    outCol.setJavaName("outString");
    outCol.setConverterFactory(ConverterUtil.getConverterFactory(String.class, StringCell.TYPE).get());
    settings.getJavaSnippetFields().getOutColFields().add(outCol);
    snippet.setSettings(settings);
    final AbstractJSnippet s = snippet.createSnippetInstance();
    assertNotNull(s);
    s.snippet();
    final Field outStringField = s.getClass().getField("outString");
    final String string = (String) outStringField.get(s);
    assertEquals(string, "���");
}
Also used : Field(java.lang.reflect.Field) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet) JavaSnippetSettings(org.knime.base.node.jsnippet.util.JavaSnippetSettings) Test(org.junit.Test)

Aggregations

AbstractJSnippet (org.knime.base.node.jsnippet.expression.AbstractJSnippet)5 Test (org.junit.Test)3 JavaSnippetSettings (org.knime.base.node.jsnippet.util.JavaSnippetSettings)3 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)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 Abort (org.knime.base.node.jsnippet.expression.Abort)1 JavaSnippetCompiler (org.knime.base.node.jsnippet.util.JavaSnippetCompiler)1 InCol (org.knime.base.node.jsnippet.util.field.InCol)1 DataCellToJavaConverterRegistry (org.knime.core.data.convert.java.DataCellToJavaConverterRegistry)1 MultiParentClassLoader (org.knime.core.data.convert.util.MultiParentClassLoader)1