Java Package.isAnnotationPresent()方法
2017-08-07 11:11
981 查看
Java Package.isAnnotationPresent()方法
Java Package.isAnnotationPresent()方法用法实例教程。方法返回true,如果指定类型的注释存在于此元素上, 否则返回false。这种方法的设计主要是为了方便访问标记注释
描述
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()方法用法实例教程。方法返回true,如果指定类型的注释存在于此元素上, 否则返回false。这种方法的设计主要是为了方便访问标记注释
描述
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()方法实例
- Package中遇到“ORA-20001: APP-FND-02902:Multi-Org profile option is required. ”错误的处理方法
- 用Annotation,Future,动态代理实现自己的JAVA方法运行超时应用
- Flex+Java项目,Myeclipse环境下“Target runtime Apache Tomcat v6.0 is not defined ” 错误解决方法
- Java 出现java.lang.SecurityException: Prohibited package name:java.my 错误解决方法
- java读取properties 7种的方法2007-11-14 17:22package FileS;
- java中的关键字package和命令行的编译方法
- 解决Project facet Java version 6.0 is not supported的方法
- Your version is: java version "1.6.0_23". The correct version is: Java SE 1.6. 解决方法
- Linux使用yum安装时出现The program package-cleanup is found in the yum-utils package.解决方法
- Linux使用yum安装时出现The program package-cleanup is found in the yum-utils package.解决方法
- 【Java】安装Maven后,提示“Eclipse is running in a JRE, but a JDK is required”的解决方法
- Ubuntu升级之后The disk drive for / is not ready yet or not present的解决方法
- 关于 Java源文件带有package的普通程序和Applet编译和运行时错误的处理方法
- 出现java.lang.IllegalArgumentException: id to load is required for loading的解决方法
- 软件防破解方法大全 IsDebuggerPresent
- 新建T5工程报错--java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
- Your version is: java version "1.6.0_23". The correct version is: Java SE 1.6. 解决方法
- 第一课 掌握java annotation特性与使用方法