cc.voox.graphql.utils.BeanUtil Maven / Gradle / Ivy
The newest version!
package cc.voox.graphql.utils;
import org.springframework.util.Assert;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public final class BeanUtil {
public static Field getDeclaredField(final Class clazz,
final String fieldName) {
Assert.notNull(clazz);
Assert.hasText(fieldName);
for (Class superClass = clazz; superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
}
}
return null;
}
protected static Field getDeclaredField(final Object object,
final String fieldName) {
Assert.notNull(object);
return getDeclaredField(object.getClass(), fieldName);
}
protected static void makeAccessible(Field field) {
if (!Modifier.isPublic(field.getModifiers())
|| !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
field.setAccessible(true);
}
}
public static Object getFieldValue(final Object object,
final String fieldName) {
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field ["
+ fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy