org.sfm.reflect.FastTupleClassMeta Maven / Gradle / Ivy
Show all versions of simpleFlatMapper Show documentation
package org.sfm.reflect;
import org.sfm.reflect.meta.*;
import org.sfm.utils.ErrorHelper;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class FastTupleClassMeta implements ClassMeta {
private final ClassMeta delegate;
private final String[] headers;
public FastTupleClassMeta(Type target, ReflectionService reflectionService) {
try {
Class clazz = TypeHelper.toClass(target);
final List> constructorDefinitions = new ArrayList>();
constructorDefinitions.add(new ConstructorDefinition(clazz.getConstructor()));
final List> properties = getPropertyMetas(clazz, reflectionService);
this.delegate = new ObjectClassMeta(target,
constructorDefinitions, new ArrayList>(), properties, reflectionService);
this.headers = getHeaders(clazz, properties);
} catch (NoSuchMethodException e) {
ErrorHelper.rethrow(e);
throw new IllegalStateException();
}
}
private String[] getHeaders(Class> clazz, List> properties) {
try {
clazz.getDeclaredField("unsafe");
return null;
} catch(NoSuchFieldException e) {
final Field[] declaredFields = clazz.getDeclaredFields();
String[] headers = new String[properties.size()];
for(int i = 0; i < declaredFields.length; i++) {
String name = declaredFields[i].getName();
if (isPresent(properties, name)) {
headers[i] = name;
}
}
return headers;
}
}
private boolean isPresent(List> properties, String name) {
for(PropertyMeta pm : properties) {
if (pm.getName().equals(name)) {
return true;
}
}
return false;
}
private ArrayList> getPropertyMetas(Class clazz, ReflectionService reflectionService) throws NoSuchMethodException {
final ArrayList> propertyMetas = new ArrayList>();
for(Method m : clazz.getDeclaredMethods()) {
if (m.getParameterTypes().length == 0 && GetterHelper.methodModifiersMatches(m.getModifiers())) {
String field = m.getName();
Method setter = clazz.getDeclaredMethod(field, m.getReturnType());
MethodPropertyMeta propertyMeta = newPropertyMethod(field, m, setter, reflectionService);
propertyMetas.add(propertyMeta);
}
}
return propertyMetas;
}
private MethodPropertyMeta newPropertyMethod(String field, Method getter, Method setter, ReflectionService reflectionService) {
return new MethodPropertyMeta(field, reflectionService, setter, getter, getter.getGenericReturnType());
}
@Override
public ReflectionService getReflectionService() {
return delegate.getReflectionService();
}
@Override
public PropertyFinder newPropertyFinder() {
return delegate.newPropertyFinder();
}
@Override
public Type getType() {
return delegate.getType();
}
@Override
public String[] generateHeaders() {
if (headers == null) {
throw new UnsupportedOperationException("Cannot generate headers on directMemory tuple");
} else {
return headers;
}
}
}