Search in sources :

Example 21 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class InstantiatorFactory method getInstantiator.

@SuppressWarnings("unchecked")
public <S, T> Instantiator<S, T> getInstantiator(InstantiatorDefinition instantiatorDefinition, Class<S> source, Map<Parameter, Getter<? super S, ?>> injections, boolean useAsmIfEnabled, boolean builderIgnoresNullValues) {
    checkParameters(instantiatorDefinition, injections.keySet());
    if (asmFactory != null && useAsmIfEnabled) {
        if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) {
            ExecutableInstantiatorDefinition executableInstantiatorDefinition = (ExecutableInstantiatorDefinition) instantiatorDefinition;
            Member executable = executableInstantiatorDefinition.getExecutable();
            if (Modifier.isPublic(executable.getModifiers())) {
                try {
                    return asmFactory.createInstantiator(source, executableInstantiatorDefinition, injections, builderIgnoresNullValues);
                } catch (Exception e) {
                    // fall back on reflection
                    if (failOnAsmError)
                        ErrorHelper.rethrow(e);
                }
            }
        } else {
            try {
                return asmFactory.createInstantiator(source, (BuilderInstantiatorDefinition) instantiatorDefinition, injections, builderIgnoresNullValues);
            } catch (Exception e) {
                // fall back on reflection
                if (failOnAsmError)
                    ErrorHelper.rethrow(e);
            }
        }
    }
    switch(instantiatorDefinition.getType()) {
        case CONSTRUCTOR:
            return constructorInstantiator((ExecutableInstantiatorDefinition) instantiatorDefinition, injections);
        case METHOD:
            return methodInstantiator((ExecutableInstantiatorDefinition) instantiatorDefinition, injections);
        case BUILDER:
            return builderInstantiator((BuilderInstantiatorDefinition) instantiatorDefinition, injections, useAsmIfEnabled, builderIgnoresNullValues);
        default:
            throw new IllegalArgumentException("Unsupported executable type " + instantiatorDefinition);
    }
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)

Example 22 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class ReflectionInstantiatorDefinitionFactory method extractDefinitions.

@SuppressWarnings("unchecked")
private static List<InstantiatorDefinition> extractDefinitions(Type target, ParameterBuilder parameterBuilder) {
    Class<?> clazz = TypeHelper.toClass(target);
    List<InstantiatorDefinition> instantiatorDefinitions = new ArrayList<InstantiatorDefinition>();
    for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
        if (Modifier.isPublic(constructor.getModifiers())) {
            InstantiatorDefinition definition = new ExecutableInstantiatorDefinition(constructor, parameterBuilder.getParameters(constructor, target));
            instantiatorDefinitions.add(definition);
        }
    }
    for (Method m : clazz.getDeclaredMethods()) {
        if (Modifier.isPublic(m.getModifiers()) && Modifier.isStatic(m.getModifiers()) && clazz.isAssignableFrom(m.getReturnType())) {
            InstantiatorDefinition definition = new ExecutableInstantiatorDefinition(m, parameterBuilder.getParameters(m, target));
            instantiatorDefinitions.add(definition);
        }
    }
    return instantiatorDefinitions;
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ArrayList(java.util.ArrayList)

Example 23 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class BuilderInstantiatorDefinitionFactory method getDefinitionForBuilder.

public static BuilderInstantiatorDefinition getDefinitionForBuilder(Member e, Type target) {
    ExecutableInstantiatorDefinition def = new ExecutableInstantiatorDefinition(e);
    Type builderType;
    if (e instanceof Constructor) {
        builderType = ((Constructor) e).getDeclaringClass();
    } else {
        builderType = ((Method) e).getGenericReturnType();
    }
    return getDefinitionForBuilder(def, builderType, target);
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)

Aggregations

ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)23 Test (org.junit.Test)12 Parameter (org.simpleflatmapper.reflect.Parameter)12 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)11 HashMap (java.util.HashMap)6 InstantiatorDefinitions (org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions)4 InputStream (java.io.InputStream)3 Getter (org.simpleflatmapper.reflect.Getter)3 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)3 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)3 ArrayList (java.util.ArrayList)2 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)2 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)2 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)2 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)2 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 BiFunction (org.simpleflatmapper.util.BiFunction)2 IOException (java.io.IOException)1