
org.sfm.reflect.ReflectionService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.reflect;
import org.sfm.reflect.asm.AsmConstructorDefinitionFactory;
import org.sfm.reflect.asm.AsmFactory;
import org.sfm.reflect.asm.AsmHelper;
import org.sfm.reflect.meta.*;
import org.sfm.tuples.Tuples;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
public class ReflectionService {
@SuppressWarnings("raw")
private static final Map predefinedConstructors = new HashMap();
static {
try {
predefinedConstructors.put(String.class, Arrays.asList(
new ConstructorDefinition(String.class.getConstructor(String.class), new ConstructorParameter("value", String.class)),
new ConstructorDefinition(String.class.getConstructor(char[].class), new ConstructorParameter("value", char[].class))
));
} catch (NoSuchMethodException e) {
throw new Error("Could not find new String(String)");
}
}
private final SetterFactory setterFactory;
private final InstantiatorFactory instantiatorFactory;
private final AsmFactory asmFactory;
private final AliasProvider aliasProvider;
private final boolean asmPresent;
private final boolean asmActivated;
public ReflectionService(final boolean asmPresent, final boolean asmActivated, final AsmFactory asmFactory) {
this.asmPresent = asmPresent;
this.asmActivated = asmActivated && asmPresent;
if (asmActivated) {
this.asmFactory = asmFactory;
} else {
this.asmFactory = null;
}
this.setterFactory = new SetterFactory(asmFactory);
this.instantiatorFactory = new InstantiatorFactory(asmFactory);
this.aliasProvider = AliasProviderFactory.getAliasProvider();
}
public SetterFactory getSetterFactory() {
return setterFactory;
}
public InstantiatorFactory getInstantiatorFactory() {
return instantiatorFactory;
}
public boolean isAsmPresent() {
return asmPresent;
}
public boolean isAsmActivated() {
return asmActivated;
}
public AsmFactory getAsmFactory() {
return asmFactory;
}
public ClassMeta getClassMeta(Type target, boolean root) {
Class clazz = TypeHelper.toClass(target);
if (List.class.isAssignableFrom(clazz)) {
ParameterizedType pt = (ParameterizedType) target;
return new ArrayClassMeta(ArrayList.class, pt.getActualTypeArguments()[0], this);
} else if (clazz.isArray()) {
return new ArrayClassMeta(clazz, clazz.getComponentType(), this);
} else if (Tuples.isTuple(target)) {
return new TupleClassMeta(target, this);
}
if (root) {
return new SingletonClassMeta(new ObjectClassMeta(target, this));
} else {
if (TypeHelper.isJavaLang(target)|| TypeHelper.isEnum(target) || TypeHelper.areEquals(target, Date.class)) {
return null;
} else {
return new ObjectClassMeta(target, this);
}
}
}
public ClassMeta getRootClassMeta(Type mapToClass) {
return getClassMeta(mapToClass, true);
}
public String getColumnName(Method method) {
return aliasProvider.getAliasForMethod(method);
}
public String getColumnName(Field field) {
return aliasProvider.getAliasForField(field);
}
@SuppressWarnings("unchecked")
public List> extractConstructors(Type target) throws IOException {
List> list;
if (predefinedConstructors.containsKey(target)) {
List constructorDefinitions = predefinedConstructors.get(target);
list = (List>) constructorDefinitions;
} else if (isAsmPresent()) {
list = AsmConstructorDefinitionFactory.extractConstructors(target);
} else {
list = ReflectionConstructorDefinitionFactory.extractConstructors(target);
}
return list;
}
public static ReflectionService newInstance() {
return newInstance(false, true);
}
private static AsmFactory _asmFactory = new AsmFactory(Thread.currentThread().getContextClassLoader());
public static ReflectionService newInstance(boolean disableAsm, boolean useAsmGeneration) {
boolean asmPresent = AsmHelper.isAsmPresent() && !disableAsm;
boolean hasClassLoaderIncapacity = cannotSeeSetterFromContextClassLoader();
if (hasClassLoaderIncapacity) {
useAsmGeneration = false;
}
return new ReflectionService(asmPresent, useAsmGeneration, asmPresent && useAsmGeneration ? _asmFactory : null);
}
private static boolean cannotSeeSetterFromContextClassLoader() {
try {
Class.forName(Setter.class.getName(), false, Thread.currentThread().getContextClassLoader());
} catch(Exception e) {
return true;
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy