Here you can find the source of getAnnotation(final Member member, final Class annotation)
member
has the annotation
.
Parameter | Description |
---|---|
member | the member |
annotation | the type of the annotation instance to return |
A | the class of annotation |
annotation
instance if the member
has the annotation or null
public static <A extends Annotation> A getAnnotation(final Member member, final Class<A> annotation)
//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); } }