1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pipeline.stage;
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22
23 import org.apache.commons.pipeline.StageException;
24
25
26
27
28
29
30
31
32 public class InvokeMethodStage extends BaseStage {
33
34
35 private Method method;
36 private Object[] arguments;
37
38
39
40
41 public InvokeMethodStage(Method method){
42 this.method = method;
43 this.arguments = new Object[] { };
44 }
45
46
47
48
49 public InvokeMethodStage(Method method, Object... arguments) {
50 this.method = method;
51 this.arguments = arguments;
52 }
53
54
55
56
57 public InvokeMethodStage(String className, String methodName, Object... arguments) throws ClassNotFoundException, NoSuchMethodException{
58 Class<?> clazz = InvokeMethodStage.class.getClassLoader().loadClass(className);
59 Class[] argTypes = new Class[arguments.length];
60 for (int i = 0; i < arguments.length; i++) argTypes[i] = arguments[i].getClass();
61
62 this.method = clazz.getMethod(methodName, argTypes);
63 this.arguments = arguments;
64 }
65
66
67
68
69 public Method getMethod(){
70 return this.method;
71 }
72
73
74
75
76 public Object[] getArguments(){
77 return this.arguments;
78 }
79
80
81
82
83 public void process(Object obj) throws org.apache.commons.pipeline.StageException {
84 try {
85 Object result = method.invoke(obj, arguments);
86 this.emit(result);
87 } catch (IllegalAccessException e){
88 throw new StageException(this, e);
89 } catch (InvocationTargetException e){
90 throw new StageException(this, e);
91 }
92 }
93 }