JAL-2083 removed use of reflection for Groovy
[jalview.git] / src / jalview / gui / Desktop.java
index 392bf32..7f36f7f 100644 (file)
@@ -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;
   }