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;
}
}
}
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;
}
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);
}
}
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();
}
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, "���");
}
Aggregations