find annotated Methods Recursively - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

find annotated Methods Recursively

Demo Code

/*/*  w w w .  j av a 2s .c  o  m*/
 * 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 clazz = String.class;
        Class annotationClass = String.class;
        List methods = java.util.Arrays.asList("asdf", "java2s.com");
        findRecursively(clazz, annotationClass, methods);
    }

    /**
     * 
     * @deprecated since 2.0.4 use getAnnotatedMethods
     */
    @Deprecated
    public static void findRecursively(Class clazz,
            Class<? extends Annotation> annotationClass,
            List<Method> methods) {
        for (Method m : clazz.getDeclaredMethods()) {
            if (m.getAnnotation(annotationClass) != null) {
                methods.add(0, m);
            }
        }
        if (clazz.getSuperclass() != Object.class) {
            findRecursively(clazz.getSuperclass(), annotationClass, methods);
        }
    }
}

Related Tutorials