From 98d257982c0c37db2712f2f8f0fc33e703700df7 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sat, 7 Dec 2024 17:10:28 +0100 Subject: [PATCH] [groovyscripting] Prevent CNFE for scoped classes unavailable to the class loader (#17860) Fixes the ClassNotFoundException when using Thing actions caused by #17383. The GroovyClassLoader loads classes by name however the Thing actions classes cannot be loaded by name because they are internal classes. Fixes #17683 Signed-off-by: Wouter Born --- .../internal/GroovyScriptEngineFactory.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java index d2dac88609d..327fb0f615f 100644 --- a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java +++ b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java @@ -51,7 +51,15 @@ public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory { ImportCustomizer importCustomizer = new ImportCustomizer(); for (Map.Entry entry : scopeValues.entrySet()) { if (entry.getValue() instanceof Class clazz) { - importCustomizer.addImport(entry.getKey(), clazz.getCanonicalName()); + String canonicalName = clazz.getCanonicalName(); + try { + // Only add imports for classes that are available to the classloader + getClass().getClassLoader().loadClass(canonicalName); + importCustomizer.addImport(entry.getKey(), canonicalName); + logger.debug("Added import for {} as {}", entry.getKey(), canonicalName); + } catch (ClassNotFoundException e) { + logger.debug("Unable to add import for {} as {}", entry.getKey(), canonicalName, e); + } } else { scriptEngine.put(entry.getKey(), entry.getValue()); }