/** * Sets the value of the named static field. * * @param c the class containing the static field * @param fieldName the name of the field */ public static void setStaticValue(Class c, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = getField(c, fieldName); field.setAccessible(true); field.set(null, value); }
/** * Gets the value of the named static field and returns it as an object. * * @param c the class containing the static field * @param fieldName the name of the field * @return an object representing the value of the field */ public static Object getStaticValue(Class c, String fieldName) throws IllegalAccessException, NoSuchFieldException { Field field = getField(c, fieldName); field.setAccessible(true); return field.get(null); }
public static void setValue(Object instance, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = getField(instance.getClass(), fieldName); field.setAccessible(true); field.set(instance, value); }
/** * Gets the value of the named field and returns it as an object. * * @param instance the object instance * @param fieldName the name of the field * @return an object representing the value of the field */ public static Object getValue(Object instance, String fieldName) throws IllegalAccessException, NoSuchFieldException { Field field = getField(instance.getClass(), fieldName); field.setAccessible(true); return field.get(instance); }
/** * Return the named field from the given class. * @param thisClass The class * @param fieldName The field name */ public static Field getField(Class thisClass, String fieldName) throws NoSuchFieldException { if (thisClass == null) throw new NoSuchFieldException("Invalid field : " + fieldName); try { return thisClass.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { return getField(thisClass.getSuperclass(), fieldName); } }