From 1eb2c4e903341904671283699dc22bc611212b08 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 28 Apr 2016 14:38:48 +0100 Subject: [PATCH] JAL-2083 removed use of reflection for Groovy --- src/jalview/bin/Jalview.java | 18 ++------- src/jalview/gui/AlignFrame.java | 12 +----- src/jalview/gui/Desktop.java | 80 +++++++++++---------------------------- 3 files changed, 28 insertions(+), 82 deletions(-) diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index 1eeda98..6c8750f 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -515,20 +515,10 @@ public class Jalview { // Execute the groovy script after we've done all the rendering stuff // and before any images or figures are generated. - if (jalview.bin.Cache.groovyJarsPresent()) - { - System.out.println("Executing script " + groovyscript); - executeGroovyScript(groovyscript, new Object[] { desktop, af }); - - System.out.println("CMD groovy[" + groovyscript - + "] executed successfully!"); - } - else - { - System.err - .println("Sorry. Groovy Support is not available, so ignoring the provided groovy script " - + groovyscript); - } + System.out.println("Executing script " + groovyscript); + executeGroovyScript(groovyscript, new Object[] { desktop, af }); + System.out.println("CMD groovy[" + groovyscript + + "] executed successfully!"); groovyscript = null; } String imageName = "unnamed.png"; diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 44d1d94..8ec5366 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -131,7 +131,6 @@ import java.awt.print.PageFormat; import java.awt.print.PrinterJob; import java.beans.PropertyChangeEvent; import java.io.File; -import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -6121,19 +6120,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, protected void runGroovy_actionPerformed() { Desktop.setCurrentAlignFrame(this); - Object console = Desktop.instance.getGroovyConsole(); + groovy.ui.Console console = Desktop.getGroovyConsole(); if (console != null) { - /* - * use reflection here to avoid compile-time dependency - * on Groovy libraries - */ try { - Class gcClass = getClass().getClassLoader().loadClass( - "groovy.ui.Console"); - Method runScript = gcClass.getMethod("runScript"); - runScript.invoke(console); + console.runScript(); } catch (Exception ex) { System.err.println((ex.toString())); diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 392bf32..7f36f7f 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -72,9 +72,6 @@ import java.beans.PropertyVetoException; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Hashtable; @@ -315,7 +312,20 @@ public class Desktop extends jalview.jbgui.GDesktop implements */ instance = this; doVamsasClientCheck(); - doGroovyCheck(); + + groovyShell = new JMenuItem(); + groovyShell.setText(MessageManager.getString("label.groovy_console")); + groovyShell.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + groovyShell_actionPerformed(); + } + }); + toolsMenu.add(groovyShell); + groovyShell.setVisible(true); + doConfigureStructurePrefs(); setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION")); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -2389,25 +2399,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements protected JMenuItem groovyShell; - public void doGroovyCheck() - { - if (jalview.bin.Cache.groovyJarsPresent()) - { - groovyShell = new JMenuItem(); - groovyShell.setText(MessageManager.getString("label.groovy_console")); - groovyShell.addActionListener(new ActionListener() - { - @Override - public void actionPerformed(ActionEvent e) - { - groovyShell_actionPerformed(); - } - }); - toolsMenu.add(groovyShell); - groovyShell.setVisible(true); - } - } - /** * Accessor method to quickly get all the AlignmentFrames loaded. * @@ -2495,12 +2486,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements */ public void groovyShell_actionPerformed() { - if (!jalview.bin.Cache.groovyJarsPresent()) - { - throw new Error( - MessageManager - .getString("error.implementation_error_cannot_create_groovyshell")); - } try { openGroovyConsole(); @@ -2516,36 +2501,21 @@ public class Desktop extends jalview.jbgui.GDesktop implements } /** - * Open the Groovy console, using reflection to avoid creating compile-time - * dependency on Groovy libraries - * - * @throws ClassNotFoundException - * @throws NoSuchMethodException - * @throws InstantiationException - * @throws IllegalAccessException - * @throws InvocationTargetException + * Open the Groovy console */ - void openGroovyConsole() throws ClassNotFoundException, - NoSuchMethodException, InstantiationException, - IllegalAccessException, InvocationTargetException + void openGroovyConsole() { - Class gcClass = Desktop.class.getClassLoader().loadClass( - "groovy.ui.Console"); - Constructor gccons = gcClass.getConstructor(); - groovyConsole = gccons.newInstance(); + groovyConsole = new groovy.ui.Console(); /* * bind groovy variable 'Jalview' to the Desktop object */ - Method setvar = gcClass.getMethod("setVariable", new Class[] { - String.class, Object.class }); - setvar.invoke(groovyConsole, new Object[] { "Jalview", this }); + groovyConsole.setVariable("Jalview", this); /* * start the console */ - Method run = gcClass.getMethod("run"); - run.invoke(groovyConsole); + groovyConsole.run(); /* * Allow only one console at a time, so that the AlignFrame menu option @@ -2553,8 +2523,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements * Disable 'new console', and enable 'Run script', when the console is * opened, and the reverse when it is closed */ - Method getFrame = gcClass.getMethod("getFrame"); - Window window = (Window) getFrame.invoke(groovyConsole); + Window window = (Window) groovyConsole.getFrame(); window.addWindowListener(new WindowAdapter() { @Override @@ -2986,12 +2955,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements */ private java.util.concurrent.Semaphore block = new Semaphore(0); - /* - * groovy.ui.Console object - if Groovy jars are present and the - * user has activated the Groovy console. Use via reflection to - * avoid compile-time dependency on Groovy libraries. - */ - private static Object groovyConsole; + private static groovy.ui.Console groovyConsole; /** * add another dialog thread to the queue @@ -3221,7 +3185,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements Desktop.currentAlignFrame = currentAlignFrame; } - public static Object getGroovyConsole() + public static groovy.ui.Console getGroovyConsole() { return groovyConsole; } -- 1.7.10.2