[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 <github@maindrain.net>
This commit is contained in:
Wouter Born 2024-12-07 17:10:28 +01:00 committed by GitHub
parent 37d910d318
commit 98d257982c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -51,7 +51,15 @@ public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory {
ImportCustomizer importCustomizer = new ImportCustomizer();
for (Map.Entry<String, Object> 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());
}