diff options
| author | Joshua Rahm <joshua.rahm@colorado.edu> | 2015-01-27 18:40:32 -0700 |
|---|---|---|
| committer | Joshua Rahm <joshua.rahm@colorado.edu> | 2015-01-27 18:40:32 -0700 |
| commit | 5f3fb9afece2125cbeba79d61a8d88460b7878d7 (patch) | |
| tree | b0e1e60bae9927a9449561bf7fe9431a54d12be9 /project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedMethod.java | |
| download | LegacyQBar-5f3fb9afece2125cbeba79d61a8d88460b7878d7.tar.gz LegacyQBar-5f3fb9afece2125cbeba79d61a8d88460b7878d7.tar.bz2 LegacyQBar-5f3fb9afece2125cbeba79d61a8d88460b7878d7.zip | |
initial commit
Diffstat (limited to 'project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedMethod.java')
| -rw-r--r-- | project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedMethod.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedMethod.java b/project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedMethod.java new file mode 100644 index 0000000..479e504 --- /dev/null +++ b/project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedMethod.java @@ -0,0 +1,95 @@ +package com.modulus.qbar.integration; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import com.modulus.qbar.core.QBFunction; +import com.modulus.qbar.core.QBObject; + +public class QBWrappedMethod extends QBFunction{ + private transient Method wrapped; + private boolean isStatic; + + private String methodName; + private Class<?> clazz; + + public QBWrappedMethod(Method toWrap) { + super(toWrap.getParameterTypes().length); + this.wrapped = toWrap; + + isStatic = Modifier.isStatic(toWrap.getModifiers()); + + if(!isStatic){ + this.setArgc(this.getArgc() + 1); + } + } + + public QBWrappedMethod( Class<?> clazz, String methodName, int argc ){ + super( argc ); + + this.clazz = clazz; + this.methodName = methodName; + } + + // since java is sometimes annoying and will not let you serialize Methods, + // we need to send the information and generate them on the fly + private void setup(){ + Method[] methods = this.clazz.getMethods(); + + for(Method method : methods ){ + if(method.getName().equals(methodName)){ + this.wrapped = method; + break; + } + } + + isStatic = Modifier.isStatic(this.wrapped.getModifiers()); + } + + @Override + public QBObject execute(QBObject[] args){ + try { + if(wrapped == null) + setup(); + + if(isStatic){ + return executeStatic(args); + } else{ + Object[] objs = new Object[args.length - 1]; + Object caller =args[args.length - 1].getWrapped(); + + for( int i = 0;i < args.length-1; i++){ + // System.out.println("args["+i+"]: " + args[i] + " wrap: " + args[i].getWrapped()); + objs[i] = args[i].getWrapped(); + } + + // System.out.println("Method " + wrapped + "\nargs: " + Arrays.toString(objs) + "\nargs2: " + Arrays.toString(args)); + return QBWrappedObject.wrap(wrapped.invoke(caller, objs)); + } + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return null; + } + + private QBObject executeStatic(QBObject[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ + Object[] objs = new Object[args.length]; + for( int i = 0;i < objs.length; i++){ + objs[i] = args[i].getWrapped(); + } + // System.out.println(Arrays.toString(objs) + " " + this.getArgc() + "\n\t" + wrapped); + + return QBWrappedObject.wrap(wrapped.invoke(null, objs)); + } + +} |