@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field named \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", fieldName, type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field named \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", fieldName, type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s fields were declared in %s.", isInstanceField ? "instance" : "static", type.getName())); } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s fields were declared in %s.", isInstanceField ? "instance" : "static", type.getName())); } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field of type \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field of type \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field assignable from \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field assignable from \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException("No field that has any of the annotation types \"" + getAnnotationNames() + "\" could be found in the class hierarchy of " + type.getName() + "."); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException("No field that has any of the annotation types \"" + getAnnotationNames() + "\" could be found in the class hierarchy of " + type.getName() + "."); }
/** * Throw exception if field was not found. * * @param type the type * @param fieldName the field name * @param field the field */ public static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field) { if (field == null) { throw new FieldNotFoundException("No field was found with name '" + fieldName + "' in class " + getOriginalUnmockedType(type).getName() + "."); } }
/** * Gets the field. * * @param fieldName the field name * @param where the where * @return the field */ private static Field getField(String fieldName, Class<?> where) { if (where == null) { throw new IllegalArgumentException("where cannot be null"); } Field field = null; try { field = where.getDeclaredField(fieldName); field.setAccessible(true); } catch (NoSuchFieldException e) { throw new FieldNotFoundException("Field '" + fieldName + "' was not found in class " + where.getName() + "."); } return field; }
/** * Gets the field. * * @param fieldName * the field name * @param where * the where * @return the field */ private static Field getField(String fieldName, Class<?> where) { if (where == null) { throw new IllegalArgumentException("where cannot be null"); } Field field = null; try { field = where.getDeclaredField(fieldName); field.setAccessible(true); } catch (NoSuchFieldException e) { throw new FieldNotFoundException("Field '" + fieldName + "' was not found in class " + where.getName() + "."); } return field; }
/** * Throw exception if field was not found. * * @param type * the type * @param fieldName * the field name * @param field * the field */ public static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field) { if (field == null) { throw new FieldNotFoundException("No field was found with name '" + fieldName + "' in class " + getUnmockedType(type).getName() + "."); } }
/** * Find field or throw exception. * * @param fieldType * the field type * @param where * the where * @return the field */ private static Field findFieldOrThrowException(Class<?> fieldType, Class<?> where) { if (fieldType == null || where == null) { throw new IllegalArgumentException("fieldType and where cannot be null"); } Field field = null; for (Field currentField : where.getDeclaredFields()) { currentField.setAccessible(true); if (currentField.getType().equals(fieldType)) { field = currentField; break; } } if (field == null) { throw new FieldNotFoundException("Cannot find a field of type " + fieldType + "in where."); } return field; }
/** * Find field or throw exception. * * @param fieldType the field type * @param where the where * @return the field */ private static Field findFieldOrThrowException(Class<?> fieldType, Class<?> where) { if (fieldType == null || where == null) { throw new IllegalArgumentException("fieldType and where cannot be null"); } Field field = null; for (Field currentField : where.getDeclaredFields()) { currentField.setAccessible(true); if (currentField.getType().equals(fieldType)) { field = currentField; break; } } if (field == null) { throw new FieldNotFoundException("Cannot find a field of type " + fieldType + "in where."); } return field; }
/** * Get the value of a field using reflection. Use this method when you need * to specify in which class the field is declared. This might be useful * when you have mocked the instance you are trying to access. Use this * method to avoid casting. * * @param <T> the expected type of the field * @param object the object to modify * @param fieldName the name of the field * @param where which class the field is defined * @return the internal state */ @SuppressWarnings("unchecked") public static <T> T getInternalState(Object object, String fieldName, 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."); } Field field = null; try { field = where.getDeclaredField(fieldName); field.setAccessible(true); return (T) field.get(object); } catch (NoSuchFieldException e) { throw new FieldNotFoundException("Field '" + fieldName + "' was not found in class " + where.getName() + "."); } catch (Exception e) { throw new RuntimeException("Internal error: Failed to get field in method getInternalState.", e); } }
return (T) field.get(object); } catch (NoSuchFieldException e) { throw new FieldNotFoundException("Field '" + fieldName + "' was not found in class " + where.getName() + "."); } catch (Exception e) {
/** * Get an array of {@link Field}'s that matches the supplied list of field * names. Both instance and static fields are taken into account. * * @param clazz The class that should contain the fields. * @param fieldNames Names of the fields that will be returned. * @return An array of Field's. May be of length 0 but not . */ public static Field[] getFields(Class<?> clazz, String... fieldNames) { final List<Field> fields = new LinkedList<Field>(); for (Field field : getAllFields(clazz)) { for (String fieldName : fieldNames) { if (field.getName().equals(fieldName)) { fields.add(field); } } } final Field[] fieldArray = fields.toArray(new Field[fields.size()]); if (fieldArray.length == 0) { throw new FieldNotFoundException(String.format( "No fields matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(fieldNames), getType(clazz))); } return fieldArray; }
/** * Get an array of {@link Field}'s that matches the supplied list of field * names. Both instance and static fields are taken into account. * * @param clazz * The class that should contain the fields. * @param fieldNames * Names of the fields that will be returned. * @return An array of Field's. May be of length 0 but not . */ public static Field[] getFields(Class<?> clazz, String... fieldNames) { final List<Field> fields = new LinkedList<Field>(); for (Field field : getAllFields(clazz)) { for (String fieldName : fieldNames) { if (field.getName().equals(fieldName)) { fields.add(field); } } } final Field[] fieldArray = fields.toArray(new Field[fields.size()]); if (fieldArray.length == 0) { throw new FieldNotFoundException(String.format( "No fields matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(fieldNames), getType(clazz))); } return fieldArray; }