Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package pl.jalokim.utils.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static pl.jalokim.utils.collection.CollectionUtils.mapToList;
import static pl.jalokim.utils.reflection.MetadataReflectionUtils.getField;
import static pl.jalokim.utils.reflection.MetadataReflectionUtils.getMethod;
/**
* Set of utils methods for set value, get value for field, invoke methods etc by reflection.
*/
@SuppressWarnings({"SuppressWarnings", "unchecked"})
public final class InvokableReflectionUtils {
private InvokableReflectionUtils() {
}
/**
* Setup new value for target object for given field name.
* It will looks in whole hierarchy if find first match field name then will change value.
* It not check that field and new value will have the same type.
*
* @param targetObject reference for object for which will be changed value
* @param fieldName field name
* @param newValue new value for set.
*/
public static void setValueForField(Object targetObject, String fieldName, Object newValue) {
setValueForField(targetObject, targetObject.getClass(), fieldName, newValue);
}
/**
* Setup new value for target object for given field name.
* It will looks in whole hierarchy but it will start from targetClass if find first match field name then will change value.
* It not check that field and new value will have the same type.
* It can't override primitive final fields and final String fields.
*
* Some interesting links below:
* * @see Stackoverflow thread: Change private static final field using Java reflection
* * @see Constant Expressions
*
* @param targetObject reference for object for which will be changed value
* @param targetClass target class from which will start looking for field with that name.
* @param fieldName field name
* @param newValue new value for set.
*/
public static void setValueForField(Object targetObject, Class> targetClass,
String fieldName, Object newValue) {
Field foundField = getField(targetClass, fieldName);
try {
Field modifiersField = null;
int oldModifiers = -1;
if (Modifier.isFinal(foundField.getModifiers())) {
modifiersField = Field.class.getDeclaredField("modifiers");
oldModifiers = foundField.getModifiers();
modifiersField.setAccessible(true);
modifiersField.setInt(foundField, foundField.getModifiers() & ~Modifier.FINAL);
}
foundField.setAccessible(true);
foundField.set(targetObject, newValue);
if (modifiersField != null) {
modifiersField.setInt(foundField, oldModifiers);
modifiersField.setAccessible(false);
}
foundField.setAccessible(false);
} catch (Exception e) {
throw new ReflectionOperationException(e);
}
}
/**
* It can't override primitive static final fields and static final String fields.
*
* Some interesting links below:
* * @see Stackoverflow thread: Change private static final field using Java reflection
* * @see Constant Expressions
*
* @param targetClass class for which will be changed static field
* @param fieldName name of static field
* @param newValue new value for static field.
*/
public static void setValueForStaticField(Class> targetClass,
String fieldName, Object newValue) {
setValueForField(null, targetClass, fieldName, newValue);
}
/**
* It gets value for given field name from target object.
* It looks in whole class hierarchy for target object.
*
* @param targetObject target object
* @param fieldName field name
* @param expected return type
* @return value from field.
*/
public static T getValueOfField(Object targetObject, String fieldName) {
return getValueOfField(targetObject, targetObject.getClass(), fieldName);
}
/**
* It gets value for given field name from target object.
* It looks in whole class hierarchy but starts from target class.
*
* @param targetObject target object
* @param targetClass target class
* @param fieldName field name
* @param expected return type
* @return value from field.
*/
public static T getValueOfField(Object targetObject, Class targetClass, String fieldName) {
Field field = getField(targetClass, fieldName);
if (!Modifier.isStatic(field.getModifiers()) && targetObject == null) {
throw new ReflectionOperationException("Cannot find non static field on null target object");
}
try {
field.setAccessible(true);
Object result = field.get(targetObject);
field.setAccessible(false);
return (T) result;
} catch (IllegalAccessException e) {
throw new ReflectionOperationException(e);
}
}
/**
* It gets value for static field by name from target class.
* It starts looking for field from target class up in hierarchy.
*
* @param targetClass target class
* @param fieldName field name
* @param expected return type
* @return value from field.
*/
public static T getValueForStaticField(Class targetClass, String fieldName) {
return getValueOfField(null, targetClass, fieldName);
}
/**
* Invokes first match method in hierarchy of target object which has arguments with explicitly provided
* types, and invoke method with list of arguments.
* If target is null, then it will invoke it as static method.
*
* @param target on that instance will be invoked method
* @param methodName name of method.
* @param argClasses explicit list of classes of arguments.
* @param args list of arguments for method.
* @param type of returned object.
* @return return result of invoked method.
*/
public static T invokeMethod(Object target, String methodName,
List> argClasses, List