Adds all methods with the specified Annotation of class clazz and its superclasses to all Fields - Java java.lang.annotation

Java examples for java.lang.annotation:Field Annotation

Description

Adds all methods with the specified Annotation of class clazz and its superclasses to all Fields

Demo Code

/*//from   www .  j a v  a  2s. c  om
 * Copyright 2002-2006,2009 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import java.util.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class annotationClass = String.class;
        Class clazz = String.class;
        List allMethods = java.util.Arrays.asList("asdf", "java2s.com");
        addAllMethods(annotationClass, clazz, allMethods);
    }

    /**
     * Adds all methods with the specified Annotation of class clazz and its superclasses to allFields
     *
     * @param annotationClass
     * @param clazz
     * @param allMethods
     */
    public static void addAllMethods(Class annotationClass, Class clazz,
            List<Method> allMethods) {

        if (clazz == null) {
            return;
        }

        Method[] methods = clazz.getDeclaredMethods();

        for (Method method : methods) {
            Annotation ann = method.getAnnotation(annotationClass);
            if (ann != null) {
                allMethods.add(method);
            }
        }
        addAllMethods(annotationClass, clazz.getSuperclass(), allMethods);
    }
}

Related Tutorials