KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constructor JavaDoc;
21
22 /**
23  * Utility class to handle reflection on java objects.
24  * The class is a holder class for an object and
25  * uses java reflection to call methods on the objects.
26  * If things go wrong, BuildExceptions are thrown.
27  */

28
29 public class ReflectWrapper {
30     private Object JavaDoc obj;
31     /**
32      * Construct a wrapped object using the no arg constructor.
33      * @param loader the classloader to use to construct the class.
34      * @param name the classname of the object to construct.
35      */

36     public ReflectWrapper(ClassLoader JavaDoc loader, String JavaDoc name) {
37         try {
38             Class JavaDoc clazz;
39             clazz = Class.forName(name, true, loader);
40             Constructor JavaDoc constructor;
41             constructor = clazz.getConstructor((Class JavaDoc[]) null);
42             obj = constructor.newInstance((Object JavaDoc[]) null);
43         } catch (Exception JavaDoc t) {
44             ReflectUtil.throwBuildException(t);
45         }
46     }
47
48     /**
49      * Constructor using a passed in object.
50      * @param obj the object to wrap.
51      */

52     public ReflectWrapper(Object JavaDoc obj) {
53         this.obj = obj;
54     }
55
56     /**
57      * @return the wrapped object.
58      */

59     public Object JavaDoc getObject() {
60         return obj;
61     }
62
63     /**
64      * Call a method on the object with no parameters.
65      * @param methodName the name of the method to call
66      * @return the object returned by the method
67      */

68     public Object JavaDoc invoke(String JavaDoc methodName) {
69         return ReflectUtil.invoke(obj, methodName);
70     }
71
72     /**
73      * Call a method on the object with one argument.
74      * @param methodName the name of the method to call
75      * @param argType the type of argument.
76      * @param arg the value of the argument.
77      * @return the object returned by the method
78      */

79     public Object JavaDoc invoke(
80         String JavaDoc methodName, Class JavaDoc argType, Object JavaDoc arg) {
81         return ReflectUtil.invoke(obj, methodName, argType, arg);
82     }
83
84     /**
85      * Call a method on the object with one argument.
86      * @param methodName the name of the method to call
87      * @param argType1 the type of the first argument.
88      * @param arg1 the value of the first argument.
89      * @param argType2 the type of the second argument.
90      * @param arg2 the value of the second argument.
91      * @return the object returned by the method
92      */

93     public Object JavaDoc invoke(
94         String JavaDoc methodName, Class JavaDoc argType1, Object JavaDoc arg1,
95         Class JavaDoc argType2, Object JavaDoc arg2) {
96         return ReflectUtil.invoke(
97             obj, methodName, argType1, arg1, argType2, arg2);
98     }
99 }
100
Popular Tags