List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:com.sparkplatform.api.core.PropertyAsserter.java
/** * See {@link #assertBasicGetterSetterBehavior(Object,String)} method. Only difference is that here we accept an * explicit argument for the setter method. * * @param target the object on which to invoke the getter and setter * @param property the property name, e.g. "firstName" * @param argument the property value, i.e. the value the setter will be invoked with *//*from w w w .j a v a2 s . c o m*/ public static void assertBasicGetterSetterBehavior(Object target, String property, Object argument) { try { PropertyDescriptor descriptor = new PropertyDescriptor(property, target.getClass()); Object arg = argument; Class type = descriptor.getPropertyType(); if (arg == null) { if (type.isArray()) { arg = Array.newInstance(type.getComponentType(), new int[] { TEST_ARRAY_SIZE }); } else if (type.isEnum()) { arg = type.getEnumConstants()[0]; } else if (TYPE_ARGUMENTS.containsKey(type)) { arg = TYPE_ARGUMENTS.get(type); } else { arg = invokeDefaultConstructorEvenIfPrivate(type); } } Method writeMethod = descriptor.getWriteMethod(); Method readMethod = descriptor.getReadMethod(); writeMethod.invoke(target, arg); Object propertyValue = readMethod.invoke(target); if (type.isPrimitive()) { assertEquals(property + " getter/setter failed test", arg, propertyValue); } else { assertSame(property + " getter/setter failed test", arg, propertyValue); } } catch (IntrospectionException e) { String msg = "Error creating PropertyDescriptor for property [" + property + "]. Do you have a getter and a setter?"; log.error(msg, e); fail(msg); } catch (IllegalAccessException e) { String msg = "Error accessing property. Are the getter and setter both accessible?"; log.error(msg, e); fail(msg); } catch (InvocationTargetException e) { String msg = "Error invoking method on target"; log.error(msg, e); fail(msg); } }
From source file:org.paxml.util.ReflectUtils.java
/** * Get a specific type of property descriptors, except the "class" property. * //from w w w .ja va 2 s . c o m * @param clazz * the class * @param type * the type filter, null means no filtering * @return the list of property descriptors. */ public static List<PropertyDescriptor> getPropertyDescriptors(Class clazz, PropertyDescriptorType type) { List<PropertyDescriptor> list = new ArrayList<PropertyDescriptor>(); for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(clazz)) { if ("class".equals(pd.getName())) { continue; } switch (type) { case GETTER: if (pd.getReadMethod() != null) { list.add(pd); } break; case SETTER: if (pd.getWriteMethod() != null) { list.add(pd); } break; default: list.add(pd); } } 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; }/*from w w w. j a v a 2 s.co m*/ 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:org.dozer.util.ReflectionUtils.java
static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceClass) { List<PropertyDescriptor> propDescriptors = new ArrayList<PropertyDescriptor>(); // Add prop descriptors for interface passed in propDescriptors.addAll(Arrays.asList(PropertyUtils.getPropertyDescriptors(interfaceClass))); // Look for interface inheritance. If super interfaces are found, recurse up the hierarchy tree and add prop // descriptors for each interface found. // PropertyUtils.getPropertyDescriptors() does not correctly walk the inheritance hierarchy for interfaces. Class<?>[] interfaces = interfaceClass.getInterfaces(); if (interfaces != null) { for (Class<?> superInterfaceClass : interfaces) { List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays .asList(getInterfacePropertyDescriptors(superInterfaceClass)); /*//from w w w. j a va 2 s .co m * #1814758 * Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added * to the result list. This caused issues when getter and setter of an attribute on different interfaces in * an inheritance hierarchy */ for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) { PropertyDescriptor existingPropDescriptor = findPropDescriptorByName(propDescriptors, superPropDescriptor.getName()); if (existingPropDescriptor == null) { propDescriptors.add(superPropDescriptor); } else { try { if (existingPropDescriptor.getReadMethod() == null) { existingPropDescriptor.setReadMethod(superPropDescriptor.getReadMethod()); } if (existingPropDescriptor.getWriteMethod() == null) { existingPropDescriptor.setWriteMethod(superPropDescriptor.getWriteMethod()); } } catch (IntrospectionException e) { throw new MappingException(e); } } } } } return propDescriptors.toArray(new PropertyDescriptor[propDescriptors.size()]); }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceClass) { List<PropertyDescriptor> propDescriptors = new ArrayList<>(); // Add prop descriptors for interface passed in propDescriptors.addAll(Arrays.asList(PropertyUtils.getPropertyDescriptors(interfaceClass))); // Look for interface inheritance. If super interfaces are found, recurse up the hierarchy tree and add prop // descriptors for each interface found. // PropertyUtils.getPropertyDescriptors() does not correctly walk the inheritance hierarchy for interfaces. Class<?>[] interfaces = interfaceClass.getInterfaces(); if (interfaces != null) { for (Class<?> superInterfaceClass : interfaces) { List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays .asList(getInterfacePropertyDescriptors(superInterfaceClass)); /*/*from w w w .j a v a2s . com*/ * #1814758 * Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added * to the result list. This caused issues when getter and setter of an attribute on different interfaces in * an inheritance hierarchy */ for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) { PropertyDescriptor existingPropDescriptor = findPropDescriptorByName(propDescriptors, superPropDescriptor.getName()); if (existingPropDescriptor == null) { propDescriptors.add(superPropDescriptor); } else { try { if (existingPropDescriptor.getReadMethod() == null) { existingPropDescriptor.setReadMethod(superPropDescriptor.getReadMethod()); } if (existingPropDescriptor.getWriteMethod() == null) { existingPropDescriptor.setWriteMethod(superPropDescriptor.getWriteMethod()); } } catch (IntrospectionException e) { throw new MappingException(e); } } } } } return propDescriptors.toArray(new PropertyDescriptor[propDescriptors.size()]); }
From source file:com.krm.dbaudit.common.utils.Collections3.java
/** * Map JavaBean //from w ww. j a va 2 s.c o m * @param type ? * @param map ? map * @return ? JavaBean * @throws IntrospectionException ? * @throws IllegalAccessException JavaBean * @throws InstantiationException JavaBean * @throws InvocationTargetException setter */ public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); // ? Object obj = type.newInstance(); // JavaBean // JavaBean PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // ??? try ??? Object value = map.get(propertyName); if (propertyName.equals("id") || propertyName.equals("parentId")) { value = Long.valueOf(value.toString()); } if (value instanceof Number) { value = Long.valueOf(value.toString()); } else if (value instanceof Date) { value = (Date) value; } Object[] args = new Object[1]; args[0] = value; descriptor.getWriteMethod().invoke(obj, args); } } return obj; }
From source file:com.subakva.formicid.options.ParameterHandler.java
protected HashMap<String, PropertyDescriptor> getWritableProperties(Class taskClass) { HashMap<String, PropertyDescriptor> writableProperties = new HashMap<String, PropertyDescriptor>(); PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(taskClass); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getWriteMethod() != null && !excluded.contains(descriptor.getName())) { writableProperties.put(descriptor.getName().toLowerCase(), descriptor); }/*from w w w . j av a 2s . co m*/ } return writableProperties; }
From source file:com.hengyi.japp.sap.convert.impl.FieldCopyBoolean.java
@Override protected void initBeanPropertyWriteMethod(Object bean, JCoRecord record) throws Exception { PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(bean, beanPropertyName); Method m = descriptor.getWriteMethod(); if (m == null) { beanPropertyWriteMethod = null;//from ww w.j av a 2 s . com } else { beanPropertyWriteMethod = MethodUtils.getMatchingAccessibleMethod(bean.getClass(), m.getName(), BOOLEAN_CLASS); } }
From source file:stroom.util.AbstractCommandLineTool.java
public void init(final String[] args) throws Exception { map = new HeaderMap(); validArguments = new ArrayList<String>(); map.loadArgs(args);// w w w .j a v a 2 s . c om final BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass()); for (final PropertyDescriptor field : beanInfo.getPropertyDescriptors()) { if (field.getWriteMethod() != null) { if (field.getName().length() > maxPropLength) { maxPropLength = field.getName().length(); } if (map.containsKey(field.getName())) { validArguments.add(field.getName()); field.getWriteMethod().invoke(this, getAsType(field)); } } } checkArgs(); }
From source file:org.eclipse.wb.internal.core.xml.model.description.rules.CreatePropertiesPropertyDescriptorRule.java
private void addSingleProperty(ComponentDescription componentDescription, PropertyDescriptor propertyDescriptor) throws Exception { Method setMethod = propertyDescriptor.getWriteMethod(); if (setMethod == null) { return;/*w ww. ja v a 2 s . c o m*/ } if (!ReflectionUtils.isPublic(setMethod)) { return; } // prepare description parts String title = propertyDescriptor.getName(); String attribute = StringUtils.substringBeforeLast(StringUtils.uncapitalize(title), "("); Method getMethod = propertyDescriptor.getReadMethod(); Class<?> propertyType = resolvePropertyType(componentDescription, setMethod); // prepare property parts String id = setMethod.getName() + "(" + ReflectionUtils.getFullyQualifiedName(propertyType, false) + ")"; ExpressionAccessor accessor = new MethodExpressionAccessor(attribute, setMethod, getMethod); ExpressionConverter converter = DescriptionPropertiesHelper.getConverterForType(propertyType); PropertyEditor editor = DescriptionPropertiesHelper.getEditorForType(propertyType); // create property GenericPropertyDescription property = new GenericPropertyDescription(id, title, propertyType, accessor); property.setConverter(converter); property.setEditor(editor); // add property componentDescription.addProperty(property); }