java反射机制

来源:互联网 发布:linux syslog server 编辑:程序博客网 时间:2024/06/06 21:04

参考文章:http://blog.csdn.net/justinavril/article/details/2873664


测试代码:


创建类:

Convey.java

package com.domain;import java.util.HashSet;import java.util.Set;/** * Convey entity. @author MyEclipse Persistence Tools * 啊是的撒F多少 */public class Convey implements java.io.Serializable {// Fieldsprivate Integer id;private String name;private Double price;private Set order = new HashSet(0);// Constructors/** default constructor */public Convey() {}/** minimal constructor */public Convey(String name, Double price) {this.name = name;this.price = price;}/** full constructor */public Convey(String name, Double price, Set order) {this.name = name;this.price = price;this.order = order;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Double getPrice() {return this.price;}public void setPrice(Double price) {this.price = price;}/** * sad * @return */public Set getOrder() {return this.order;}public void setOrder(Set order) {this.order = order;}}

测试类:

Start.java

package com.domain;import java.beans.MethodDescriptor;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Start {/** * @param args * @throws NoSuchMethodException  * @throws SecurityException  * @throws InvocationTargetException  * @throws IllegalAccessException  * @throws IllegalArgumentException  */public static void main(String[] args) throws Exception{// TODO Auto-generated method stub//获得类中的方法Class class1=Convey.class;//实例化对象Object object=class1.newInstance();System.out.println("类名:"+class1.getName());System.out.println("包名:"+class1.getPackage());//获得字段System.out.println("字段:");Field [] field=class1.getDeclaredFields();//class1.getFields();System.out.println(field.length);for (Field field2 : field) {System.out.println(field2.getName()+" type:"+field2.getType());}//方法System.out.println("全部方法");Method []  methods= class1.getMethods();for (Method method : methods) {System.out.println(method.getName());}//获得公开的方法System.out.println("公开的方法");Method [] pMethods= class1.getDeclaredMethods();for (Method method : pMethods) {System.out.println(method);}//获得释文System.out.println("释文"); Annotation[] annotation= class1.getDeclaredAnnotations(); for (Annotation annotation2 : annotation) {System.out.println(annotation2);}  System.out.println("操作对象");//invoke方法 Class partypes[] = new Class[1]; //参数数量         partypes[0] = String.class; //参数类型         Method meth = class1.getMethod("setName", partypes);                  //赋值         Object arglist[] = new Object[1];         arglist[0]="你好啊!";         //调用方法         Object retobj = meth.invoke(object, arglist);                   //调用方法 返回结果         meth = class1.getMethod("getName", new Class[0]);         retobj= meth.invoke(object, new Class[0]);         System.out.println(retobj);}}
主要作用说明:

获取指定类中的所有字段、方面、实例化类、设置属性、调用方法