KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > util > CompleteIntrospector


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.util;
54
55 import java.beans.*;
56 import java.util.*;
57 import java.lang.ref.*;
58
59 /******************************************************************************
60  * A JavaBean Introspector that ensures interface properties are properly
61  * discovered.
62  *
63  * @author Brian S O'Neill
64  * @version
65  * <!--$$Revision:--> 1 <!-- $-->, <!--$$JustDate:--> 00/12/13 <!-- $-->
66  */

67 public class CompleteIntrospector {
68     // Weakly maps Class objects to softly referenced PropertyDescriptor maps.
69
private static Map cPropertiesCache;
70
71     static {
72         cPropertiesCache = new IdentityMap();
73
74         // The Introspector has a poor design in that a special GLOBAL setting
75
// is used. The default setting has a negative affect because the
76
// BeanInfo search path disregards packages. By default, the search
77
// path provides a BeanInfo for Component, which is for
78
// java.awt.Component. However, any class with this name, in any
79
// package, will be given the visible properties of java.awt.Component.
80
Introspector.setBeanInfoSearchPath(new String JavaDoc[0]);
81     }
82
83     /**
84      * Test program.
85      */

86     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
87         Map map = getAllProperties(Class.forName(args[0]));
88         Iterator keys = map.keySet().iterator();
89         while (keys.hasNext()) {
90             String JavaDoc key = (String JavaDoc)keys.next();
91             PropertyDescriptor desc = (PropertyDescriptor)map.get(key);
92             System.out.println(key + " = " + desc);
93         }
94     }
95
96     /**
97      * A function that returns a Map of all the available properties on
98      * a given class including write-only properties. The properties returned
99      * is mostly a superset of those returned from the standard JavaBeans
100      * Introspector except more properties are made available to interfaces.
101      *
102      * @return an unmodifiable mapping of property names (Strings) to
103      * PropertyDescriptor objects.
104      *
105      */

106     public static Map getAllProperties(Class JavaDoc clazz)
107         throws IntrospectionException {
108         
109         synchronized (cPropertiesCache) {
110             Map properties;
111
112             Reference ref = (Reference)cPropertiesCache.get(clazz);
113             if (ref != null) {
114                 properties = (Map)ref.get();
115                 if (properties != null) {
116                     return properties;
117                 }
118                 else {
119                     // Clean up cleared reference.
120
cPropertiesCache.remove(clazz);
121                 }
122             }
123
124             properties = Collections.unmodifiableMap(createProperties(clazz));
125             cPropertiesCache.put(clazz, new SoftReference(properties));
126             return properties;
127         }
128     }
129
130     private static Map createProperties(Class JavaDoc clazz)
131         throws IntrospectionException {
132
133         Map properties = new HashMap();
134
135         if (clazz == null || clazz.isPrimitive()) {
136             return properties;
137         }
138         
139         BeanInfo info;
140         try {
141             info = Introspector.getBeanInfo(clazz);
142         }
143         catch (LinkageError JavaDoc e) {
144             throw new IntrospectionException(e.toString());
145         }
146
147         if (info != null) {
148             PropertyDescriptor[] pdArray = info.getPropertyDescriptors();
149             
150             // Standard properties.
151
int length = pdArray.length;
152             for (int i=0; i<length; i++) {
153                 properties.put(pdArray[i].getName(), pdArray[i]);
154             }
155         }
156
157         // Properties defined in Object are also available to interfaces.
158
if (clazz.isInterface()) {
159             properties.putAll(getAllProperties(Object JavaDoc.class));
160         }
161
162         // Ensure that all implemented interfaces are properly analyzed.
163
Class JavaDoc[] interfaces = clazz.getInterfaces();
164         for (int i=0; i<interfaces.length; i++) {
165             properties.putAll(getAllProperties(interfaces[i]));
166         }
167
168         // Filter out properties with names that contain '$' characters.
169
Iterator it = properties.keySet().iterator();
170         while (it.hasNext()) {
171             String JavaDoc propertyName = (String JavaDoc)it.next();
172             if (propertyName.indexOf('$') >= 0) {
173                 it.remove();
174             }
175         }
176
177         return properties;
178     }
179 }
180
Popular Tags