public void setProperties(Map<String, Object> properties) { Introspector introspector = new Introspector(target.getClass(), new PropertyComparator()); for (Map.Entry<String, Object> property : properties.entrySet()) { Method setter = introspector.getSetter(property.getKey()); if (setter != null) { invoke(setter, property.getValue()); } } }
public Method getSetter(String name) { for (PropertyDescriptor descriptor : getPropertyDescriptors(target)) { Method setter = descriptor.getWriteMethod(); if (setter == null) { continue; } if (hasAliasAnnotation(setter) && compareWithAlias(setter.getAnnotation(Alias.class), name)) { return setter; } if (compare(descriptor.getName(), name)) { return setter; } } return null; }
public Field getField(String name) { for (Field field : target.getFields()) { if (hasAliasAnnotation(field) && compareWithAlias(field.getAnnotation(Alias.class), name)) { return field; } if (compare(field.getName(), name)) { return field; } } return null; }
protected Method getSetter(Class< ? extends Object> type, String name) { return introspector(type).getSetter(toJavaIdentifierForm(name)); }
protected Field getField(Class< ? extends Object> type, String name) { return introspector(type).getField(toJavaIdentifierForm(name)); }
protected Method getGetter(Class< ? extends Object> type, String name) { return introspector(type).getGetter(toJavaIdentifierForm(name)); }
protected List<Method> getMethods(Class< ? extends Object> type, String name) { return introspector(type).getMethods(toJavaIdentifierForm(name)); }
public static Introspector ignoringCase(Class< ? > target) { return new Introspector(target, new IgnoringCaseComparator()); }
public List<Method> getMethods(String name) { List<Method> methods = new ArrayList<Method>(); for (Method method : target.getMethods()) { if (hasAliasAnnotation(method) && compareWithAlias(method.getAnnotation(Alias.class), name)) { methods.add(method); } if (compare(method.getName(), name)) { methods.add(method); } } return methods; }
@Test public void testShouldFindSettersOnTarget() throws Exception { Method setter = introspector.getSetter("field"); assertNotNull(setter); assertEquals("setField", setter.getName()); }
@Test public void testShouldFindFieldsOnTarget() throws Exception { Field field = introspector.getField("field"); assertNotNull(field); assertEquals("field", field.getName()); }
@Test public void testShouldFindGettersOnTarget() throws Exception { Method setter = introspector.getGetter("field"); assertNotNull(setter); assertEquals("getField", setter.getName()); }
@Test public void testShouldMethodsOnTarget() throws Exception { List<Method> methods = introspector.getMethods("method"); assertNotNull(methods); assertEquals(2, methods.size()); assertEquals("method", methods.get(0).getName()); assertEquals("method", methods.get(1).getName()); }
public static Introspector respectingCase(Class< ? > target) { return new Introspector(target, new NaturalComparator()); }
public Method getGetter(String name) { for (PropertyDescriptor descriptor : getPropertyDescriptors(target)) { Method getter = descriptor.getReadMethod(); if (getter == null) { continue; } if (hasAliasAnnotation(getter) && compareWithAlias(getter.getAnnotation(Alias.class), name)) { return getter; } if (compare(descriptor.getName(), name)) { return getter; } } return null; }