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 长辈见面花束祝福语简短