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