[Swift-commit] r5312 - branches/release-0.93/src/org/griphyn/vdl/karajan/lib/swiftscript

jonmon at ci.uchicago.edu jonmon at ci.uchicago.edu
Sat Nov 19 17:31:35 CST 2011


Author: jonmon
Date: 2011-11-19 17:31:34 -0600 (Sat, 19 Nov 2011)
New Revision: 5312

Modified:
   branches/release-0.93/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java
Log:
Fixed issue with @java not picking the correct method. Now matches name and parameter list.



Modified: branches/release-0.93/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java
===================================================================
--- branches/release-0.93/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java	2011-11-18 17:43:01 UTC (rev 5311)
+++ branches/release-0.93/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java	2011-11-19 23:31:34 UTC (rev 5312)
@@ -1,6 +1,7 @@
 package org.griphyn.vdl.karajan.lib.swiftscript;
 
 import java.lang.reflect.Method;
+import java.util.Arrays;
 
 import org.globus.cog.karajan.arguments.Arg;
 import org.globus.cog.karajan.stack.VariableStack;
@@ -8,78 +9,97 @@
 import org.griphyn.vdl.karajan.lib.VDLFunction;
 import org.griphyn.vdl.mapping.AbstractDataNode;
 import org.griphyn.vdl.mapping.DSHandle;
-import org.griphyn.vdl.mapping.HandleOpenException;
 import org.griphyn.vdl.mapping.RootDataNode;
 import org.griphyn.vdl.type.Type;
 import org.griphyn.vdl.type.Types;
 
-public class Java extends VDLFunction {
+public class Java extends VDLFunction
+{
 
-    static {
+    static
+    {
         setArguments(Java.class, new Arg[] { Arg.VARGS });
     }
-    
-    protected Object function(VariableStack stack) throws ExecutionException {
+
+    protected Object function(VariableStack stack) throws ExecutionException
+    {
         AbstractDataNode[] args = waitForAllVargs(stack);
-        
-        Method method = getMethod(args); 
+
+        Method method = getMethod(args);
         Object[] p = convertInputs(method, args);
         Type type = returnType(method);
         Object value = invoke(method, p);
         DSHandle result = swiftResult(type, value);
-        
+
         return result;
     }
- 
-    /** 
+
+    /**
        Given the user args, locate the Java Method.
     */
-    Method getMethod(DSHandle[] args) {
-        Method result = null;
-        Class clazz;
-        
+
+    Method getMethod(DSHandle[] args)
+    {
+        Method result;
+        Class<?> clazz;
+
         String lib = "unset";
         String name = "unset";
-        
+
+        Class[] parameterTypes = new Class[args.length-2];
+
         if (args.length < 2)
             throw new RuntimeException
-            ("@java() requires at least two arguments");
-        
-        try {
-            lib = (String)args[0].getValue();
-            name = (String)args[1].getValue();
-            clazz = Class.forName(lib); 
-            Method[] methods = clazz.getMethods();
-            result = null;
-            for (Method m : methods) { 
-                if (m.getName().equals(name)) { 
-                    result = m;
-                    break;
-                }
+                ("@java() requires at least two arguments");
+
+        try
+        {
+            lib = (String) args[0].getValue();
+            name = (String) args[1].getValue();
+            clazz = Class.forName(lib);
+
+            for (int i = 2; i < args.length; i++)
+            {
+                Class p = null;
+                Type  t = args[i].getType();
+
+                if (t.equals(Types.FLOAT))        p = double.class;
+                else if (t.equals(Types.INT))     p = int.class;
+                else if (t.equals(Types.BOOLEAN)) p = boolean.class;
+                else if (t.equals(Types.STRING))  p = String.class;
+                else                              throw new RuntimeException("Cannot use @java with non-primitive types");
+
+                parameterTypes[i-2] = p;
             }
-        } 
-        catch (Exception e) { 
-            e.printStackTrace(); 
+            result = clazz.getMethod(name, parameterTypes);
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
             throw new RuntimeException
-            ("@java(): Error attempting to use: " + args[0]); 
+                ("@java(): Error attempting to use: " + args[0].getValue());
         }
-        
-        if (result == null) 
+
+        if (result == null)
             throw new RuntimeException
-            ("No method: " + name + " in " + lib); 
+                ("No method: " + name + " in " + lib + "with parameter types" + Arrays.toString(parameterTypes));
+
         return result;
     }
-    
-    /** 
-       Convert the user args to a Java Object array. 
+
+    /**
+       Convert the user args to a Java Object array.
     */
-    Object[] convertInputs(Method method, DSHandle[] args) {
+    Object[] convertInputs(Method method, DSHandle[] args)
+    {
         Object[] result = new Object[args.length-2];
         Object a = null;
-        try {
-            for (int i = 2; i < args.length; i++) {
+        try
+        {
+            for (int i = 2; i < args.length; i++)
+            {
                 Type   t = args[i].getType();
-                Object v = args[i].getValue(); 
+                Object v = args[i].getValue();
                 if (t.equals(Types.FLOAT))
                     a = (Double) v;
                 else if (t.equals(Types.INT))
@@ -88,25 +108,25 @@
                     a = (Boolean) v;
                 else if (t.equals(Types.STRING))
                     a = (String) v;
-                result[i-2] = a;   
+                result[i-2] = a;
             }
         }
         catch (Exception e) {
             e.printStackTrace();
             throw new RuntimeException
-            ("Error converting input arguments: \n" + 
-                " to: " + method.getDeclaringClass() + 
-                "." + method + " \n argument: " + a);
+                ("Error converting input arguments: \n" +
+                 " to: " + method.getDeclaringClass() +
+                 "." + method + " \n argument: " + a);
         }
         return result;
     }
-    
+
     Type returnType(Method method) {
         Type result = null;
-        
-        Class rt = method.getReturnType(); 
+
+        Class<?> rt = method.getReturnType();
         if (rt.equals(Double.TYPE))
-            result = Types.FLOAT; 
+            result = Types.FLOAT;
         else if (rt.equals(Integer.TYPE))
             result = Types.INT;
         else if (rt.equals(Boolean.TYPE))
@@ -115,22 +135,26 @@
             result = Types.STRING;
         return result;
     }
-    
-    Object invoke(Method method, Object[] p) { 
-        Object result = null; 
-        try { 
+
+    Object invoke(Method method, Object[] p)
+    {
+        Object result = null;
+        try
+        {
             result = method.invoke(null, p);
         }
-        catch (Exception e) { 
-            e.printStackTrace(); 
+        catch (Exception e)
+        {
+            e.printStackTrace();
             throw new RuntimeException
-            ("Error attempting to invoke: " + 
-                method.getDeclaringClass() + "." + method); 
+                ("Error attempting to invoke: " +
+                 method.getDeclaringClass() + "." + method);
         }
         return result;
     }
-    
-    DSHandle swiftResult(Type type, Object value) { 
+
+    DSHandle swiftResult(Type type, Object value)
+    {
         DSHandle result = new RootDataNode(type);
         result.setValue(value);
         result.closeShallow();




More information about the Swift-commit mailing list