Search in sources :

Example 6 with AsmFactory

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

the class AbstractConstantTargetMapperBuilder method mapper.

@SuppressWarnings("unchecked")
public Mapper<T, S> mapper() {
    final List<FieldMapper<T, S>> mappers = new ArrayList<FieldMapper<T, S>>();
    final MappingContextFactoryBuilder mappingContextFactoryBuilder = new MappingContextFactoryBuilder(new KeySourceGetter<K, T>() {

        @Override
        public Object getValue(K key, T source) throws Exception {
            throw new UnsupportedOperationException();
        }
    });
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> pm) {
            preFieldProcess(mappers, pm);
            FieldMapper<T, S> fieldMapper = fieldAppenderFactory.newFieldMapper(pm, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler());
            mappers.add(fieldMapper);
            postFieldProcess(mappers, pm);
        }
    });
    postMapperProcess(mappers);
    Mapper<T, S> mapper;
    FieldMapper[] fields = mappers.toArray(new FieldMapper[0]);
    BiInstantiator<T, MappingContext<? super T>, S> instantiator = getInstantiator();
    if (mappers.size() < 256) {
        try {
            mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {

                @Override
                public MapperAsmFactory newInstance(AsmFactory asmFactory) {
                    return new MapperAsmFactory(asmFactory);
                }
            }).<T, S>createMapper(getKeys(), fields, new FieldMapper[0], instantiator, TypeHelper.<T>toClass(classMeta.getType()), sourceClass);
        } catch (Throwable e) {
            if (mapperConfig.failOnAsm()) {
                return ErrorHelper.rethrow(e);
            } else {
                mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
            }
        }
    } else {
        mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
    }
    return new ContextualMapper<T, S>(mapper, mappingContextFactoryBuilder.newFactory());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ArrayList(java.util.ArrayList) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Example 7 with AsmFactory

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

the class ObjectSetterFactoryTest method testFailFallBackToMethod.

@Test
public void testFailFallBackToMethod() throws Exception {
    Setter<Foo, String> setter = new ObjectSetterFactory(new AsmFactory(Thread.currentThread().getContextClassLoader()) {

        @Override
        public <T, P> Setter<T, P> createSetter(Method m) throws Exception {
            throw new UnsupportedOperationException();
        }
    }).getSetter(Foo.class, "foo");
    assertTrue(setter instanceof MethodSetter);
    SetterHelperTest.validateFooSetter(setter);
}
Also used : Foo(org.simpleflatmapper.test.beans.Foo) ObjectSetterFactory(org.simpleflatmapper.reflect.ObjectSetterFactory) CharacterMethodSetter(org.simpleflatmapper.reflect.primitive.CharacterMethodSetter) LongMethodSetter(org.simpleflatmapper.reflect.primitive.LongMethodSetter) ShortMethodSetter(org.simpleflatmapper.reflect.primitive.ShortMethodSetter) ByteMethodSetter(org.simpleflatmapper.reflect.primitive.ByteMethodSetter) BooleanMethodSetter(org.simpleflatmapper.reflect.primitive.BooleanMethodSetter) FloatMethodSetter(org.simpleflatmapper.reflect.primitive.FloatMethodSetter) IntMethodSetter(org.simpleflatmapper.reflect.primitive.IntMethodSetter) MethodSetter(org.simpleflatmapper.reflect.setter.MethodSetter) DoubleMethodSetter(org.simpleflatmapper.reflect.primitive.DoubleMethodSetter) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 8 with AsmFactory

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

the class AsmFactoryTest method testRegisterOrCreate.

@Test
public void testRegisterOrCreate() {
    AsmFactory asmFactory = new AsmFactory(getClass().getClassLoader());
    final MyService[] services = new MyService[] { new MyService(), new MyService() };
    UnaryFactory<AsmFactory, MyService> factory = new UnaryFactory<AsmFactory, MyService>() {

        int i = 0;

        @Override
        public MyService newInstance(AsmFactory asmFactory) {
            return services[i++];
        }
    };
    assertSame(services[0], asmFactory.registerOrCreate(MyService.class, factory));
    assertSame(services[0], asmFactory.registerOrCreate(MyService.class, factory));
}
Also used : AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Example 9 with AsmFactory

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

the class BuilderBiInstantiatorDefinitionFactoryTest 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, 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 : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) 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 10 with AsmFactory

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

the class BuilderInstantiatorDefinitionFactoryTest 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, Getter<? super Void, ?>> params = new HashMap<Parameter, Getter<? super Void, ?>>();
    params.put(parameters[2], new ConstantGetter<Void, Object>(null));
    params.put(parameters[0], new ConstantGetter<Void, Integer>(null));
    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(0, o.getId());
    assertEquals(null, 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)

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