List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyDescriptors
public static PropertyDescriptor[] getPropertyDescriptors(Object bean)
Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.
For more details see PropertyUtilsBean
.
From source file:com.vecna.taglib.processor.JspAnnotationsProcessor.java
/** * Build a JSP tag model object from an annotated tag class * @param type tag class//from w ww .j av a2 s. com * @return JSP tag model */ public JspTagModel getTagMetadata(Class<?> type) { JspTagModel metadata = new JspTagModel(); JspTag jspTagAnnotation = type.getAnnotation(JspTag.class); metadata.bodyContent = jspTagAnnotation.bodyContent(); metadata.name = jspTagAnnotation.name(); metadata.tagClass = type.getName(); metadata.dynamicAttributes = jspTagAnnotation.dynamicAttributes(); for (JspVariable jspVariableAnnotation : jspTagAnnotation.variables()) { JspVariableModel variable = new JspVariableModel(); variable.declare = jspVariableAnnotation.declare(); variable.nameFromAttribute = StringUtils.stripToNull(jspVariableAnnotation.nameFromAttribute()); variable.nameGiven = StringUtils.stripToNull(jspVariableAnnotation.nameGiven()); variable.scope = jspVariableAnnotation.scope(); variable.variableClass = jspVariableAnnotation.variableClass().getName(); metadata.variables.add(variable); } for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(type)) { s_log.debug("processing property {}", pd.getName()); if (pd.getWriteMethod() != null && pd.getWriteMethod().isAnnotationPresent(JspAttribute.class)) { s_log.debug("attribute metadata present on {}", pd.getName()); JspAttributeModel attr = new JspAttributeModel(); attr.name = pd.getName(); attr.type = pd.getPropertyType().getName(); JspAttribute jspAttributeAnnotation = pd.getWriteMethod().getAnnotation(JspAttribute.class); attr.required = jspAttributeAnnotation.required(); attr.rtExprValue = jspAttributeAnnotation.rtExprValue(); attr.fragment = jspAttributeAnnotation.fragment(); attr.deferredMethod = jspAttributeAnnotation.deferredMethod() ? true : null; attr.deferredValue = jspAttributeAnnotation.deferredValue() ? true : null; metadata.attributes.add(attr); } } return metadata; }
From source file:io.github.moosbusch.lumpi.util.FormUtil.java
public static Map<String, Object> getPropertyValuesMap(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Map<String, Object> result = new HashMap<>(); PropertyDescriptor[] propDescs = PropertyUtils.getPropertyDescriptors(bean.getClass()); for (PropertyDescriptor propDesc : propDescs) { String propertyName = propDesc.getName(); Object propertyValue = PropertyUtils.getProperty(bean, propertyName); result.put(propertyName, propertyValue); }/*from ww w.j a v a 2s. co m*/ return result; }
From source file:com.mindquarry.persistence.jcr.model.DefaultProperty.java
private PropertyDescriptor getPropertyDescriptor() { PropertyDescriptor result = null; for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(getDeclaringClass())) { if (getName().equals(descriptor.getName())) { result = descriptor;// w ww . j av a 2 s . co m break; } } return result; }
From source file:com.ettrema.httpclient.calsync.parse.CalDavBeanPropertyMapper.java
public void toBean(Object bean, String icalText) { ByteArrayInputStream fin = null; try {//from w w w . j ava2s. c o m fin = new ByteArrayInputStream(icalText.getBytes("UTF-8")); CalendarBuilder builder = new CalendarBuilder(); net.fortuna.ical4j.model.Calendar cal4jCalendar; try { cal4jCalendar = builder.build(fin); } catch (IOException ex) { throw new RuntimeException(icalText, ex); } catch (ParserException ex) { throw new RuntimeException(icalText, ex); } PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean); for (PropertyDescriptor pd : pds) { if (pd.getReadMethod() != null && pd.getWriteMethod() != null) { Method read = pd.getReadMethod(); Annotation[] annotations = read.getAnnotations(); for (Annotation anno : annotations) { Mapper mapper = mapOfMappers.get(anno.annotationType()); if (mapper != null) { mapper.mapToBean(cal4jCalendar, bean, pd); } } } } } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); } finally { IOUtils.closeQuietly(fin); } }
From source file:io.kahu.hawaii.domain.FieldChecker.java
private Set<String> getFieldNames(Object bean) { Class<?> beanClass = bean.getClass(); Set<String> fieldNames = new HashSet<String>(); /*/* w w w. ja v a 2 s . co m*/ * Add all properties from this class. */ PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(beanClass); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { fieldNames.add(propertyDescriptor.getName()); } /* * Remove all properties from the interface 'ValueHolder'. */ propertyDescriptors = PropertyUtils.getPropertyDescriptors(ValueHolder.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { fieldNames.remove(propertyDescriptor.getName()); } /* * Remove all properties from the interface 'ValueHolder'. */ propertyDescriptors = PropertyUtils.getPropertyDescriptors(ValidatableDomainObject.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { fieldNames.remove(propertyDescriptor.getName()); } /* * Ignore class */ fieldNames.remove("callbacks"); fieldNames.remove("callback"); fieldNames.remove("class"); return fieldNames; }
From source file:net.sf.dsig.Environment.java
/** * <p>Initialize an object's public properties, using any environmental * values that have been declared./*from www . java 2s .c o m*/ * * @param obj the object to initialize. * @param prefix the prefix to add while looking at the environmental values */ public void init(Object obj, String prefix) { // Iterate through all the properties declared for the applet class PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(obj); for (PropertyDescriptor descriptor : descriptors) { String propertyName = descriptor.getName(); logger.debug("Checking" + ": obj.class=" + obj.getClass() + ", propertyName=" + propertyName); if (descriptor.getWriteMethod() == null) { continue; } // Check if an environment parameter has been specified; if so, // override the value with the one supplied String key = (prefix != null) ? prefix + propertyName : propertyName; Object value; if (descriptor.getPropertyType().isArray()) { value = getValues(key); } else { value = getValue(key); } if (value != null) { logger.debug("Setting" + ": obj.class=" + obj.getClass() + ", propertyName=" + propertyName + ", value=" + value); try { BeanUtils.setProperty(obj, propertyName, value); } catch (Exception e) { logger.warn("Object initialization error", e); } } } }
From source file:ca.sqlpower.architect.swingui.TestPlayPenComponent.java
/** * Checks that the properties of an instance from the copy constructor are equal to the original. * In the case of a mutable property, it also checks that they don't share the same instance. * //from w w w .j a v a 2 s.c om * @throws Exception */ public void testCopyConstructor() throws Exception { PlayPenComponent comp = getTarget(); List<PropertyDescriptor> settableProperties = Arrays .asList(PropertyUtils.getPropertyDescriptors(comp.getClass())); copyIgnoreProperties.add("UI"); copyIgnoreProperties.add("UIClassID"); copyIgnoreProperties.add("UUID"); copyIgnoreProperties.add("allowedChildTypes"); copyIgnoreProperties.add("background"); copyIgnoreProperties.add("bounds"); copyIgnoreProperties.add("class"); copyIgnoreProperties.add("children"); copyIgnoreProperties.add("fontRenderContext"); copyIgnoreProperties.add("height"); copyIgnoreProperties.add("insets"); copyIgnoreProperties.add("lengths"); copyIgnoreProperties.add("location"); copyIgnoreProperties.add("locationOnScreen"); copyIgnoreProperties.add("magicEnabled"); copyIgnoreProperties.add("opaque"); copyIgnoreProperties.add("parent"); copyIgnoreProperties.add("playPen"); copyIgnoreProperties.add("popup"); copyIgnoreProperties.add("preferredLocation"); copyIgnoreProperties.add("preferredSize"); copyIgnoreProperties.add("selected"); copyIgnoreProperties.add("session"); copyIgnoreProperties.add("workspaceContainer"); copyIgnoreProperties.add("runnableDispatcher"); copyIgnoreProperties.add("size"); copyIgnoreProperties.add("toolTipText"); copyIgnoreProperties.add("width"); copyIgnoreProperties.add("x"); copyIgnoreProperties.add("y"); // no setters for this and it depends on the playpen's font copyIgnoreProperties.add("font"); // not so sure if this should be duplicated, it's changed as the model properties changes copyIgnoreProperties.add("modelName"); // copy and original should point to same business object copySameInstanceIgnoreProperties.add("model"); // First pass: set all settable properties, because testing the duplication of // an object with all its properties at their defaults is not a // very convincing test of duplication! for (PropertyDescriptor property : settableProperties) { if (copyIgnoreProperties.contains(property.getName())) continue; Object oldVal; try { oldVal = PropertyUtils.getSimpleProperty(comp, property.getName()); // check for a setter if (property.getWriteMethod() != null) { Object newVal = getNewDifferentValue(property, oldVal); BeanUtils.copyProperty(comp, property.getName(), newVal); } } catch (NoSuchMethodException e) { System.out.println("Skipping non-settable property " + property.getName() + " on " + comp.getClass().getName()); } } // Second pass get a copy make sure all of // the origional mutable objects returned from getters are different // between the two objects, but have the same values. PlayPenComponent duplicate = getTargetCopy(); for (PropertyDescriptor property : settableProperties) { if (copyIgnoreProperties.contains(property.getName())) continue; Object oldVal; try { oldVal = PropertyUtils.getSimpleProperty(comp, property.getName()); Object copyVal = PropertyUtils.getSimpleProperty(duplicate, property.getName()); if (oldVal == null) { throw new NullPointerException("We forgot to set " + property.getName()); } else { assertEquals("The two values for property " + property.getDisplayName() + " in " + comp.getClass().getName() + " should be equal", oldVal, copyVal); if (isPropertyInstanceMutable(property) && !copySameInstanceIgnoreProperties.contains(property.getName())) { assertNotSame("Copy shares mutable property with original, property name: " + property.getDisplayName(), copyVal, oldVal); } } } catch (NoSuchMethodException e) { System.out.println("Skipping non-settable property " + property.getName() + " on " + comp.getClass().getName()); } } }
From source file:com.handpay.ibenefit.framework.util.WebUtils.java
public static Map<String, Object> getSearchParameterMap(HttpServletRequest request, Class DomainObjecClass) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Map<String, Object> parameterMap = new HashMap(); Map requestMap = getParametersStartingWith(request, DEFAULT_SEARCH_PREFIX); boolean matchAll = true; PropertyDescriptor[] beandesc = PropertyUtils.getPropertyDescriptors(DomainObjecClass); for (int i = 0; i < beandesc.length; i++) { PropertyDescriptor descriptor = beandesc[i]; String propertyName = descriptor.getName(); Class propertyClass = descriptor.getPropertyType(); if ("class".equals(propertyName)) { continue; }//ww w . j av a2 s .com if (propertyClass == Date.class) { String after = (String) requestMap.get(propertyName + "After"); if (after != null && after.trim().length() > 0) { try { Date afterDate = dateFormat.parse(after); parameterMap.put(propertyName + "After", afterDate); } catch (Exception e) { } } String befor = (String) requestMap.get(propertyName + "Befor"); if (befor != null && befor.trim().length() > 0) { try { Date beforDate = dateFormat.parse(befor); // to the end of the day long dayTail = beforDate.getTime() + 24 * 3600 * 1000 - 1; parameterMap.put(propertyName + "Befor", new Date(dayTail)); } catch (Exception e) { } } } else if (propertyClass == String.class) { String value = (String) requestMap.get(propertyName); if (value != null && value.trim().length() > 0) { if (matchAll) { parameterMap.put(propertyName, "%" + value + "%"); } else { parameterMap.put(propertyName, value + "%"); } } } else if (propertyClass == Long.class) { String value = (String) requestMap.get(propertyName); if (value != null && value.trim().length() > 0) { parameterMap.put(propertyName, Long.parseLong(value)); } } else if (propertyClass == Integer.class) { String value = (String) requestMap.get(propertyName); if (value != null && value.trim().length() > 0) { parameterMap.put(propertyName, Integer.parseInt(value)); } } else if (propertyClass == Double.class) { String value = (String) requestMap.get(propertyName); if (value != null && value.trim().length() > 0) { parameterMap.put(propertyName, Double.parseDouble(value)); } } else if (propertyClass == Float.class) { String value = (String) requestMap.get(propertyName); if (value != null && value.trim().length() > 0) { parameterMap.put(propertyName, Float.parseFloat(value)); } } else if (propertyClass == Boolean.class) { String value = (String) requestMap.get(propertyName); if (value != null && value.trim().length() > 0) { parameterMap.put(propertyName, Boolean.parseBoolean(value)); } } } return parameterMap; }
From source file:info.magnolia.content2bean.TypeDescriptor.java
/** * This method is not synchronized to avoid thread blocking, but the method guarantees that the returned map is not mutated afterward. *///w w w. j a va 2s . c o m public Map<String, PropertyTypeDescriptor> getPropertyDescriptors(TypeMapping typeMapping) { //TODO ---- moved this out to TypeDescriptorFactory or something ? if (this.descriptors == null) { // for not making this method synchronized we create a local variable first // this guarantees that the map you get is not changed after return final Map<String, PropertyTypeDescriptor> tmpDescriptors = new HashMap<String, PropertyTypeDescriptor>(); PropertyDescriptor[] dscrs = PropertyUtils.getPropertyDescriptors(this.getType()); for (int i = 0; i < dscrs.length; i++) { PropertyDescriptor descriptor = dscrs[i]; tmpDescriptors.put(descriptor.getName(), typeMapping.getPropertyTypeDescriptor(this.getType(), descriptor.getName())); } this.descriptors = Collections.unmodifiableMap(tmpDescriptors); } return this.descriptors; }
From source file:name.martingeisse.common.javascript.serialize.BeanToJavascriptObjectSerializer.java
/** * // ww w .ja va 2s .c o m */ private void serializeAllFields(final T bean, final JavascriptAssembler assembler) throws Exception { for (final PropertyDescriptor property : PropertyUtils.getPropertyDescriptors(bean)) { final String beanPropertyName = property.getName(); if (beanPropertyName.equals("class")) { continue; } final String serializedFieldName = mapPropertyNameToSerializedName(beanPropertyName); final Object value = property.getReadMethod().invoke(bean); assembler.prepareObjectProperty(serializedFieldName); serializeFieldValue(bean, assembler, beanPropertyName, serializedFieldName, value); } }