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.OptionalClassMeta Maven / Gradle / Ivy
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, ?>> 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);
}
}
}