TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
java 反射是Java语言的一个很重要的特征。它允许运行中的 Java 程序对自身进行检查,并能直接操作程序的内部属性。例如,使用它能获得Java 类中各成员的名称并显示出来。
Java 反射机制主要提供了以下功能:
a.在运行时判断任意一个对象所属的类。
b.在运行时构造任意一个类的对象。
c.在运行时判断任意一个类所具有的成员变量和方法。
d.在运行时调用任意一个对象的方法。
在JDK中,主要由以下类来实现Java反射机制,这些类在java.lang.reflect包中:
Class类:代表一个类。
Field 类:代表类的成员变量(成员变量也称为类的属性)。
Method类:代表类的方法。
Constructor 类:代表类的构造方法。
Array类:提供了动态创建数组,以及访问数组的元素的静态方法。
下面写了一个程序:设计了一个POJO类。所谓POJO类,本人粗浅的理解即和JavaBean类似,只有字段和setter/getter方法。然后在主函数通过反射,在控制台打印该POJO类的所有字段和方法。
本人设计的POJO类为WorkerPOJO.java,然后另一个测试类为POJOReflection.java,在main函数中负责打印该类的所有字段和方法。程序见下:
WorkerPOJO.java:
package com.xpec.landon.trainjava.annotation;
/**
* POJO类,和JavaBean相似
* @author lvwenyong
*
*/
public class WorkerPOJO{
private String name;
private int age;
/**
* 用Annotation修饰
* @return 姓名
*/
@WorkerPOJOAnnotation(name = "landon",age = 22)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
POJOReflection.java:
package com.xpec.landon.trainjava.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Method;
/**
* 运用Java的反射机制,输出POJO类的字段和方法(新增了Annotation的修饰)
* @author lvwenyong
*
*/
public class POJOReflectionTest {
public static void main(String[] args)
{
try
{
//加载WorkPOJO,注意这里一定要写全类名,包括包名,因为包名是类名的一部分
Class pojo = Class.forName("com.xpec.landon.trainjava.annotation.WorkerPOJO");
//获取域的数组
Field []fieldList = pojo.getDeclaredFields();
//获取方法的数组
Method []methodList = pojo.getDeclaredMethods();
System.out.println("WorkerPOJO类的所有字段:");
System.out.println("修饰符" + " " + "类型" + " " + "字段名");
for(int i = 0;i < fieldList.length;i++)
{
Field field = fieldList;
//用下面的形式获取具体的修饰符
System.out.println(Modifier.toString(field.getModifiers()) + " " + field.getType() + " " + field.getName());
}
System.out.println();
System.out.println("WorkerPOJO类的所有方法(不包括annotation修饰的方法):");
for(int j = 0;j < methodList.length;j++)
{
Method method = methodList[j];
//判断方法是否被Annotation修饰
boolean methodAnnotation = method.isAnnotationPresent(WorkerPOJOAnnotation.class);
//如果被annotation修饰,则过滤掉该方法,即不输出
if(methodAnnotation)
{
continue;
}
//获取方法参数列表
Class parameters[] = method.getParameterTypes();
System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " " + method.getName() + " (");
for(int k = 0;k < parameters.length;k++)
{
System.out.print(parameters[k].toString());
}
System.out.println(")");
}
}
catch(ClassNotFoundException exception1)
{
exception1.printStackTrace();
}
}
}
下面是程序的一个运行截图:

可以看到,在WorkerPOJO类中引入了Annotation。下面,我们详细介绍一下Annotation:
在使用JUnit4中,我们可以看到在每个测试方法前面都有一个@Test标记,这就是传说中的Annotation。
Annotation 提供了一条与程序元素关联任何信息或者任何元数据(metadata)的途径。从某些方面看,annotation就像修饰符一样被使用,并应用于包、类型、构造方法、方法、成员变量、参数、本地变量的声明中。这些信息被存储在annotation的“name=value”结构对中。 annotation类型是一种接口,能够通过java反射API的方式提供对其信息的访问。
annotation能被用来为某个程序元素(类、方法、成员变量等)关联任何的信息。需要注意的是,这里存在着一个基本的潜规则:annotaion不能影响程序代码的执行,无论增加、删除 annotation,代码都始终如一的执行。另外,尽管一些annotation通过java的反射api方法在运行时被访问,而java语言解释器在工作时忽略了这些annotation。正是由于java虚拟机忽略了annotation,导致了annotation类型在代码中是“不起作用”的;只有通过某种配套的工具才会对annotation类型中的信息进行访问和处理。
annotation是与一个程序元素相关联信息或者元数据的标注。它从不影响
ava程序的执行,但是对例如编译器警告或者像文档生成器等辅助工具产生影响。
我的理解是:Annotation是继承自java.lang.annotation.Annotation的类,用于向程序分析工具或虚拟机提供package class field methed 等方面的信息,它和其他类没什么区别,除了使用方式。
下面写了一个简单的Annotation:WorkerPOJOAnnotation.java
package com.xpec.landon.trainjava.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 修饰WorkerPOJO类方法的一个Annotation
* @author lvwenyong
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WorkerPOJOAnnotation {
String name();
int age();
}
其中@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的,包括TYPE(类型),METHOD(方法),FIELD(字段),PARAMETER(参数)等。其中TYPE是指可以用在Class,Interface等类型上。下面给出用jad反编译出的ElementType中的静态变量的截图:

另外@Retention中的RetentionPolicy是指Annotation中的信息保留方式,分别是SOURCE,CLASS 和 RUNTIME. SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。 ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS. 第三个,是RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
下面给出用jad反编译出的RetentionPolicy中的静态变量的截图

最后的一个Annotation@Documented是指目的就是让这一个Annotation类型的信息能够显示在Java API说明文档上。
下面将上面自己设计的WorkerPOJOAnnotation应用在了WorkerPOJO类的一个方法前面:
然后在控制台输出了没有被Annotation注释的字段和方法,运行后可以看到不包括getName方法。
最后我们可以用Junit4书写一个测试用例: POJOReflectionJunit4Test.java
package com.xpec.landon.trainjava.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.activation.FileDataSource;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 关于Java反射以及Annotation的一个TestCase
* @author lvwenyong
*
*/
public class POJOReflectionJunit4Test {
private Class pojo;
private Field []fieldList;
private Method[] methodList;
@Before
public void setUp() throws Exception {
//加载类WorkPOJO
pojo = Class.forName("com.xpec.landon.trainjava.annotation.WorkerPOJO");
//获取域的数组
fieldList = pojo.getDeclaredFields();
//获取方法的数组
methodList = pojo.getDeclaredMethods();
}
//测试字段和方法的个数
@Test
public void testSize()
{
Assert.assertEquals(2, fieldList.length);
Assert.assertEquals(4, methodList.length);
}
//测试字段是否带有annotations
@Test
public void isFieldAnnotation()
{
for(int i = 0;i < fieldList.length;i++)
{
Assert.assertEquals(false, fieldList.isAnnotationPresent(WorkerPOJOAnnotation.class));
}
}
//测试方法是否带有annotations
@Test
public void isMethodAnnotation()
{
for(int i = 0;i < methodList.length;i++)
{
Assert.assertEquals(false, methodList.isAnnotationPresent(WorkerPOJOAnnotation.class));
}
}
@After
public void tearDown() throws Exception {
}
}
源码下载:http://file.javaxxz.com/2014/10/28/235626859.zip |
|