List of usage examples for java.beans BeanInfo getPropertyDescriptors
PropertyDescriptor[] getPropertyDescriptors();
From source file:org.jaffa.util.BeanHelper.java
/** This will inspect the specified java bean, and extract an Object from the beans * getXxx() method, where 'xxx' is the field name passed in. * A null will be returned in case there is any error in invoking the getter. * @param bean The Java Bean.//w w w . j a va 2 s . co m * @param field The field. * @throws NoSuchMethodException if there is no getter for the input field. * @return the output of the getter for the field. */ public static Object getField(Object bean, String field) throws NoSuchMethodException { java.lang.reflect.Method method = null; try { if (bean instanceof DynaBean) { try { return PropertyUtils.getProperty(bean, field); } catch (NoSuchMethodException e) { // If the bean is a FlexBean instance, then the field could exist on the associated persistentObject if (bean instanceof FlexBean && ((FlexBean) bean).getPersistentObject() != null) return PropertyUtils.getProperty(((FlexBean) bean).getPersistentObject(), field); else throw e; } } else { // Get the Java Bean Info java.beans.BeanInfo info = java.beans.Introspector.getBeanInfo(bean.getClass()); if (info != null) { // Get all the properties java.beans.PropertyDescriptor[] pds = info.getPropertyDescriptors(); if (pds != null) { // Loop for a matching method for (PropertyDescriptor pd : pds) { if (StringHelper.equalsIgnoreCaseFirstChar(pd.getName(), field)) { // Match found.... method = pd.getReadMethod(); break; } } } } if (method != null) { return method.invoke(bean, new Object[] {}); } // Finally, check the FlexBean if (bean instanceof IFlexFields) { FlexBean flexBean = ((IFlexFields) bean).getFlexBean(); if (flexBean != null && flexBean.get(field) != null) { return flexBean.get(field); } else { throw new NoSuchMethodException(); } } } } catch (NoSuchMethodException ex) { throw ex; } catch (Exception ex) { log.error("Introspection of Property " + field + " on Bean " + bean + " failed. Reason : " + ex.getMessage(), ex); return null; } // If we reach here, the method was not found throw new NoSuchMethodException("Field Name = " + field); }
From source file:ch.flashcard.HibernateDetachUtility.java
private static void nullOutFieldsByAccessors(Object value, Map<Integer, Object> checkedObjects, Map<Integer, List<Object>> checkedObjectCollisionMap, int depth, SerializationType serializationType) throws Exception { // Null out any collections that aren't loaded BeanInfo bi = Introspector.getBeanInfo(value.getClass(), Object.class); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { Object propertyValue = null; try {/*from w w w. j a va2 s. c o m*/ propertyValue = pd.getReadMethod().invoke(value); } catch (Throwable lie) { if (LOG.isDebugEnabled()) { LOG.debug("Couldn't load: " + pd.getName() + " off of " + value.getClass().getSimpleName(), lie); } } if (!Hibernate.isInitialized(propertyValue)) { try { if (LOG.isDebugEnabled()) { LOG.debug("Nulling out: " + pd.getName() + " off of " + value.getClass().getSimpleName()); } Method writeMethod = pd.getWriteMethod(); if ((writeMethod != null) && (writeMethod.getAnnotation(XmlTransient.class) == null)) { pd.getWriteMethod().invoke(value, new Object[] { null }); } else { nullOutField(value, pd.getName()); } } catch (Exception lie) { LOG.debug("Couldn't null out: " + pd.getName() + " off of " + value.getClass().getSimpleName() + " trying field access", lie); nullOutField(value, pd.getName()); } } else { if ((propertyValue instanceof Collection) || ((propertyValue != null) && propertyValue.getClass().getName().startsWith("org.rhq.core.domain"))) { nullOutUninitializedFields(propertyValue, checkedObjects, checkedObjectCollisionMap, depth + 1, serializationType); } } } }
From source file:com.seovic.core.objects.DynamicObject.java
public static Map<String, Object> getPropertyMap(Object obj) { Assert.notNull(obj, "Argument cannot be null"); try {//from www . java 2 s. c o m BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Map<String, Object> propertyMap = new HashMap<String, Object>(propertyDescriptors.length); for (PropertyDescriptor pd : propertyDescriptors) { Method getter = pd.getReadMethod(); if (getter != null) { propertyMap.put(pd.getName(), getter.invoke(obj)); } } return propertyMap; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.cocoon.transformation.TagTransformer.java
private static Map getWriteMethodMap(Class beanClass) throws IntrospectionException { Map map = (Map) TAG_PROPERTIES_MAP.get(beanClass); if (map != null) { return map; }/*from w w w . java 2 s .c om*/ BeanInfo info = Introspector.getBeanInfo(beanClass); if (info != null) { PropertyDescriptor pds[] = info.getPropertyDescriptors(); map = new HashMap(pds.length * 4 / 3, 1); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; String name = pd.getName(); Method method = pd.getWriteMethod(); Class type = pd.getPropertyType(); if (type != String.class) // only String properties continue; map.put(name, method); } } TAG_PROPERTIES_MAP.put(beanClass, map); return map; }
From source file:org.bibsonomy.plugin.jabref.util.JabRefModelConverter.java
/** * @param bibtex/*from w w w . j a v a 2 s .c o m*/ * target object * @param entry * source object * @return list of all copied property names */ public static List<String> copyStringPropertiesToBibsonomyModel(final BibTex bibtex, final BibtexEntry entry) { final List<String> knownFields = new ArrayList<String>(50); final BeanInfo info; try { info = Introspector.getBeanInfo(bibtex.getClass()); } catch (IntrospectionException e) { ExceptionUtils.logErrorAndThrowRuntimeException(log, e, "could not introspect"); return knownFields; } final PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); // set all known properties of the BibTex for (PropertyDescriptor pd : descriptors) { if (String.class.equals(pd.getPropertyType()) == false) { continue; } if (present(entry.getField((pd.getName().toLowerCase()))) && !JabRefModelConverter.EXCLUDE_FIELDS.contains(pd.getName().toLowerCase())) { final Object value = entry.getField(pd.getName().toLowerCase()); try { pd.getWriteMethod().invoke(bibtex, value); } catch (Exception e) { ExceptionUtils.logErrorAndThrowRuntimeException(log, e, "could not convert property " + pd.getName()); return knownFields; } knownFields.add(pd.getName()); } } return knownFields; }
From source file:net.sf.jrf.domain.PersistentObjectDynaClass.java
/** Factory method to create a <code>PersistentObjectDynaClass</code> based on bean properties * of the <code>PersistentObject</code> managed by the <code>AbstractDomain</code> instance parameter. * If the object is a composite, <code>Map</code> and <code>List</code> properties will * be included.//from w w w. j a v a2 s . c o m * @param domain <code>AbstractDomain</code> instance to inspect. * @param beanClass class name of the implementer of <code>DynaBean</code> * returned <code>PersistentObjectDynaClass</code>'s <code>PersistentObjectDynaProperty</code> list. * @return <code>PersistentObjectDynaClass</code> based on internal column specifications. */ static public PersistentObjectDynaClass createPersistentObjectDynaClass(AbstractDomain domain, Class beanClass) { BeanInfo beanInfo; PersistentObject obj = domain.newPersistentObject(); // Introspect the Persistent Object. try { beanInfo = Introspector.getBeanInfo(obj.getClass()); } catch (IntrospectionException ex) { throw new ConfigurationException(ex, "Unexpected introspection exception on " + obj.getClass()); } PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); HashMap propertyMap = new HashMap(); for (int i = 0; i < properties.length; i++) { if (properties[i].getReadMethod() != null) { Class returnType = properties[i].getReadMethod().getReturnType(); /** NO - include them (Ignore Maps and Lists from composites) if (java.util.Map.class.isAssignableFrom(returnType) || java.util.List.class.isAssignableFrom(returnType)) continue; **/ String writeMethodName = properties[i].getWriteMethod() == null ? null : properties[i].getWriteMethod().getName(); propertyMap.put(properties[i].getName(), new PersistentObjectDynaProperty(properties[i].getName(), returnType, properties[i].getReadMethod().getName(), writeMethodName)); } } // Reconcile bean properties with the column specifications. Iterator columnSpecs = domain.getColumnSpecs().iterator(); while (columnSpecs.hasNext()) { ColumnSpec c = (ColumnSpec) columnSpecs.next(); if (c instanceof CompoundPrimaryKeyColumnSpec) { CompoundPrimaryKeyColumnSpec cp = (CompoundPrimaryKeyColumnSpec) c; Iterator iter = cp.getColumnSpecs().iterator(); while (iter.hasNext()) { PersistentObjectDynaProperty p = updatePropertyMap((ColumnSpec) iter.next(), propertyMap); // Force dyna property for primary key to true. Value for compound keys does not // have this value set automatically. p.setPrimaryKey(true); } } else { updatePropertyMap(c, propertyMap); } } List list = new ArrayList(propertyMap.values()); return new PersistentObjectDynaClass(domain.getPropertyName(), beanClass, obj.getClass(), list); }
From source file:com.webbfontaine.valuewebb.model.util.Utils.java
public static PropertyDescriptor[] getBeanProperties(Class _class) { try {/*from w w w . j a v a 2 s. com*/ BeanInfo bi = Introspector.getBeanInfo(_class); return bi.getPropertyDescriptors(); } catch (IntrospectionException e) { LOGGER.error("", e); } return null; }
From source file:acmi.l2.clientmod.xdat.Controller.java
private static List<PropertySheetItem> loadProperties(Object obj) { Class<?> objClass = obj.getClass(); List<PropertySheetItem> list = new ArrayList<>(); while (objClass != Object.class) { try {//from w ww . ja v a 2s.c o m List<String> names = Arrays.stream(objClass.getDeclaredFields()) .map(field -> field.getName().replace("Prop", "")).collect(Collectors.toList()); BeanInfo beanInfo = Introspector.getBeanInfo(objClass, objClass.getSuperclass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Arrays.sort(propertyDescriptors, (pd1, pd2) -> Integer.compare(names.indexOf(pd1.getName()), names.indexOf(pd2.getName()))); for (PropertyDescriptor descriptor : propertyDescriptors) { if ("metaClass".equals(descriptor.getName())) continue; if (Collection.class.isAssignableFrom(descriptor.getPropertyType())) continue; AnnotatedElement getter = descriptor.getReadMethod(); if (getter.isAnnotationPresent(Deprecated.class) || getter.isAnnotationPresent(Hide.class)) continue; String description = ""; if (getter.isAnnotationPresent(Description.class)) description = getter.getAnnotation(Description.class).value(); Class<? extends PropertyEditor<?>> propertyEditorClass = null; if (descriptor.getPropertyType() == Boolean.class || descriptor.getPropertyType() == Boolean.TYPE) { propertyEditorClass = BooleanPropertyEditor.class; } else if (getter.isAnnotationPresent(Tex.class)) { propertyEditorClass = TexturePropertyEditor.class; } else if (getter.isAnnotationPresent(Sysstr.class)) { propertyEditorClass = SysstringPropertyEditor.class; } BeanProperty property = new BeanProperty(descriptor, objClass.getSimpleName(), description, propertyEditorClass); list.add(property); } } catch (IntrospectionException e) { e.printStackTrace(); } objClass = objClass.getSuperclass(); } return list; }
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 . j a 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; }
From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java
public static Set<String> getBeanPropertyNames(Class<?> beanClass) { Set<String> result = new HashSet<>(); BeanInfo beanInfo = null; try {//from w w w. j a v a 2 s . c om beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException ex) { Logger.getLogger(LumpiUtil.class.getName()).log(Level.SEVERE, null, ex); } finally { if (beanInfo != null) { PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (ArrayUtils.isNotEmpty(propertyDescriptors)) { for (PropertyDescriptor pd : propertyDescriptors) { result.add(pd.getName()); } } } } return result; }