Retrieves all fields from the class, including its super classes, filtering out only ones annotated by the given annotation. - Java java.lang.annotation

Java examples for java.lang.annotation:Field Annotation

Description

Retrieves all fields from the class, including its super classes, filtering out only ones annotated by the given annotation.

Demo Code

/**//from w  w w  .  j  a va2s.  com
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class beanClass = String.class;
        Class annotation = String.class;
        System.out.println(getAnnotatedFields(beanClass,annotation));
    }
    /**
     * Retrieves all fields from the class, including its super classes,
     * filtering out only ones annotated by the given annotation.
     * 
     * @param beanClass class whose fields will be retrieved
     * @param annotation class to look for
     * @param <A> annotation
     * 
     * @return fields marked by the given annotation
     */
    public static <A extends Annotation> List<AnnotatedField<A>> getAnnotatedFields(
            Class<?> beanClass, Class<A> annotation) {
        List<AnnotatedField<A>> fields = new ArrayList<AnnotatedField<A>>();

        for (Field field : getAllFields(beanClass)) {
            for (Annotation a : field.getAnnotations()) {
                if (annotation.isInstance(a)) {
                    @SuppressWarnings("unchecked")
                    A casted = (A) a;
                    fields.add(new AnnotatedField<A>(field, casted));
                }
            }
        }

        return fields;
    }
    /**
     * Retrieves all fields from the class, including its super classes. <br>
     * 
     * <i>Leaves out all static fields.</i>
     * 
     * @param beanClass class whose fields will be extracted
     * @return list of extracted fields
     */
    public static List<Field> getAllFields(Class<?> beanClass) {
        List<Class<?>> allClasses = getClassHierarchy(beanClass);

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

        for (Class<?> current : allClasses) {
            for (Field field : current.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    fields.add(field);
                }
            }
        }

        return fields;
    }
    /**
     * Creates all class hierarchy from the given class to Object,
     * <b>exclusive</b>, i.e. Object won't go to the list). Interfaces won't be
     * listed in the result.
     * 
     * @param beanClass class for building its hierarchy
     * @return list of all classes from the given class up to {@link Object}
     */
    public static List<Class<?>> getClassHierarchy(Class<?> beanClass) {
        List<Class<?>> classes = new ArrayList<Class<?>>();

        Class<?> current = beanClass;
        while (current != Object.class) {
            classes.add(current);
            current = current.getSuperclass();
        }

        return classes;
    }
}

Related Tutorials