Java包isAnnotationPresent()方法与示例
Package类isAnnotationPresent()方法
isAnnotationPresent()方法在java.lang包中可用。
当给定类型的注释存在于此实体上时,isAnnotationPresent()方法将返回true,否则将返回false。
isAnnotationPresent()方法是一个非静态方法,只能使用类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
isAnnotationPresent()方法在检查当前注释时可能会引发异常。
NullPointerException:在此异常中,当给定的注释类为null时。
语法:
public boolean isAnnotationPresent(Class ann_class);
参数:
Classan_class–表示与注释类型相似或对应的Class对象。
返回值:
此方法的返回类型为布尔值,它根据给定的情况返回以下值:
当给定类型的注释存在于此实体上时,它返回true。
当给定类型的注释不存在时,它返回false。
示例
//Java程序演示示例
//isAnnotationPresent(Class an_class)
//包装方式
import java.security.*;
public class IsAnnotationPresentOfClass {
public static void main(String[] args) throws Exception {
Class ann1 = Identity.class;
Class ann2 = Deprecated.class;
//我们正在检查已弃用的注解当前类型
//使用方法isAnnotationPresent()
boolean b1 = ann2.isAnnotationPresent(ann2);
System.out.println("is Deprecated an Annotation Present type" + " " + b1);
//我们正在检查Identity类的Annotation Present类型
//通过使用方法isAnnotationPresent()
boolean b2 = ann1.isAnnotationPresent(ann1);
System.out.println("is Deprecated an Annotation Present type" + " " + b2);
}
}输出结果
is Deprecated an Annotation Present type false is Deprecated an Annotation Present type false
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短