pl.jalokim.propertiestojson.util.ReflectionUtils Maven / Gradle / Ivy
package pl.jalokim.propertiestojson.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unchecked")
public class ReflectionUtils {
public static void setValue(Object target, String fieldName, Object value) {
Field field = null;
Class currentClass = target.getClass();
List noSuchFieldExceptions = new ArrayList<>();
while(currentClass != null) {
try {
field = currentClass.getDeclaredField(fieldName);
} catch(NoSuchFieldException e) {
noSuchFieldExceptions.add(e.getMessage());
}
if(field != null) {
break;
}
currentClass = currentClass.getSuperclass();
}
if(field == null) {
throw new RuntimeException(new NoSuchFieldException(noSuchFieldExceptions.toString()));
}
try {
field.setAccessible(true);
field.set(target, value);
field.setAccessible(false);
} catch(ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public static T invokeMethod(Object target, String methodName,
List> argClasses, List