Search in sources :

Example 16 with Parameter

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

the class BiInstantiatorFactoryTest method testInstantiateConstructorWithArgsAllPr.

@Test
public void testInstantiateConstructorWithArgsAllPr() throws Exception {
    BiInstantiator<Object, Object, DbFinalPrimitiveObject> instantiator = DISABLE_ASM.getBiInstantiator(DbFinalPrimitiveObject.class, Object.class, Object.class, AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalPrimitiveObject.class), new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>(), true, true);
    DbFinalPrimitiveObject object = instantiator.newInstance(null, null);
    assertNotNull(object);
}
Also used : DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Example 17 with Parameter

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

the class BiInstantiatorFactoryTest method testInstantiateConstructorWithArgsAllPrAsm.

@Test
public void testInstantiateConstructorWithArgsAllPrAsm() throws Exception {
    BiInstantiator<Object, Object, DbFinalPrimitiveObject> instantiator = ASM.getBiInstantiator(DbFinalPrimitiveObject.class, Object.class, Object.class, AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalPrimitiveObject.class), new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>(), true, true);
    DbFinalPrimitiveObject object = instantiator.newInstance(null, null);
    assertNotNull(object);
}
Also used : DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Example 18 with Parameter

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

the class CsvMapperCellHandlerImpAsmInstantiatorTest method testInstantiateStringWithCharArray.

@SuppressWarnings("unchecked")
@Test
public void testInstantiateStringWithCharArray() throws Exception {
    HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>> injections = new HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>>();
    Parameter parameter = new Parameter(0, "arg0", char[].class);
    DelayedGetter delayedGetter = new DelayedGetter(0);
    injections.put(parameter, delayedGetter);
    Class<CsvMapperCellHandlerImpl<String>> source = (Class) CsvMapperCellHandlerImpl.class;
    Instantiator<CsvMapperCellHandlerImpl<String>, String> instantiator = factory.createInstantiator(source, new ExecutableInstantiatorDefinition(String.class.getConstructor(char[].class), parameter), injections, true);
    DelayedCellSetterImpl delayedCellSetter = new DelayedCellSetterImpl(null, new CellValueReader() {

        @Override
        public char[] read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return new char[] { 'h' };
        }
    });
    delayedCellSetter.set(null, 0, 0, null);
    CsvMapperCellHandlerImpl targetSettersImpl = new CsvMapperCellHandlerImpl(instantiator, new DelayedCellSetter[] { delayedCellSetter }, new CellSetter[] {}, null, null, null);
    Assert.assertNotNull(instantiator.newInstance(targetSettersImpl));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ParsingContext(org.simpleflatmapper.csv.ParsingContext) HashMap(java.util.HashMap) DelayedCellSetterImpl(org.simpleflatmapper.csv.impl.cellreader.DelayedCellSetterImpl) Getter(org.simpleflatmapper.reflect.Getter) CellValueReader(org.simpleflatmapper.csv.CellValueReader) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 19 with Parameter

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

the class BiInstantiatorBuilder method createInstantiator.

public static <S1, S2> byte[] createInstantiator(final String className, final Class<?> s1, final Class<?> s2, final ExecutableInstantiatorDefinition instantiatorDefinition, final Map<Parameter, BiFunction<? super S1, ? super S2, ?>> injectionsMap) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    Class<?> targetClass = TypeHelper.toClass(getTargetType(instantiatorDefinition));
    String targetType = AsmUtils.toAsmType(targetClass);
    String s1Type = AsmUtils.toWrapperType(s1);
    String s2Type = AsmUtils.toWrapperType(s2);
    String classType = AsmUtils.toAsmType(className);
    String instantiatorType = AsmUtils.toAsmType(BI_INSTANTIATOR_CLASS);
    List<InjectionPoint> injections = toInjections(injectionsMap);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + instantiatorType + "<L" + targetType + ";>;", "java/lang/Object", new String[] { instantiatorType });
    Parameter[] parameters = instantiatorDefinition.getParameters();
    appendFunctionsField(injections, cw);
    appendInit(injections, cw, s1Type, s2Type, classType);
    appendNewInstance(s1, s2, instantiatorDefinition, injections, cw, targetType, s1Type, s2Type, classType, parameters);
    appendBridgeMethod(cw, targetType, s1Type, s2Type, classType);
    appendToString(injections, cw, parameters);
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : Parameter(org.simpleflatmapper.reflect.Parameter) ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter)

