Debian Codegraph @

src/main/java/net/sf/antcontrib/util/Reflector.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Utility class to handle reflection on java objects.
26 * Its main purpose is to allow ant-contrib classes
27 * to compile under ant1.5 but allow the classes to
28 * use ant1.6 classes and code if present.
29 * The class is a holder class for an object and
30 * uses java reflection to call methods on the objects.
31 * If things go wrong, BuildExceptions are thrown.
32 * @author Peter Reilly
33 */
34
35 public class Reflector {
36 private Object obj;
37 /**
38 * Constructor for the wrapper using a classname
39 * @param name the classname of the object to construct.
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 * Constructor using a passed in object.
55 * @param obj the object to wrap.
56 */
57
58 public Reflector(Object obj) {
59 this.obj = obj;
60 }
61
62 /**
63 * @return the wrapped object.
64 */
65 public Object getObject() {
66 return obj;
67 }
68
69 /**
70 * Call a method on the object with no parameters.
71 * @param methodName the name of the method to call
72 * @return the object returned by the method
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 * Call a method with an object using a specific
93 * type as for the method parameter.
94 * @param methodName the name of the method
95 * @param className the name of the class of the parameter of the method
96 * @param o the object to use as the argument of the method
97 * @return the object returned by the method
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 * Call a method with an object using a specific
120 * type as for the method parameter.
121 * @param methodName the name of the method
122 * @param classType the class of the parameter of the method
123 * @param o the object to use as the argument of the method
124 * @return the object returned by the method
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 * Call a method with one parameter.
146 * @param methodName the name of the method to call
147 * @param o the object to use as the parameter, this must
148 * be of the same type as the method parameter (not a subclass).
149 * @return the object returned by the method
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 * Call a method with two parameters.
170 * @param methodName the name of the method to call
171 * @param o1 the object to use as the first parameter, this must
172 * be of the same type as the method parameter (not a subclass).
173 * @param o2 the object to use as the second parameter, this must
174 * be of the same type as the method parameter (not a subclass).
175 * @return the object returned by the method
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 }