Java Reflection Annotation getAnnotationClass(Class entityClass, Class annotationClass)

Here you can find the source of getAnnotationClass(Class entityClass, Class annotationClass)

Description

Return a specific annotation class if exist in class, otherwise return false

License

Apache License

Parameter

Parameter Description
entityClass a parameter
annotationClass a parameter

Return

a specific annotation class if exist in class, otherwise return false

Declaration

public static final <T extends Annotation> T getAnnotationClass(Class<?> entityClass,
        Class<T> annotationClass) 

Method Source Code


//package com.java2s;
/*/*from  w  ww.ja va2s  .c  o  m*/
 * Copyright 2015 Miguel Augusto Caligares <mcaligares@gmail.com>
 * 
 * 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;

public class Main {
    /**
     * Return a specific annotation class if exist in class, otherwise return false
     * 
     * @param entityClass
     * @param annotationClass
     * @return a specific annotation class if exist in class, otherwise return false
     * @see {@link #hasAnnotationClass(Class, Class)}
     */
    public static final <T extends Annotation> T getAnnotationClass(Class<?> entityClass,
            Class<T> annotationClass) {
        return hasAnnotationClass(entityClass, annotationClass) ? entityClass.getAnnotation(annotationClass) : null;
    }

    /**
     * An convenience method, equivalent to {@link #hasAnnotationClass(Class, Class)}
     */
    public static final <T extends Annotation> boolean hasAnnotationClass(Object object, Class<T> annotationClass) {
        return object != null ? hasAnnotationClass(object.getClass(), annotationClass) : null;
    }

    /**
     * Return true if class object has a specific annotation class, otherwise return false
     * 
     * @param entityClass
     * @param annotationClass
     * @return Return true if class object has a specific annotation class, otherwise return false
     * @see {@link #hasAnnotation(Field, Class)}
     */
    public static final <T extends Annotation> boolean hasAnnotationClass(Class<?> entityClass,
            Class<T> annotationClass) {
        return entityClass != null && entityClass.isAnnotationPresent(annotationClass);
    }
}

Related

  1. getAnnotationByType(AnnotatedElement element, Class annotationType)
  2. getAnnotationByType(List annotations, Class annotationType)
  3. getAnnotationClass(Annotation a)
  4. getAnnotationClass(Annotation annotation)
  5. getAnnotationClass(Class clazz, Class annotation)
  6. getAnnotationClass(String name)
  7. getAnnotationDeep(Annotation from, Class toFind)
  8. getAnnotationDefault(Class annotationClass, String element)
  9. getAnnotationDefaultMap(Class annotationClass)