Returns true if an annotation for the specified annotation Type is declared locally on the supplied clazz, else false. - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Type

Description

Returns true if an annotation for the specified annotation Type is declared locally on the supplied clazz, else false.

Demo Code

/*/*from  w w  w  .  j a va  2s .  c o  m*/
 * Copyright 2002-2007 the original author or authors.
 *
 * 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.
 */
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main{

    /**
     * <p>
     * Returns <code>true</code> if an annotation for the specified
     * <code>annotationType</code> is declared locally on the supplied
     * <code>clazz</code>, else <code>false</code>. The supplied
     * {@link Class} object may represent any type.
     * </p>
     * <p>
     * Note: this method does <strong>not</strong> determine if the annotation
     * is {@link java.lang.annotation.Inherited inherited}. For greater clarity
     * regarding inherited annotations, consider using
     * {@link #isAnnotationInherited(Class, Class)} instead.
     * </p>
     *
     * @param annotationType the Class object corresponding to the annotation type
     * @param clazz          the Class object corresponding to the class on which to
     *                       check for the annotation
     *
     * @return <code>true</code> if an annotation for the specified
     *         <code>annotationType</code> is declared locally on the supplied <code>clazz</code>
     *
     * @see Class#getDeclaredAnnotations()
     * @see #isAnnotationInherited(Class, Class)
     */
    public static boolean isAnnotationDeclaredLocally(
            final Class<? extends Annotation> annotationType,
            final Class<?> clazz) {
        Assert.notNull(annotationType, "annotationType must not be null");
        Assert.notNull(clazz, "clazz must not be null");
        boolean declaredLocally = false;
        for (final Annotation annotation : Arrays.asList(clazz
                .getDeclaredAnnotations())) {
            if (annotation.annotationType().equals(annotationType)) {
                declaredLocally = true;
                break;
            }
        }
        return declaredLocally;
    }
}

Related Tutorials