001: /* ====================================================================
002: * Tea - Copyright (c) 1997-2000 Walt Disney Internet Group
003: * ====================================================================
004: * The Tea Software License, Version 1.1
005: *
006: * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Walt Disney Internet Group (http://opensource.go.com/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact opensource@dig.com.
031: *
032: * 5. Products derived from this software may not be called "Tea",
033: * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
034: * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
035: * written permission of the Walt Disney Internet Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
041: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
042: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
043: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
044: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
045: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
046: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
047: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
048: * ====================================================================
049: *
050: * For more information about Tea, please see http://opensource.go.com/.
051: */
052:
053: package com.go.tea.util;
054:
055: import java.beans.*;
056: import java.util.*;
057: import java.lang.reflect.Field;
058: import java.lang.reflect.Method;
059: import java.lang.reflect.Modifier;
060: import com.go.trove.util.IdentityMap;
061:
062: /******************************************************************************
063: * The JavaBean Introspector for Tea.
064: *
065: * @author Brian S O'Neill
066: * @version
067: * <!--$$Revision:--> 13 <!-- $-->, <!--$$JustDate:--> 00/12/13 <!-- $-->
068: *
069: * @see java.beans.Introspector
070: */
071: public class BeanAnalyzer {
072: /** The name given to the length property: "length" */
073: public static final String LENGTH_PROPERTY_NAME = "length";
074:
075: /** The name given to keyed properties: "[]" */
076: public static final String KEYED_PROPERTY_NAME = "[]";
077:
078: /**
079: * The name of the special field that specializes a
080: * keyed property type: "ELEMENT_TYPE"
081: */
082: public static final String ELEMENT_TYPE_FIELD_NAME = "ELEMENT_TYPE";
083:
084: /** A cache of properties for classes. Maps classes to property maps. */
085: private static Map cPropertiesCache;
086:
087: static {
088: Introspector.setBeanInfoSearchPath(new String[0]);
089: cPropertiesCache = new IdentityMap();
090: }
091:
092: /**
093: * Test program.
094: */
095: public static void main(String[] args) throws Exception {
096: Map map = getAllProperties(Class.forName(args[0]));
097: Iterator keys = map.keySet().iterator();
098: while (keys.hasNext()) {
099: String key = (String) keys.next();
100: PropertyDescriptor desc = (PropertyDescriptor) map.get(key);
101: System.out.println(key + " = " + desc);
102: }
103: }
104:
105: /**
106: * A function that returns a Map of all the available properties on
107: * a given class including write-only properties. The properties returned
108: * is mostly a superset of those returned from the standard JavaBeans
109: * Introspector except pure indexed properties are discarded.
110: *
111: * <p>Interfaces receive all the properties available in Object. Arrays,
112: * Strings and Collections all receive a "length" property. An array's
113: * "length" PropertyDescriptor has no read or write methods.
114: *
115: * <p>Instead of indexed properties, there may be keyed properties in the
116: * map, represented by a {@link KeyedPropertyDescriptor}. Arrays, Strings
117: * and Lists always have keyed properties with a key type of int.
118: *
119: * <p>Because the value returned from a keyed property method may be more
120: * specific than the method signature describes (such is often the case
121: * with collections), a bean class can contain a special field that
122: * indicates what that specific type should be. The signature of this field
123: * is as follows:
124: * <tt>public static final Class ELEMENT_TYPE = <type>.class;</tt>.
125: *
126: * @return an unmodifiable mapping of property names (Strings) to
127: * PropertyDescriptor objects.
128: *
129: */
130: public static Map getAllProperties(Class clazz)
131: throws IntrospectionException {
132:
133: Map properties = (Map) cPropertiesCache.get(clazz);
134: if (properties == null) {
135: properties = Collections
136: .unmodifiableMap(createProperties(clazz));
137: cPropertiesCache.put(clazz, properties);
138: }
139:
140: return properties;
141: }
142:
143: private static Map createProperties(Class clazz)
144: throws IntrospectionException {
145:
146: Map properties = new HashMap();
147:
148: if (clazz == null || clazz.isPrimitive()) {
149: return properties;
150: }
151:
152: BeanInfo info;
153: try {
154: info = Introspector.getBeanInfo(clazz);
155: } catch (LinkageError e) {
156: throw new IntrospectionException(e.toString());
157: }
158:
159: if (info != null) {
160: PropertyDescriptor[] pdArray = info
161: .getPropertyDescriptors();
162:
163: // Standard properties.
164: int length = pdArray.length;
165: for (int i = 0; i < length; i++) {
166: PropertyDescriptor desc = pdArray[i];
167: // This check will discard standard pure indexed properties.
168: if (desc.getPropertyType() != null) {
169: properties.put(desc.getName(), desc);
170: }
171: }
172: }
173:
174: // Properties defined in Object are also available to interfaces.
175: if (clazz.isInterface()) {
176: properties.putAll(getAllProperties(Object.class));
177: }
178:
179: // Ensure that all implemented interfaces are properly analyzed.
180: Class[] interfaces = clazz.getInterfaces();
181: for (int i = 0; i < interfaces.length; i++) {
182: properties.putAll(getAllProperties(interfaces[i]));
183: }
184:
185: // All arrays have a "length" property and a keyed property.
186: PropertyDescriptor property;
187: if (clazz.isArray()) {
188: property = new ArrayLengthProperty(clazz);
189: properties.put(LENGTH_PROPERTY_NAME, property);
190: }
191:
192: // Strings also have a "length" property.
193: if (String.class.isAssignableFrom(clazz)) {
194: try {
195: Method readMethod = String.class.getMethod("length",
196: null);
197: property = new PropertyDescriptor(LENGTH_PROPERTY_NAME,
198: readMethod, null);
199: properties.put(LENGTH_PROPERTY_NAME, property);
200: } catch (NoSuchMethodException e) {
201: throw new LinkageError(e.toString());
202: }
203: }
204:
205: // Collections also have a "length" property.
206: if (Collection.class.isAssignableFrom(clazz)) {
207: try {
208: Method readMethod = Collection.class.getMethod("size",
209: null);
210: property = new PropertyDescriptor(LENGTH_PROPERTY_NAME,
211: readMethod, null);
212: properties.put(LENGTH_PROPERTY_NAME, property);
213: } catch (NoSuchMethodException e) {
214: throw new LinkageError(e.toString());
215: }
216: }
217:
218: // Analyze design patterns for keyed properties.
219:
220: KeyedPropertyDescriptor keyed = new KeyedPropertyDescriptor();
221: List keyedMethods = new ArrayList();
222:
223: if (clazz.isArray()) {
224: keyed.setKeyedPropertyType(clazz.getComponentType());
225: keyedMethods.add(null);
226: }
227:
228: // Get index types and access methods.
229:
230: // Extract all the public "get" methods that have a return type and
231: // one parameter.
232:
233: Method[] methods = clazz.getMethods();
234:
235: for (int i = 0; i < methods.length; i++) {
236: Method m = methods[i];
237: if (Modifier.isPublic(m.getModifiers())
238: && "get".equals(m.getName())) {
239:
240: Class ret = m.getReturnType();
241: if (ret != null && ret != void.class) {
242: Class[] params = m.getParameterTypes();
243: if (params.length == 1) {
244: // Found a method that fits the requirements.
245: keyed.setKeyedPropertyType(ret);
246: keyedMethods.add(m);
247: }
248: }
249: }
250: }
251:
252: if (keyedMethods.size() == 0) {
253: // If no "get" methods found, but this type is a Vector or
254: // String, use elementAt or charAt as substitutes.
255:
256: if (Vector.class.isAssignableFrom(clazz)) {
257: keyed.setKeyedPropertyType(Object.class);
258: try {
259: Method m = Vector.class.getMethod("elementAt",
260: new Class[] { int.class });
261: keyedMethods.add(m);
262: } catch (NoSuchMethodException e) {
263: throw new LinkageError(e.toString());
264: }
265: } else if (String.class.isAssignableFrom(clazz)) {
266: keyed.setKeyedPropertyType(char.class);
267: try {
268: Method m = String.class.getMethod("charAt",
269: new Class[] { int.class });
270: keyedMethods.add(m);
271: } catch (NoSuchMethodException e) {
272: throw new LinkageError(e.toString());
273: }
274: }
275: }
276:
277: if (keyedMethods.size() > 0) {
278: // Try to specialize keyed property type.
279: try {
280: Field field = clazz.getField(ELEMENT_TYPE_FIELD_NAME);
281: if (field.getType() == Class.class
282: && Modifier.isStatic(field.getModifiers())) {
283:
284: Class elementType = (Class) field.get(null);
285: if (keyed.getKeyedPropertyType().isAssignableFrom(
286: elementType)) {
287:
288: keyed.setKeyedPropertyType(elementType);
289: }
290: }
291: } catch (NoSuchFieldException e) {
292: } catch (IllegalAccessException e) {
293: }
294:
295: properties.put(KEYED_PROPERTY_NAME, keyed);
296: int size = keyedMethods.size();
297: keyed.setKeyedReadMethods((Method[]) keyedMethods
298: .toArray(new Method[size]));
299: }
300:
301: // Filter out properties with names that contain '$' characters.
302: Iterator it = properties.keySet().iterator();
303: while (it.hasNext()) {
304: String propertyName = (String) it.next();
305: if (propertyName.indexOf('$') >= 0) {
306: it.remove();
307: }
308: }
309:
310: return properties;
311: }
312:
313: private static class ArrayLengthProperty extends PropertyDescriptor {
314: public ArrayLengthProperty(Class clazz)
315: throws IntrospectionException {
316: super (LENGTH_PROPERTY_NAME, clazz, null, null);
317: }
318:
319: public Class getPropertyType() {
320: return int.class;
321: }
322: }
323: }
|