Java examples for Reflection:Class
Displays properties for a given class using introspection.
/* ==================================================================== * The VM Systems, Inc. Software License, Version 1.0 * * Copyright (c) 2002 VM Systems, Inc. All rights reserved. * * THIS SOFTWARE IS PROVIDED PURSUANT TO THE TERMS OF THIS LICENSE. * ANY USE, REPRODUCTION, OR DISTRIBUTION OF THE SOFTWARE OR ANY PART * THEREOF CONSTITUTES ACCEPTANCE OF THE TERMS AND CONDITIONS HEREOF. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met://from w w w.j av a 2 s . co m * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by * VM Systems, Inc. (http://www.vmguys.com/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "VM Systems" must not be used to endorse or promote products * derived from this software without prior written permission. For written * permission, please contact info@vmguys.com. * * 5. VM Systems, Inc. and any other person or entity that creates or * contributes to the creation of any modifications to the original * software specifically disclaims any liability to any person or * entity for claims brought based on infringement of intellectual * property rights or otherwise. No assurances are provided that the * software does not infringe on the property rights of others. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE TITLE * AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VM SYSTEMS, INC., * ITS SHAREHOLDERS, DIRECTORS OR EMPLOYEES BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. EACH RECIPIENT OR USER IS SOLELY RESPONSIBLE * FOR DETERMINING THE APPROPRIATENESS OF USING AND DISTRIBUTING THE SOFTWARE * AND ASSUMES ALL RISKS ASSOCIATED WITH ITS EXERCISE OF RIGHTS HEREUNDER, * INCLUDING BUT NOT LIMITED TO THE RISKS (INCLUDING COSTS) OF ERRORS, * COMPLIANCE WITH APPLICABLE LAWS OR INTERRUPTION OF OPERATIONS. * ==================================================================== */ //package com.java2s; import java.beans.*; public class Main { /** Displays properties for a given class using introspection. */ static public String displayProperties(Class cls) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(cls); if (beanInfo == null) { return "No beans; getBeanInfo() returned null for " + cls.getName(); } PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); StringBuffer result = new StringBuffer(); for (int i = 0; i < properties.length; i++) { result.append("Property " + (i + 1) + " of " + properties.length + ":\n"); if (properties[i].getPropertyType() != null) { result.append("\tProperty Class: " + properties[i].getPropertyType().getName() + "\n"); } else result.append("\tProperty Class is NULL.\n"); if (properties[i].getReadMethod() != null) { result.append("\tRead method: " + properties[i].getReadMethod().getName() + "\n"); } else result.append("\tRead method is NULL.\n"); if (properties[i].getWriteMethod() != null) { result.append("\tWrite method: " + properties[i].getWriteMethod().getName() + "\n"); } else result.append("\tWrite method is NULL.\n"); } return result.toString(); } /** Returns a list of bean methods in a class. * @param className class name to analyze. * @return list of bean methods. */ static public String displayProperties(String className) throws ClassNotFoundException, IntrospectionException { Class cls = java.lang.Class.forName(className); return displayProperties(cls); } }