Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.LinkedList; import java.util.List; public class Main { /** * Returns all fields declared in the class passed as argument or in its super classes annotated with * the supplied annotation. */ public static List<Field> getAllDeclaredField(Class<?> clazz, Class<? extends Annotation> annotationClass) { final List<Field> result = new LinkedList<Field>(); for (final Field field : clazz.getDeclaredFields()) { final Object jakeOption = field.getAnnotation(annotationClass); if (jakeOption != null) { result.add(field); } } final Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { result.addAll(getAllDeclaredField(superClass, annotationClass)); } return result; } /** * Returns all fields declared in the class passed as argument or in its super classes. */ public static List<Field> getAllDeclaredField(Class<?> clazz, boolean includeSuperClass) { final List<Field> result = new LinkedList<Field>(); for (final Field field : clazz.getDeclaredFields()) { result.add(field); } final Class<?> superClass = clazz.getSuperclass(); if (superClass != null && includeSuperClass) { result.addAll(getAllDeclaredField(superClass, true)); } return result; } }