From dbda52c5d16b3e2b5257bae557bc2087c6aff1e7 Mon Sep 17 00:00:00 2001 From: Lars Hamann Date: Mon, 15 Nov 2021 10:40:59 +0100 Subject: [PATCH 1/2] Support for newer Java versions - Upgraded JRuby from 1.7.2 to 9.3.1.0 - Added prefixed the internal self variable with $ to keep downward compatibility - Added Writer for extended error messages - Fixed error reporting --- use-core/pom.xml | 2 +- .../uml/ocl/extension/ExtensionOperation.java | 37 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/use-core/pom.xml b/use-core/pom.xml index 5c5b11a69..22bc67901 100644 --- a/use-core/pom.xml +++ b/use-core/pom.xml @@ -45,7 +45,7 @@ org.jruby jruby-core - 1.7.2 + 9.3.1.0 com.ximpleware diff --git a/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java b/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java index 8313baa23..0aa9b0643 100644 --- a/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java +++ b/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java @@ -1,13 +1,5 @@ package org.tzi.use.uml.ocl.extension; -import java.util.ArrayList; -import java.util.List; - -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; - import org.jruby.embed.EvalFailedException; import org.tzi.use.uml.ocl.expr.EvalContext; import org.tzi.use.uml.ocl.expr.operations.OpGeneric; @@ -15,9 +7,16 @@ import org.tzi.use.uml.ocl.value.UndefinedValue; import org.tzi.use.uml.ocl.value.Value; import org.tzi.use.util.Log; -import org.tzi.use.util.NullWriter; import org.tzi.use.util.rubyintegration.RubyHelper; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + public class ExtensionOperation extends OpGeneric { public static class Parameter { @@ -75,14 +74,15 @@ public void addParameter(String name, String typeName) { public Value eval(EvalContext ctx, Value[] args, Type resultType) { ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine rubyEngine = m.getEngineByName("jruby"); - + StringWriter errorWriter = new StringWriter(); + if (rubyEngine == null) throw new RuntimeException("Did not find the ruby engine. Please verify your classpath"); ScriptContext context = rubyEngine.getContext(); - context.setErrorWriter(new NullWriter()); + context.setErrorWriter(errorWriter); - context.setAttribute("self", RubyHelper.useValueToRubyValue(args[0]), ScriptContext.ENGINE_SCOPE); + context.setAttribute("$self", RubyHelper.useValueToRubyValue(args[0]), ScriptContext.ENGINE_SCOPE); for (int i = 0; i < parameter.size(); i++) { context.setAttribute(parameter.get(i).getName(), @@ -104,10 +104,9 @@ public Value eval(EvalContext ctx, Value[] args, Type resultType) { return resultValue; } - } catch (ScriptException e) { - Log.error(e.getMessage()); - } catch (EvalFailedException e) { - Log.error(e.getMessage()); + } catch (ScriptException | EvalFailedException e) { + Log.error(e.getMessage()); + Log.error(errorWriter.toString()); } return UndefinedValue.instance; @@ -149,17 +148,17 @@ public String name() { public void initialize() { this.sourceType = ExtensionManager.getInstance().getType(this.sourceTypeName); if (this.sourceType == null) - throw new RuntimeException("Unknown source type '" + this.sourceType + "'"); + throw new RuntimeException("Unknown source type '" + this.sourceTypeName + "'"); this.resultType = ExtensionManager.getInstance().getType(this.resultTypeName); if (this.resultType == null) - throw new RuntimeException("Unknown result type '" + this.resultType + "'"); + throw new RuntimeException("Unknown result type '" + this.resultTypeName + "'"); for (Parameter par : this.parameter) { par.setType(ExtensionManager.getInstance().getType(par.getTypeName())); if (par.getType() == null) - throw new RuntimeException("Unknown parameter type '" + this.resultType + "'"); + throw new RuntimeException("Unknown parameter type '" + par.getTypeName() + "'"); } } } From e25ec23b1f0e5063e2dbb3a14a426cb42b76f91c Mon Sep 17 00:00:00 2001 From: Lars Hamann Date: Mon, 15 Nov 2021 10:51:20 +0100 Subject: [PATCH 2/2] Support for newer Java versions - Minor improvements for Ruby-operation calls - Output error stream only if data is contained - marked fields as final --- .../tzi/use/uml/ocl/extension/ExtensionOperation.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java b/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java index 0aa9b0643..8983c29ea 100644 --- a/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java +++ b/use-core/src/main/java/org/tzi/use/uml/ocl/extension/ExtensionOperation.java @@ -20,8 +20,8 @@ public class ExtensionOperation extends OpGeneric { public static class Parameter { - private String name; - private String typeName; + private final String name; + private final String typeName; private Type type; public Parameter(String name, String typeName) { @@ -106,7 +106,10 @@ public Value eval(EvalContext ctx, Value[] args, Type resultType) { } catch (ScriptException | EvalFailedException e) { Log.error(e.getMessage()); - Log.error(errorWriter.toString()); + + if (errorWriter.getBuffer().length() > 0) { + Log.error(errorWriter.toString()); + } } return UndefinedValue.instance;