org.simpleflatmapper.reflect.meta.PassThroughClassMeta 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.InstantiatorDefinition;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.TypeAffinity;
import org.simpleflatmapper.util.Consumer;
import org.simpleflatmapper.util.ErrorHelper;
import org.simpleflatmapper.util.TypeHelper;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
public class PassThroughClassMeta implements ClassMeta {
private final ReflectionService reflectionService;
private final Type type;
private final InstantiatorDefinition instantiatorDefinition;
private final PropertyMeta propertyMeta;
private final ClassMeta innerMeta;
public PassThroughClassMeta(Type type, ReflectionService reflectionService) {
this.type = type;
this.reflectionService = reflectionService;
try {
String value = TypeHelper.toClass(type).getAnnotation(ReflectionService.PassThrough.class).value();
ObjectClassMeta objectClassMeta = new ObjectClassMeta(type, reflectionService);
this.instantiatorDefinition = objectClassMeta.getInstantiatorDefinitions().get(0);
PropertyFinder.PropertyFilter predicate = PropertyFinder.PropertyFilter.trueFilter();
this.propertyMeta = objectClassMeta
.newPropertyFinder()
.findProperty(DefaultPropertyNameMatcher.of(value), new Object[0], (TypeAffinity) null, predicate);
this.innerMeta = propertyMeta.getPropertyClassMeta();
} catch(Exception e) {
ErrorHelper.rethrow(e);
throw new IllegalStateException();
}
}
public PassThroughClassMeta(ReflectionService reflectionService, Type type, InstantiatorDefinition instantiatorDefinition, PropertyMeta propertyMeta, ClassMeta innerMeta) {
this.reflectionService = reflectionService;
this.type = type;
this.instantiatorDefinition = instantiatorDefinition;
this.propertyMeta = propertyMeta;
this.innerMeta = innerMeta;
}
@Override
public ClassMeta withReflectionService(ReflectionService reflectionService) {
return new PassThroughClassMeta(reflectionService, type, instantiatorDefinition, propertyMeta.withReflectionService(reflectionService), reflectionService.getClassMeta(innerMeta.getType()));
}
@Override
public ReflectionService getReflectionService() {
return reflectionService;
}
@Override
public PropertyFinder newPropertyFinder() {
return new PassThroughPropertyFinder(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;
}
}