From 2ed0c7c1f3e98bcf64b71bf9bca5299ced4eb340 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 30 Sep 2022 14:47:53 -0600 Subject: [PATCH] [jrubyscripting] clean up assignment of class objects (#13451) pass the actual class objects through, and post-process in Ruby, to avoid the rescue nil -- and by extension, any issues with visibility into private objects from JRuby. Signed-off-by: Cody Cutrer --- .../internal/JRubyScriptEngineFactory.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java b/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java index 4879b79227bbd..326ffbc594b62 100644 --- a/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java +++ b/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java @@ -19,6 +19,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptException; @@ -123,12 +124,12 @@ public void scopeValues(ScriptEngine scriptEngine, Map scopeValu } private void importClassesToRuby(ScriptEngine scriptEngine, Map objects) { - String import_statements = objects.entrySet() // - .stream() // - .map(entry -> "java_import " + ((Class) entry.getValue()).getName() + " rescue nil") // - .collect(Collectors.joining("\n")); try { - scriptEngine.eval(import_statements); + scriptEngine.put("__classes", objects); + final String code = "__classes.each { |(name, klass)| Object.const_set(name, klass.ruby_class) }"; + scriptEngine.eval(code); + // clean up our temporary variable + scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).remove("__classes"); } catch (ScriptException e) { logger.debug("Error importing java classes", e); }