Search in sources :

Example 16 with Getter

use of org.simpleflatmapper.reflect.Getter in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorBuilder method getFunctionCall.

private static InjectionPoint getFunctionCall(Parameter parameter, BiFunction biFunction) {
    if (biFunction.getClass().equals(BiFunctionGetter.class)) {
        Getter getter = ((BiFunctionGetter) biFunction).getGetter();
        Class<?> getterClass = getter.getClass();
        if (TypeHelper.isPrimitive(parameter.getType())) {
            Class<?> primitiveGetter = getPrimitiveGetter(parameter.getType());
            if (primitiveGetter == null) {
                throw new IllegalStateException("No primitive getter for primitive " + parameter.getType());
            }
            Type publicGetterClass = AsmUtils.findClosestPublicTypeExposing(getterClass, primitiveGetter);
            if (publicGetterClass != null) {
                return new InjectionPoint(parameter, biFunction, true, "get" + getPrimitiveMethodSuffix(parameter.getType()), publicGetterClass, true);
            }
        }
        Type publicGetterClass = AsmUtils.findClosestPublicTypeExposing(getterClass, Getter.class);
        return new InjectionPoint(parameter, biFunction, true, "get", publicGetterClass, false);
    } else {
        return new InjectionPoint(parameter, biFunction, false, "apply", biFunction.getClass(), false);
    }
}
Also used : Type(java.lang.reflect.Type) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Getter(org.simpleflatmapper.reflect.Getter) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter)

Example 17 with Getter

use of org.simpleflatmapper.reflect.Getter in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomReaderOnSetterStatic.

@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(// email, creation_time, type_ordinal, type_name
    "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,"));
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Getter(org.simpleflatmapper.reflect.Getter) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) Test(org.junit.Test)

Example 18 with Getter

use of org.simpleflatmapper.reflect.Getter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorBuilder method appendGetters.

private static <S> void appendGetters(Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw) {
    FieldVisitor fv;
    for (Entry<Parameter, Getter<? super S, ?>> entry : injections.entrySet()) {
        GetterCall getterCall = getGetterCall(entry.getKey().getType(), entry.getValue().getClass());
        fv = cw.visitField(ACC_FINAL, "getter_" + entry.getKey().getName(), AsmUtils.toTargetTypeDeclaration(getterCall.getterType), null, null);
        fv.visitEnd();
    }
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Parameter(org.simpleflatmapper.reflect.Parameter) FieldVisitor(org.simpleflatmapper.ow2asm.FieldVisitor)

Example 19 with Getter

use of org.simpleflatmapper.reflect.Getter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorBuilder method appendInitBuilder.

private static <S> void appendInitBuilder(Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, String sourceType, String classType, BuilderInstantiatorDefinition instantiatorDefinition) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/util/Map;L" + AsmUtils.toAsmType(Instantiator.class) + ";)V", "(Ljava/util/Map<Ljava.lang.String;L" + AsmUtils.toAsmType(Getter.class) + "<" + AsmUtils.toTargetTypeDeclaration(sourceType) + "*>;>;" + "L" + AsmUtils.toAsmType(Instantiator.class) + "<Ljava/lang/Void;L" + AsmUtils.toAsmType(BiInstantiatorBuilder.getTargetType(instantiatorDefinition.getBuilderInstantiator())) + ";>;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitFieldInsn(PUTFIELD, classType, "builderInstantiator", "L" + AsmUtils.toAsmType(Instantiator.class) + ";");
    appendInitGetters(injections, classType, mv);
    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Instantiator(org.simpleflatmapper.reflect.Instantiator) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 20 with Getter

use of org.simpleflatmapper.reflect.Getter in project SimpleFlatMapper by arnaudroger.

the class ParamNameDeductor method listAccessors.

private List<Accessor<T>> listAccessors() {
    final List<Accessor<T>> list = new ArrayList<Accessor<T>>();
    ClassVisitor.visit(target, new FieldAndMethodCallBack() {

        ObjectGetterFactory objectGetterFactory = new ObjectGetterFactory(null);

        @Override
        public void method(Method method) {
            if (GetterHelper.isGetter(method)) {
                Getter<T, Object> methodGetter = objectGetterFactory.getMethodGetter(method);
                list.add(new Accessor<T>(GetterHelper.getPropertyNameFromMethodName(method.getName()), methodGetter));
            }
        }

        @Override
        public void field(Field field) {
            Getter<T, Object> fieldGetter = objectGetterFactory.getFieldGetter(field);
            list.add(new Accessor<T>(field.getName(), fieldGetter));
        }
    });
    return list;
}
Also used : FieldAndMethodCallBack(org.simpleflatmapper.reflect.meta.FieldAndMethodCallBack) Field(java.lang.reflect.Field) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) ObjectGetterFactory(org.simpleflatmapper.reflect.ObjectGetterFactory) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Aggregations

Getter (org.simpleflatmapper.reflect.Getter)20 Type (java.lang.reflect.Type)6 Test (org.junit.Test)6 Parameter (org.simpleflatmapper.reflect.Parameter)6 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)6 HashMap (java.util.HashMap)4 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ResultSet (java.sql.ResultSet)3 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)3 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Converter (org.simpleflatmapper.converter.Converter)2 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)2 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)2 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)2 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)2