`
zwt2001267
  • 浏览: 435985 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 反射资料 列子

阅读更多
import java.lang.reflect.Field;
import java.lang.reflect.Method;


public class ReflectTester {
	
	
	public Object copy(Object object) throws Exception{
		
		/**
		 * 获得实体对象的运行时类
		 */
		Class<?> classType = object.getClass();
		//System.out.println("the run class is:"+classType.getName());
		
		/**
		 * 获得实体的构造方法
		 */
		Object objectCopy = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
		
		/**
		 * getDeclaredFields()获得类的所有属性
		 * getFields()获得public类型的类的属性
		 */		
		Field fields[] = classType.getDeclaredFields();
		
		for(int i=0;i<fields.length;i++){
			
			/**
			 * 取出第i个类的属性
			 * 例如:private java.lang.String Custom.name
			 */
			Field field=fields[i];
			
			System.out.println("field:"+field);
			
			String fieldName = field.getName();
			
			/**
			 * 获得属性的名字
			 * 例如:name
			 */
			System.out.println("fieldName:"+fieldName);
			
			/**
			 * 将属性名字的第一个字母大写例如name的n变成N
			 */
			String firstletter = fieldName.substring(0, 1).toUpperCase();
			
			/**
			 * 得到getName
			 */
			String getMethodName = "get"+firstletter+fieldName.substring(1);
			
			/**
			 * 得到setName
			 */
			String setMethodName = "set"+firstletter+fieldName.substring(1);
			
			/**
			 * 得到getName()方法
			 */
			Method getMethod = classType.getMethod(getMethodName, new Class[]{});
			
			Method setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});
			
			/**
			 * 调用method的invoke()方法即执行原对象的getXXX()方法
			 */
			Object value = getMethod.invoke(object, new Object[]{});
			System.out.println(fieldName+" :"+value);
			
			/**
			 * 调用method的invoke()方法即执行原对象的setXXX()方法
			 */
			setMethod.invoke(object,new Object[]{value});
			
			
		}
		
		return objectCopy;
		
	}
	
	
	public static void main(String args[]) throws Exception{
		
		
		Custom custom = new Custom("tom",22);
		custom.setId(1);
		
		Custom customcopy = (Custom)new ReflectTester().copy(custom);
		
		System.out.println("copy information:"+customcopy.getId() + " " + customcopy.getName() + " " + custom.getAge());
	}
	
}	

    /**
     * the class
     * @author Administrator
     *
     */
	class Custom{
	
	   private long id;
	   
	   private String name;
	   
	   private int age;
	   
	   public Custom(){
		   
	   }
	   
	   /**
	    *  Constructor
	    * @param name
	    * @param age
	    */
	   public Custom(String name,int age){
		   
		   this.name=name;
		   
		   this.age=age;
		   
	   }

	   
	   /**
	    * the get and set method of fields
	    * @return
	    */
		public long getId() {
			return id;
		}
	
		public void setId(long id) {
			this.id = id;
		}
	
		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;
		}
		
	}

 

 

import java.lang.reflect.Method;

/**
 * 联系invoke方法的使用
 * @author Administrator
 *
 */
public class InvokeTester {
	
	 public int add(int param1,int param2){
		 
		 return param1+param2;
	 }
	 
	 public String echo(String msg){
		 
		 return "echo:"+msg;
		 
	 }
	 
	 
	 
	 public static void main(String [] args) throws Exception{
		 
		 Class<?> classType = InvokeTester.class;
		 
		 /**
		  * 下面为调用类的默认构造方法如果有多个构造方法,则用下面的方法实现
		  * classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
		  */
		 
		 Object invokeTester = classType.newInstance();
		 
		 /**
		  * 获得get()方法
		  */
		 Method addMethod = classType.getMethod("add", new Class[]{int.class,int.class});
		 /**
		  * 执行get()方法
		  */
		 Object result = addMethod.invoke(invokeTester, new Object[]{new Integer(100),new Integer(200)});
		 
		 System.out.println("the result is:" + (Integer)result);
		 
		 Method echoMethod = classType.getMethod("echo", new Class[]{String.class});
		 
		 Object echores = echoMethod.invoke(invokeTester, new Object[]{new String("hello world")});
		 
		 System.out.println("echo result is:" + (String) echores);
		 
	 }
	 

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics