Search in sources :

Example 1 with AsmFactory

use of org.simpleflatmapper.reflect.asm.AsmFactory in project SimpleFlatMapper by arnaudroger.

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodAsmBoxing.

@Test
public void testBuilderFromMethodAsmBoxing() throws Exception {
    final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
    assertEquals(1, instantiatorDefinitions.size());
    BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
    final Parameter[] parameters = b.getParameters();
    Arrays.sort(parameters, new Comparator<Parameter>() {

        @Override
        public int compare(Parameter o1, Parameter o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    Map<Parameter, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
    params.put(parameters[1], new ConstantBiFunction<Void, Object, String>("myname"));
    params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertFalse((instantiator instanceof BuilderBiInstantiator));
    assertEquals("myname", o.getName());
    assertEquals(3, o.getId());
}
Also used : HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ConstantBiFunction(org.simpleflatmapper.util.ConstantBiFunction) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 2 with AsmFactory

use of org.simpleflatmapper.reflect.asm.AsmFactory in project SimpleFlatMapper by arnaudroger.

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoid.

@Test
public void testBuilderFromMethodVoid() throws Exception {
    final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
    assertEquals(1, instantiatorDefinitions.size());
    BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
    final Parameter[] parameters = b.getParameters();
    Arrays.sort(parameters, new Comparator<Parameter>() {

        @Override
        public int compare(Parameter o1, Parameter o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    Map<Parameter, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
    params.put(parameters[2], new ConstantBiFunction<Void, Object, String>("zrux"));
    params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertFalse((instantiator instanceof BuilderBiInstantiator));
    assertEquals(3, o.getId());
    assertEquals("zrux", o.getZrux());
}
Also used : HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ConstantBiFunction(org.simpleflatmapper.util.ConstantBiFunction) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 3 with AsmFactory

use of org.simpleflatmapper.reflect.asm.AsmFactory in project SimpleFlatMapper by arnaudroger.

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoidNullHandling.

@Test
public void testBuilderFromMethodVoidNullHandling() throws Exception {
    final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
    assertEquals(1, instantiatorDefinitions.size());
    BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
    final Parameter[] parameters = b.getParameters();
    Arrays.sort(parameters, new Comparator<Parameter>() {

        @Override
        public int compare(Parameter o1, Parameter o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    Map<Parameter, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
    params.put(parameters[2], new ConstantBiFunction<Void, Object, String>(null));
    params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(null));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertFalse((instantiator instanceof BuilderBiInstantiator));
    assertEquals(0, o.getId());
    assertEquals(null, o.getZrux());
}
Also used : HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ConstantBiFunction(org.simpleflatmapper.util.ConstantBiFunction) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 4 with AsmFactory

use of org.simpleflatmapper.reflect.asm.AsmFactory in project SimpleFlatMapper by arnaudroger.

the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoid.

@Test
public void testBuilderFromMethodVoid() throws Exception {
    final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
    assertEquals(1, instantiatorDefinitions.size());
    BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
    final Parameter[] parameters = b.getParameters();
    Arrays.sort(parameters, new Comparator<Parameter>() {

        @Override
        public int compare(Parameter o1, Parameter o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    Map<Parameter, Getter<? super Void, ?>> params = new HashMap<Parameter, Getter<? super Void, ?>>();
    params.put(parameters[2], new ConstantGetter<Void, Object>("zrux"));
    params.put(parameters[0], new ConstantGetter<Void, Integer>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final Instantiator<Void, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, ClassBuilderWithMethod>getInstantiator(b, Void.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null);
    assertFalse((instantiator instanceof BuilderInstantiator));
    assertEquals(3, o.getId());
    assertEquals("zrux", o.getZrux());
}
Also used : HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiator(org.simpleflatmapper.reflect.impl.BuilderInstantiator) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) Test(org.junit.Test)

Example 5 with AsmFactory

use of org.simpleflatmapper.reflect.asm.AsmFactory in project SimpleFlatMapper by arnaudroger.

the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethod.

@Test
public void testBuilderFromMethod() throws Exception {
    final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
    assertEquals(1, instantiatorDefinitions.size());
    BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
    assertEquals(ClassBuilderWithMethod.Builder.class.getName(), b.getName());
    // builder instantiator
    final ExecutableInstantiatorDefinition builderInstantiator = (ExecutableInstantiatorDefinition) b.getBuilderInstantiator();
    assertEquals(ClassBuilderWithMethod.class.getMethod("builder"), builderInstantiator.getExecutable());
    assertEquals(0, builderInstantiator.getParameters().length);
    final Parameter[] parameters = b.getParameters();
    assertEquals(3, parameters.length);
    Arrays.sort(parameters, new Comparator<Parameter>() {

        @Override
        public int compare(Parameter o1, Parameter o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    assertEquals("name", parameters[1].getName());
    assertEquals("id", parameters[0].getName());
    Map<Parameter, Getter<? super Void, ?>> params = new HashMap<Parameter, Getter<? super Void, ?>>();
    params.put(parameters[1], new ConstantGetter<Void, Object>("myname"));
    params.put(parameters[0], new ConstantIntGetter<Void>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final Instantiator<Void, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, ClassBuilderWithMethod>getInstantiator(b, Void.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null);
    assertFalse((instantiator instanceof BuilderInstantiator));
    assertEquals("myname", o.getName());
    assertEquals(3, o.getId());
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiator(org.simpleflatmapper.reflect.impl.BuilderInstantiator) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) Test(org.junit.Test)

Aggregations

AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)12 Test (org.junit.Test)10 HashMap (java.util.HashMap)8 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)8 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)5 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)4 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)4 InstantiatorFactory (org.simpleflatmapper.reflect.InstantiatorFactory)4 Parameter (org.simpleflatmapper.reflect.Parameter)4 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)4 BuilderBiInstantiator (org.simpleflatmapper.reflect.impl.BuilderBiInstantiator)4 BuilderInstantiator (org.simpleflatmapper.reflect.impl.BuilderInstantiator)4 BiFunction (org.simpleflatmapper.util.BiFunction)4 ConstantBiFunction (org.simpleflatmapper.util.ConstantBiFunction)4 FieldMapper (org.simpleflatmapper.map.FieldMapper)2 MapperAsmFactory (org.simpleflatmapper.map.asm.MapperAsmFactory)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 MappingContext (org.simpleflatmapper.map.MappingContext)1