Java Reflection Annotation getAnnotation(final Member member, final Class annotation)

Here you can find the source of getAnnotation(final Member member, final Class annotation)

Description

Returns the annotation instance if the member has the annotation.

License

Open Source License

Parameter

Parameter Description
member the member
annotation the type of the annotation instance to return
A the class of annotation

Return

the annotation instance if the member has the annotation or null

Declaration

public static <A extends Annotation> A getAnnotation(final Member member, final Class<A> annotation) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w .j a va2s .c  o m*/
 * Copyright (c) 2012-2013, Batu Alp Ceylan
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

public class Main {
    /**
     * Returns the annotation instance if the <code>member</code> has the <code>annotation</code>.
     * 
     * @param member
     *            the member
     * @param annotation
     *            the type of the annotation instance to return
     * @return the <code>annotation</code> instance if the <code>member</code> has the annotation or null
     * 
     * @param <A>
     *            the class of annotation
     * 
     * @since 2.0.1
     */
    public static <A extends Annotation> A getAnnotation(final Member member, final Class<A> annotation) {
        if (member instanceof Field) {
            return ((Field) member).getAnnotation(annotation);
        }

        if (member instanceof Method) {
            return ((Method) member).getAnnotation(annotation);
        }

        throw new IllegalArgumentException("Member is neither field nor method: " + member);
    }
}

Related

  1. getAnnotation(final Class c, final Class annClass)
  2. getAnnotation(final Class annotatedClass, final Class annotationClass)
  3. getAnnotation(final Class type, final Class annotation)
  4. getAnnotation(final Class reference, final AccessibleObject obj)
  5. getAnnotation(final Class annotationClass, final AnnotatedElement... elements)
  6. getAnnotation(final Method method, final Class annotationClass)
  7. getAnnotation(final Object obj, final Class annoType)
  8. getAnnotation(final Object object, final Class annotationClass)
  9. getAnnotation(Member m, Class annotationClass)