Annotation与反射的结合的实例程序
2015-09-02 11:46
387 查看
今天学习了杨帆老师的Annotation与反射的结合视频教学结合教学视频编写了测试练习程序
需求:建立学生类,包含若干属性,属性中增加注解,在运行时通过反射方法打印各属性名(含有注解使用注解)和属性值。
首先建立Annotation注解
然后建立Student类
编写工具类Utils其中含有静态toString方法用于转换对象的转字符串方法方便用于打印
package AnnotationReflect;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.text.GapContent;
public class Utils {
public static String toString(Object obj) throws Exception {
StringBuffer result = new StringBuffer(); //定义返回类型
Class<?> c = obj.getClass(); //通过对象反射获得对应的类对象
int i = 0;
result.append("类名:" + c.getSimpleName()); //类名
result.append("[");
for(Field f:c.getDeclaredFields()) { //循环遍历类对象的所有属性字段
if (i>0) result.append(","); //添加分割符
i++;
Field_name fn = f.getAnnotation(Field_name.class); //获得属性字段的注解
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), c); //反射方法获得对应字段的属性描述
String field_value = "";
Method m = pd.getReadMethod(); //从属性描述中获取读方法
field_value = m.invoke(obj, null) + ""; //进行读方法调用将obj字段值读出
if ((fn != null) && fn.value().length() > 0) { //判断此字段是否有注解信息
result.append(fn.value());
result.append("=");
result.append(field_value);
}else { //此字段没有注解信息直接取类的字段名
result.append(f.getName());
result.append("=");
result.append(field_value);
}
}
result.append("]");
return result.toString();
}
}
增加了Retention的结果
屏蔽retention的运行结果
需求:建立学生类,包含若干属性,属性中增加注解,在运行时通过反射方法打印各属性名(含有注解使用注解)和属性值。
首先建立Annotation注解
package AnnotationReflect; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) //设置此注解只能应用于类中的属性字段中 @Retention(RetentionPolicy.RUNTIME) //设置此注解有效性为运行时 public @interface Field_name { //注解名称为Field_name字段名用于记录类中字段名称 String value(); //注解字段的值 }
然后建立Student类
package AnnotationReflect; public class Student { //依次对各字段添加注解信息 @Field_name("学号") private int id; @Field_name("学生姓名") private String name; @Field_name("年龄") private int age; @Field_name("籍贯") private String loc; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the loc */ public String getLoc() { return loc; } /** * @param loc the loc to set */ public void setLoc(String loc) { this.loc = loc; } public Student(int id, String name, int age, String loc) { //初始化构造方法 super(); this.id = id; this.name = name; this.age = age; this.loc = loc; } @Override public String toString() { String s = ""; try { s = Utils.toString(this); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } public static void main(String[] args){ Student s = new Student(001, "zhangsan", 24, "山东"); System.out.println(s); } }
编写工具类Utils其中含有静态toString方法用于转换对象的转字符串方法方便用于打印
package AnnotationReflect;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.text.GapContent;
public class Utils {
public static String toString(Object obj) throws Exception {
StringBuffer result = new StringBuffer(); //定义返回类型
Class<?> c = obj.getClass(); //通过对象反射获得对应的类对象
int i = 0;
result.append("类名:" + c.getSimpleName()); //类名
result.append("[");
for(Field f:c.getDeclaredFields()) { //循环遍历类对象的所有属性字段
if (i>0) result.append(","); //添加分割符
i++;
Field_name fn = f.getAnnotation(Field_name.class); //获得属性字段的注解
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), c); //反射方法获得对应字段的属性描述
String field_value = "";
Method m = pd.getReadMethod(); //从属性描述中获取读方法
field_value = m.invoke(obj, null) + ""; //进行读方法调用将obj字段值读出
if ((fn != null) && fn.value().length() > 0) { //判断此字段是否有注解信息
result.append(fn.value());
result.append("=");
result.append(field_value);
}else { //此字段没有注解信息直接取类的字段名
result.append(f.getName());
result.append("=");
result.append(field_value);
}
}
result.append("]");
return result.toString();
}
}
增加了Retention的结果
屏蔽retention的运行结果
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树