KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > ReflectUtil


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import java.lang.reflect.Field JavaDoc;
24
25 /**
26  * Utility class to handle reflection on java objects.
27  * The class contains static methods to call reflection
28  * methods, catch any exceptions, converting them
29  * to BuildExceptions.
30  */

31
32 public class ReflectUtil {
33
34     /** private constructor */
35     private ReflectUtil() {
36     }
37
38     /**
39      * Call a method on the object with no parameters.
40      * @param obj the object to invoke the method on.
41      * @param methodName the name of the method to call
42      * @return the object returned by the method
43      */

44     public static Object JavaDoc invoke(Object JavaDoc obj, String JavaDoc methodName) {
45         try {
46             Method JavaDoc method;
47             method = obj.getClass().getMethod(
48                 methodName, (Class JavaDoc[]) null);
49             return method.invoke(obj, (Object JavaDoc[]) null);
50         } catch (Exception JavaDoc t) {
51             throwBuildException(t);
52             return null; // NotReached
53
}
54     }
55
56     /**
57      * Call a method on the object with one argument.
58      * @param obj the object to invoke the method on.
59      * @param methodName the name of the method to call
60      * @param argType the type of argument.
61      * @param arg the value of the argument.
62      * @return the object returned by the method
63      */

64     public static Object JavaDoc invoke(
65         Object JavaDoc obj, String JavaDoc methodName, Class JavaDoc argType, Object JavaDoc arg) {
66         try {
67             Method JavaDoc method;
68             method = obj.getClass().getMethod(
69                 methodName, new Class JavaDoc[] {argType});
70             return method.invoke(obj, new Object JavaDoc[] {arg});
71         } catch (Exception JavaDoc t) {
72             throwBuildException(t);
73             return null; // NotReached
74
}
75     }
76
77     /**
78      * Call a method on the object with two argument.
79      * @param obj the object to invoke the method on.
80      * @param methodName the name of the method to call
81      * @param argType1 the type of the first argument.
82      * @param arg1 the value of the first argument.
83      * @param argType2 the type of the second argument.
84      * @param arg2 the value of the second argument.
85      * @return the object returned by the method
86      */

87     public static Object JavaDoc invoke(
88         Object JavaDoc obj, String JavaDoc methodName, Class JavaDoc argType1, Object JavaDoc arg1,
89         Class JavaDoc argType2, Object JavaDoc arg2) {
90         try {
91             Method JavaDoc method;
92             method = obj.getClass().getMethod(
93                 methodName, new Class JavaDoc[] {argType1, argType2});
94             return method.invoke(obj, new Object JavaDoc[] {arg1, arg2});
95         } catch (Exception JavaDoc t) {
96             throwBuildException(t);
97             return null; // NotReached
98
}
99     }
100
101     /**
102      * Get the value of a field in an object.
103      * @param obj the object to look at.
104      * @param fieldName the name of the field in the object.
105      * @return the value of the field.
106      * @throws BuildException if there is an error.
107      */

108     public static Object JavaDoc getField(Object JavaDoc obj, String JavaDoc fieldName)
109         throws BuildException {
110         try {
111             Field JavaDoc field = obj.getClass().getDeclaredField(fieldName);
112             field.setAccessible(true);
113             return field.get(obj);
114         } catch (Exception JavaDoc t) {
115             throwBuildException(t);
116             return null; // NotReached
117
}
118     }
119
120     /**
121      * A method to convert an invocationTargetException to
122      * a buildexception and throw it.
123      * @param t the invocation target exception.
124      * @throws BuildException the converted exception.
125      */

126     public static void throwBuildException(Exception JavaDoc t)
127         throws BuildException {
128         if (t instanceof InvocationTargetException JavaDoc) {
129             Throwable JavaDoc t2 = ((InvocationTargetException JavaDoc) t)
130                 .getTargetException();
131             if (t2 instanceof BuildException) {
132                 throw (BuildException) t2;
133             }
134             throw new BuildException(t2);
135         } else {
136             throw new BuildException(t);
137         }
138     }
139 }
140
Popular Tags