0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
243N/Aimport java.beans.BeanDescriptor;
0N/Aimport java.beans.EventSetDescriptor;
0N/Aimport java.beans.IndexedPropertyDescriptor;
0N/Aimport java.beans.IntrospectionException;
0N/Aimport java.beans.Introspector;
0N/Aimport java.beans.MethodDescriptor;
0N/Aimport java.beans.PropertyDescriptor;
0N/A
0N/A/**
0N/A * This class contains utilities useful for JavaBeans regression testing.
0N/A */
0N/Apublic final class BeanUtils {
0N/A /**
0N/A * Disables instantiation.
0N/A */
0N/A private BeanUtils() {
0N/A }
0N/A
0N/A /**
243N/A * Returns a bean descriptor for specified class.
243N/A *
243N/A * @param type the class to introspect
243N/A * @return a bean descriptor
243N/A */
243N/A public static BeanDescriptor getBeanDescriptor(Class type) {
243N/A try {
243N/A return Introspector.getBeanInfo(type).getBeanDescriptor();
243N/A } catch (IntrospectionException exception) {
243N/A throw new Error("unexpected exception", exception);
243N/A }
243N/A }
243N/A
243N/A /**
0N/A * Returns an array of property descriptors for specified class.
0N/A *
0N/A * @param type the class to introspect
0N/A * @return an array of property descriptors
0N/A */
0N/A public static PropertyDescriptor[] getPropertyDescriptors(Class type) {
0N/A try {
0N/A return Introspector.getBeanInfo(type).getPropertyDescriptors();
0N/A } catch (IntrospectionException exception) {
0N/A throw new Error("unexpected exception", exception);
0N/A }
0N/A }
0N/A
0N/A /**
243N/A * Returns an array of event set descriptors for specified class.
243N/A *
243N/A * @param type the class to introspect
243N/A * @return an array of event set descriptors
243N/A */
243N/A public static EventSetDescriptor[] getEventSetDescriptors(Class type) {
243N/A try {
243N/A return Introspector.getBeanInfo(type).getEventSetDescriptors();
243N/A } catch (IntrospectionException exception) {
243N/A throw new Error("unexpected exception", exception);
243N/A }
243N/A }
243N/A
243N/A /**
243N/A * Finds an event set descriptor for the class
243N/A * that matches the event set name.
243N/A *
243N/A * @param type the class to introspect
243N/A * @param name the name of the event set to search
243N/A * @return the {@code EventSetDescriptor} or {@code null}
243N/A */
243N/A public static EventSetDescriptor findEventSetDescriptor(Class type, String name) {
243N/A EventSetDescriptor[] esds = getEventSetDescriptors(type);
243N/A for (EventSetDescriptor esd : esds) {
243N/A if (esd.getName().equals(name)) {
243N/A return esd;
243N/A }
243N/A }
243N/A return null;
243N/A }
243N/A
243N/A /**
0N/A * Finds a property descriptor for the class
0N/A * that matches the property name.
0N/A *
0N/A * @param type the class to introspect
0N/A * @param name the name of the property to search
0N/A * @return the {@code PropertyDescriptor}, {@code IndexedPropertyDescriptor} or {@code null}
0N/A */
0N/A public static PropertyDescriptor findPropertyDescriptor(Class type, String name) {
0N/A PropertyDescriptor[] pds = getPropertyDescriptors(type);
0N/A for (PropertyDescriptor pd : pds) {
0N/A if (pd.getName().equals(name)) {
0N/A return pd;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
243N/A * Returns a event set descriptor for the class
243N/A * that matches the property name.
243N/A *
243N/A * @param type the class to introspect
243N/A * @param name the name of the event set to search
243N/A * @return the {@code EventSetDescriptor}
243N/A */
243N/A public static EventSetDescriptor getEventSetDescriptor(Class type, String name) {
243N/A EventSetDescriptor esd = findEventSetDescriptor(type, name);
243N/A if (esd != null) {
243N/A return esd;
243N/A }
243N/A throw new Error("could not find event set '" + name + "' in " + type);
243N/A }
243N/A
243N/A /**
0N/A * Returns a property descriptor for the class
0N/A * that matches the property name.
0N/A *
0N/A * @param type the class to introspect
0N/A * @param name the name of the property to search
0N/A * @return the {@code PropertyDescriptor}
0N/A */
0N/A public static PropertyDescriptor getPropertyDescriptor(Class type, String name) {
0N/A PropertyDescriptor pd = findPropertyDescriptor(type, name);
0N/A if (pd != null) {
0N/A return pd;
0N/A }
0N/A throw new Error("could not find property '" + name + "' in " + type);
0N/A }
0N/A
0N/A /**
0N/A * Returns an indexed property descriptor for the class
0N/A * that matches the property name.
0N/A *
0N/A * @param type the class to introspect
0N/A * @param name the name of the property to search
0N/A * @return the {@code IndexedPropertyDescriptor}
0N/A */
0N/A public static IndexedPropertyDescriptor getIndexedPropertyDescriptor(Class type, String name) {
0N/A PropertyDescriptor pd = findPropertyDescriptor(type, name);
0N/A if (pd instanceof IndexedPropertyDescriptor) {
0N/A return (IndexedPropertyDescriptor) pd;
0N/A }
0N/A reportPropertyDescriptor(pd);
0N/A throw new Error("could not find indexed property '" + name + "' in " + type);
0N/A }
0N/A
0N/A /**
0N/A * Reports all the interesting information in an Indexed/PropertyDescrptor.
0N/A */
0N/A public static void reportPropertyDescriptor(PropertyDescriptor pd) {
0N/A System.out.println("property name: " + pd.getName());
0N/A System.out.println(" type: " + pd.getPropertyType());
0N/A System.out.println(" read: " + pd.getReadMethod());
0N/A System.out.println(" write: " + pd.getWriteMethod());
0N/A if (pd instanceof IndexedPropertyDescriptor) {
0N/A IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
0N/A System.out.println(" indexed type: " + ipd.getIndexedPropertyType());
0N/A System.out.println(" indexed read: " + ipd.getIndexedReadMethod());
0N/A System.out.println(" indexed write: " + ipd.getIndexedWriteMethod());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reports all the interesting information in an EventSetDescriptor
0N/A */
0N/A public static void reportEventSetDescriptor(EventSetDescriptor esd) {
0N/A System.out.println("event set name: " + esd.getName());
0N/A System.out.println(" listener type: " + esd.getListenerType());
0N/A System.out.println(" method get: " + esd.getGetListenerMethod());
0N/A System.out.println(" method add: " + esd.getAddListenerMethod());
0N/A System.out.println(" method remove: " + esd.getRemoveListenerMethod());
0N/A }
0N/A
0N/A /**
0N/A * Reports all the interesting information in a MethodDescriptor
0N/A */
0N/A public static void reportMethodDescriptor(MethodDescriptor md) {
0N/A System.out.println("method name: " + md.getName());
0N/A System.out.println(" method: " + md.getMethod());
0N/A }
0N/A}