[Swift-commit] r5305 - trunk/src/org/griphyn/vdl/karajan/lib/swiftscript

jonmon at ci.uchicago.edu jonmon at ci.uchicago.edu
Wed Nov 16 09:17:43 CST 2011


Author: jonmon
Date: 2011-11-16 09:17:42 -0600 (Wed, 16 Nov 2011)
New Revision: 5305

Modified:
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java
Log:
@java now selects to correct function based on parameter types and function name.



Modified: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java	2011-11-15 16:27:02 UTC (rev 5304)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java	2011-11-16 15:17:42 UTC (rev 5305)
@@ -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;
@@ -12,73 +13,93 @@
 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;
+
+    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 {
+                ("@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;
-                }
+            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))
@@ -87,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))
@@ -114,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