blob: 4b1e76335412ac906b522b7fcb741690048d2dd1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.beans.EventSetDescriptor;
25import java.beans.IndexedPropertyDescriptor;
26import java.beans.IntrospectionException;
27import java.beans.Introspector;
28import java.beans.MethodDescriptor;
29import java.beans.PropertyDescriptor;
30
31/**
32 * This class contains utilities useful for JavaBeans regression testing.
33 */
34public final class BeanUtils {
35 /**
36 * Disables instantiation.
37 */
38 private BeanUtils() {
39 }
40
41 /**
42 * Returns an array of property descriptors for specified class.
43 *
44 * @param type the class to introspect
45 * @return an array of property descriptors
46 */
47 public static PropertyDescriptor[] getPropertyDescriptors(Class type) {
48 System.out.println(type);
49 try {
50 return Introspector.getBeanInfo(type).getPropertyDescriptors();
51 } catch (IntrospectionException exception) {
52 throw new Error("unexpected exception", exception);
53 }
54 }
55
56 /**
57 * Finds a property descriptor for the class
58 * that matches the property name.
59 *
60 * @param type the class to introspect
61 * @param name the name of the property to search
62 * @return the {@code PropertyDescriptor}, {@code IndexedPropertyDescriptor} or {@code null}
63 */
64 public static PropertyDescriptor findPropertyDescriptor(Class type, String name) {
65 PropertyDescriptor[] pds = getPropertyDescriptors(type);
66 for (PropertyDescriptor pd : pds) {
67 if (pd.getName().equals(name)) {
68 return pd;
69 }
70 }
71 return null;
72 }
73
74 /**
75 * Returns a property descriptor for the class
76 * that matches the property name.
77 *
78 * @param type the class to introspect
79 * @param name the name of the property to search
80 * @return the {@code PropertyDescriptor}
81 */
82 public static PropertyDescriptor getPropertyDescriptor(Class type, String name) {
83 PropertyDescriptor pd = findPropertyDescriptor(type, name);
84 if (pd != null) {
85 return pd;
86 }
87 throw new Error("could not find property '" + name + "' in " + type);
88 }
89
90 /**
91 * Returns an indexed property descriptor for the class
92 * that matches the property name.
93 *
94 * @param type the class to introspect
95 * @param name the name of the property to search
96 * @return the {@code IndexedPropertyDescriptor}
97 */
98 public static IndexedPropertyDescriptor getIndexedPropertyDescriptor(Class type, String name) {
99 PropertyDescriptor pd = findPropertyDescriptor(type, name);
100 if (pd instanceof IndexedPropertyDescriptor) {
101 return (IndexedPropertyDescriptor) pd;
102 }
103 reportPropertyDescriptor(pd);
104 throw new Error("could not find indexed property '" + name + "' in " + type);
105 }
106
107 /**
108 * Reports all the interesting information in an Indexed/PropertyDescrptor.
109 */
110 public static void reportPropertyDescriptor(PropertyDescriptor pd) {
111 System.out.println("property name: " + pd.getName());
112 System.out.println(" type: " + pd.getPropertyType());
113 System.out.println(" read: " + pd.getReadMethod());
114 System.out.println(" write: " + pd.getWriteMethod());
115 if (pd instanceof IndexedPropertyDescriptor) {
116 IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
117 System.out.println(" indexed type: " + ipd.getIndexedPropertyType());
118 System.out.println(" indexed read: " + ipd.getIndexedReadMethod());
119 System.out.println(" indexed write: " + ipd.getIndexedWriteMethod());
120 }
121 }
122
123 /**
124 * Reports all the interesting information in an EventSetDescriptor
125 */
126 public static void reportEventSetDescriptor(EventSetDescriptor esd) {
127 System.out.println("event set name: " + esd.getName());
128 System.out.println(" listener type: " + esd.getListenerType());
129 System.out.println(" method get: " + esd.getGetListenerMethod());
130 System.out.println(" method add: " + esd.getAddListenerMethod());
131 System.out.println(" method remove: " + esd.getRemoveListenerMethod());
132 }
133
134 /**
135 * Reports all the interesting information in a MethodDescriptor
136 */
137 public static void reportMethodDescriptor(MethodDescriptor md) {
138 System.out.println("method name: " + md.getName());
139 System.out.println(" method: " + md.getMethod());
140 }
141}