/** * Convenience method to get a field from a class type. * <p> * The method will first try to look for a declared field in the same class. * If the field is not declared in this class it will look for the field in * the super class. This will continue throughout the whole class hierarchy. * If the field is not found an {@link IllegalArgumentException} is thrown. * * @param type * The type of the class where the method is located. * @param fieldName * The method names. * @return A {@code java.lang.reflect.Field}. * @throws FieldNotFoundException * If a field cannot be found in the hierarchy. */ public static Field getField(Class<?> type, String fieldName) { return WhiteboxImpl.getField(type, fieldName); }
/** * Convenience method to get a field from a class type. * <p> * The method will first try to look for a declared field in the same class. * If the field is not declared in this class it will look for the field in * the super class. This will continue throughout the whole class hierarchy. * If the field is not found an {@link IllegalArgumentException} is thrown. * * @param type * The type of the class where the method is located. * @param fieldName * The method names. * @return A <code>java.lang.reflect.Field</code>. * @throws FieldNotFoundException * If a field cannot be found in the hierarchy. */ public static Field getField(Class<?> type, String fieldName) { return WhiteboxImpl.getField(type, fieldName); }
/** * Set the value of a field using reflection. Use this method when you need * to specify in which class the field is declared. This is useful if you * have two fields in a class hierarchy that has the same name but you like * to modify the latter. * * @param object the object to modify * @param fieldName the name of the field * @param value the new value of the field * @param where which class the field is defined */ public static void setInternalState(Object object, String fieldName, Object value, Class<?> where) { if (object == null || fieldName == null || fieldName.equals("") || fieldName.startsWith(" ")) { throw new IllegalArgumentException("object, field name, and \"where\" must not be empty or null."); } final Field field = getField(fieldName, where); try { field.set(object, value); } catch (Exception e) { throw new RuntimeException("Internal Error: Failed to set field in method setInternalState.", e); } }
/** * Set the value of a field using reflection. Use this method when you need * to specify in which class the field is declared. This is useful if you * have two fields in a class hierarchy that has the same name but you like * to modify the latter. * * @param object * the object to modify * @param fieldName * the name of the field * @param value * the new value of the field * @param where * which class the field is defined */ public static void setInternalState(Object object, String fieldName, Object value, Class<?> where) { if (object == null || fieldName == null || fieldName.equals("") || fieldName.startsWith(" ")) { throw new IllegalArgumentException("object, field name, and \"where\" must not be empty or null."); } final Field field = getField(fieldName, where); try { field.set(object, value); } catch (Exception e) { throw new RuntimeException("Internal Error: Failed to set field in method setInternalState.", e); } }
@SuppressWarnings("UnusedDeclaration") public static Object fieldCall(Object instanceOrClassContainingTheField, Class<?> classDefiningField, String fieldName, Class<?> fieldType) { if (MockRepository.shouldSuppressField(WhiteboxImpl.getField(classDefiningField, fieldName))) { return TypeUtils.getDefaultValue(fieldType); } return PROCEED; }