Java examples for Reflection:Java Bean
Returns the property descriptor of the given bean class and the given property.
/******************************************************************************* * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com>, Pascal Leclercq <pascal.leclercq@gmail.com> * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://from w ww.j av a 2s .co m * Angelo ZERR - initial API and implementation * Pascal Leclercq - initial API and implementation *******************************************************************************/ //package com.java2s; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.List; public class Main { /** * Returns the property descriptor of the given bean class and the given * property. * * @param beanClass * @param propertyName * @return the PropertyDescriptor for the named property on the given bean * class */ private static PropertyDescriptor getPropertyDescriptor( Class beanClass, String propertyName) { if (!beanClass.isInterface()) { BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException e) { // cannot introspect, give up return null; } PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; if (descriptor.getName().equals(propertyName)) { return descriptor; } } } else { try { PropertyDescriptor propertyDescriptors[]; List pds = new ArrayList(); getInterfacePropertyDescriptors(pds, beanClass); if (pds.size() > 0) { propertyDescriptors = (PropertyDescriptor[]) pds .toArray(new PropertyDescriptor[pds.size()]); PropertyDescriptor descriptor; for (int i = 0; i < propertyDescriptors.length; i++) { descriptor = propertyDescriptors[i]; if (descriptor.getName().equals(propertyName)) return descriptor; } } } catch (IntrospectionException e) { // cannot introspect, give up return null; } } throw new IllegalArgumentException( "Could not find property with name " + propertyName + " in class " + beanClass); //$NON-NLS-1$ //$NON-NLS-2$ } /** * Goes recursively into the interface and gets all defined * propertyDescriptors * * @param propertyDescriptors * The result list of all PropertyDescriptors the given interface * defines (hierarchical) * @param iface * The interface to fetch the PropertyDescriptors * @throws IntrospectionException */ private static void getInterfacePropertyDescriptors( List propertyDescriptors, Class iface) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(iface); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; propertyDescriptors.add(pd); } Class[] subIntfs = iface.getInterfaces(); for (int j = 0; j < subIntfs.length; j++) { getInterfacePropertyDescriptors(propertyDescriptors, subIntfs[j]); } } }