com.g2forge.alexandria.reflection.object.implementations.JavaFieldReflection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ax-reflection Show documentation
Show all versions of ax-reflection Show documentation
Generic type safe reflection library, which adds significant usability over the standard Java runtime.
package com.g2forge.alexandria.reflection.object.implementations;
import java.lang.reflect.Field;
import com.g2forge.alexandria.generic.type.java.member.IJavaFieldType;
import com.g2forge.alexandria.generic.type.java.type.implementations.ReflectionException;
import com.g2forge.alexandria.java.adt.tuple.ITuple1GS;
import com.g2forge.alexandria.reflection.object.AJavaMemberReflection;
import com.g2forge.alexandria.reflection.object.IJavaFieldReflection;
import com.g2forge.alexandria.reflection.object.IJavaTypeReflection;
import com.g2forge.alexandria.reflection.object.HReflection;
public class JavaFieldReflection extends AJavaMemberReflectionimplements IJavaFieldReflection {
public JavaFieldReflection(IJavaFieldType type) {
super(type);
}
@Override
public ITuple1GS getAccessor(final T object) {
final Field field = getType().getJavaMember();
field.setAccessible(true);
return new ITuple1GS() {
@Override
public F get0() {
try {
@SuppressWarnings("unchecked")
final F retVal = (F) field.get(object);
return retVal;
} catch (IllegalArgumentException | IllegalAccessException exception) {
throw new ReflectionException(exception);
}
}
@Override
public ITuple1GS set0(F value) {
try {
field.set(object, value);
} catch (IllegalArgumentException | IllegalAccessException exception) {
throw new ReflectionException(exception);
}
return this;
}
};
}
@Override
public IJavaTypeReflection getFieldType() {
return HReflection.toReflection(getType().getJavaMember().getGenericType());
}
}