
com.spikeify.FieldMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Simple ORM for Aerospike
package com.spikeify;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
@SuppressWarnings({"unchecked", "WeakerAccess"})
public class FieldMapper {
public FieldMapper(String binName, Converter converter, Field field) {
this.binName = binName;
this.converter = converter;
this.field = field;
field.setAccessible(true);
this.field = field;
try {
this.getter = MethodHandles.lookup().unreflectGetter(field);
this.setter = MethodHandles.lookup().unreflectSetter(field);
} catch (ReflectiveOperationException e) {
throw new SpikeifyError(e);
}
}
public String getBinName() {
return binName;
}
public String getFieldName() {
return field.getName();
}
public P getPropertyValue(Object object) {
try {
F fieldValue = (F) field.get(object);
if (fieldValue == null) {
return null;
}
return converter.fromField(fieldValue);
} catch (IllegalAccessException e) {
throw new SpikeifyError(e); //todo nicer error
}
}
public void setFieldValue(Object targetObject, P propertyValue) {
try {
F value = converter.fromProperty(propertyValue);
if (!(value == null && field.getType().isPrimitive())) { // do not set value if primitive type, leave it default
field.set(targetObject, value);
}
} catch (IllegalAccessException e) {
throw new SpikeifyError(e); //todo nicer error
}
}
public F getFieldValue(P propertyValue) {
return converter.fromProperty(propertyValue);
}
public final String binName;
// public Class propType;
public final Converter converter;
// public Class fieldType;
public Field field;
public MethodHandle getter;
public MethodHandle setter;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy