List of usage examples for org.springframework.beans BeanUtils getPropertyDescriptor
@Nullable public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) throws BeansException
From source file:net.eusashead.hateoas.header.impl.PropertyUtil.java
public static Object getValue(Object target, String name) { PropertyDescriptor property = BeanUtils.getPropertyDescriptor(target.getClass(), name); try {//from w ww .ja v a 2 s. co m return property.getReadMethod().invoke(target); } catch (Exception e) { throw new RuntimeException("Unable to get value for target " + target.getClass()); } }
From source file:org.zkybase.cmdb.util.ReflectionUtils.java
public static <T, U> U copySimpleProperties(T from, U to) { try {//from w w w .j a v a2 s . c o m PropertyDescriptor[] fromDescs = BeanUtils.getPropertyDescriptors(from.getClass()); for (PropertyDescriptor fromDesc : fromDescs) { String propName = fromDesc.getName(); PropertyDescriptor toDesc = BeanUtils.getPropertyDescriptor(to.getClass(), propName); if (toDesc != null) { Method readMethod = fromDesc.getReadMethod(); Object value = readMethod.invoke(from); boolean write = (value instanceof Long || value instanceof String); if (write) { Method writeMethod = toDesc.getWriteMethod(); writeMethod.invoke(to, value); } } } return to; } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:org.atemsource.atem.impl.hibernate.PropertyDescriptor.java
public static PropertyDescriptor createInstance(final Class clazz, String propertyName) { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(); try {//from ww w . java2 s .com propertyDescriptor.declaringClass = clazz; Class currentClass = clazz; // this is probably necessary because the field is named exactly like the getter if (propertyName.startsWith("is")) { propertyName = propertyName.substring(2, 3).toLowerCase() + propertyName.substring(3); } else if (propertyName.startsWith("get")) { propertyName = propertyName.substring(3, 4).toLowerCase() + propertyName.substring(4); } java.beans.PropertyDescriptor propertyDescriptor2 = BeanUtils.getPropertyDescriptor(clazz, propertyName); if (propertyDescriptor2 != null) { propertyDescriptor.readMethod = propertyDescriptor2.getReadMethod(); propertyDescriptor.writeMethod = propertyDescriptor2.getWriteMethod(); } else { PropertyDescriptor propertyDescriptor3 = new PropertyDescriptor(); while (currentClass != null) { try { propertyDescriptor3.field = clazz.getDeclaredField(propertyName); propertyDescriptor3.propertyName = propertyName; propertyDescriptor3.declaringClass = currentClass; return propertyDescriptor3; } catch (NoSuchFieldException e) { } currentClass = currentClass.getSuperclass(); } } while (currentClass != null) { try { propertyDescriptor.field = clazz.getDeclaredField(propertyName); } catch (NoSuchFieldException e) { } currentClass = currentClass.getSuperclass(); } } catch (SecurityException e) { return null; } propertyDescriptor.propertyName = propertyName; return propertyDescriptor; }
From source file:com.nortal.petit.beanmapper.BeanMappingUtils.java
/** * Returns the initialized property./*from w w w. j ava 2s. c o m*/ * In case of Embedded property the root property is returned for reference. Embedde properties themselves are expanded * in the props variable. * * Null is returned if the property is not valid (either not readable/writable or Transient) * * @param props * @param name * @param type */ public static <B> Property<B, Object> initProperty(Map<String, Property<B, Object>> props, String name, Class<B> type) { PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, name); if (!isPropertyReadableAndWritable(pd)) { return null; } List<Annotation> ans = BeanMappingReflectionUtils.readAnnotations(type, pd.getName()); if (BeanMappingReflectionUtils.getAnnotation(ans, Transient.class) != null) { return null; } Column column = BeanMappingReflectionUtils.getAnnotation(ans, Column.class); ReflectionProperty<B, Object> prop = new ReflectionProperty<B, Object>(name, (Class<Object>) pd.getPropertyType(), inferColumn(name, column), pd.getWriteMethod(), pd.getReadMethod()); if (column != null) { prop.readOnly(!column.insertable()); } if (BeanMappingReflectionUtils.getAnnotation(ans, Id.class) != null) { prop.setIdProperty(true); } if (useAdditionalConfiguration()) { prop.getConfiguration().setAnnotations(ans); if (Collection.class.isAssignableFrom(pd.getPropertyType())) { prop.getConfiguration().setCollectionTypeArguments( ((ParameterizedType) pd.getReadMethod().getGenericReturnType()).getActualTypeArguments()); } } if (BeanMappingReflectionUtils.getAnnotation(ans, Embedded.class) != null) { props.putAll(getCompositeProperties(prop, ans)); } else { props.put(prop.name(), prop); } return prop; }
From source file:com.ctlts.wfaas.data.orchestrate.query.PropertyMetadata.java
public PropertyMetadata(Class<?> type, String name) { this.field = FieldUtils.getDeclaredField(type, name, true); this.descriptor = BeanUtils.getPropertyDescriptor(type, name); }
From source file:com.nortal.petit.beanmapper.BeanMappingReflectionUtils.java
private static void readAnnotations(List<Annotation> l, Class<?> type, String name) { Column ao = getAttributeOverride(type, name); if (ao != null) { l.add(ao);/*from www. j av a2 s . c o m*/ } Field field = FieldUtils.getDeclaredField(type, name, true); if (field != null) { addAll(l, field.getAnnotations()); } PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, name); if (pd != null) { if (pd.getReadMethod() != null) { addAll(l, pd.getReadMethod().getAnnotations()); } } if (type.getSuperclass() != null) { readAnnotations(l, type.getSuperclass(), name); } }
From source file:org.jdal.text.FormatUtils.java
/** * Get a formatter for class and property name * @param clazz the class//from www .ja va 2s . c om * @param propertyName the property name * @return the formatter or null if none */ public static Formatter<?> getFormatter(Class<?> clazz, String propertyName) { PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, propertyName); if (pd != null) { NumberFormat format = getAnnotation(pd, NumberFormat.class); if (format != null) { return (Formatter<?>) numberFormatFactory.getPrinter(format, pd.getPropertyType()); } PeriodFormat periodFormat = getAnnotation(pd, PeriodFormat.class); if (periodFormat != null) return new PeriodFormatter(); } return null; }
From source file:org.dataconservancy.packaging.tool.ser.SerializationAnnotationUtil.java
/** * Answers a {@code Map} of {@link PropertyDescriptor} instances, which are used to reflectively access the * {@link Serialize serializable} streams on {@code annotatedClass} instances. * <p>/*from www .j a v a2 s .co m*/ * Use of {@code PropertyDescriptor} is simply a convenience in lieu of the use of underlying Java reflection. * </p> * <p> * This method looks for fields annotated by the {@code Serialize} annotation on the {@code annotatedClass}. * A {@code PropertyDescriptor} is created for each field, and is keyed by the {@code StreamId} in the returned * {@code Map}. * </p> * * @param annotatedClass the class to scan for the presense of {@code @Serialize} * @return a Map of PropertyDescriptors keyed by their StreamId. */ public static Map<StreamId, PropertyDescriptor> getStreamDescriptors(Class annotatedClass) { HashMap<StreamId, PropertyDescriptor> results = new HashMap<>(); Arrays.stream(annotatedClass.getDeclaredFields()) .filter(candidateField -> AnnotationUtils.getAnnotation(candidateField, Serialize.class) != null) .forEach(annotatedField -> { AnnotationAttributes attributes = AnnotationUtils.getAnnotationAttributes(annotatedField, AnnotationUtils.getAnnotation(annotatedField, Serialize.class)); StreamId streamId = (StreamId) attributes.get("streamId"); PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(PackageState.class, annotatedField.getName()); results.put(streamId, descriptor); }); return results; }
From source file:org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.java
public void testFindHandler_WithASupportedHandler() throws Exception { PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor( org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.TestBean.class, "name"); Element element = createElement("bla"); handler1Control.expectAndReturn(handler1.supports(element, org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.TestBean.class, descriptor), false);// w w w .ja v a 2s.co m handler2Control.expectAndReturn(handler2.supports(element, org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.TestBean.class, descriptor), true); replay(); assertSame(handler2, registry.findPropertyHandler(element, org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.TestBean.class, descriptor)); verify(); }
From source file:org.jdal.vaadin.ui.form.AnnotationFieldFactory.java
/** * {@inheritDoc}/*ww w . j a v a 2 s.c o m*/ */ public Field createField(Item item, Object propertyId, Component uiContext) { if (item instanceof BeanItem<?>) { BeanItem<?> bi = (BeanItem<?>) item; String name = (String) propertyId; Class<?> clazz = bi.getBean().getClass(); java.lang.reflect.Field field = ReflectionUtils.findField(clazz, name); Annotation[] fa = new Annotation[] {}; if (field != null) { fa = field.getAnnotations(); } java.lang.reflect.Method method = BeanUtils.getPropertyDescriptor(clazz, name).getReadMethod(); Annotation[] ma = method.getAnnotations(); Annotation[] annotations = (Annotation[]) ArrayUtils.addAll(fa, ma); Field f = null; for (Annotation a : annotations) { f = findField(a, clazz, name); if (f != null) { f.setCaption(createCaptionByPropertyId(propertyId)); applyFieldProcessors(f, propertyId); return f; } } } // fall back to default return super.createField(item, propertyId, uiContext); }