Search in sources :

Example 1 with Type

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

the class JavaSnippet method resolveBuildPathForJavaType.

/**
 * Get file and jar urls required for compiling with given java type
 */
private static synchronized Set<File> resolveBuildPathForJavaType(final Class<?> javaType) {
    if (javaType.isPrimitive()) {
        return Collections.emptySet();
    }
    final Set<File> javaTypeCache = CLASSPATH_FOR_CLASS_CACHE.get(javaType);
    if (javaTypeCache != null) {
        return javaTypeCache;
    }
    final Set<File> result = new LinkedHashSet<>();
    final Set<URL> urls = new LinkedHashSet<>();
    ClassUtil.streamForClassHierarchy(javaType).filter(c -> c.getClassLoader() instanceof ModuleClassLoader).flatMap(c -> {
        final ModuleClassLoader moduleClassLoader = (ModuleClassLoader) c.getClassLoader();
        return Arrays.stream(moduleClassLoader.getClasspathManager().getHostClasspathEntries());
    }).forEach(entry -> {
        final BundleFile file = entry.getBundleFile();
        try {
            final URL url = file.getBaseFile().toURI().toURL();
            urls.add(url);
        } catch (MalformedURLException e) {
            LOGGER.error("Could not resolve URL for bundle file \"" + file.toString() + "\" while assembling build path for custom java type \"" + javaType.getName() + "\"");
        }
        result.add(file.getBaseFile());
    });
    /* Check whether the java snippet compiler can later find the class with this classpath */
    try (final URLClassLoader classpathClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]))) {
        classpathClassLoader.loadClass(javaType.getName());
    } catch (NoClassDefFoundError | ClassNotFoundException e) {
        LOGGER.error("Classpath for \"" + javaType.getName() + "\" could not be assembled.", e);
        // indicate that this type should not be provided in java snippet
        return null;
    } catch (IOException e) {
        // thrown by URLClassLoader.close()
        LOGGER.error("Unable to close classloader used for testing of custom type classpath.", e);
    }
    if (result.contains(null)) {
        throw new IllegalStateException("Couldn't assemble classpath for custom type \"" + javaType + "\", illegal <null> value in list:\n  " + result.stream().map(f -> f == null ? "<NULL>" : f.getAbsolutePath()).collect(Collectors.joining("\n  ")));
    }
    CLASSPATH_FOR_CLASS_CACHE.put(javaType, result);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) RowKey(org.knime.core.data.RowKey) Parser(org.fife.ui.rsyntaxtextarea.parser.Parser) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) JavaSnippetSettings(org.knime.base.node.jsnippet.util.JavaSnippetSettings) CollectionConverterFactory(org.knime.core.data.convert.java.CollectionConverterFactory) JavaSnippetDocument(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument) URLClassLoader(java.net.URLClassLoader) ConverterUtil(org.knime.base.node.jsnippet.type.ConverterUtil) Map(java.util.Map) EclipseFileObject(org.eclipse.jdt.internal.compiler.tool.EclipseFileObject) JavaSnippetUtil(org.knime.base.node.jsnippet.util.JavaSnippetUtil) MultiParentClassLoader(org.knime.core.data.convert.util.MultiParentClassLoader) Set(java.util.Set) BadLocationException(javax.swing.text.BadLocationException) StandardCharsets(java.nio.charset.StandardCharsets) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JavaFileObject(javax.tools.JavaFileObject) JavaFieldList(org.knime.base.node.jsnippet.util.JavaFieldList) Stream(java.util.stream.Stream) Type(org.knime.base.node.jsnippet.expression.Type) JavaToDataCellConverterRegistry(org.knime.core.data.convert.datacell.JavaToDataCellConverterRegistry) Kind(javax.tools.JavaFileObject.Kind) FlowVariable(org.knime.core.node.workflow.FlowVariable) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BundleNamespace(org.osgi.framework.namespace.BundleNamespace) JavaSnippetFields(org.knime.base.node.jsnippet.util.JavaSnippetFields) DocumentEvent(javax.swing.event.DocumentEvent) DataCell(org.knime.core.data.DataCell) LinkedHashSet(java.util.LinkedHashSet) InCol(org.knime.base.node.jsnippet.util.field.InCol) ClassUtil(org.knime.core.data.convert.util.ClassUtil) DefaultRow(org.knime.core.data.def.DefaultRow) JavaSnippetTemplate(org.knime.base.node.jsnippet.template.JavaSnippetTemplate) ValidationReport(org.knime.base.node.jsnippet.util.ValidationReport) KNIMEConstants(org.knime.core.node.KNIMEConstants) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) GUARDED_FIELDS(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_FIELDS) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Field(java.lang.reflect.Field) FlowVariableException(org.knime.base.node.jsnippet.expression.FlowVariableException) File(java.io.File) DataRow(org.knime.core.data.DataRow) DataCellToJavaConverterRegistry(org.knime.core.data.convert.java.DataCellToJavaConverterRegistry) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) GuardedDocument(org.knime.base.node.jsnippet.guarded.GuardedDocument) Platform(org.eclipse.core.runtime.Platform) FileUtil(org.knime.core.util.FileUtil) DataType(org.knime.core.data.DataType) DocumentListener(javax.swing.event.DocumentListener) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) URL(java.net.URL) Abort(org.knime.base.node.jsnippet.expression.Abort) GUARDED_BODY_END(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_BODY_END) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) JSnippet(org.knime.base.node.jsnippet.util.JSnippet) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) CompilationTask(javax.tools.JavaCompiler.CompilationTask) AbstractJSnippet(org.knime.base.node.jsnippet.expression.AbstractJSnippet) DataColumnSpec(org.knime.core.data.DataColumnSpec) GUARDED_IMPORTS(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_IMPORTS) Locale(java.util.Locale) Diagnostic(javax.tools.Diagnostic) FlowVariableRepository(org.knime.base.node.jsnippet.util.FlowVariableRepository) Cell(org.knime.base.node.jsnippet.expression.Cell) Bundle(org.osgi.framework.Bundle) DiagnosticCollector(javax.tools.DiagnosticCollector) GUARDED_BODY_START(org.knime.base.node.jsnippet.guarded.JavaSnippetDocument.GUARDED_BODY_START) Collection(java.util.Collection) JavaField(org.knime.base.node.jsnippet.util.field.JavaField) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) ColumnException(org.knime.base.node.jsnippet.expression.ColumnException) Objects(java.util.Objects) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) Writer(java.io.Writer) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) Optional(java.util.Optional) Document(javax.swing.text.Document) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) GuardedSection(org.knime.base.node.jsnippet.guarded.GuardedSection) DataTableSpec(org.knime.core.data.DataTableSpec) TypeException(org.knime.base.node.jsnippet.expression.TypeException) JavaSnippetCompiler(org.knime.base.node.jsnippet.util.JavaSnippetCompiler) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) ExecutionContext(org.knime.core.node.ExecutionContext) JarEntry(java.util.jar.JarEntry) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) CellFactory(org.knime.core.data.container.CellFactory) NodeLogger(org.knime.core.node.NodeLogger) OutputStreamWriter(java.io.OutputStreamWriter) JarOutputStream(java.util.jar.JarOutputStream) BundleWiring(org.osgi.framework.wiring.BundleWiring) MalformedURLException(java.net.MalformedURLException) InVar(org.knime.base.node.jsnippet.util.field.InVar) FileLocator(org.eclipse.core.runtime.FileLocator) JSnippetParser(org.knime.base.node.jsnippet.ui.JSnippetParser) Closeable(java.io.Closeable) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BundleWire(org.osgi.framework.wiring.BundleWire) Collections(java.util.Collections) InputStream(java.io.InputStream) MalformedURLException(java.net.MalformedURLException) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) IOException(java.io.IOException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) File(java.io.File) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 Closeable (java.io.Closeable)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1