org.simpleflatmapper.reflect.meta.OptionalClassMeta Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-reflect Show documentation
Show all versions of sfm-reflect Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.simpleflatmapper.reflect.meta;
import org.simpleflatmapper.reflect.ScoredSetter;
import org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.InstantiatorDefinition;
import org.simpleflatmapper.reflect.Parameter;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.ScoredGetter;
import org.simpleflatmapper.util.Consumer;
import org.simpleflatmapper.util.ErrorHelper;
import org.simpleflatmapper.util.Predicate;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
public class OptionalClassMeta implements ClassMeta> {
private final ReflectionService reflectionService;
private final Type type;
private final InstantiatorDefinition instantiatorDefinition;
private final ConstructorPropertyMeta, ?> propertyMeta;
private final ClassMeta innerMeta;
public OptionalClassMeta(ReflectionService reflectionService, Type type, InstantiatorDefinition instantiatorDefinition, ConstructorPropertyMeta, ?> propertyMeta, ClassMeta innerMeta) {
this.reflectionService = reflectionService;
this.type = type;
this.instantiatorDefinition = instantiatorDefinition;
this.propertyMeta = propertyMeta;
this.innerMeta = innerMeta;
}
public OptionalClassMeta(Type type, ReflectionService reflectionService) {
this.type = type;
this.reflectionService = reflectionService;
try {
this.instantiatorDefinition = getInstantiatorDefinition(type);
this.propertyMeta = new ConstructorPropertyMeta, Object>("value",
type, reflectionService,
instantiatorDefinition.getParameters()[0],
new ScoredGetter, Object>(Integer.MAX_VALUE, new OptionalGetter()),
ScoredSetter., Object>nullSetter(),
instantiatorDefinition, null);
this.innerMeta = reflectionService.getClassMeta(instantiatorDefinition.getParameters()[0].getGenericType());
} catch(Exception e) {
ErrorHelper.rethrow(e);
throw new IllegalStateException();
}
}
@Override
public ClassMeta> withReflectionService(ReflectionService reflectionService) {
return new OptionalClassMeta(reflectionService, type, instantiatorDefinition, propertyMeta.withReflectionService(reflectionService), reflectionService.getClassMeta(innerMeta.getType()));
}
private InstantiatorDefinition getInstantiatorDefinition(Type type) throws NoSuchMethodException {
ParameterizedType pt = (ParameterizedType) type;
InstantiatorDefinition id = new ExecutableInstantiatorDefinition(Optional.class.getMethod("ofNullable", Object.class),
new Parameter(0, "value", Object.class, pt.getActualTypeArguments()[0]));
return id;
}
@Override
public ReflectionService getReflectionService() {
return reflectionService;
}
@Override
public PropertyFinder> newPropertyFinder() {
return new OptionalPropertyFinder(this, reflectionService.selfScoreFullName());
}
public Type getType() {
return type;
}
public ClassMeta getInnerMeta() {
return innerMeta;
}
public PropertyMeta, ?> getProperty() {
return propertyMeta;
}
@Override
public List getInstantiatorDefinitions() {
return Arrays.asList(instantiatorDefinition);
}
@Override
public void forEachProperties(Consumer super PropertyMeta, ?>> consumer) {
consumer.accept(propertyMeta);
}
@Override
public int getNumberOfProperties() {
return 1;
}
@Override
public boolean needTransformer() {
return false;
}
private static class OptionalGetter implements Getter, Object> {
@Override
public Object get(Optional target) throws Exception {
return target.orElse(null);
}
}
}