Skip to content
Permalink
Browse files

fix: Deprecate redundant methods

The now deprecated methods were merely wrapping calls to methods of the engine passed as a parameter of the deprecated method without providing any additional error handling or other benifit, so deprecate them for eventual removal and use the engine directly in calling JMRI classes.
  • Loading branch information...
rhwood committed Sep 18, 2019
1 parent f6c2474 commit 90c37768c3875c7dc80936ade877106c535ad467
Showing with 46 additions and 17 deletions.
  1. +7 −7 java/src/jmri/plaf/macosx/Jdk9Application.java
  2. +39 −10 java/src/jmri/script/JmriScriptEngineManager.java
@@ -38,17 +38,17 @@
*/
class Jdk9Application extends Application {

private final static Logger log = LoggerFactory.getLogger(Jdk9Application.class);
private static final Logger log = LoggerFactory.getLogger(Jdk9Application.class);

Jdk9Application() {
}

private void setHandler(String methodName, String handlerType, Object handler) {
try {
Class parameterType = Class.forName(handlerType);
Class parameterTypes[] = {parameterType};
Class<?> parameterType = Class.forName(handlerType);
Class<?>[] parameterTypes = {parameterType};
Method method = java.awt.Desktop.class.getDeclaredMethod(methodName, parameterTypes);
Object parameters[] = {handler};
Object[] parameters = {handler};
method.invoke(Desktop.getDesktop(), parameters);
} catch (NoClassDefFoundError | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
log.debug("Exception calling {} with {}", methodName, handlerType, ex);
@@ -61,7 +61,7 @@ public void setAboutHandler(final AboutHandler handler) {
try {
InputStreamReader reader = new InputStreamReader(Jdk9Application.class.getResourceAsStream("AboutHandler.js")); // NOI18N
ScriptEngine engine = JmriScriptEngineManager.getDefault().getEngineByMimeType("js"); // NOI18N
JmriScriptEngineManager.getDefault().eval(reader, engine, this.getContext(handler));
engine.eval(reader, this.getContext(handler));
} catch (ScriptException ex) {
log.error("Unable to execute script AboutHandler.js", ex);
}
@@ -76,7 +76,7 @@ public void setPreferencesHandler(final PreferencesHandler handler) {
try {
InputStreamReader reader = new InputStreamReader(Jdk9Application.class.getResourceAsStream("PreferencesHandler.js")); // NOI18N
ScriptEngine engine = JmriScriptEngineManager.getDefault().getEngineByMimeType("js"); // NOI18N
JmriScriptEngineManager.getDefault().eval(reader, engine, this.getContext(handler));
engine.eval(reader, this.getContext(handler));
} catch (ScriptException ex) {
log.error("Unable to execute script PreferencesHandler.js", ex);
}
@@ -91,7 +91,7 @@ public void setQuitHandler(final QuitHandler handler) {
try {
InputStreamReader reader = new InputStreamReader(Jdk9Application.class.getResourceAsStream("QuitHandler.js")); // NOI18N
ScriptEngine engine = JmriScriptEngineManager.getDefault().getEngineByMimeType("js"); // NOI18N
JmriScriptEngineManager.getDefault().eval(reader, engine, this.getContext(handler));
engine.eval(reader, this.getContext(handler));
} catch (ScriptException ex) {
log.error("Unable to execute script QuitHandler.js", ex);
}
@@ -279,7 +279,9 @@ public Object eval(String script, ScriptEngine engine) throws ScriptException {
* @param engine The script engine.
* @return The results of evaluating the script.
* @throws javax.script.ScriptException if there is an error in the script.
* @deprecated since 4.17.5 without direct replacement
*/
@Deprecated
public Object eval(Reader reader, ScriptEngine engine) throws ScriptException {
return engine.eval(reader);
}
@@ -292,7 +294,9 @@ public Object eval(Reader reader, ScriptEngine engine) throws ScriptException {
* @param bindings Bindings passed to the script.
* @return The results of evaluating the script.
* @throws javax.script.ScriptException if there is an error in the script.
* @deprecated since 4.17.5 without direct replacement
*/
@Deprecated
public Object eval(Reader reader, ScriptEngine engine, Bindings bindings) throws ScriptException {
return engine.eval(reader, bindings);
}
@@ -305,7 +309,9 @@ public Object eval(Reader reader, ScriptEngine engine, Bindings bindings) throws
* @param context Context for the script.
* @return The results of evaluating the script.
* @throws javax.script.ScriptException if there is an error in the script.
* @deprecated since 4.17.5 without direct replacement
*/
@Deprecated
public Object eval(Reader reader, ScriptEngine engine, ScriptContext context) throws ScriptException {
return engine.eval(reader, context);
}
@@ -373,33 +379,56 @@ public Object eval(File file, ScriptContext context) throws ScriptException, IOE
* @throws java.io.FileNotFoundException if the script file cannot be found.
* @throws java.io.IOException if the script file cannot be read.
*/

@CheckForNull
private Object eval(File file, @CheckForNull ScriptContext context, @CheckForNull Bindings bindings)
throws ScriptException, IOException {
ScriptEngine engine = this.getEngine(FilenameUtils.getExtension(file.getName()), EXTENSION);
ScriptEngine engine;
Object result = null;
if (PYTHON.equals(engine.getFactory().getEngineName()) && this.jython != null) {
try (FileInputStream fi = new FileInputStream(file)) {
this.jython.execfile(fi);
}
} else {
if ((engine = getEngineOrEval(file)) != null) {
try (FileReader fr = new FileReader(file)) {
if (context != null) {
result = eval(fr, engine, context);
result = engine.eval(fr, context);
} else if (bindings != null) {
result = eval(fr, engine, bindings);
result = engine.eval(fr, bindings);
} else {
result = eval(fr, engine);
result = engine.eval(fr);
}
}
}
return result;
}

/**
* Get the ScriptEngine to evaluate the file with; if not using a
* ScriptEngine to evaluate Python files, evaluate the file with a
* {@link org.python.util.PythonInterpreter} and do not return a
* ScriptEngine.
*
* @param file the script file to evaluate.
* @return the ScriptEngine or null if evaluated with a PythonInterpreter.
* @throws javax.script.ScriptException if there is an error evaluating the
* script.
* @throws java.io.FileNotFoundException if the script file cannot be found.
* @throws java.io.IOException if the script file cannot be read.
*/
@CheckForNull
private ScriptEngine getEngineOrEval(File file) throws ScriptException, IOException {
ScriptEngine engine = this.getEngine(FilenameUtils.getExtension(file.getName()), EXTENSION);
if (PYTHON.equals(engine.getFactory().getEngineName()) && this.jython != null) {
try (FileInputStream fi = new FileInputStream(file)) {
this.jython.execfile(fi);
}
return null;
}
return engine;
}

/**
* Run a script, suppressing common errors. Note that the file needs to have
* a registered extension, or a NullPointerException will be thrown.
* <p>
* <strong>Note:</strong> this will eventually be deprecated in favor of using
* {@link #eval(File)} and having callers handle exceptions.
*
* @param file the script to run.
*/

0 comments on commit 90c3776

Please sign in to comment.
You can’t perform that action at this time.