public static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotation,
												 boolean checkSuperclass) {
		Field [] fields = clazz.getDeclaredFields();

		List<Field> result = new ArrayList<>();

		for (Field field : fields) {
			if (field.isAnnotationPresent(annotation)) {
				result.add(field);
			}
		}

		if (checkSuperclass && clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) {
			result.addAll(getAnnotatedFields(clazz.getSuperclass(), annotation, true));
		}

		return result;
	}
public static List < Field > getAnnotatedFields ( Class < ? > clazz , Class < ? extends Annotation > annotation , boolean checkSuperclass ) { Field [ ] fields = clazz . getDeclaredFields ( ) ; List < Field > result = new ArrayList < > ( ) ; for ( Field field : fields ) { if ( field . isAnnotationPresent ( annotation ) ) { result . add ( field ) ; } } if ( checkSuperclass && clazz . getSuperclass ( ) != null && ! clazz . getSuperclass ( ) . equals ( Object . class ) ) { result . addAll ( getAnnotatedFields ( clazz . getSuperclass ( ) , annotation , true ) ) ; } return result ; }
Returns all field from a given class that are annotated with provided annotation type.
@param clazz source class
@param annotation target annotation
@param checkSuperclass if true, method will follow class hierarchy to look for fields with target annotation
@return list of fields or empty collection in case no fields were found