List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:generators.ExpressionMetaValidator.java
public static void main(String[] args) { ClassicEngineBoot.getInstance().start(); int invalidExpressionsCounter = 0; int deprecatedExpressionsCounter = 0; final HashNMap expressionsByGroup = new HashNMap(); ExpressionRegistry expressionRegistry = ExpressionRegistry.getInstance(); final ExpressionMetaData[] allExpressions = expressionRegistry.getAllExpressionMetaDatas(); for (int i = 0; i < allExpressions.length; i++) { final ExpressionMetaData expression = allExpressions[i]; if (expression == null) { logger.warn("Null Expression encountered"); continue; }//from w w w .j a v a 2s. com missingProperties.clear(); final Class type = expression.getExpressionType(); if (type == null) { logger.warn("Expression class is null"); } logger.debug("Processing " + type); final Class resultType = expression.getResultType(); if (resultType == null) { logger.warn("Expression '" + expression.getExpressionType() + " is null"); } try { final BeanInfo beanInfo = expression.getBeanDescriptor(); if (beanInfo == null) { logger.warn( "Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor: Null"); } } catch (IntrospectionException e) { logger.warn("Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor", e); } final Locale locale = Locale.getDefault(); final String displayName = expression.getDisplayName(locale); if (isValid(displayName, expression.getName()) == false) { logger.warn("Expression '" + expression.getExpressionType() + ": No valid display name"); } if (expression.isDeprecated()) { deprecatedExpressionsCounter += 1; final String deprecateMessage = expression.getDeprecationMessage(locale); if (isValid(deprecateMessage, "Use a Formula instead") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": No valid deprecate message"); } } final String grouping = expression.getGrouping(locale); if (isValid(grouping, "Group") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": No valid grouping message"); } expressionsByGroup.add(grouping, expression); final ExpressionPropertyMetaData[] properties = expression.getPropertyDescriptions(); for (int j = 0; j < properties.length; j++) { final ExpressionPropertyMetaData propertyMetaData = properties[j]; final String name = propertyMetaData.getName(); if (StringUtils.isEmpty(name)) { logger.warn("Expression '" + expression.getExpressionType() + ": Property without a name"); } final String propertyDisplayName = propertyMetaData.getDisplayName(locale); if (isValid(propertyDisplayName, name) == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": No DisplayName"); } final String propertyGrouping = propertyMetaData.getGrouping(locale); if (isValid(propertyGrouping, "Group") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Grouping is not valid"); } final int groupingOrdinal = propertyMetaData.getGroupingOrdinal(locale); if (groupingOrdinal == Integer.MAX_VALUE) { if (propertyMetaData instanceof DefaultExpressionMetaData) { final DefaultExpressionPropertyMetaData demd = (DefaultExpressionPropertyMetaData) propertyMetaData; missingProperties.add(demd.getKeyPrefix() + "grouping.ordinal=1000"); } logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Grouping ordinal is not valid"); } final int ordinal = propertyMetaData.getItemOrdinal(locale); if (groupingOrdinal == Integer.MAX_VALUE) { if (propertyMetaData instanceof DefaultExpressionMetaData) { final DefaultExpressionPropertyMetaData demd = (DefaultExpressionPropertyMetaData) propertyMetaData; missingProperties.add(demd.getKeyPrefix() + "ordinal=1000"); } logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Ordinal is not valid"); } final String propertyDescription = propertyMetaData.getDescription(locale); if (isValid(propertyDescription, "") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Description is not valid"); } final String propertyDeprecated = propertyMetaData.getDeprecationMessage(locale); if (isValid(propertyDeprecated, "") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Deprecation is not valid"); } final String role = propertyMetaData.getPropertyRole(); if (isValid(role, "Value") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Role is not valid"); } final Class propertyType = propertyMetaData.getPropertyType(); if (propertyType == null) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Property Type is not valid"); } // should not crash! final PropertyDescriptor propertyDescriptor = propertyMetaData.getBeanDescriptor(); if (propertyMetaData.isDeprecated()) { final String deprecateMessage = propertyMetaData.getDeprecationMessage(locale); if (isValid(deprecateMessage, "Deprecated") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": No valid deprecate message"); } } } try { final BeanInfo beanInfo = Introspector.getBeanInfo(expression.getExpressionType()); final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (int propIdx = 0; propIdx < descriptors.length; propIdx++) { final PropertyDescriptor descriptor = descriptors[propIdx]; final String key = descriptor.getName(); if ("runtime".equals(key)) { continue; } if ("active".equals(key)) { continue; } if ("preserve".equals(key)) { continue; } if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null) { continue; } if (expression.getPropertyDescription(key) == null) { logger.warn("Expression '" + expression.getExpressionType() + ": No property definition for " + key); missingPropertyDefs.add(" <property name=\"" + key + "\" mandatory=\"false\" preferred=\"false\" value-role=\"Value\" expert=\"false\" hidden=\"false\"/>"); } } } catch (Throwable e) { logger.warn("Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor", e); } System.err.flush(); try { Thread.sleep(25); } catch (InterruptedException e) { } for (int x = 0; x < missingProperties.size(); x++) { final String property = (String) missingProperties.get(x); System.out.println(property); } for (int j = 0; j < missingPropertyDefs.size(); j++) { final String def = (String) missingPropertyDefs.get(j); System.out.println(def); } if (missingProperties.isEmpty() == false || missingPropertyDefs.isEmpty() == false) { invalidExpressionsCounter += 1; missingProperties.clear(); missingPropertyDefs.clear(); } System.out.flush(); try { Thread.sleep(25); } catch (InterruptedException e) { } } logger.info("Validated " + allExpressions.length + " expressions. Invalid: " + invalidExpressionsCounter + " Deprecated: " + deprecatedExpressionsCounter); final Object[] keys = expressionsByGroup.keySet().toArray(); Arrays.sort(keys); for (int i = 0; i < keys.length; i++) { final Object key = keys[i]; logger.info("Group: '" + key + "' Size: " + expressionsByGroup.getValueCount(key)); final Object[] objects = expressionsByGroup.toArray(key); for (int j = 0; j < objects.length; j++) { ExpressionMetaData metaData = (ExpressionMetaData) objects[j]; logger.info(" " + metaData.getExpressionType()); } } }
From source file:org.beangle.struts2.view.component.ComponentHelper.java
public static final void registe(Class<?> clazz) { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz); Map<String, Method> keys = CollectUtils.newHashMap(); for (PropertyDescriptor a : descriptors) { if (null != a.getWriteMethod()) { keys.put(a.getName(), a.getWriteMethod()); }//w w w . j av a 2 s . co m } writables.put(clazz, keys); }
From source file:com.eclecticlogic.pedal.dm.internal.MetamodelUtil.java
/** * @param attribute JPA metamodel attribute. * @param entity Entity to set the value on. * @param value Value to set.//from w w w .j av a 2s . c o m */ public static <E extends Serializable, T extends Serializable> void set(Attribute<? super E, T> attribute, E entity, T value) { Member member = attribute.getJavaMember(); if (member instanceof Field) { Field field = (Field) member; field.setAccessible(true); try { field.set(entity, value); } catch (Exception e) { throw new RuntimeException(e); } } else if (member instanceof Method) { PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member); if (pd.getWriteMethod() != null) { pd.getWriteMethod().setAccessible(true); try { pd.getWriteMethod().invoke(entity, value); } catch (Exception e) { throw new RuntimeException(e); } } else { throw new RuntimeException( "No setter for " + attribute.getName() + " in " + entity.getClass().getName()); } } else { throw new RuntimeException("Failed to set " + attribute.getName() + " of type " + member.getClass().getName() + " in entity " + entity.getClass().getName()); } }
From source file:es.pode.soporte.auditoria.registrar.BeanDescripcion.java
/** * Mtodo para recuperar informacin de un objeto a travs de reflexin * /*from w w w . ja v a2 s . com*/ * @param objeto Objeto al cual se le realiza la reflexin * @param atributo Valor que se recupera del objeto * @return valor Se devuelve el valor del atributo buscado */ public static String describe(Object objeto, String atributo) { if (objeto == null) return null; Object valor = null; Class clase = objeto.getClass(); if (clase.isArray() || java.util.Collection.class.isAssignableFrom(clase)) log.warn("El atributo es un array y debera ser un String"); else { log("Reflexin del objeto: " + objeto); BeanWrapper wrapper = new BeanWrapperImpl(objeto); PropertyDescriptor descriptors[] = wrapper.getPropertyDescriptors(); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor pd = descriptors[i]; if (pd.getReadMethod() != null && pd.getWriteMethod() != null) { String name = pd.getName(); /* Capturamos el valor del atributo que nos interesa */ if (name.equals(atributo)) { log("Nombre atributo: " + name); valor = wrapper.getPropertyValue(name); /* Si el valor es nulo registramos un "" */ if (valor == null) { log("El valor del atributo interceptado es nulo"); return null; } else return valor.toString(); } } } } return null; }
From source file:org.kuali.kfs.module.cam.util.ObjectValueUtils.java
/** * This method uses simple getter/setter methods to copy object values from a original object to destination object * // www. j a v a 2 s . c om * @param origin original object * @param destination destination object */ public static void copySimpleProperties(Object origin, Object destination) { try { Object[] empty = new Object[] {}; PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(origin.getClass()); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) { Object value = propertyDescriptor.getReadMethod().invoke(origin, empty); if (value != null) { propertyDescriptor.getWriteMethod().invoke(destination, value); } } } } catch (Exception e) { throw new RuntimeException("Unexpected error while copying properties.", e); } }
From source file:org.beangle.spring.bind.AutoConfigProcessor.java
public static boolean isExcludedFromDependencyCheck(PropertyDescriptor pd) { Method wm = pd.getWriteMethod(); if (wm == null) { return false; }//from www . jav a2 s . c o m if (!wm.getDeclaringClass().getName().contains("$$")) { // Not a CGLIB method so it's OK. return false; } // It was declared by CGLIB, but we might still want to autowire it // if it was actually declared by the superclass. Class<?> superclass = wm.getDeclaringClass().getSuperclass(); return !ClassUtils.hasMethod(superclass, wm.getName(), wm.getParameterTypes()); }
From source file:com.baomidou.framework.common.util.BeanUtil.java
/** * ?/* w w w. j av a 2 s . co m*/ * * @param source * ? * @param target * ? */ public static void copy(Object source, Object target) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } /* * no copy null properties */ Object value = readMethod.invoke(source); if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
From source file:Main.java
private static void setterValue(PropertyDescriptor property, Object mapValue, Object object) throws InvocationTargetException, IllegalAccessException, ParseException { Method setter = property.getWriteMethod(); if (mapValue == null) { setter.invoke(object, mapValue); return;//from w w w. ja v a 2 s. c om } Class propertyType = property.getPropertyType(); String type = propertyType.getName(); String value = mapValue.toString(); if (type.equals("java.lang.String")) { setter.invoke(object, value); } else if (type.equals("java.lang.Integer")) { setter.invoke(object, Integer.parseInt(value)); } else if (type.equals("java.lang.Long")) { setter.invoke(object, Long.parseLong(value)); } else if (type.equals("java.math.BigDecimal")) { setter.invoke(object, BigDecimal.valueOf(Double.parseDouble(value))); } else if (type.equals("java.math.BigInteger")) { setter.invoke(object, BigInteger.valueOf(Long.parseLong(value))); } else if (type.equals("java.util.Date")) { setter.invoke(object, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value)); } else if (type.equals("java.lang.Boolean")) { setter.invoke(object, Boolean.valueOf(value)); } else if (type.equals("java.lang.Float")) { setter.invoke(object, Float.parseFloat(value)); } else if (type.equals("java.lang.Double")) { setter.invoke(object, Double.parseDouble(value)); } else if (type.equals("java.lang.byte[]")) { setter.invoke(object, value.getBytes()); } else { setter.invoke(object, value); } }
From source file:com.aw.support.beans.BeanUtils.java
public static int countPropertyFilled(Object bean) { BeanWrapper wrap = new BeanWrapperImpl(bean); int count = 0; for (PropertyDescriptor descriptor : wrap.getPropertyDescriptors()) { if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null) continue; Object value = wrap.getPropertyValue(descriptor.getName()); if (value instanceof String) { if (StringUtils.hasText((String) value)) count++;/*w ww . j a v a2s .co m*/ } else if (value != null) count++; } return count; }
From source file:org.bibsonomy.util.ObjectUtils.java
/** * copies all property values (all properties that have a getter and setter) from the source to the target * // w w w.j ava 2 s . com * @param <T> * @param source the source object * @param target the target object */ public static <T> void copyPropertyValues(final T source, final T target) { try { final BeanInfo biSource = Introspector.getBeanInfo(source.getClass()); final BeanInfo biTarget = Introspector.getBeanInfo(target.getClass()); /* * source can be a subtype of target * to ensure that a setter of the subtype isn't called choose the array * that contains less properties */ final PropertyDescriptor[] sourceProperties = biSource.getPropertyDescriptors(); final PropertyDescriptor[] targetProperties = biTarget.getPropertyDescriptors(); final PropertyDescriptor[] copyProperties = sourceProperties.length > targetProperties.length ? targetProperties : sourceProperties; /* * loop through all properties */ for (final PropertyDescriptor d : copyProperties) { final Method getter = d.getReadMethod(); final Method setter = d.getWriteMethod(); if (present(getter) && present(setter)) { // get the value from the source final Object value = getter.invoke(source, (Object[]) null); // and set in in the target setter.invoke(target, value); } } } catch (Exception ex) { log.error("error while copying property values from an object " + source.getClass() + " to an object " + target.getClass(), ex); } }