Java Package.isAnnotationPresent()方法
2016-12-08 16:15
453 查看
Java Package.isAnnotationPresent()方法用法实例教程。方法返回true,如果指定类型的注释存在于此元素上,否则返回false。这种方法的设计主要是为了方便访问标记注释
描述
java.lang.Package.isAnnotationPresent(Class<? extends Annotation> annotationClass) 方法返回true,如果指定类型的注释存在于此元素上,
否则返回false。这种方法的设计主要是为了方便访问标记注释
以下是声明java.lang.Package.isAnnotationPresent()方法
annotationClass --对应于注释类型的Class对象
这种方法指定注释类型的注释存在于此元素上,则返回true,否则返回false
NullPointerException -- 如果给定的注释类是null
下面的例子说明了如何使用lang.Object.isAnnotationPresent()方法。
让我们来编译和运行上面的程序,这将产生以下结果:
描述
java.lang.Package.isAnnotationPresent(Class<? extends Annotation> annotationClass) 方法返回true,如果指定类型的注释存在于此元素上,
否则返回false。这种方法的设计主要是为了方便访问标记注释
声明
以下是声明java.lang.Package.isAnnotationPresent()方法public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
参数
annotationClass --对应于注释类型的Class对象
返回值
这种方法指定注释类型的注释存在于此元素上,则返回true,否则返回false
异常
NullPointerException -- 如果给定的注释类是null
实例
下面的例子说明了如何使用lang.Object.isAnnotationPresent()方法。package com.yiibai; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; // declare a new annotation @Retention(RetentionPolicy.RUNTIME) @interface Demo { String str(); int val(); } public class PackageDemo { // set values for the annotation @Demo(str = "Demo Annotation", val = 100) // a method to call in the main public static void example() { PackageDemo ob = new PackageDemo(); try { Class c = ob.getClass(); // get the method example Method m = c.getMethod("example"); // get the annotation for class Demo Demo annotation = m.getAnnotation(Demo.class); // print the annotation System.out.println(annotation.str() + " " + annotation.val()); } catch (NoSuchMethodException exc) { exc.printStackTrace(); } } public static void main(String args[]) { example(); Package[] pack = Package.getPackages(); // check if annotation hello exists for (int i = 0; i < pack.length; i++) { System.out.println("" + pack[i].isAnnotationPresent(Demo.class)); } } }
让我们来编译和运行上面的程序,这将产生以下结果:
Demo Annotation 100 false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false
相关文章推荐
- Java Package.isAnnotationPresent()方法
- java.lang.Package.getAnnotation()方法实例
- [JAVA][String]StringUtils工具类的常用方法(isNotEmpty isNotBlank等)
- Package中遇到“ORA-20001: APP-FND-02902:Multi-Org profile option is required. ”错误的处理方法
- Your version is: java version "1.6.0_23". The correct version is: Java SE 1.6. 解决方法
- spring2.5报出tx:annotation-driven is not bound异常解决方法
- java.sql.SQLException: ResultSet is closed解决方法
- 软件防破解方法大全 IsDebuggerPresent
- Java 类在package中,编译找不到main class解决方法
- The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法
- java.lang.IllegalStateException: No FacesContext is available to process this request的另一种处理方法
- Linux使用yum安装时出现The program package-cleanup is found in the yum-utils package.解决方法
- 新建T5工程报错--java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
- an existing tansporter instance is currently uploading this package 解决方法
- 关于 Java源文件带有package的普通程序和Applet编译和运行时错误的处理方法
- java中的关键字package和命令行的编译方法
- 第一课 掌握java annotation特性与使用方法
- 出现java.lang.IllegalArgumentException: id to load is required for loading的解决方法
- 用Annotation,Future,动态代理实现自己的JAVA方法运行超时应用
- android TimerTask 的简单应用,以及java.lang.IllegalStateException: TimerTask is scheduled already错误的解决方法