use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class InstantiatorDefinitionTest method setUp.
@Before
public void setUp() throws Exception {
c = InstantiatorDefinitionTest.class.getConstructor();
setUpMethod = InstantiatorDefinitionTest.class.getMethod("setUp");
valueOfMethod = InstantiatorDefinitionTest.class.getMethod("valueOf");
parameter = new Parameter(0, "i0", String.class);
}
use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class InstantiatorDefinitionTest method testTypeAffinityCompatibilityScorers.
@Test
public void testTypeAffinityCompatibilityScorers() {
InstantiatorDefinitions.CompatibilityScorer compatibilityScorer = InstantiatorDefinitions.getCompatibilityScorer(new TypeAffinity() {
@Override
public Class<?>[] getAffinities() {
return new Class<?>[] { Date.class, URL.class };
}
});
assertEquals(0, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", InputStream.class))));
assertEquals(12, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Date.class))));
assertEquals(11, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", URL.class))));
assertEquals(1, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Number.class))));
}
use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class InstantiatorBuilder method createInstantiator.
public static <S> byte[] createInstantiator(final String className, final Class<?> sourceClass, final ExecutableInstantiatorDefinition instantiatorDefinition, final Map<Parameter, Getter<? super S, ?>> injections) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
Class<?> targetClass = TypeHelper.toClass(BiInstantiatorBuilder.getTargetType(instantiatorDefinition));
String targetType = AsmUtils.toAsmType(targetClass);
String sourceType = AsmUtils.toWrapperType(sourceClass);
String classType = AsmUtils.toAsmType(className);
String instantiatorType = AsmUtils.toAsmType(Instantiator.class);
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();
appendGetters(injections, cw);
appendInit(injections, cw, sourceType, classType);
appendNewInstanceBuilderOnMethod(sourceClass, instantiatorDefinition, injections, cw, targetType, sourceType, classType, parameters);
appendBridgeMethod(cw, targetType, sourceType, 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 InstantiatorBuilder method appendNewInstanceBuilderOnBuilder.
private static <S> void appendNewInstanceBuilderOnBuilder(Class<?> sourceClass, BuilderInstantiatorDefinition instantiatorDefinition, Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, String targetType, String sourceType, String classType, Map<Parameter, Method> setters, boolean ignoreNullValues) throws NoSuchMethodException {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(sourceType) + ")" + 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 = BiInstantiatorBuilder.getTargetType(instantiatorDefinition.getBuilderInstantiator());
final String builderType = AsmUtils.toAsmType(builderClass);
mv.visitTypeInsn(CHECKCAST, builderType);
mv.visitVarInsn(ASTORE, 2);
boolean first = true;
for (final Entry<Parameter, Method> e : setters.entrySet()) {
Parameter p = e.getKey();
Getter<? super S, ?> getter = injections.get(p);
if (getter != null) {
final Class<?> parameterType = p.getType();
final boolean checkIfNull = ignoreNullValues && !getGetterCall(parameterType, getter.getClass()).isPrimitive;
mv.visitVarInsn(ALOAD, 2);
invokeGetter(p, getter, classType, sourceClass, mv, new Consumer<MethodVisitor>() {
@Override
public void accept(MethodVisitor mv) {
if (checkIfNull) {
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(AsmUtils.getLoadOps(parameterType), 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);
if (checkIfNull) {
if (first && !Void.TYPE.equals(e.getValue().getReturnType())) {
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { builderType }, 0, null);
} else {
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}
first = false;
}
}
}
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();
}
use of org.simpleflatmapper.reflect.Parameter 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();
}
}
Aggregations