public void copy(final Object target, final Object source) { try { final List<PropertyDescriptor> propertyDescriptors = BeanIntrospector.getPropertyDescriptorsWithGettersAndSetters(beanInterfaceClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final String propertyName = propertyDescriptor.getName(); if (baseInterfaceMap.containsKey(propertyName)) { final Method getter = propertyDescriptor.getReadMethod(); // only copies properties defined as copyFrom-able Object value = getter.invoke(source, NO_PARAMS); if (value != null) { final Method setter = propertyDescriptor.getWriteMethod(); final Class<?> baseInterface = baseInterfaceMap.get(propertyName); value = doCopy(value, baseInterface); setter.invoke(target, new Object[] { value }); } } } } catch (final Exception e) { LOG.error("Error while copying object", e); throw new RuntimeException("Could not do a copyFrom " + e, e); } }
@Override public void generate(final Module module, final Element element) { final GoogleBaseImpl mod = (GoogleBaseImpl) module; final HashMap<Object, Object> props2tags = new HashMap<Object, Object>(GoogleBaseParser.PROPS2TAGS); final List<PropertyDescriptor> pds = GoogleBaseParser.pds; for (final PropertyDescriptor pd : pds) { final String tagName = (String) props2tags.get(pd.getName()); if (tagName == null) { continue; } Object[] values = null; try { if (pd.getPropertyType().isArray()) { values = (Object[]) pd.getReadMethod().invoke(mod, (Object[]) null); } else { values = new Object[] {pd.getReadMethod().invoke(mod, (Object[]) null)}; } for (int j = 0; values != null && j < values.length; j++) { if (values[j] != null) { element.addContent(generateTag(values[j], tagName)); } } } catch (final Exception e) { LOG.error("Error", e); } } }
private static List<PropertyDescriptor> merge(final Map<String, PropertyDescriptor> getters, final Map<String, PropertyDescriptor> setters) { final List<PropertyDescriptor> props = new ArrayList<PropertyDescriptor>(); final Set<String> processedProps = new HashSet<String>(); for (final String propertyName : getters.keySet()) { final PropertyDescriptor getter = getters.get(propertyName); final PropertyDescriptor setter = setters.get(propertyName); if (setter != null) { processedProps.add(propertyName); final PropertyDescriptor prop = new PropertyDescriptor(propertyName, getter.getReadMethod(), setter.getWriteMethod()); props.add(prop); } else { props.add(getter); } } final Set<String> writeOnlyProperties = new HashSet<String>(); writeOnlyProperties.removeAll(processedProps); for (final String propertyName : writeOnlyProperties) { final PropertyDescriptor setter = setters.get(propertyName); props.add(setter); } return props; }
public PropertyDescriptor(String name, Method getter, Method setter) { if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Bad property name"); } this.name = name; this.getter = checkGetter(getter); this.setter = checkSetter(setter); }
if (pd.getPropertyType() == Integer.class || pd.getPropertyType().getComponentType() == Integer.class) { tagValue = new Integer(GoogleBaseParser.stripNonValidCharacters(GoogleBaseParser.INTEGER_CHARS, tag.getText())); } else if (pd.getPropertyType() == Float.class || pd.getPropertyType().getComponentType() == Float.class) { tagValue = new Float(GoogleBaseParser.stripNonValidCharacters(GoogleBaseParser.FLOAT_CHARS, tag.getText())); } else if (pd.getPropertyType() == String.class || pd.getPropertyType().getComponentType() == String.class) { tagValue = tag.getText(); } else if (pd.getPropertyType() == URL.class || pd.getPropertyType().getComponentType() == URL.class) { tagValue = new URL(tag.getText().trim()); } else if (pd.getPropertyType() == Boolean.class || pd.getPropertyType().getComponentType() == Boolean.class) { tagValue = new Boolean(tag.getText().trim()); } else if (pd.getPropertyType() == Date.class || pd.getPropertyType().getComponentType() == Date.class) { final String text = tag.getText().trim(); } else if (pd.getPropertyType() == IntUnit.class || pd.getPropertyType().getComponentType() == IntUnit.class) { tagValue = new IntUnit(tag.getText()); } else if (pd.getPropertyType() == FloatUnit.class || pd.getPropertyType().getComponentType() == FloatUnit.class) { tagValue = new FloatUnit(tag.getText()); } else if (pd.getPropertyType() == DateTimeRange.class || pd.getPropertyType().getComponentType() == DateTimeRange.class) { tagValue = new DateTimeRange(LONG_DT_FMT.parse(tag.getChild("start", GoogleBaseParser.NS).getText().trim()), LONG_DT_FMT.parse(tag .getChild("end", GoogleBaseParser.NS).getText().trim())); } else if (pd.getPropertyType() == ShippingType.class || pd.getPropertyType().getComponentType() == ShippingType.class) { final FloatUnit price = new FloatUnit(tag.getChild("price", GoogleBaseParser.NS).getText().trim()); ShippingType.ServiceEnumeration service = } else if (pd.getPropertyType() == PaymentTypeEnumeration.class || pd.getPropertyType().getComponentType() == PaymentTypeEnumeration.class) { tagValue = PaymentTypeEnumeration.findByValue(tag.getText().trim()); } else if (pd.getPropertyType() == PriceTypeEnumeration.class || pd.getPropertyType().getComponentType() == PriceTypeEnumeration.class) { tagValue = PriceTypeEnumeration.findByValue(tag.getText().trim());
/** * Returns the String representation of the bean given in the constructor. * <p> * * @param prefix to use for bean properties. * @return bean object String representation. * */ private static String toString(final Class<?> beanClass, final Object obj, final String prefix) { final StringBuffer sb = new StringBuffer(128); try { final List<PropertyDescriptor> propertyDescriptors = BeanIntrospector.getPropertyDescriptorsWithGetters(beanClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final String propertyName = propertyDescriptor.getName(); final Method getter = propertyDescriptor.getReadMethod(); final Object value = getter.invoke(obj, NO_PARAMS); printProperty(sb, prefix + "." + propertyName, value); } } catch (final Exception e) { LOG.error("Error while generating toString", e); final Class<?> clazz = obj.getClass(); final String errorMessage = e.getMessage(); sb.append(String.format("\n\nEXCEPTION: Could not complete %s.toString(): %s\n", clazz, errorMessage)); } return sb.toString(); }
private Method checkSetter(Method method) { if (method != null) { int modifiers = method.getModifiers(); if (!Modifier.isPublic(modifiers)) { throw new IllegalArgumentException("Modifier for setter method should be public"); } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { throw new IllegalArgumentException("Number of parameters in setter method is not equal to 1"); } Class<?> parameterType = parameterTypes[0]; Class<?> propertyType = getPropertyType(); if (propertyType != null && !propertyType.equals(parameterType)) { throw new IllegalArgumentException("Parameter type in setter does not correspond to getter"); } } return method; } }
/** * Extract all {@link PropertyDescriptor}s for properties with a getter that does not come from * {@link Object} and does not accept parameters. * * @param clazz The class to extract the desired {@link PropertyDescriptor}s from * @return All {@link PropertyDescriptor}s for properties with a getter that does not come from * {@link Object} and does not accept parameters. */ public static List<PropertyDescriptor> getPropertyDescriptorsWithGetters(final Class<?> clazz) { final List<PropertyDescriptor> relevantDescriptors = new ArrayList<PropertyDescriptor>(); final PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz); if (propertyDescriptors != null) { for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final Method getter = propertyDescriptor.getReadMethod(); final boolean getterExists = getter != null; if (getterExists) { final boolean getterFromObject = getter.getDeclaringClass() == Object.class; final boolean getterWithoutParams = getter.getParameterTypes().length == 0; if (!getterFromObject && getterWithoutParams) { relevantDescriptors.add(propertyDescriptor); } } } } return relevantDescriptors; }
/** * Extract all {@link PropertyDescriptor}s for properties with a getter (that does not come from * {@link Object} and does not accept parameters) and a setter. * * @param clazz The class to extract the desired {@link PropertyDescriptor}s from * @return All {@link PropertyDescriptor}s for properties with a getter (that does not come from * {@link Object} and does not accept parameters) and a setter. */ public static List<PropertyDescriptor> getPropertyDescriptorsWithGettersAndSetters(final Class<?> clazz) { final List<PropertyDescriptor> relevantDescriptors = new ArrayList<PropertyDescriptor>(); final List<PropertyDescriptor> propertyDescriptors = getPropertyDescriptorsWithGetters(clazz); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final Method setter = propertyDescriptor.getWriteMethod(); final boolean setterExists = setter != null; if (setterExists) { relevantDescriptors.add(propertyDescriptor); } } return relevantDescriptors; }
if (methodName.startsWith(SETTER) && returnType == void.class && nrOfParameters == 1) { propertyName = decapitalize(methodName.substring(3)); propertyDescriptor = new PropertyDescriptor(propertyName, null, method); propertyDescriptor = new PropertyDescriptor(propertyName, method, null); } else if (methodName.startsWith(BOOLEAN_GETTER) && returnType == boolean.class && nrOfParameters == 0) { propertyName = decapitalize(methodName.substring(2)); propertyDescriptor = new PropertyDescriptor(propertyName, method, null);
if (pd.getPropertyType() == Integer.class || pd.getPropertyType().getComponentType() == Integer.class) { tagValue = new Integer(GoogleBaseParser.stripNonValidCharacters(GoogleBaseParser.INTEGER_CHARS, tag.getText())); } else if (pd.getPropertyType() == Float.class || pd.getPropertyType().getComponentType() == Float.class) { tagValue = new Float(GoogleBaseParser.stripNonValidCharacters(GoogleBaseParser.FLOAT_CHARS, tag.getText())); } else if (pd.getPropertyType() == String.class || pd.getPropertyType().getComponentType() == String.class) { tagValue = tag.getText(); } else if (pd.getPropertyType() == URL.class || pd.getPropertyType().getComponentType() == URL.class) { tagValue = new URL(tag.getText().trim()); } else if (pd.getPropertyType() == Boolean.class || pd.getPropertyType().getComponentType() == Boolean.class) { tagValue = new Boolean(tag.getText().trim()); } else if (pd.getPropertyType() == Date.class || pd.getPropertyType().getComponentType() == Date.class) { final String text = tag.getText().trim(); } else if (pd.getPropertyType() == IntUnit.class || pd.getPropertyType().getComponentType() == IntUnit.class) { tagValue = new IntUnit(tag.getText()); } else if (pd.getPropertyType() == FloatUnit.class || pd.getPropertyType().getComponentType() == FloatUnit.class) { tagValue = new FloatUnit(tag.getText()); } else if (pd.getPropertyType() == DateTimeRange.class || pd.getPropertyType().getComponentType() == DateTimeRange.class) { tagValue = new DateTimeRange(LONG_DT_FMT.parse(tag.getChild("start", GoogleBaseParser.NS).getText().trim()), LONG_DT_FMT.parse(tag .getChild("end", GoogleBaseParser.NS).getText().trim())); } else if (pd.getPropertyType() == ShippingType.class || pd.getPropertyType().getComponentType() == ShippingType.class) { final FloatUnit price = new FloatUnit(tag.getChild("price", GoogleBaseParser.NS).getText().trim()); ShippingType.ServiceEnumeration service = } else if (pd.getPropertyType() == PaymentTypeEnumeration.class || pd.getPropertyType().getComponentType() == PaymentTypeEnumeration.class) { tagValue = PaymentTypeEnumeration.findByValue(tag.getText().trim()); } else if (pd.getPropertyType() == PriceTypeEnumeration.class || pd.getPropertyType().getComponentType() == PriceTypeEnumeration.class) { tagValue = PriceTypeEnumeration.findByValue(tag.getText().trim());
/** * Returns the String representation of the bean given in the constructor. * <p> * * @param prefix to use for bean properties. * @return bean object String representation. * */ private static String toString(final Class<?> beanClass, final Object obj, final String prefix) { final StringBuffer sb = new StringBuffer(128); try { final List<PropertyDescriptor> propertyDescriptors = BeanIntrospector.getPropertyDescriptorsWithGetters(beanClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final String propertyName = propertyDescriptor.getName(); final Method getter = propertyDescriptor.getReadMethod(); final Object value = getter.invoke(obj, NO_PARAMS); printProperty(sb, prefix + "." + propertyName, value); } } catch (final Exception e) { LOG.error("Error while generating toString", e); final Class<?> clazz = obj.getClass(); final String errorMessage = e.getMessage(); sb.append(String.format("\n\nEXCEPTION: Could not complete %s.toString(): %s\n", clazz, errorMessage)); } return sb.toString(); }
private Method checkSetter(Method method) { if (method != null) { int modifiers = method.getModifiers(); if (!Modifier.isPublic(modifiers)) { throw new IllegalArgumentException("Modifier for setter method should be public"); } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { throw new IllegalArgumentException("Number of parameters in setter method is not equal to 1"); } Class<?> parameterType = parameterTypes[0]; Class<?> propertyType = getPropertyType(); if (propertyType != null && !propertyType.equals(parameterType)) { throw new IllegalArgumentException("Parameter type in setter does not correspond to getter"); } } return method; } }
/** * Extract all {@link PropertyDescriptor}s for properties with a getter that does not come from * {@link Object} and does not accept parameters. * * @param clazz The class to extract the desired {@link PropertyDescriptor}s from * @return All {@link PropertyDescriptor}s for properties with a getter that does not come from * {@link Object} and does not accept parameters. */ public static List<PropertyDescriptor> getPropertyDescriptorsWithGetters(final Class<?> clazz) { final List<PropertyDescriptor> relevantDescriptors = new ArrayList<PropertyDescriptor>(); final PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz); if (propertyDescriptors != null) { for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final Method getter = propertyDescriptor.getReadMethod(); final boolean getterExists = getter != null; if (getterExists) { final boolean getterFromObject = getter.getDeclaringClass() == Object.class; final boolean getterWithoutParams = getter.getParameterTypes().length == 0; if (!getterFromObject && getterWithoutParams) { relevantDescriptors.add(propertyDescriptor); } } } } return relevantDescriptors; }
public PropertyDescriptor(String name, Method getter, Method setter) { if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Bad property name"); } this.name = name; this.getter = checkGetter(getter); this.setter = checkSetter(setter); }
/** * Extract all {@link PropertyDescriptor}s for properties with a getter (that does not come from * {@link Object} and does not accept parameters) and a setter. * * @param clazz The class to extract the desired {@link PropertyDescriptor}s from * @return All {@link PropertyDescriptor}s for properties with a getter (that does not come from * {@link Object} and does not accept parameters) and a setter. */ public static List<PropertyDescriptor> getPropertyDescriptorsWithGettersAndSetters(final Class<?> clazz) { final List<PropertyDescriptor> relevantDescriptors = new ArrayList<PropertyDescriptor>(); final List<PropertyDescriptor> propertyDescriptors = getPropertyDescriptorsWithGetters(clazz); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final Method setter = propertyDescriptor.getWriteMethod(); final boolean setterExists = setter != null; if (setterExists) { relevantDescriptors.add(propertyDescriptor); } } return relevantDescriptors; }
if (methodName.startsWith(SETTER) && returnType == void.class && nrOfParameters == 1) { propertyName = decapitalize(methodName.substring(3)); propertyDescriptor = new PropertyDescriptor(propertyName, null, method); propertyDescriptor = new PropertyDescriptor(propertyName, method, null); } else if (methodName.startsWith(BOOLEAN_GETTER) && returnType == boolean.class && nrOfParameters == 0) { propertyName = decapitalize(methodName.substring(2)); propertyDescriptor = new PropertyDescriptor(propertyName, method, null);
public void copy(final Object target, final Object source) { try { final List<PropertyDescriptor> propertyDescriptors = BeanIntrospector.getPropertyDescriptorsWithGettersAndSetters(beanInterfaceClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final String propertyName = propertyDescriptor.getName(); if (baseInterfaceMap.containsKey(propertyName)) { final Method getter = propertyDescriptor.getReadMethod(); // only copies properties defined as copyFrom-able Object value = getter.invoke(source, NO_PARAMS); if (value != null) { final Method setter = propertyDescriptor.getWriteMethod(); final Class<?> baseInterface = baseInterfaceMap.get(propertyName); value = doCopy(value, baseInterface); setter.invoke(target, new Object[] { value }); } } } } catch (final Exception e) { LOG.error("Error while copying object", e); throw new RuntimeException("Could not do a copyFrom " + e, e); } }