Close

Java Reflection - Method.isAnnotationPresent() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)

Returns true if an annotation for the specified type is present on this element, else false.


Examples


package com.logicbig.example.method;

import java.lang.reflect.Method;

public class IsAnnotationPresentExample {
private static class Test {
@Deprecated
private void process() {
}

private void init() {}
}

public static void main(String... args) throws NoSuchMethodException {
Method m = Test.class.getDeclaredMethod("process");
boolean b = m.isAnnotationPresent(Deprecated.class);
System.out.println(b);

Method m2 = Test.class.getDeclaredMethod("init");
boolean b2 = m2.isAnnotationPresent(Deprecated.class);
System.out.println(b2);
}
}

Output

true
false




See Also