use of org.knime.ext.sun.nodes.script.compile.JavaCodeCompiler in project knime-core by knime.
the class Expression method createClass.
/* Called from the constructor. */
@SuppressWarnings("unchecked")
private Class<? extends AbstractSnippetExpression> createClass(final String body, final JavaScriptingSettings settings) throws CompilationFailedException {
Class<?> rType = settings.getReturnType();
int version = settings.getExpressionVersion();
boolean isArrayReturn = settings.isArrayReturn();
String header = settings.getHeader();
String source;
String name = "Expression" + COUNTER.getAndIncrement();
String[] imports = null != settings.getImports() ? settings.getImports() : getDefaultImports();
// Generate the well known source of the Expression
switch(version) {
case VERSION_1X:
source = generateSourceVersion1(name, body, rType, imports);
break;
case VERSION_2X:
source = generateSourceVersion2(name, body, header, rType, imports, isArrayReturn);
break;
default:
throw new CompilationFailedException("Unknown snippet version number: " + version);
}
try {
ensureTempClassPathExists();
} catch (IOException e1) {
throw new CompilationFailedException("Unable to copy required class path files", e1);
}
File[] additionalJarFiles;
try {
additionalJarFiles = settings.getJarFilesAsFiles();
} catch (InvalidSettingsException e1) {
throw new CompilationFailedException(e1.getMessage(), e1);
}
File[] classPathFiles = new File[additionalJarFiles.length + 1];
classPathFiles[0] = tempClassPath;
System.arraycopy(additionalJarFiles, 0, classPathFiles, 1, additionalJarFiles.length);
JavaCodeCompiler compiler = new JavaCodeCompiler();
compiler.setSources(new InMemorySourceJavaFileObject(name, source));
compiler.setClasspaths(classPathFiles);
compiler.compile();
ClassLoader loader = compiler.createClassLoader(compiler.getClass().getClassLoader());
try {
return (Class<? extends AbstractSnippetExpression>) loader.loadClass(name);
} catch (ClassNotFoundException e) {
throw new CompilationFailedException("Could not load generated class", e);
}
}
Aggregations