学习java中的反照(二)

学习java中的反射(二)

      写这个主要是模仿一些 框架的javabean的注入功能,仅为自己学习使用

 首先定义一个javabean

 package com.model;

public class Person {
	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int 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;
	}
	

}

 然后就写个测试的类对该类进行注入

package com.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

import com.model.Person;

public class TestRef {
	static Class p;
	public static void main(String[] args) throws InstantiationException, IllegalAccessException,
 IllegalArgumentException, InvocationTargetException {
		try {
			p=Class.forName("com.model.Person");
			/**
			 * 在实际开发中获取p的class有多种方法
			 * 这里在实际开发中可以抽象出一个方法用来或去Person.class这样的参数类型也就是Class类型的
			 * p=Person.class;
			 *
			 */
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("类不存在");
		}
		
		//pp=Person.class.newInstance		
		Person pp=(Person) p.newInstance();
		
		/**
		 * getDeclaredMethods 只获得在当前类中声明的方法,能获得私有的方法,
		 * 必须把setAccessible(true)加上,这样才能正确访问private和protected方法                                              ;
		 * getMethods         获得包括继承的所有方法,只能获得公有的方法
		 */
		Method[] me=p.getDeclaredMethods();
		for (Method method : me) {
			System.out.print("方法名称:"+method.getName()+"  所在类:"+method.getDeclaringClass());
			String mname=method.getName();
			//为方法赋值,javabean中对方法进行设置都是以set开头
			if(mname.startsWith("set"))
			{
				/**
				 * 由于一般来说javabean或者说pojo中以set开始的方法中只有一个参数
				 */
				Class<?>[] c=method.getParameterTypes();
				/**
				 * name---对应的方法为setName
				 * 要获得字段的名称,就要把N改为小写
				 * Character.toLowerCase(mname.charAt(3))
				 */
				String prop = Character.toLowerCase(mname.charAt(3)) + mname.substring(4);
				
				System.out.println("参数名称   "+prop);
				//进行set的方法里面只有一个参数,进行判断当为一个参数的时候进行设置
				if(c.length==1)
				{
					String type=c[0].getName();//获得参数类型
					//进行赋值,我这里就把数据固定整形为1,字符型为xiaofei
					//在实际开发中我们要把通过request.getParameter("xxx")得到的值根据参数相应的类型进行转换
					if(type=="java.lang.String")
					{
						//如这里为字符类型String.valueOf("param's name");
						method.invoke(pp, new Object[]{"xiaofei"});	
					}
					
					if(type=="int")
					{
						method.invoke(pp, new Object[]{1});	
					}
					
				}
				
				System.out.println("toGenericString: "+method.toGenericString());
			 	Type [] ts=method.getGenericParameterTypes();
			 	for (Type type : ts) {
					System.out.println("type: "+type);
				}
			}
			
			System.out.println("");
		}
		Field[] fields=p.getDeclaredFields();
	    for (Field f : fields) {
			System.out.println(f.getName()+"---"+f.getType().getName());
			f.setAccessible(true);
           
		}
		
		//通过反射之后看赋值是否成功
	    System.out.println("name:"+pp.getName()+"  age:"+pp.getAge()+" id: "+pp.getId());
	}

}