public FieldMapperColumnDefinition<K> addGetter(Getter<?, ?> getter) { return add(new GetterProperty(getter)); } public FieldMapperColumnDefinition<K> addGetterFactory(GetterFactory<?, K> getterFactory) {
@Override public void accept(Predicate<? super K> predicate, GetterProperty getterProperty) { if (predicate instanceof CaseInsensitiveFieldKeyNamePredicate) { CaseInsensitiveFieldKeyNamePredicate p = (CaseInsensitiveFieldKeyNamePredicate) predicate; ExtendPropertyFinder.CustomProperty cp = new ExtendPropertyFinder.CustomProperty(getterProperty.getSourceType(), classMeta.getReflectionService(), p.getName(), getterProperty.getReturnType(), NullSetter.NULL_SETTER, getterProperty.getGetter()); if (propertyPredicate.test(cp)) { customProperties.add(cp); } } } });
public GetterProperty(Getter<?, ?> getter) { this(getter, getSourceType(getter), getReturnType(getter)); }
@Test public void testGetterProperty() { Getter<Object, Integer> getter = new Getter<Object, Integer>() { @Override public Integer get(Object target) throws Exception { return null; } @Override public String toString() { return "G"; } }; GetterProperty getterProperty = new GetterProperty(getter); assertSame(getter, getterProperty.getGetter()); assertEquals(Integer.class, getterProperty.getReturnType()); assertEquals("Getter{G}", getterProperty.toString()); }
public Type getCustomSourceReturnTypeFrom(Type ownerType) { GetterProperty getterProperty = getCustomGetterPropertyFrom(ownerType); return getterProperty != null ? getterProperty.getReturnType() : null; }
public Getter<?, ?> getCustomGetterFrom(Type fromType) { GetterProperty getterPropertyFrom = getCustomGetterPropertyFrom(fromType); return getterPropertyFrom != null ? getterPropertyFrom.getGetter() : null; }
public GetterProperty getCustomGetterPropertyFrom(Type fromType) { for(GetterProperty getterProperty : lookForAll(GetterProperty.class)) { if (getterProperty.getSourceType() == null || TypeHelper.isAssignable(getterProperty.getSourceType(), fromType)) { return getterProperty; } } return null; }
public GetterProperty(Getter<?, ?> getter) { this(getter, getSourceType(getter), getReturnType(getter)); }
public Type getCustomSourceReturnTypeFrom(Type ownerType) { GetterProperty getterProperty = getCustomGetterPropertyFrom(ownerType); return getterProperty != null ? getterProperty.getReturnType() : null; }
public Getter<?, ?> getCustomGetterFrom(Type fromType) { GetterProperty getterPropertyFrom = getCustomGetterPropertyFrom(fromType); return getterPropertyFrom != null ? getterPropertyFrom.getGetter() : null; }
public GetterProperty getCustomGetterPropertyFrom(Type fromType) { for(GetterProperty getterProperty : lookForAll(GetterProperty.class)) { if (getterProperty.getSourceType() == null || TypeHelper.isAssignable(getterProperty.getSourceType(), fromType)) { return getterProperty; } } return null; }
private Object upgrade(Object property) { if (property instanceof Setter) { return new SetterProperty((Setter<?, ?>) property); } if (property instanceof Getter) { return new GetterProperty((Getter<?, ?>) property); } return property; }
@Override public void accept(Predicate<? super K> predicate, GetterProperty getterProperty) { if (predicate instanceof CaseInsensitiveFieldKeyNamePredicate) { CaseInsensitiveFieldKeyNamePredicate p = (CaseInsensitiveFieldKeyNamePredicate) predicate; ExtendPropertyFinder.CustomProperty cp = new ExtendPropertyFinder.CustomProperty(getterProperty.getSourceType(), classMeta.getReflectionService(), p.getName(), getterProperty.getReturnType(), NullSetter.NULL_SETTER, getterProperty.getGetter()); if (propertyPredicate.test(cp)) { customProperties.add(cp); } } } });
private Object upgrade(Object property) { if (property instanceof Setter) { return new SetterProperty((Setter<?, ?>) property); } if (property instanceof Getter) { return new GetterProperty((Getter<?, ?>) property); } return property; }
public FieldMapperColumnDefinition<K> addGetter(Getter<?, ?> getter) { return add(new GetterProperty(getter)); } public FieldMapperColumnDefinition<K> addGetterFactory(GetterFactory<?, K> getterFactory) {
@Test public void testCustomGetter() throws Exception { PreparedStatementMapperBuilder<DMClass> mapperBuilder = JdbcMapperFactory.newInstance() .addColumnProperty("value", new GetterProperty(new Getter<Object, String>() { @Override public String get(Object target) throws Exception { return "value2"; } })) .buildFrom(DMClass.class); mapperBuilder.addColumn("value"); FieldMapper<DMClass, PreparedStatement> mapper = mapperBuilder.mapper(); DMClass dmClass = new DMClass(); PreparedStatement ps = mock(PreparedStatement.class); mapper.mapTo(dmClass, ps, null); verify(ps).setString(1, "value2"); }
@Test public void jdbcMapperCustomGetter() { JdbcMapperFactory .newInstance() .addColumnProperty("bar", new GetterProperty(new Getter<ResultSet, Bar2>() { @Override public Bar2 get(ResultSet target) throws Exception { return new Bar2(target.getString("bar"), 2); } })) .newBuilder(Foo2.class) .addKey("bar").mapper(); }
@Test public void jdbcMapperCustomGetter() { JdbcMapperFactory .newInstance() .addColumnProperty("bar", new GetterProperty(new Getter<ResultSet, Bar2>() { @Override public Bar2 get(ResultSet target) throws Exception { return new Bar2(target.getString("bar"), 2); } })) .newBuilder(Foo2.class) .addKey("bar").mapper(); }
private MapperConfig<SampleFieldKey, Object[]> mapperConfig() { FieldMapperColumnDefinitionProviderImpl<SampleFieldKey> provider = new FieldMapperColumnDefinitionProviderImpl<SampleFieldKey>(); provider.addColumnDefinition("benchmark", FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new SetterProperty(SETTER)).add(new GetterProperty(GETTER))); return MapperConfig.<SampleFieldKey, Object[]>fieldMapperConfig().columnDefinitions(provider); } }
@SuppressWarnings("unchecked") @Test public void testCustomReaderOnSetterStatic() throws Exception { final JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().newBuilder(DbObject.class) .addMapping("id", new GetterProperty(new Getter<ResultSet, Long>() { @Override public Long get(ResultSet target) throws Exception { return 1l; } })) .addMapping("name") //email, creation_time, type_ordinal, type_name .addMapping("email") .addMapping("creation_time") .addMapping("type_ordinal") .addMapping("type_name") .mapper(); DbHelper.testQuery(new TestRowHandler<PreparedStatement>() { @Override public void handle(PreparedStatement t) throws Exception { ResultSet r = t.executeQuery(); r.next(); DbHelper.assertDbObjectMapping(mapper.map(r)); } }, DbHelper.TEST_DB_OBJECT_QUERY.replace("id,", "33 as id,")); }