Returns true if the specified class contains any public method annotated with any of the specified annotations. - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

Returns true if the specified class contains any public method annotated with any of the specified annotations.

Demo Code

/*// w  ww .j av  a 2s  .co  m
 * Copyright (c) 2008 Kasper Nielsen.
 *
 * 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;

public class Main {


    /**
     * Returns <tt>true</tt> if the specified class contains any public method annotated with any of the specified
     * annotations.
     * 
     * @param type
     *            the class to check
     * @param annotations
     *            the annotations to look for
     * @return true if the class has a public method with any of the annotations
     */
    @SafeVarargs
    public static boolean hasAnyAnnotation(Class<?> type,
            Class<? extends Annotation>... annotations) {
        for (Method m : type.getMethods()) {
            if (hasAnyAnnotation(m, annotations)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns <tt>true</tt> if the specified method has any of the specified annotations
     * 
     * @param m
     *            the method to test
     * @param annotations
     *            the annotations to look for
     * @return true if the method contains any of the annotations
     */
    @SafeVarargs
    public static boolean hasAnyAnnotation(Method m,
            Class<? extends Annotation>... annotations) {
        for (Class<? extends Annotation> annotation : annotations) {
            if (m.isAnnotationPresent(annotation)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials