List of usage examples for java.beans PropertyDescriptor getPropertyType
public synchronized Class<?> getPropertyType()
From source file:org.bibsonomy.model.util.BibTexUtils.java
/** * return a bibtex string representation of the given bibtex object * /*from w w w. j a v a2s.com*/ * @param bib - a bibtex object * @param mode - the serializing mode (parse misc fields or include misc fields as they are) * @return String bibtexString * * TODO use BibTex.DEFAULT_OPENBRACKET etc. * */ public static String toBibtexString(final BibTex bib, SerializeBibtexMode mode) { try { final BeanInfo bi = Introspector.getBeanInfo(bib.getClass()); /* * start with entrytype and key */ final StringBuilder buffer = new StringBuilder( "@" + bib.getEntrytype() + "{" + bib.getBibtexKey() + ",\n"); /* * append all other fields */ for (final PropertyDescriptor d : bi.getPropertyDescriptors()) { final Method getter = d.getReadMethod(); // loop over all String attributes final Object o = getter.invoke(bib, (Object[]) null); if (String.class.equals(d.getPropertyType()) && o != null && !EXCLUDE_FIELDS.contains(d.getName())) { /* * Strings containing whitespace give empty fields ... we ignore them */ String value = ((String) o); if (present(value)) { if (!NUMERIC_PATTERN.matcher(value).matches()) { value = DEFAULT_OPENING_BRACKET + value + DEFAULT_CLOSING_BRACKET; } buffer.append(" " + d.getName().toLowerCase() + " = " + value + ",\n"); } } } /* * process miscFields map, if present */ if (present(bib.getMiscFields())) { if (mode.equals(SerializeBibtexMode.PARSED_MISCFIELDS) && !bib.isMiscFieldParsed()) { // parse misc field, if not yet done bib.parseMiscField(); } buffer.append(serializeMiscFields(bib.getMiscFields(), true)); } /* * include plain misc fields if desired */ if (mode.equals(SerializeBibtexMode.PLAIN_MISCFIELDS) && present(bib.getMisc())) { buffer.append(" " + bib.getMisc() + ",\n"); } /* * add month */ final String month = bib.getMonth(); if (present(month)) { // we don't add {}, this is done by getMonth(), if necessary buffer.append(" month = " + getMonth(month) + ",\n"); } /* * add abstract */ final String bibAbstract = bib.getAbstract(); if (present(bibAbstract)) { buffer.append(" abstract = {" + bibAbstract + "},\n"); } /* * remove last comma */ buffer.delete(buffer.lastIndexOf(","), buffer.length()); buffer.append("\n}"); return buffer.toString(); } catch (IntrospectionException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } return null; }
From source file:com.github.erchu.beancp.PropertyBindingSide.java
/** * * Creates binding to property from property information. * * @param propertyDescriptor property information used to create binding *//*w w w. java 2s . c o m*/ public PropertyBindingSide(final PropertyDescriptor propertyDescriptor) { this._valueClass = propertyDescriptor.getPropertyType(); this._readMethod = propertyDescriptor.getReadMethod(); this._writeMethod = propertyDescriptor.getWriteMethod(); this._name = propertyDescriptor.getName(); }
From source file:org.apache.syncope.core.persistence.jpa.entity.AbstractEntity.java
/** * @return fields to be excluded when computing equals() or hashcode() *//* w w w . jav a 2 s. c om*/ private String[] getExcludeFields() { Set<String> excludeFields = new HashSet<>(); for (PropertyDescriptor propDesc : BeanUtils.getPropertyDescriptors(getClass())) { if (propDesc.getPropertyType().isInstance(Collections.emptySet()) || propDesc.getPropertyType().isInstance(Collections.emptyList())) { excludeFields.add(propDesc.getName()); } } return excludeFields.toArray(new String[] {}); }
From source file:org.nekorp.workflow.desktop.view.model.validacion.ValidacionParticular.java
public void evaluaTodo(ViewValidIndicator evaluacionGeneral) { try {/* w w w .j a va2 s. co m*/ for (PropertyDescriptor x : PropertyUtils.getPropertyDescriptors(this)) { if (EstatusValidacion.class.isAssignableFrom(x.getPropertyType())) { EstatusValidacion arg = (EstatusValidacion) PropertyUtils.getProperty(this, x.getName()); if (!arg.isValido()) { evaluacionGeneral.setValido(false); return; } } } evaluacionGeneral.setValido(true); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalArgumentException("No se logro evaluar", ex); } }
From source file:net.jakubholy.jeeutils.jsfelcheck.util.BeanPropertyUtils.java
/** * Find the type of the given property.//w w w .j a v a2 s . c om * @param propertyName (required) * @return the type of the property or null if not found */ public Class<?> getPropertyTypeOf(String propertyName) { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(type); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getName().equals(propertyName)) { return descriptor.getPropertyType(); } } LOG.fine("No property '" + propertyName + "' found on the class " + type.getName()); return null; }
From source file:org.nekorp.workflow.desktop.view.model.validacion.ValidacionParticular.java
public String concatenaErrores() { try {//from w ww . j ava 2 s. c o m String result = ""; for (PropertyDescriptor x : PropertyUtils.getPropertyDescriptors(this)) { if (EstatusValidacion.class.isAssignableFrom(x.getPropertyType())) { EstatusValidacion arg = (EstatusValidacion) PropertyUtils.getProperty(this, x.getName()); if (!arg.isValido()) { if (!StringUtils.isEmpty(result)) { result = result + "\n"; } result = result + arg.getDetalle(); } } } return result; } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalArgumentException("No se logro evaluar", ex); } }
From source file:com.cyclopsgroup.tornado.utils.BeanDynaTagSupport.java
/** * Overwrite or implement method getAttributeType() * * @see com.cyclopsgroup.waterview.utils.DynaTagSupport#getAttributeType(java.lang.String) *//*from www . j a v a 2s .c om*/ public Class getAttributeType(String attributeName) throws JellyTagException { if (beanClass == null) { return super.getAttributeType(attributeName); } PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; if (descriptor.getName().equals(attributeName)) { return descriptor.getPropertyType(); } } return super.getAttributeType(attributeName); }
From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java
/** * Creates a bean from a JSONObject, with the specific configuration. *///w w w . ja v a 2 s . c o m public static Object toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig) { if (jsonObject == null || jsonObject.isNullObject() || root == null) { return root; } Class rootClass = root.getClass(); if (rootClass.isInterface()) { throw new JSONException("Root bean is an interface. " + rootClass); } Map classMap = jsonConfig.getClassMap(); if (classMap == null) { classMap = Collections.EMPTY_MAP; } Map props = JSONUtils.getProperties(jsonObject); PropertyFilter javaPropertyFilter = jsonConfig.getJavaPropertyFilter(); for (Iterator entries = jsonObject.names().iterator(); entries.hasNext();) { String name = (String) entries.next(); Class type = (Class) props.get(name); Object value = jsonObject.get(name); if (javaPropertyFilter != null && javaPropertyFilter.apply(root, name, value)) { continue; } String key = JSONUtils.convertToJavaIdentifier(name, jsonConfig); try { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(root, key); if (pd != null && pd.getWriteMethod() == null) { log.warn("Property '" + key + "' has no write method. SKIPPED."); continue; } if (!JSONUtils.isNull(value)) { if (value instanceof JSONArray) { if (pd == null || List.class.isAssignableFrom(pd.getPropertyType())) { Class targetClass = findTargetClass(key, classMap); targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass; Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null); List list = JSONArray.toList((JSONArray) value, newRoot, jsonConfig); setProperty(root, key, list, jsonConfig); } else { Class innerType = JSONUtils.getInnerComponentType(pd.getPropertyType()); Class targetInnerType = findTargetClass(key, classMap); if (innerType.equals(Object.class) && targetInnerType != null && !targetInnerType.equals(Object.class)) { innerType = targetInnerType; } Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(innerType, null); Object array = JSONArray.toArray((JSONArray) value, newRoot, jsonConfig); if (innerType.isPrimitive() || JSONUtils.isNumber(innerType) || Boolean.class.isAssignableFrom(innerType) || JSONUtils.isString(innerType)) { array = JSONUtils.getMorpherRegistry() .morph(Array.newInstance(innerType, 0).getClass(), array); } else if (!array.getClass().equals(pd.getPropertyType())) { if (!pd.getPropertyType().equals(Object.class)) { Morpher morpher = JSONUtils.getMorpherRegistry() .getMorpherFor(Array.newInstance(innerType, 0).getClass()); if (IdentityObjectMorpher.getInstance().equals(morpher)) { ObjectArrayMorpher beanMorpher = new ObjectArrayMorpher( new BeanMorpher(innerType, JSONUtils.getMorpherRegistry())); JSONUtils.getMorpherRegistry().registerMorpher(beanMorpher); } array = JSONUtils.getMorpherRegistry() .morph(Array.newInstance(innerType, 0).getClass(), array); } } setProperty(root, key, array, jsonConfig); } } else if (String.class.isAssignableFrom(type) || JSONUtils.isBoolean(type) || JSONUtils.isNumber(type) || JSONUtils.isString(type) || JSONFunction.class.isAssignableFrom(type)) { if (pd != null) { if (jsonConfig.isHandleJettisonEmptyElement() && "".equals(value)) { setProperty(root, key, null, jsonConfig); } else if (!pd.getPropertyType().isInstance(value)) { Morpher morpher = JSONUtils.getMorpherRegistry() .getMorpherFor(pd.getPropertyType()); if (IdentityObjectMorpher.getInstance().equals(morpher)) { log.warn("Can't transform property '" + key + "' from " + type.getName() + " into " + pd.getPropertyType().getName() + ". Will register a default BeanMorpher"); JSONUtils.getMorpherRegistry().registerMorpher( new BeanMorpher(pd.getPropertyType(), JSONUtils.getMorpherRegistry())); } setProperty(root, key, JSONUtils.getMorpherRegistry().morph(pd.getPropertyType(), value), jsonConfig); } else { setProperty(root, key, value, jsonConfig); } } else if (root instanceof Map) { setProperty(root, key, value, jsonConfig); } else { log.warn("Tried to assign property " + key + ":" + type.getName() + " to bean of class " + root.getClass().getName()); } } else { if (pd != null) { Class targetClass = pd.getPropertyType(); if (jsonConfig.isHandleJettisonSingleElementArray()) { JSONArray array = new JSONArray().element(value, jsonConfig); Class newTargetClass = findTargetClass(key, classMap); newTargetClass = newTargetClass == null ? findTargetClass(name, classMap) : newTargetClass; Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(newTargetClass, null); if (targetClass.isArray()) { setProperty(root, key, JSONArray.toArray(array, newRoot, jsonConfig), jsonConfig); } else if (Collection.class.isAssignableFrom(targetClass)) { setProperty(root, key, JSONArray.toList(array, newRoot, jsonConfig), jsonConfig); } else if (JSONArray.class.isAssignableFrom(targetClass)) { setProperty(root, key, array, jsonConfig); } else { setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig); } } else { if (targetClass == Object.class) { targetClass = findTargetClass(key, classMap); targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass; } Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null); setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig); } } else if (root instanceof Map) { Class targetClass = findTargetClass(key, classMap); targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass; Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null); setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig); } else { log.warn("Tried to assign property " + key + ":" + type.getName() + " to bean of class " + rootClass.getName()); } } } else { if (type.isPrimitive()) { // assume assigned default value log.warn("Tried to assign null value to " + key + ":" + type.getName()); setProperty(root, key, JSONUtils.getMorpherRegistry().morph(type, null), jsonConfig); } else { setProperty(root, key, null, jsonConfig); } } } catch (JSONException jsone) { throw jsone; } catch (Exception e) { throw new JSONException("Error while setting property=" + name + " type " + type, e); } } return root; }
From source file:it.geosolutions.geoserver.jms.impl.utils.BeanUtils.java
/** * This is a 'smart' (perform checks for some special cases) update function which should be used to copy of the properties for objects * of the catalog and configuration./* ww w . ja va 2s . c om*/ * * @param <T> the type of the bean to update * @param info the bean instance to update * @param properties the list of string of properties to update * @param values the list of new values to update * * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException */ public static <T> void smartUpdate(final T info, final List<String> properties, final List<Object> values) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Iterator<String> itPropertyName = properties.iterator(); final Iterator<Object> itValue = values.iterator(); while (itPropertyName.hasNext() && itValue.hasNext()) { String propertyName = itPropertyName.next(); final Object value = itValue.next(); PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(info, propertyName); // return null if there is no such descriptor if (pd == null) { // this is a special case used by the NamespaceInfoImpl setURI // the propertyName coming from the ModificationProxy is set to 'uRI' // lets set it to uri propertyName = propertyName.toUpperCase(); pd = PropertyUtils.getPropertyDescriptor(info, propertyName); if (pd == null) { return; } } if (pd.getWriteMethod() != null) { PropertyUtils.setProperty(info, propertyName, value); } else { // T interface do not declare setter method for this property // lets use getter methods to get the property reference final Object property = PropertyUtils.getProperty(info, propertyName); // check type of property to apply new value if (Collection.class.isAssignableFrom(pd.getPropertyType())) { final Collection<?> liveCollection = (Collection<?>) property; liveCollection.clear(); liveCollection.addAll((Collection) value); } else if (Map.class.isAssignableFrom(pd.getPropertyType())) { final Map<?, ?> liveMap = (Map<?, ?>) property; liveMap.clear(); liveMap.putAll((Map) value); } else { if (CatalogUtils.LOGGER.isLoggable(java.util.logging.Level.SEVERE)) CatalogUtils.LOGGER.severe("Skipping unwritable property " + propertyName + " with property type " + pd.getPropertyType()); } } } }
From source file:com.cyclopsgroup.tornado.hibernate.taglib.NewTag.java
/** * Overwrite or implement method getAttributeType() * * @see com.cyclopsgroup.waterview.utils.DynaTagSupport#getAttributeType(java.lang.String) *//* w w w. j ava2s . com*/ public Class getAttributeType(String attributeName) throws JellyTagException { if (attributeName.equals("var")) { return String.class; } if (descriptors == null) { ClassTag classTag = (ClassTag) requireParent(ClassTag.class); descriptors = PropertyUtils.getPropertyDescriptors(classTag.getEntityClass()); } for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; if (descriptor.getName().equals(attributeName)) { return descriptor.getPropertyType(); } } throw new JellyTagException("Attribute " + attributeName + " is not supported"); }