List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:org.openlmis.fulfillment.testutils.DtoGenerator.java
private static <T> void generate(Class<T> clazz) { Object instance;// ww w . jav a 2 s . c o m try { instance = clazz.newInstance(); } catch (Exception exp) { throw new IllegalStateException("Missing no args constructor", exp); } for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(clazz)) { if ("class".equals(descriptor.getName())) { continue; } if (null == descriptor.getReadMethod() || null == descriptor.getWriteMethod()) { // we support only full property (it has to have a getter and setter) continue; } try { Object value = generateValue(clazz, descriptor.getPropertyType()); PropertyUtils.setProperty(instance, descriptor.getName(), value); } catch (Exception exp) { throw new IllegalStateException("Can't set value for property: " + descriptor.getName(), exp); } } REFERENCES.put(clazz, instance); }
From source file:de.hasait.clap.impl.CLAPClassNode.java
private static <T extends Annotation> T getAnnotation(final PropertyDescriptor pPropertyDescriptor, final Class<T> pAnnotationClass) { final Method writeMethod = pPropertyDescriptor.getWriteMethod(); if (writeMethod != null) { final T annotation = writeMethod.getAnnotation(pAnnotationClass); if (annotation != null) { return annotation; }//from w ww. ja v a2 s . c o m } final Method readMethod = pPropertyDescriptor.getReadMethod(); if (readMethod != null) { final T annotation = readMethod.getAnnotation(pAnnotationClass); if (annotation != null) { return annotation; } } return null; }
From source file:org.soybeanMilk.core.bean.PropertyInfo.java
private static PropertyInfo getPropertyInfoAnatomized(Class<?> beanClass, Map<Class<?>, PropertyInfo> localExists, int depth) { PropertyInfo cached = propertyInfoCache.get(beanClass); if (cached != null) { if (log.isDebugEnabled()) log.debug(getSpace(depth) + "got " + SbmUtils.toString(beanClass) + " property information from cache"); return cached; }//from w w w. ja va2s. c om if (log.isDebugEnabled()) log.debug(getSpace(depth) + "start anatomizing " + SbmUtils.toString(beanClass) + " property information"); PropertyInfo beanInfo = new PropertyInfo(beanClass); localExists.put(beanInfo.getPropType(), beanInfo); PropertyDescriptor[] pds = null; try { pds = Introspector.getBeanInfo(beanInfo.getPropType()).getPropertyDescriptors(); } catch (IntrospectionException e) { throw new RuntimeException(e); } if (pds == null || pds.length == 0) ; else { for (PropertyDescriptor pd : pds) { String name = pd.getName(); Method wm = pd.getWriteMethod(); Method rm = pd.getReadMethod(); Class<?> propertyClazz = pd.getPropertyType(); //? if (wm == null || rm == null || !Modifier.isPublic(wm.getModifiers()) || !Modifier.isPublic(rm.getModifiers())) continue; //localExists?PropertyInfo PropertyInfo exist = localExists.get(propertyClazz); if (exist == null) exist = getPropertyInfoAnatomized(propertyClazz, localExists, depth + 1); //??? PropertyInfo copied = new PropertyInfo(beanClass, propertyClazz, name, rm, wm); copied.setSubPropertyInfos(exist.getSubPropertyInfos()); beanInfo.addSubPropertyInfo(copied); if (log.isDebugEnabled()) log.debug(getSpace(depth) + " add " + SbmUtils.toString(copied)); } } if (log.isDebugEnabled()) log.debug(getSpace(depth) + "finish anatomizing " + SbmUtils.toString(beanClass) + " property information"); propertyInfoCache.putIfAbsent(beanClass, beanInfo); return beanInfo; }
From source file:no.abmu.test.utilh3.DomainTestHelper.java
public static Object populateBean(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (propertyDescriptor.getName().equals("id")) { continue; } else if (propertyDescriptor.getName().equals("valueAsString")) { continue; } else if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje;/*w w w.ja v a 2s. c o m*/ // obje=ConvertUtils.convert(String.valueOf(System.currentTimeMillis() // +(long)(Math.random()*100d)),propertyDescriptor.getPropertyType()); obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; }
From source file:no.abmu.test.domainmodels.DomainTestHelper.java
public static Object populateBean(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (propertyDescriptor.getName().equals("id")) { continue; } else if (propertyDescriptor.getName().equals("valueAsString")) { continue; } else if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje;//from w ww . j av a2 s. c om // obje=ConvertUtils.convert(String.valueOf(System.currentTimeMillis() // +(long)(Math.random()*100d)),propertyDescriptor.getPropertyType()); obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; }
From source file:org.jaxygen.converters.properties.PropertiesToBeanConverter.java
/** * Fill the field in bean by the value pointed by the name. Name format name=<(KEY([N])?)+> where KEY bean property name, N index in table (if bean field is * List of java array).//from w w w . ja v a2s .c o m * * @param name * @param value * @param beanClass * @param baseBean * @param conversionReport * @return * @throws IntrospectionException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException * @throws WrongProperyIndex */ private static Object fillBeanValueByName(final String name, Object value, Class<?> beanClass, Object baseBean) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, WrongProperyIndex { // parse name x.y[i].z[n].v Object bean = baseBean; if (bean == null) { bean = beanClass.newInstance(); } Class<?> c = beanClass; BeanInfo beanInfo = Introspector.getBeanInfo(c, Object.class); final String childName = name.substring(name.indexOf(".") + 1); String path[] = name.split("\\."); final String fieldName = path[0]; // parse arrays [n] if (fieldName.endsWith("]")) { int bracketStart = fieldName.indexOf("["); int len = fieldName.length(); if (bracketStart > 0) { fillBeanArrayField(name, value, bean, beanInfo, path, fieldName, bracketStart, len); } else { throw new WrongProperyIndex(name); } } else { // parse non arrays for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(fieldName)) { Method writter = pd.getWriteMethod(); Method reader = pd.getReadMethod(); if (writter != null && reader != null) { Class<?> valueType = reader.getReturnType(); if (path.length == 1) { Object valueObject = parsePropertyToValue(value, valueType); writter.invoke(bean, valueObject); } else { Object childBean = reader.invoke(bean); Object valueObject = fillBeanValueByName(childName, value, valueType, childBean); writter.invoke(bean, valueObject); } } } } } // Object bean = c.newInstance(); return bean; }
From source file:org.jaxygen.converters.properties.PropertiesToBeanConverter.java
private static void fillBeanArrayField(final String name, Object value, Object bean, BeanInfo beanInfo, String[] path, final String fieldName, int bracketStart, int len) throws IllegalAccessException, InvocationTargetException, IntrospectionException, InstantiationException, IllegalArgumentException, WrongProperyIndex { final String indexStr = fieldName.substring(bracketStart + 1, len - 1); final String propertyName = fieldName.substring(0, bracketStart); int index = Integer.parseInt(indexStr); String childName = ""; int firstDot = name.indexOf("."); if (firstDot > 0) { childName = name.substring(firstDot + 1); }// ww w .java 2 s. c o m for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) { Method writter = pd.getWriteMethod(); Method reader = pd.getReadMethod(); if (writter != null && reader != null) { Object array = reader.invoke(bean); if (pd.getPropertyType().isArray()) { if (array == null) { array = Array.newInstance(pd.getPropertyType().getComponentType(), index + 1); writter.invoke(bean, array); } if (Array.getLength(array) < (index + 1)) { array = resizeArray(array, index + 1); writter.invoke(bean, array); } if (path.length == 1) { Object valueObject = parsePropertyToValue(value, array.getClass().getComponentType()); Array.set(array, index, valueObject); } else { Object valueObject = fillBeanValueByName(childName, value, array.getClass().getComponentType(), Array.get(array, index)); Array.set(array, index, valueObject); } } else if (pd.getPropertyType().equals(List.class)) { if (array == null) { array = pd.getPropertyType().newInstance(); writter.invoke(bean, array); } Class<?> genericClass = array.getClass().getTypeParameters()[0].getClass(); if (path.length == 1) { Object valueObject = parsePropertyToValue(value, genericClass); Array.set(array, index, valueObject); } else { Object valueObject = fillBeanValueByName(childName, value, genericClass, null); Array.set(array, index, valueObject); } } } } } }
From source file:org.jaffa.qm.util.PropertyFilter.java
private static void getFieldList(Class clazz, List<String> fieldList, String prefix, Deque<Class> classStack) throws IntrospectionException { //To avoid recursion, bail out if the input Class has already been introspected if (classStack.contains(clazz)) { if (log.isDebugEnabled()) log.debug("Fields from " + clazz + " prefixed by " + prefix + " will be ignored, since the class has already been introspected as per the stack " + classStack);/*from ww w . jav a 2s . c om*/ return; } else classStack.addFirst(clazz); //Introspect the input Class BeanInfo beanInfo = Introspector.getBeanInfo(clazz); if (beanInfo != null) { PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); if (pds != null) { for (PropertyDescriptor pd : pds) { if (pd.getReadMethod() != null && pd.getWriteMethod() != null) { String name = pd.getName(); String qualifieldName = prefix == null || prefix.length() == 0 ? name : prefix + '.' + name; Class type = pd.getPropertyType(); if (type.isArray()) type = type.getComponentType(); if (type == String.class || type == Boolean.class || Number.class.isAssignableFrom(type) || IDateBase.class.isAssignableFrom(type) || Currency.class.isAssignableFrom(type) || type.isPrimitive() || type.isEnum()) fieldList.add(qualifieldName); else getFieldList(type, fieldList, qualifieldName, classStack); } } } } classStack.removeFirst(); }
From source file:org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper.java
/** * Set the id of theObj to the uniqueId. Attempts to find a set method and * invoke it. If it fails it just swallows the exceptions and doesn't throw * them further.//from www. j a va 2 s . c o m * * @param theObj Object * @param uniqueId String */ public static void setUniqueId(Object theObj, String uniqueId) { Method annotatedMethod = ResourceInspector.findUniqueIdMethod(theObj.getClass()); if (annotatedMethod != null) { PropertyDescriptor pDesc = BeanUtils.findPropertyForMethod(annotatedMethod); if (pDesc != null) { Method writeMethod = pDesc.getWriteMethod(); if (writeMethod != null) { try { writeMethod.invoke(theObj, uniqueId); if (logger.isDebugEnabled()) { logger.debug("Unique id set for property: " + pDesc.getName()); } } catch (IllegalArgumentException error) { logger.warn("Invocation error", error); } catch (IllegalAccessException error) { logger.warn("IllegalAccessException", error); } catch (InvocationTargetException error) { logger.warn("InvocationTargetException", error); } } else { logger.warn("No setter method for property: " + pDesc.getName()); } } } }
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 av a 2 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); } }