KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > util > ReflectionUtil


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 package org.apache.el.util;
18
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.lang.reflect.Array JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Arrays JavaDoc;
25
26 import javax.el.ELException;
27 import javax.el.MethodNotFoundException;
28 import javax.el.PropertyNotFoundException;
29
30 import org.apache.el.lang.ELSupport;
31
32
33 /**
34  * Utilities for Managing Serialization and Reflection
35  *
36  * @author Jacob Hookom [jacob@hookom.net]
37  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
38  */

39 public class ReflectionUtil {
40
41     protected static final String JavaDoc[] EMPTY_STRING = new String JavaDoc[0];
42
43     protected static final String JavaDoc[] PRIMITIVE_NAMES = new String JavaDoc[] { "boolean",
44             "byte", "char", "double", "float", "int", "long", "short", "void" };
45
46     protected static final Class JavaDoc[] PRIMITIVES = new Class JavaDoc[] { boolean.class,
47             byte.class, char.class, double.class, float.class, int.class,
48             long.class, short.class, Void.TYPE };
49
50     /**
51      *
52      */

53     private ReflectionUtil() {
54         super();
55     }
56
57     public static Class JavaDoc forName(String JavaDoc name) throws ClassNotFoundException JavaDoc {
58         if (null == name || "".equals(name)) {
59             return null;
60         }
61         Class JavaDoc c = forNamePrimitive(name);
62         if (c == null) {
63             if (name.endsWith("[]")) {
64                 String JavaDoc nc = name.substring(0, name.length() - 2);
65                 c = Class.forName(nc, true, Thread.currentThread().getContextClassLoader());
66                 c = Array.newInstance(c, 0).getClass();
67             } else {
68                 c = Class.forName(name, true, Thread.currentThread().getContextClassLoader());
69             }
70         }
71         return c;
72     }
73
74     protected static Class JavaDoc forNamePrimitive(String JavaDoc name) {
75         if (name.length() <= 8) {
76             int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
77             if (p >= 0) {
78                 return PRIMITIVES[p];
79             }
80         }
81         return null;
82     }
83
84     /**
85      * Converts an array of Class names to Class types
86      * @param s
87      * @return
88      * @throws ClassNotFoundException
89      */

90     public static Class JavaDoc[] toTypeArray(String JavaDoc[] s) throws ClassNotFoundException JavaDoc {
91         if (s == null)
92             return null;
93         Class JavaDoc[] c = new Class JavaDoc[s.length];
94         for (int i = 0; i < s.length; i++) {
95             c[i] = forName(s[i]);
96         }
97         return c;
98     }
99
100     /**
101      * Converts an array of Class types to Class names
102      * @param c
103      * @return
104      */

105     public static String JavaDoc[] toTypeNameArray(Class JavaDoc[] c) {
106         if (c == null)
107             return null;
108         String JavaDoc[] s = new String JavaDoc[c.length];
109         for (int i = 0; i < c.length; i++) {
110             s[i] = c[i].getName();
111         }
112         return s;
113     }
114
115     /**
116      * Returns a method based on the criteria
117      * @param base the object that owns the method
118      * @param property the name of the method
119      * @param paramTypes the parameter types to use
120      * @return the method specified
121      * @throws MethodNotFoundException
122      */

123     public static Method JavaDoc getMethod(Object JavaDoc base, Object JavaDoc property,
124             Class JavaDoc[] paramTypes) throws MethodNotFoundException {
125         if (base == null || property == null) {
126             throw new MethodNotFoundException(MessageFactory.get(
127                     "error.method.notfound", base, property,
128                     paramString(paramTypes)));
129         }
130
131         String JavaDoc methodName = (property instanceof String JavaDoc) ? (String JavaDoc) property
132                 : property.toString();
133
134         Method JavaDoc method = null;
135         try {
136             method = base.getClass().getMethod(methodName, paramTypes);
137         } catch (NoSuchMethodException JavaDoc nsme) {
138             throw new MethodNotFoundException(MessageFactory.get(
139                     "error.method.notfound", base, property,
140                     paramString(paramTypes)));
141         }
142         return method;
143     }
144
145     protected static final String JavaDoc paramString(Class JavaDoc[] types) {
146         if (types != null) {
147             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
148             for (int i = 0; i < types.length; i++) {
149                 sb.append(types[i].getName()).append(", ");
150             }
151             if (sb.length() > 2) {
152                 sb.setLength(sb.length() - 2);
153             }
154             return sb.toString();
155         }
156         return null;
157     }
158
159     /**
160      * @param base
161      * @param property
162      * @return
163      * @throws ELException
164      * @throws PropertyNotFoundException
165      */

166     public static PropertyDescriptor JavaDoc getPropertyDescriptor(Object JavaDoc base,
167             Object JavaDoc property) throws ELException, PropertyNotFoundException {
168         String JavaDoc name = ELSupport.coerceToString(property);
169         PropertyDescriptor JavaDoc p = null;
170         try {
171             PropertyDescriptor JavaDoc[] desc = Introspector.getBeanInfo(
172                     base.getClass()).getPropertyDescriptors();
173             for (int i = 0; i < desc.length; i++) {
174                 if (desc[i].getName().equals(name)) {
175                     return desc[i];
176                 }
177             }
178         } catch (IntrospectionException JavaDoc ie) {
179             throw new ELException(ie);
180         }
181         throw new PropertyNotFoundException(MessageFactory.get(
182                 "error.property.notfound", base, name));
183     }
184 }
185
Popular Tags