List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:com.gmail.sretof.db.jdbc.processor.CamelBeanProcessor.java
/** * Returns a PropertyDescriptor[] for the given Class. * /*w w w. j av a 2s .co m*/ * @param c * The Class to retrieve PropertyDescriptors for. * @return A PropertyDescriptor[] describing the Class. * @throws SQLException * if introspection failed. */ private PropertyDescriptor[] propertyDescriptors(Class<?> c) throws SQLException { // Introspector caches BeanInfo classes for better performance BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(c); } catch (IntrospectionException e) { throw new SQLException("Bean introspection failed: " + e.getMessage()); } List<PropertyDescriptor> propList = Lists.newArrayList(); PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor prop : props) { String propName = prop.getName(); try { Field field = ReflectionUtils.findField(c, propName); if (field != null && !field.isAnnotationPresent(Transient.class)) {// 1.field=null // 2.Transient?? propList.add(prop); } } catch (SecurityException e) { throw new SQLException("Bean Get Field failed: " + e.getMessage()); } } return propList.toArray(new PropertyDescriptor[propList.size()]); }
From source file:org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.BeanSupport.java
/** * @return {@link PropertyDescriptor} properties for given bean {@link Class}. *//* w w w .j a v a 2 s. co m*/ public static List<PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass) throws Exception { List<PropertyDescriptor> descriptors = Lists.newArrayList(); // handle interfaces if (beanClass.isInterface() || Modifier.isAbstract(beanClass.getModifiers())) { List<Class<?>> interfaces = CoreUtils.cast(ClassUtils.getAllInterfaces(beanClass)); for (Class<?> i : interfaces) { BeanInfo beanInfo = Introspector.getBeanInfo(i); addDescriptors(descriptors, beanInfo.getPropertyDescriptors()); } } // handle bean BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); addDescriptors(descriptors, beanInfo.getPropertyDescriptors()); // return descriptors; }
From source file:BeanVector.java
/** * This method returns the index of an object representing the * mode value of a property name./*from w w w . j a v a2 s. c o m*/ * @param propertyName String value of the property name to calculate. * @throws java.lang.IllegalAccessException reflection exception * @throws java.beans.IntrospectionException reflection exception * @throws java.lang.reflect.InvocationTargetException reflection exception * @return int value of the mode index */ public int getModeIndex(String propertyName) throws java.lang.IllegalAccessException, java.beans.IntrospectionException, java.lang.reflect.InvocationTargetException { int index = -1; int max = 0; int count = 0; Object o = null; Object hold = null; Hashtable cache = new Hashtable(); String currentClass = ""; PropertyDescriptor pd = null; BeanVector bv = new BeanVector(this.size(), 0, this); bv.sortOnProperty(propertyName); for (int i = 0; i < bv.size(); i++) { if (!currentClass.equals(bv.elementAt(i).getClass().getName())) { pd = (PropertyDescriptor) cache.get(bv.elementAt(i).getClass().getName()); if (pd == null) { PropertyDescriptor[] pds = Introspector.getBeanInfo(bv.elementAt(i).getClass()) .getPropertyDescriptors(); boolean foundProperty = false; for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) { if (pds[pdi].getName().equals(propertyName)) { pd = pds[pdi]; cache.put(bv.elementAt(i).getClass().getName(), pd); foundProperty = true; } } } } if (hold == null) { hold = pd.getReadMethod().invoke(bv.elementAt(i)); } else { o = pd.getReadMethod().invoke(bv.elementAt(i)); if ((o != null) && o.equals(hold)) { count++; if (count > max) { max = count; index = this.indexOf(bv.elementAt(i)); } } else { count = 1; } hold = o; } } return index; }
From source file:BeanArrayList.java
/** * This method returns the index of an object representing the * mode value of a property name.//w w w . ja va 2 s .co m * @param propertyName String value of the property name to calculate. * @throws java.lang.IllegalAccessException reflection exception * @throws java.beans.IntrospectionException reflection exception * @throws java.lang.reflect.InvocationTargetException reflection exception * @return int value of the mode index */ public int getModeIndex(String propertyName) throws java.lang.IllegalAccessException, java.beans.IntrospectionException, java.lang.reflect.InvocationTargetException { int index = -1; int max = 0; int count = 0; Object o = null; Object hold = null; HashMap cache = new HashMap(); String currentClass = ""; PropertyDescriptor pd = null; BeanArrayList bv = new BeanArrayList(this.size(), 0, this); bv.sortOnProperty(propertyName); for (int i = 0; i < bv.size(); i++) { if (!currentClass.equals(bv.get(i).getClass().getName())) { pd = (PropertyDescriptor) cache.get(bv.get(i).getClass().getName()); if (pd == null) { PropertyDescriptor[] pds = Introspector.getBeanInfo(bv.get(i).getClass()) .getPropertyDescriptors(); boolean foundProperty = false; for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) { if (pds[pdi].getName().equals(propertyName)) { pd = pds[pdi]; cache.put(bv.get(i).getClass().getName(), pd); foundProperty = true; } } } } if (hold == null) { hold = pd.getReadMethod().invoke(bv.get(i)); } else { o = pd.getReadMethod().invoke(bv.get(i)); if ((o != null) && o.equals(hold)) { count++; if (count > max) { max = count; index = this.indexOf(bv.get(i)); } } else { count = 1; } hold = o; } } return index; }
From source file:org.apache.myfaces.el.PropertyResolverImpl.java
public static PropertyDescriptor getPropertyDescriptor(Object base, String name) { PropertyDescriptor propertyDescriptor; try {//w w w .j a va 2 s .co m propertyDescriptor = getPropertyDescriptor(Introspector.getBeanInfo(base.getClass()), name); } catch (IntrospectionException e) { throw new PropertyNotFoundException(getMessage(base, name), e); } return propertyDescriptor; }
From source file:es.logongas.ix3.web.json.impl.JsonReaderImplEntityJackson.java
/** * Obtiene el valor de la propiedad de un Bean * * @param obj El objeto Bean/*from w ww . j a va 2 s . c om*/ * @param propertyName El nombre de la propiedad * @return El valor de la propiedad */ private Object getValueFromBean(Object obj, String propertyName) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Method readMethod = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(propertyName)) { readMethod = propertyDescriptor.getReadMethod(); } } if (readMethod == null) { throw new RuntimeException( "No existe la propiedad:" + propertyName + " en " + obj.getClass().getName()); } return readMethod.invoke(obj); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:org.rimudb.DataObjectNode.java
public PropertyDescriptor getPropertyDescriptor(String name) { PropertyDescriptor pd = (PropertyDescriptor) pdescMap.get(name); if (pd == null) { try {//from ww w. j av a 2s.co m BeanInfo info = Introspector.getBeanInfo(doClass); PropertyDescriptor pdesc[] = info.getPropertyDescriptors(); for (int i = 0; i < pdesc.length; i++) { String propname = pdesc[i].getName(); if (propname.equals(name)) { // these are properties to keep pdescMap.put(propname, pdesc[i]); pd = pdesc[i]; } } } catch (IntrospectionException e) { error("property " + name + " does not exist in " + getName()); } } return pd; }
From source file:org.jaffa.util.BeanHelper.java
/** Clones the input bean, performing a deep copy of its properties. * @param bean the bean to be cloned.// ww w. j ava 2s . c o m * @param deepCloneForeignField if false, then the foreign-fields of a GraphDataObject will not be deep cloned. * @return a clone of the input bean. * @throws IllegalAccessException if the underlying method is inaccessible. * @throws InvocationTargetException if the underlying method throws an exception. * @throws InstantiationException if the bean cannot be instantiated. * @throws IntrospectionException if an exception occurs during introspection. */ public static Object cloneBean(Object bean, boolean deepCloneForeignField) throws IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException { if (bean == null) return bean; Class beanClass = bean.getClass(); // Return the input as-is, if immutable if (beanClass == String.class || beanClass == Boolean.class || Number.class.isAssignableFrom(beanClass) || IDateBase.class.isAssignableFrom(beanClass) || Currency.class.isAssignableFrom(beanClass) || beanClass.isPrimitive()) { return bean; } // Handle an array if (beanClass.isArray()) { Class componentType = beanClass.getComponentType(); int length = Array.getLength(bean); Object clone = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Object arrayElementClone = cloneBean(Array.get(bean, i), deepCloneForeignField); Array.set(clone, i, arrayElementClone); } return clone; } // Handle a Collection if (bean instanceof Collection) { Collection clone = (Collection) bean.getClass().newInstance(); for (Object collectionElement : (Collection) bean) { Object collectionElementClone = cloneBean(collectionElement, deepCloneForeignField); clone.add(collectionElementClone); } return clone; } // Handle a Map if (bean instanceof Map) { Map clone = (Map) bean.getClass().newInstance(); for (Iterator i = ((Map) bean).entrySet().iterator(); i.hasNext();) { Map.Entry me = (Map.Entry) i.next(); Object keyClone = cloneBean(me.getKey(), deepCloneForeignField); Object valueClone = cloneBean(me.getValue(), deepCloneForeignField); clone.put(keyClone, valueClone); } return clone; } // Invoke the 'public Object clone()' method, if available if (bean instanceof Cloneable) { try { Method cloneMethod = beanClass.getMethod("clone"); return cloneMethod.invoke(bean); } catch (NoSuchMethodException e) { // do nothing } } // Create a clone using bean introspection Object clone = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); if (beanInfo != null) { PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); if (pds != null) { // Obtain a GraphMapping; only if foreign-fields are not to be cloned //Use reflection to achieve the following: //GraphMapping graphMapping = !deepCloneForeignField && bean instanceof GraphDataObject ? MappingFactory.getInstance(bean) : null; Object graphMapping = null; Method isForeignFieldMethod = null; try { if (!deepCloneForeignField && Class.forName("org.jaffa.soa.graph.GraphDataObject").isInstance(bean)) { graphMapping = Class.forName("org.jaffa.soa.dataaccess.MappingFactory") .getMethod("getInstance", Object.class).invoke(null, bean); isForeignFieldMethod = graphMapping.getClass().getMethod("isForeignField", String.class); } } catch (Exception e) { // do nothing since JaffaSOA may not be deployed if (log.isDebugEnabled()) log.debug("Exception in obtaining the GraphMapping", e); } for (PropertyDescriptor pd : pds) { if (pd.getReadMethod() != null && pd.getWriteMethod() != null) { // Do not clone a foreign-field Object property = pd.getReadMethod().invoke(bean); //Use reflection to achieve the following: //Object propertyClone = graphMapping != null && graphMapping.isForeignField(pd.getName()) ? property : cloneBean(property, deepCloneForeignField); Object propertyClone = null; boolean propertyCloned = false; if (graphMapping != null && isForeignFieldMethod != null) { try { if ((Boolean) isForeignFieldMethod.invoke(graphMapping, pd.getName())) { propertyClone = property; propertyCloned = true; } } catch (Exception e) { // do nothing since JaffaSOA may not be deployed if (log.isDebugEnabled()) log.debug("Exception in invoking GraphMapping.isForeignField()", e); } } if (!propertyCloned) propertyClone = cloneBean(property, deepCloneForeignField); pd.getWriteMethod().invoke(clone, propertyClone); } } } } return clone; }
From source file:org.jtrfp.trcl.file.LVLFile.java
private static DataKey[] generateUsedKeys() { ArrayList<DataKey> result = new ArrayList<DataKey>(); try {//from w ww . jav a 2s. co m for (PropertyDescriptor pd : Introspector.getBeanInfo(LVLFile.class).getPropertyDescriptors()) result.add(new DataKey(pd.getName(), pd.getDisplayName())); } catch (Exception e) { e.printStackTrace(); } return result.toArray(new DataKey[result.size()]); }
From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java
static List<ActionParameterType> findBeanInfo(final Class<?> beanType, final List<ActionParameterType> previous) throws IntrospectionException, NoSuchFieldException { // TODO support Option provider by other method args? final BeanInfo beanInfo = Introspector.getBeanInfo(beanType); final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); // add input field for every setter for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String propertyName = propertyDescriptor.getName(); if (isDefinedAlready(propertyName, previous)) { continue; }/* w w w . ja va 2 s . c om*/ final Method writeMethod = propertyDescriptor.getWriteMethod(); ActionParameterType type = null; if (writeMethod != null) { Field field = getFormAnnotated(propertyName, beanType); if (field != null) { type = new FieldParameterType(propertyName, field); } else { MethodParameter methodParameter = new MethodParameter(propertyDescriptor.getWriteMethod(), 0); type = new MethodParameterType(propertyName, methodParameter, propertyDescriptor.getReadMethod()); } } if (type == null) { continue; } previous.add(type); } return previous; }