Path: blob/master/test/jdk/java/beans/Introspector/BeanUtils.java
41149 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.beans.BeanDescriptor;24import java.beans.EventSetDescriptor;25import java.beans.IndexedPropertyDescriptor;26import java.beans.IntrospectionException;27import java.beans.Introspector;28import java.beans.MethodDescriptor;29import java.beans.PropertyDescriptor;3031/**32* This class contains utilities useful for JavaBeans regression testing.33*/34public final class BeanUtils {35/**36* Disables instantiation.37*/38private BeanUtils() {39}4041/**42* Returns a bean descriptor for specified class.43*44* @param type the class to introspect45* @return a bean descriptor46*/47public static BeanDescriptor getBeanDescriptor(Class type) {48try {49return Introspector.getBeanInfo(type).getBeanDescriptor();50} catch (IntrospectionException exception) {51throw new Error("unexpected exception", exception);52}53}5455/**56* Returns an array of property descriptors for specified class.57*58* @param type the class to introspect59* @return an array of property descriptors60*/61public static PropertyDescriptor[] getPropertyDescriptors(Class type) {62try {63return Introspector.getBeanInfo(type).getPropertyDescriptors();64} catch (IntrospectionException exception) {65throw new Error("unexpected exception", exception);66}67}6869/**70* Returns an array of event set descriptors for specified class.71*72* @param type the class to introspect73* @return an array of event set descriptors74*/75public static EventSetDescriptor[] getEventSetDescriptors(Class type) {76try {77return Introspector.getBeanInfo(type).getEventSetDescriptors();78} catch (IntrospectionException exception) {79throw new Error("unexpected exception", exception);80}81}8283/**84* Finds an event set descriptor for the class85* that matches the event set name.86*87* @param type the class to introspect88* @param name the name of the event set to search89* @return the {@code EventSetDescriptor} or {@code null}90*/91public static EventSetDescriptor findEventSetDescriptor(Class type, String name) {92EventSetDescriptor[] esds = getEventSetDescriptors(type);93for (EventSetDescriptor esd : esds) {94if (esd.getName().equals(name)) {95return esd;96}97}98return null;99}100101/**102* Finds a property descriptor for the class103* that matches the property name.104*105* @param type the class to introspect106* @param name the name of the property to search107* @return the {@code PropertyDescriptor}, {@code IndexedPropertyDescriptor} or {@code null}108*/109public static PropertyDescriptor findPropertyDescriptor(Class type, String name) {110PropertyDescriptor[] pds = getPropertyDescriptors(type);111for (PropertyDescriptor pd : pds) {112if (pd.getName().equals(name)) {113return pd;114}115}116return null;117}118119/**120* Returns a event set descriptor for the class121* that matches the property name.122*123* @param type the class to introspect124* @param name the name of the event set to search125* @return the {@code EventSetDescriptor}126*/127public static EventSetDescriptor getEventSetDescriptor(Class type, String name) {128EventSetDescriptor esd = findEventSetDescriptor(type, name);129if (esd != null) {130return esd;131}132throw new Error("could not find event set '" + name + "' in " + type);133}134135/**136* Returns a property descriptor for the class137* that matches the property name.138*139* @param type the class to introspect140* @param name the name of the property to search141* @return the {@code PropertyDescriptor}142*/143public static PropertyDescriptor getPropertyDescriptor(Class type, String name) {144PropertyDescriptor pd = findPropertyDescriptor(type, name);145if (pd != null) {146return pd;147}148throw new Error("could not find property '" + name + "' in " + type);149}150151/**152* Returns an indexed property descriptor for the class153* that matches the property name.154*155* @param type the class to introspect156* @param name the name of the property to search157* @return the {@code IndexedPropertyDescriptor}158*/159public static IndexedPropertyDescriptor getIndexedPropertyDescriptor(Class type, String name) {160PropertyDescriptor pd = findPropertyDescriptor(type, name);161if (pd instanceof IndexedPropertyDescriptor) {162return (IndexedPropertyDescriptor) pd;163}164reportPropertyDescriptor(pd);165throw new Error("could not find indexed property '" + name + "' in " + type);166}167168/**169* Reports all the interesting information in an Indexed/PropertyDescrptor.170*/171public static void reportPropertyDescriptor(PropertyDescriptor pd) {172System.out.println("property name: " + pd.getName());173System.out.println(" type: " + pd.getPropertyType());174System.out.println(" read: " + pd.getReadMethod());175System.out.println(" write: " + pd.getWriteMethod());176if (pd instanceof IndexedPropertyDescriptor) {177IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;178System.out.println(" indexed type: " + ipd.getIndexedPropertyType());179System.out.println(" indexed read: " + ipd.getIndexedReadMethod());180System.out.println(" indexed write: " + ipd.getIndexedWriteMethod());181}182}183184/**185* Reports all the interesting information in an EventSetDescriptor186*/187public static void reportEventSetDescriptor(EventSetDescriptor esd) {188System.out.println("event set name: " + esd.getName());189System.out.println(" listener type: " + esd.getListenerType());190System.out.println(" method get: " + esd.getGetListenerMethod());191System.out.println(" method add: " + esd.getAddListenerMethod());192System.out.println(" method remove: " + esd.getRemoveListenerMethod());193}194195/**196* Reports all the interesting information in a MethodDescriptor197*/198public static void reportMethodDescriptor(MethodDescriptor md) {199System.out.println("method name: " + md.getName());200System.out.println(" method: " + md.getMethod());201}202}203204205