-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathJavaMethodWrapper.java
More file actions
57 lines (46 loc) · 1.59 KB
/
JavaMethodWrapper.java
File metadata and controls
57 lines (46 loc) · 1.59 KB
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
package nodebox.node;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
public class JavaMethodWrapper implements NodeCode {
public static final String TYPE_JAVA = "java".intern();
private Class methodClass;
private String methodName;
private Method method;
public JavaMethodWrapper(Class methodClass, String methodName) {
this.methodClass = methodClass;
this.methodName = methodName;
try {
this.method = methodClass.getMethod(methodName, Node.class, ProcessingContext.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException("The given method does not exist.");
}
if (!Modifier.isStatic(this.method.getModifiers())) {
throw new RuntimeException("The given method is not static.");
}
}
public Class getMethodClass() {
return methodClass;
}
public String getMethodName() {
return methodName;
}
public Method getMethod() {
return method;
}
public Object cook(Node node, ProcessingContext context) {
try {
return method.invoke(null, node, context);
} catch (IllegalAccessException e) {
throw new RuntimeException("Illegal access exception", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Invocation target exception", e);
}
}
public String getSource() {
return "// Source not available";
}
public String getType() {
return TYPE_JAVA;
}
}