List of usage examples for org.apache.commons.beanutils PropertyUtilsBean getPropertyDescriptors
public 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.
FIXME - Does not work with DynaBeans.
From source file:name.ikysil.beanpathdsl.sandbox.describe.Main.java
/** * @param args the command line arguments *//*w ww . jav a 2 s.co m*/ public static void main(String[] args) { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); Reflections reflections = new Reflections(TestBean.class, new SubTypesScanner(false)); Set<Class<?>> classes = reflections.getSubTypesOf(Object.class); for (Class<?> clazz : classes) { System.out.println(String.format("Class name: %s", clazz.getName())); System.out.println(String.format("Class simple name: %s", clazz.getSimpleName())); System.out.println(String.format("Class canonical name: %s", clazz.getCanonicalName())); PropertyDescriptor[] pds = propertyUtilsBean.getPropertyDescriptors(clazz); for (PropertyDescriptor pd : pds) { System.out.println(String.format(" Property name: %s", pd.getName())); Class<?> pc = pd.getPropertyType(); System.out.println(String.format(" Class name: %s", pc.getName())); System.out.println(String.format(" Class simple name: %s", pc.getSimpleName())); System.out.println(String.format(" Class canonical name: %s", pc.getCanonicalName())); } } }
From source file:com.eryansky.core.excelTools.ExcelUtils.java
/** * JavaBeanMap// w w w. j av a 2 s. c o m * @param obj * @return */ public static Map<String, Object> beanToMap(Object obj) { Map<String, Object> params = new HashMap<String, Object>(0); try { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!StringUtils.equals(name, "class")) { params.put(name, propertyUtilsBean.getNestedProperty(obj, name)); } } } catch (Exception e) { e.printStackTrace(); } return params; }
From source file:com.clican.pluto.common.util.BeanUtils.java
/** * ??Mapmapkey??,value//from w w w .j av a 2 s . c o m * <p> * bean id name 100 zhangsan ? id=100,name="zhangsan"Map * </p> * * @param <Bean> * @param bean * @return */ public static <Bean> Map<String, Object> convertBeanToMap(Bean bean) { if (bean == null) return null; PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] psArray = propertyUtilsBean.getPropertyDescriptors(bean.getClass()); List<String> properties = new ArrayList<String>(); for (PropertyDescriptor ps : psArray) { properties.add(ps.getName()); } Map<String, Object> map = new HashMap<String, Object>(); for (String property : properties) { // ?class?map if ("class".equals(property)) { continue; } String methodName = StringUtils.getGetMethodName(property); try { Method method = bean.getClass().getMethod(methodName, new Class<?>[] {}); Object propertyValue = method.invoke(bean, new Object[] {}); if (propertyValue != null) { map.put(property, propertyValue); } } catch (Exception e) { log.error("", e); } } return map; }
From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java
/** * Initializes a TypeDefinition from a given class. The first entry in the return list is the TypeDefinition for the * parameter class; any entries after that (if any) are TypeDefinitions for any other types that were required as * fields for that root TypeDefinition./*from w ww . j a va 2s .c o m*/ * * @param klass The Class object to describe. * @param typeState The TypeState for the current operation. * @param strict True indicates that processing should stop on ambiguous entries; false indicates that null should * be entered. * @return A list of TypeDefinitions; the first entry is the root (corresponding with the klass parameter), any * other entries in the list were required to describe the root TypeDefinition's fields. The return may also * be null, if sufficient information was not provided to determine the type. */ public static TypeDefinition getTypeDefinition(Type type, TypeState typeState, boolean strict) { Class<?> klass; // we already know about this type; we're done if (typeState.isTypeKnown(ReflectTypeUtils.getTypeName(type))) { return typeState.getType(ReflectTypeUtils.getTypeName(type)); } // if the type is Object, return null, we can't figure out anything more if (type instanceof Class && Object.class.equals(type)) { return null; } // if we don't have enough information, return null if (!strict) { if (type instanceof Class && Map.class.isAssignableFrom((Class<?>) type) && !Properties.class.isAssignableFrom((Class<?>) type)) { if (!JSON.class.isAssignableFrom((Class<?>) type)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(type)); } return null; } else if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) { if (!JSON.class.isAssignableFrom((Class<?>) type)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(type)); } return null; } } TypeDefinition ret; if (type instanceof Class && Properties.class.isAssignableFrom((Class<?>) type)) { MapReflectTypeDefinition mtdret = new MapReflectTypeDefinition(); mtdret.setTypeName(ReflectTypeUtils.getTypeName(type)); mtdret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(mtdret); klass = (Class<?>) type; mtdret.setKlass(klass); TypeDefinition stringType = getTypeDefinition(String.class, typeState, false); mtdret.setKeyFieldDefinition(new GenericFieldDefinition(stringType)); mtdret.setValueFieldDefinition(new GenericFieldDefinition(stringType)); ret = mtdret; } else if (type instanceof Class && JSONUtils.isPrimitive((Class<?>) type)) { PrimitiveReflectTypeDefinition ptret; if (((Class<?>) type).isEnum()) { ptret = new EnumPrimitiveReflectTypeDefinition(); } else { ptret = new PrimitiveReflectTypeDefinition(); } ptret.setTypeName(ReflectTypeUtils.getTypeName(type)); ptret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(ptret); klass = (Class<?>) type; ptret.setKlass(klass); ret = ptret; } else if (type instanceof Class) { klass = (Class<?>) type; if (Collection.class.isAssignableFrom(klass)) { throw new WMRuntimeException(MessageResource.JSON_TYPE_NOGENERICS, klass); } else if (klass.isArray()) { throw new WMRuntimeException(MessageResource.JSON_USE_FIELD_FOR_ARRAY, klass); } else if (Map.class.isAssignableFrom(klass)) { throw new WMRuntimeException(MessageResource.JSON_TYPE_NOGENERICS, klass); } else if (ClassUtils.isPrimitiveOrWrapper(klass) || CharSequence.class.isAssignableFrom(klass)) { PrimitiveReflectTypeDefinition ptret = new PrimitiveReflectTypeDefinition(); ptret.setTypeName(ReflectTypeUtils.getTypeName(type)); ptret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(ptret); ptret.setKlass(klass); ret = ptret; } else { ObjectReflectTypeDefinition otret = new ObjectReflectTypeDefinition(); otret.setTypeName(ReflectTypeUtils.getTypeName(type)); otret.setShortName(ReflectTypeUtils.getShortName(type)); otret.setKlass(klass); typeState.addType(otret); PropertyUtilsBean pub = ((ReflectTypeState) typeState).getPropertyUtilsBean(); PropertyDescriptor[] pds = pub.getPropertyDescriptors(klass); otret.setFields(new LinkedHashMap<String, FieldDefinition>(pds.length)); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) { continue; } Type paramType; if (pd.getReadMethod() != null) { paramType = pd.getReadMethod().getGenericReturnType(); } else if (pd.getWriteMethod() != null) { paramType = pd.getWriteMethod().getGenericParameterTypes()[0]; } else { logger.warn("No getter in type " + pd.getName()); continue; } otret.getFields().put(pd.getName(), getFieldDefinition(paramType, typeState, strict, pd.getName())); } ret = otret; } } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (pt.getRawType() instanceof Class && Map.class.isAssignableFrom((Class<?>) pt.getRawType())) { MapReflectTypeDefinition mtdret = new MapReflectTypeDefinition(); mtdret.setTypeName(ReflectTypeUtils.getTypeName(type)); mtdret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(mtdret); Type[] types = pt.getActualTypeArguments(); mtdret.setKeyFieldDefinition(getFieldDefinition(types[0], typeState, strict, null)); mtdret.setValueFieldDefinition(getFieldDefinition(types[1], typeState, strict, null)); mtdret.setKlass((Class<?>) pt.getRawType()); ret = mtdret; } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE, pt.getOwnerType(), pt); } } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type, type != null ? type.getClass() : null); } return ret; }
From source file:net.firejack.platform.core.store.registry.resource.ResourceVersionStore.java
private void copyResourceVersionProperties(RV dest, RV orig) { PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils(); PropertyDescriptor[] propertyDescriptors = propertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String name = propertyDescriptor.getName(); if (ArrayUtils.contains(new String[] { "class", "id", "version", "status", "updated", "created" }, name)) {/*from www. j a v a 2s.c o m*/ continue; } if (propertyUtils.isReadable(orig, name) && propertyUtils.isWriteable(dest, name)) { try { Object value = propertyUtils.getSimpleProperty(orig, name); if (value instanceof Timestamp) { value = ConvertUtils.convert(value, Date.class); } BeanUtils.copyProperty(dest, name, value); } catch (Exception e) { // Should not happen } } } }
From source file:com.khubla.cbean.CBeanType.java
/** * build the map of getters and setters to properties *///from w w w. j av a 2 s . com private void buildGetterSetterMap() { final PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); final PropertyDescriptor[] propertyDescriptors = propertyUtilsBean.getPropertyDescriptors(clazz); if (null != propertyDescriptors) { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (null != propertyDescriptor.getReadMethod()) { getterMap.put(propertyDescriptor.getReadMethod().getName(), propertyDescriptor.getName()); } if (null != propertyDescriptor.getWriteMethod()) { setterMap.put(propertyDescriptor.getWriteMethod().getName(), propertyDescriptor.getName()); } } } }
From source file:org.javaweb.utils.ClassUtils.java
/** * ?java?Map/* ww w . ja v a2 s . c om*/ * * @param obj * @return */ public static Map<String, Object> serializeClassToMap(Object obj) { Map<String, Object> params = new HashMap<String, Object>(0); try { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!"class".equals(name)) { params.put(name, propertyUtilsBean.getNestedProperty(obj, name)); } } } catch (Exception e) { e.printStackTrace(); } return params; }
From source file:org.javaweb.utils.FreemarkerUtils.java
/** * ?java?FreemarkerTemplateModelMap/*from w w w.j a va 2 s . co m*/ * * @param clazz * @return */ public static Map<String, TemplateModel> classToTemplateModelMap(Object clazz, Map<String, TemplateModel> paramsMap) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, TemplateModelException { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(clazz); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!"class".equals(name)) { Object obj = propertyUtilsBean.getNestedProperty(clazz, name); TemplateModel model = DirectiveUtils.getDefaultObjectWrapper().wrap(obj); paramsMap.put(name, model); } } return paramsMap; }