Example 20 with Parameter

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

the class BiInstantiatorBuilder method appendNewInstanceBuilder.

private static <S1, S2> void appendNewInstanceBuilder(Class<?> s1, Class<?> s2, BuilderInstantiatorDefinition instantiatorDefinition, List<InjectionPoint> injectionPoints, ClassWriter cw, String targetType, String s1Type, String s2Type, String classType, Map<Parameter, Method> setters, boolean ignoreNullValues) throws NoSuchMethodException {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(s1Type) + AsmUtils.toTargetTypeDeclaration(s2Type) + ")" + AsmUtils.toTargetTypeDeclaration(targetType), null, new String[] { "java/lang/Exception" });
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classType, "builderInstantiator", AsmUtils.toTargetTypeDeclaration(INSTANTIATOR_CLASS));
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEINTERFACE, AsmUtils.toAsmType(INSTANTIATOR_CLASS), "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    final Type builderClass = getTargetType(instantiatorDefinition.getBuilderInstantiator());
    final String builderType = AsmUtils.toAsmType(builderClass);
    mv.visitTypeInsn(CHECKCAST, builderType);
    mv.visitVarInsn(ASTORE, 2);
    for (final Entry<Parameter, Method> e : setters.entrySet()) {
        Parameter p = e.getKey();
        final InjectionPoint injectionPoint = findFunctionCalls(p, injectionPoints);
        if (injectionPoint != null) {
            final boolean checkIfNull = ignoreNullValues && !injectionPoint.isPrimitive;
            mv.visitVarInsn(ALOAD, 2);
            invokeBiFunction(injectionPoint, classType, s1, s2, mv, new Consumer<MethodVisitor>() {

                @Override
                public void accept(MethodVisitor mv) {
                    if (checkIfNull) {
                        mv.visitVarInsn(ALOAD, 2);
                        mv.visitVarInsn(AsmUtils.getLoadOps(injectionPoint.parameter.getType()), 3);
                    }
                    AsmUtils.invoke(mv, TypeHelper.toClass(builderClass), e.getValue().getName(), AsmUtils.toSignature(e.getValue()));
                    if (!Void.TYPE.equals(e.getValue().getReturnType())) {
                        mv.visitVarInsn(ASTORE, 2);
                    }
                }
            }, ignoreNullValues);
        }
    }
    mv.visitVarInsn(ALOAD, 2);
    AsmUtils.invoke(mv, TypeHelper.toClass(builderClass), instantiatorDefinition.getBuildMethod().getName(), AsmUtils.toSignature(instantiatorDefinition.getBuildMethod()));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}
Also used : Type(java.lang.reflect.Type) Parameter(org.simpleflatmapper.reflect.Parameter) Method(java.lang.reflect.Method) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Aggregations

Parameter (org.simpleflatmapper.reflect.Parameter)37 Test (org.junit.Test)20 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)20 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)17 HashMap (java.util.HashMap)16 BiFunction (org.simpleflatmapper.util.BiFunction)16 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)10 InstantiatorFactory (org.simpleflatmapper.reflect.InstantiatorFactory)10 ConstantBiFunction (org.simpleflatmapper.util.ConstantBiFunction)9 Getter (org.simpleflatmapper.reflect.Getter)6 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)6 BuilderBiInstantiator (org.simpleflatmapper.reflect.impl.BuilderBiInstantiator)6 MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)5 Method (java.lang.reflect.Method)4 Type (java.lang.reflect.Type)4 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)4 DbFinalPrimitiveObject (org.simpleflatmapper.test.beans.DbFinalPrimitiveObject)4 InputStream (java.io.InputStream)3 Member (java.lang.reflect.Member)3 Constructor (java.lang.reflect.Constructor)2