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