Eclipse+Selenium+Java环境配置
2013-01-11 13:44
441 查看
告知编译程序如何处理@Retention:
java.lang.annotation.Retention型态可以在您定义Annotation型态时,指示编译程序该如何对待您的自定义Annotation型态。
预定义上编译程序会将Annotation信息留在.class文档中,但不被虚拟机读取,而仅用于编译程序或工具程序运行时提供信息。
java.lang.annotation.RetentionPolicy 有三个枚举类型:CLASS、RUNTIME、SOURCE
只有当Annotation被指示成RUNTIME时,在运行时通过反射机制才能被JVM读取,否则,JVM是读取不到这个Annotation的。
java 代码
package com.test;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface RetentionTest {
String hello() default "hello";
String world();
}
java 代码
package com.test;
public class MyTest {
@RetentionTest(hello = "beijing", world = "shanghai")
@Deprecated
@SuppressWarnings("unchecked")
public void output()
{
System.out.println("output");
}
}
java 代码
package com.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class ReflectRetentionTest {
public static void main(String[] args) throws Exception{
MyTest mt = new MyTest();
Class<mytest></mytest> c = MyTest.class;
Method method = c.getMethod("output",new Class[]{});
if(method.isAnnotationPresent(RetentionTest.class))
{
method.invoke(mt, new Object[]{});//output
RetentionTest retentionTest = method.getAnnotation(RetentionTest.class);
System.out.println(retentionTest.hello());//beijing
System.out.println(retentionTest.world());//shanghai
}
Annotation[] annotations = method.getAnnotations();
for(Annotation annotation: annotations)
{
System.out.println(annotation.annotationType().getName());
}
//for循环里输出的结果是com.test.RetentionTest以及java.lang.Deprecated,而没有出来java.lang.SuppressWarnings
//因为java.lang.SuppressWarnings的Retention是被设置成RetentionPolicy.SOURCE类型的,所以在运行时是不会被虚拟机读取的。
}
}
运行第三个程序
java.lang.annotation.Retention型态可以在您定义Annotation型态时,指示编译程序该如何对待您的自定义Annotation型态。
预定义上编译程序会将Annotation信息留在.class文档中,但不被虚拟机读取,而仅用于编译程序或工具程序运行时提供信息。
java.lang.annotation.RetentionPolicy 有三个枚举类型:CLASS、RUNTIME、SOURCE
只有当Annotation被指示成RUNTIME时,在运行时通过反射机制才能被JVM读取,否则,JVM是读取不到这个Annotation的。
java 代码
package com.test;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface RetentionTest {
String hello() default "hello";
String world();
}
java 代码
package com.test;
public class MyTest {
@RetentionTest(hello = "beijing", world = "shanghai")
@Deprecated
@SuppressWarnings("unchecked")
public void output()
{
System.out.println("output");
}
}
java 代码
package com.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class ReflectRetentionTest {
public static void main(String[] args) throws Exception{
MyTest mt = new MyTest();
Class<mytest></mytest> c = MyTest.class;
Method method = c.getMethod("output",new Class[]{});
if(method.isAnnotationPresent(RetentionTest.class))
{
method.invoke(mt, new Object[]{});//output
RetentionTest retentionTest = method.getAnnotation(RetentionTest.class);
System.out.println(retentionTest.hello());//beijing
System.out.println(retentionTest.world());//shanghai
}
Annotation[] annotations = method.getAnnotations();
for(Annotation annotation: annotations)
{
System.out.println(annotation.annotationType().getName());
}
//for循环里输出的结果是com.test.RetentionTest以及java.lang.Deprecated,而没有出来java.lang.SuppressWarnings
//因为java.lang.SuppressWarnings的Retention是被设置成RetentionPolicy.SOURCE类型的,所以在运行时是不会被虚拟机读取的。
}
}
运行第三个程序
相关文章推荐
- 配置ArcGIS Server 9.3的Java开发环境(eclipse)
- ubuntu14.0.4配置javaweb开发环境第三步安装eclipse
- 【java核心技术】开发环境配置-eclipse关联源码和文档
- Java + opencv学习:在Eclipse下配置基于Java的OpenCV开发环境
- Ubuntu配置Java+Eclipse+Google Plugin环境
- java1.6+tomcat5.5+mysql5.0+eclipse+myeclipse环境的配置
- Ubuntu配置Java+Eclipse+MyEclipse环境
- Java环境配置及eclipse单元测试
- Java集成开发环境--Eclipse for J2EE配置JRE运行环境
- Java开发环境:Eclipse+JDK配置
- Ubuntu配置Java+Eclipse+MyEclipse环境
- ubuntu 配置java开发环境(jdk+eclipse)
- java语言学习003_开发环境基础配置和安装(jdk,jre,eclipse&myeclipse)
- java,eclipse,tomcat,mysql,mysql-front的配置以及环境设置
- Eclipse开发环境配置(Java and C/C++)
- 如何搭建Eclipse +Apache Tomcat配置Java开发环境
- 关于安卓eclipse配置环境的无法正常产生R.java
- java环境配置与eclipse设置
- java eclipse 环境配置
- 搭建java开发环境+配置tomcat+使用eclipse