org.polkadot.common.ReflectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of polkadot-java Show documentation
Show all versions of polkadot-java Show documentation
Java Polkadot API, this is a clone of https://github.com/polkadot-java/api
The newest version!
package org.polkadot.common;
import java.lang.reflect.Field;
public class ReflectionUtils {
/**
* Find DeclaredField recursively.
*
* @param object : the child object
* @param fieldName : the field name in the parent object
* @return the field object in the parent object
*/
public static Field getDeclaredField(Object object, String fieldName) {
Field field = null;
Class> clazz = object.getClass();
for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
try {
field = clazz.getDeclaredField(fieldName);
return field;
} catch (Exception e) {
}
}
return null;
}
/**
* Read field value directly, ignore private/protected and getter
*
* @param object : the child object
* @param fieldName : the field name in the parent object
* @return : the field value in the parent object
*/
public static T getField(Object object, String fieldName) {
Field field = getDeclaredField(object, fieldName);
field.setAccessible(true);
try {
return (T) field.get(object);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}