KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > util > ReflectionUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package com.sun.el.util;
26
27 import java.beans.IntrospectionException JavaDoc;
28 import java.beans.Introspector JavaDoc;
29 import java.beans.PropertyDescriptor JavaDoc;
30 import java.lang.reflect.Array JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Modifier JavaDoc;
33 import java.util.Arrays JavaDoc;
34
35 import javax.el.ELException;
36 import javax.el.MethodNotFoundException;
37 import javax.el.PropertyNotFoundException;
38
39 import com.sun.el.lang.ELSupport;
40
41 /**
42  * Utilities for Managing Serialization and Reflection
43  *
44  * @author Jacob Hookom [jacob@hookom.net]
45  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
46  */

47 public class ReflectionUtil {
48
49     protected static final String JavaDoc[] EMPTY_STRING = new String JavaDoc[0];
50
51     protected static final String JavaDoc[] PRIMITIVE_NAMES = new String JavaDoc[] { "boolean",
52             "byte", "char", "double", "float", "int", "long", "short", "void" };
53
54     protected static final Class JavaDoc[] PRIMITIVES = new Class JavaDoc[] { boolean.class,
55             byte.class, char.class, double.class, float.class, int.class,
56             long.class, short.class, Void.TYPE };
57
58     /**
59      *
60      */

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

98     public static Class JavaDoc[] toTypeArray(String JavaDoc[] s) throws ClassNotFoundException JavaDoc {
99         if (s == null)
100             return null;
101         Class JavaDoc[] c = new Class JavaDoc[s.length];
102         for (int i = 0; i < s.length; i++) {
103             c[i] = forName(s[i]);
104         }
105         return c;
106     }
107
108     /**
109      * Converts an array of Class types to Class names
110      * @param c
111      * @return
112      */

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

131     public static Method JavaDoc getMethod(Object JavaDoc base, Object JavaDoc property,
132             Class JavaDoc[] paramTypes) throws MethodNotFoundException {
133         if (base == null || property == null) {
134             throw new MethodNotFoundException(MessageFactory.get(
135                     "error.method.notfound", base, property,
136                     paramString(paramTypes)));
137         }
138
139         String JavaDoc methodName = property.toString();
140
141         Method JavaDoc method = getMethod(base.getClass(), methodName, paramTypes);
142         if (method == null) {
143             throw new MethodNotFoundException(MessageFactory.get(
144                     "error.method.notfound", base, property,
145                     paramString(paramTypes)));
146         }
147         return method;
148     }
149
150     /*
151      * Get a public method form a public class or interface of a given method.
152      * Note that if the base is an instance of a non-public class that
153      * implements a public interface, calling Class.getMethod() with the base
154      * will not find the method. To correct this, a version of the
155      * same method must be found in a superclass or interface.
156      **/

157
158     static private Method JavaDoc getMethod(Class JavaDoc cl, String JavaDoc methodName,
159                                     Class JavaDoc[] paramTypes) {
160
161         Method JavaDoc m = null;
162         try {
163             m = cl.getMethod(methodName, paramTypes);
164         } catch (NoSuchMethodException JavaDoc ex) {
165             return null;
166         }
167
168         Class JavaDoc dclass = m.getDeclaringClass();
169         if (Modifier.isPublic(dclass.getModifiers())) {
170             return m;
171         }
172
173         for (Class JavaDoc c: dclass.getInterfaces()) {
174             m = getMethod(c, methodName, paramTypes);
175             if (m != null) {
176                 return m;
177             }
178         }
179         Class JavaDoc c = dclass.getSuperclass();
180         if (c != null) {
181             m = getMethod(c, methodName, paramTypes);
182             if (m != null) {
183                 return m;
184             }
185         }
186         return null;
187     }
188
189     protected static final String JavaDoc paramString(Class JavaDoc[] types) {
190         if (types != null) {
191             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
192             for (int i = 0; i < types.length; i++) {
193                 sb.append(types[i].getName()).append(", ");
194             }
195             if (sb.length() > 2) {
196                 sb.setLength(sb.length() - 2);
197             }
198             return sb.toString();
199         }
200         return null;
201     }
202
203     /**
204      * @param base
205      * @param property
206      * @return
207      * @throws ELException
208      * @throws PropertyNotFoundException
209      */

210     public static PropertyDescriptor JavaDoc getPropertyDescriptor(Object JavaDoc base,
211             Object JavaDoc property) throws ELException, PropertyNotFoundException {
212         String JavaDoc name = ELSupport.coerceToString(property);
213         PropertyDescriptor JavaDoc p = null;
214         try {
215             PropertyDescriptor JavaDoc[] desc = Introspector.getBeanInfo(
216                     base.getClass()).getPropertyDescriptors();
217             for (int i = 0; i < desc.length; i++) {
218                 if (desc[i].getName().equals(name)) {
219                     return desc[i];
220                 }
221             }
222         } catch (IntrospectionException JavaDoc ie) {
223             throw new ELException(ie);
224         }
225         throw new PropertyNotFoundException(MessageFactory.get(
226                 "error.property.notfound", base, name));
227     }
228 }
229
Popular Tags