All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.simpleflatmapper.reflect.meta.FastTupleClassMeta Maven / Gradle / Ivy
package org.simpleflatmapper.reflect.meta;
import org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.InstantiatorDefinition;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.ScoredGetter;
import org.simpleflatmapper.reflect.ScoredSetter;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.getter.GetterHelper;
import org.simpleflatmapper.util.Consumer;
import org.simpleflatmapper.util.ErrorHelper;
import org.simpleflatmapper.util.Predicate;
import org.simpleflatmapper.util.TypeHelper;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FastTupleClassMeta implements ClassMeta {
private final ClassMeta delegate;
private final List instantiatorDefinitions;
public FastTupleClassMeta(Type target, ReflectionService reflectionService) {
try {
Class clazz = TypeHelper.toClass(target);
instantiatorDefinitions = new ArrayList();
instantiatorDefinitions.add(new ExecutableInstantiatorDefinition(clazz.getConstructor()));
final List> properties = getPropertyMetas(target, reflectionService);
this.delegate =
new ObjectClassMeta(
target,
instantiatorDefinitions,
Collections.>emptyList(),
Collections.emptyMap(),
properties,
reflectionService,
false);
} catch (NoSuchMethodException e) {
ErrorHelper.rethrow(e);
throw new IllegalStateException();
}
}
public FastTupleClassMeta(ClassMeta delegate, List instantiatorDefinitions) {
this.delegate = delegate;
this.instantiatorDefinitions = instantiatorDefinitions;
}
@Override
public ClassMeta withReflectionService(ReflectionService reflectionService) {
return new FastTupleClassMeta(reflectionService.getClassMeta(delegate.getType()), instantiatorDefinitions);
}
private static ArrayList> getPropertyMetas(Type ownerType, ReflectionService reflectionService) throws NoSuchMethodException {
final ArrayList> propertyMetas = new ArrayList>();
Class clazz = TypeHelper.toClass(ownerType);
if (isDirect(clazz)) {
for(Method m : clazz.getDeclaredMethods()) {
if (m.getParameterTypes().length == 0 && GetterHelper.isPublicMember(m.getModifiers())) {
String field = m.getName();
Method setter = clazz.getDeclaredMethod(field, m.getReturnType());
ObjectPropertyMeta propertyMeta = newPropertyMethod(field, m, setter, reflectionService, ownerType);
propertyMetas.add(propertyMeta);
}
}
} else {
for (Field f : clazz.getDeclaredFields()) {
String field = f.getName();
try {
Method getter = clazz.getDeclaredMethod(field);
Method setter = clazz.getDeclaredMethod(field, f.getType());
ObjectPropertyMeta propertyMeta = newPropertyMethod(field, getter, setter, reflectionService, ownerType);
propertyMetas.add(propertyMeta);
} catch (NoSuchMethodException e) {
// field has no getter/setter ignore.
}
}
}
return propertyMetas;
}
private static boolean isDirect(Class clazz) {
try {
Field unsafe = clazz.getDeclaredField("unsafe");
int m = unsafe.getModifiers();
return Modifier.isStatic(m);
} catch (NoSuchFieldException e) {
return false;
}
}
private static ObjectPropertyMeta newPropertyMethod(String field, Method getter, Method setter, ReflectionService reflectionService, Type ownerType) {
Getter methodGetter = reflectionService.getObjectGetterFactory().getMethodGetter(getter);
Setter methodSetter = reflectionService.getObjectSetterFactory().getMethodSetter(setter);
return new ObjectPropertyMeta(field, ownerType, reflectionService,
getter.getGenericReturnType(),
ScoredGetter.of(methodGetter, 1),
ScoredSetter.of(methodSetter, 1), null);
}
@Override
public ReflectionService getReflectionService() {
return delegate.getReflectionService();
}
@Override
public PropertyFinder newPropertyFinder() {
return delegate.newPropertyFinder();
}
@Override
public Type getType() {
return delegate.getType();
}
@Override
public List getInstantiatorDefinitions() {
return instantiatorDefinitions;
}
@Override
public void forEachProperties(Consumer> consumer) {
delegate.forEachProperties(consumer);
}
@Override
public int getNumberOfProperties() {
return delegate.getNumberOfProperties();
}
@Override
public boolean needTransformer() {
return false;
}
}