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);
}
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);
}
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));
}
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());
}
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();
}
Aggregations