Close

Java Reflection - Field.isAnnotationPresent() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

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.field;

import java.lang.reflect.Field;

public class IsAnnotationPresentExample3 {
private static class Test {
@Deprecated
private String str;
}

public static void main(String... args) throws NoSuchFieldException {
Field f = Test.class.getDeclaredField("str");
boolean b = f.isAnnotationPresent(Deprecated.class);
System.out.println(b);
}
}

Output

true




See Also