blob: 479e5044b2fbfac9206eb2638d6dbbea92becfea (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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));
}
}
|