org.simpleflatmapper.reflect.meta.SubPropertyMeta Maven / Gradle / Ivy
Show all versions of sfm-reflect Show documentation
package org.simpleflatmapper.reflect.meta;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.getter.GetterOnGetter;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.getter.NullGetter;
import org.simpleflatmapper.reflect.setter.NullSetter;
import java.lang.reflect.Type;
/**
*
* @param the root property owner type
* @param the intermediate owner type
* @param the property type
*/
public class SubPropertyMeta extends PropertyMeta {
private final PropertyMeta ownerProperty;
private final PropertyMeta subProperty;
public SubPropertyMeta(ReflectionService reflectService, PropertyMeta ownerProperty, PropertyMeta subProperty) {
super(ownerProperty.getName(), ownerProperty.getOwnerType(), reflectService);
this.ownerProperty = ownerProperty;
this.subProperty = subProperty;
}
@Override
public PropertyMeta withReflectionService(ReflectionService reflectionService) {
return new SubPropertyMeta(reflectionService, ownerProperty.withReflectionService(reflectionService), subProperty.withReflectionService(reflectionService));
}
@SuppressWarnings("unchecked")
@Override
public Setter getSetter() {
if (subProperty.isSelf()) {
return (Setter) ownerProperty.getSetter();
}
if (!NullSetter.isNull(subProperty.getSetter()) && !NullGetter.isNull(ownerProperty.getGetter())) {
return new Setter() {
@Override
public void set(O target, P value) throws Exception {
subProperty.getSetter().set(ownerProperty.getGetter().get(target), value);
}
};
} else {
return (Setter) NullSetter.NULL_SETTER;
}
}
@Override
public Getter getGetter() {
if (subProperty.isSelf()) return (Getter) ownerProperty.getGetter();
return new GetterOnGetter(this.ownerProperty.getGetter(), subProperty.getGetter());
}
@Override
protected ClassMeta newPropertyClassMeta() {
return subProperty.getPropertyClassMeta();
}
@Override
public Type getPropertyType() {
return subProperty.getPropertyType();
}
@Override
public int typeAffinityScore(PropertyFinder.TypeAffinityScorer typeAffinityScorer) {
return subProperty.typeAffinityScore(typeAffinityScorer);
}
public PropertyMeta getOwnerProperty() {
return ownerProperty;
}
public PropertyMeta getSubProperty() {
return subProperty;
}
@Override
public boolean isSubProperty() {
return true;
}
public boolean isNonMapped() {
return subProperty.isNonMapped();
}
@Override
public boolean isValid() {
return subProperty.isValid();
}
@Override
public String getPath() {
String subPath = subProperty.getPath();
String subPathPrefix = subPath.startsWith("[") ? "" : ".";
return getOwnerProperty().getPath() + subPathPrefix + subPath;
}
@Override
public Object[] getDefinedProperties() {
return subProperty.getDefinedProperties();
}
@Override
public PropertyMeta toNonMapped() {
return new SubPropertyMeta(reflectService, ownerProperty, subProperty.toNonMapped());
}
@Override
public PropertyMeta compressSubSelf() {
if (subProperty.isSelf()) return (PropertyMeta) ownerProperty;
return new SubPropertyMeta(reflectService, ownerProperty, subProperty.compressSubSelf());
}
@Override
public String toString() {
return "SubPropertyMeta{" +
"ownerProperty=" + ownerProperty +
", subProperty=" + subProperty +
'}';
}
}