| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package net.sf.antcontrib.util; |
| 18 |
|
| 19 |
import java.lang.reflect.Constructor; |
| 20 |
import java.lang.reflect.InvocationTargetException; |
| 21 |
import java.lang.reflect.Method; |
| 22 |
import org.apache.tools.ant.BuildException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class Reflector { |
| 36 |
private Object obj; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public Reflector(String name) { |
| 42 |
try { |
| 43 |
Class clazz; |
| 44 |
clazz = Class.forName(name); |
| 45 |
Constructor constructor; |
| 46 |
constructor = clazz.getConstructor(new Class[]{}); |
| 47 |
obj = constructor.newInstance(new Object[]{}); |
| 48 |
} catch (Throwable t) { |
| 49 |
throw new BuildException(t); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public Reflector(Object obj) { |
| 59 |
this.obj = obj; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public Object getObject() { |
| 66 |
return obj; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Object call(String methodName) { |
| 75 |
try { |
| 76 |
Method method; |
| 77 |
method = obj.getClass().getMethod( |
| 78 |
methodName, new Class[] {}); |
| 79 |
return method.invoke(obj, new Object[] {}); |
| 80 |
} catch (InvocationTargetException t) { |
| 81 |
Throwable t2 = t.getTargetException(); |
| 82 |
if (t2 instanceof BuildException) { |
| 83 |
throw (BuildException) t2; |
| 84 |
} |
| 85 |
throw new BuildException(t2); |
| 86 |
} catch (Throwable t) { |
| 87 |
throw new BuildException(t); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Object callExplicit( |
| 100 |
String methodName, String className, Object o) { |
| 101 |
try { |
| 102 |
Method method; |
| 103 |
Class clazz = Class.forName(className); |
| 104 |
method = obj.getClass().getMethod( |
| 105 |
methodName, new Class[] {clazz}); |
| 106 |
return method.invoke(obj, new Object[] {o}); |
| 107 |
} catch (InvocationTargetException t) { |
| 108 |
Throwable t2 = t.getTargetException(); |
| 109 |
if (t2 instanceof BuildException) { |
| 110 |
throw (BuildException) t2; |
| 111 |
} |
| 112 |
throw new BuildException(t2); |
| 113 |
} catch (Throwable t) { |
| 114 |
throw new BuildException(t); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public Object callExplicit( |
| 127 |
String methodName, Class classType, Object o) { |
| 128 |
try { |
| 129 |
Method method; |
| 130 |
method = obj.getClass().getMethod( |
| 131 |
methodName, new Class[] {classType}); |
| 132 |
return method.invoke(obj, new Object[] {o}); |
| 133 |
} catch (InvocationTargetException t) { |
| 134 |
Throwable t2 = t.getTargetException(); |
| 135 |
if (t2 instanceof BuildException) { |
| 136 |
throw (BuildException) t2; |
| 137 |
} |
| 138 |
throw new BuildException(t2); |
| 139 |
} catch (Throwable t) { |
| 140 |
throw new BuildException(t); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public Object call(String methodName, Object o) { |
| 152 |
try { |
| 153 |
Method method; |
| 154 |
method = obj.getClass().getMethod( |
| 155 |
methodName, new Class[] {o.getClass()}); |
| 156 |
return method.invoke(obj, new Object[] {o}); |
| 157 |
} catch (InvocationTargetException t) { |
| 158 |
Throwable t2 = t.getTargetException(); |
| 159 |
if (t2 instanceof BuildException) { |
| 160 |
throw (BuildException) t2; |
| 161 |
} |
| 162 |
throw new BuildException(t2); |
| 163 |
} catch (Throwable t) { |
| 164 |
throw new BuildException(t); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public Object call(String methodName, Object o1, Object o2) { |
| 178 |
try { |
| 179 |
Method method; |
| 180 |
method = obj.getClass().getMethod( |
| 181 |
methodName, new Class[] {o1.getClass(), o2.getClass()}); |
| 182 |
return method.invoke(obj, new Object[] {o1, o2}); |
| 183 |
} catch (InvocationTargetException t) { |
| 184 |
Throwable t2 = t.getTargetException(); |
| 185 |
if (t2 instanceof BuildException) { |
| 186 |
throw (BuildException) t2; |
| 187 |
} |
| 188 |
throw new BuildException(t2); |
| 189 |
} catch (Throwable t) { |
| 190 |
throw new BuildException(t); |
| 191 |
} |
| 192 |
} |
| 193 |
} |