Skip to content

Commit 98d2579

Browse files
authored
[groovyscripting] Prevent CNFE for scoped classes unavailable to the class loader (openhab#17860)
Fixes the ClassNotFoundException when using Thing actions caused by openhab#17383. The GroovyClassLoader loads classes by name however the Thing actions classes cannot be loaded by name because they are internal classes. Fixes openhab#17683 Signed-off-by: Wouter Born <[email protected]>
1 parent 37d910d commit 98d2579

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValu
5151
ImportCustomizer importCustomizer = new ImportCustomizer();
5252
for (Map.Entry<String, Object> entry : scopeValues.entrySet()) {
5353
if (entry.getValue() instanceof Class<?> clazz) {
54-
importCustomizer.addImport(entry.getKey(), clazz.getCanonicalName());
54+
String canonicalName = clazz.getCanonicalName();
55+
try {
56+
// Only add imports for classes that are available to the classloader
57+
getClass().getClassLoader().loadClass(canonicalName);
58+
importCustomizer.addImport(entry.getKey(), canonicalName);
59+
logger.debug("Added import for {} as {}", entry.getKey(), canonicalName);
60+
} catch (ClassNotFoundException e) {
61+
logger.debug("Unable to add import for {} as {}", entry.getKey(), canonicalName, e);
62+
}
5563
} else {
5664
scriptEngine.put(entry.getKey(), entry.getValue());
5765
}

0 commit comments

Comments
 (0)