java reflect

package com.songwei.struts.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class User {
	public User() {
		// TODO Auto-generated constructor stub
		this.name = "default";
		System.out.println("name=default");
	}

	public User(String name) {
		this.name = name;
		System.out.println("name=" + name);
	}

	private int id;

	private String name;

	private Date birthday;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		try {
			Class cls1 = Class.forName("com.songwei.struts.model.User");
			try {
				try {
					Constructor con1 = cls1
							.getDeclaredConstructor(new Class[] { String.class });
					Constructor con2 = cls1
							.getDeclaredConstructor(null);

					try {
						Object obj1 = con1
								.newInstance(new Object[] { "songwei" });
						Object obj2 = con2.newInstance(new Object[] {});

						Field field1 = null, field2 = null;
						try {
							field1 = cls1.getDeclaredField("name");
							field2 = cls1.getDeclaredField("name");
						} catch (NoSuchFieldException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("field1 name=" + field1.get(obj1));
						System.out.println("field2 name=" + field2.get(obj2));
						
						Method method1=cls1.getDeclaredMethod("getName", null);
						System.out.println("method1 name="+method1.invoke(obj1, null));
						Method method2=cls1.getDeclaredMethod("getName", null);
						System.out.println("method2 name="+method2.invoke(obj2, null));
						
						Method method3=cls1.getDeclaredMethod("setName", new Class[]{String.class});
						method3.invoke(obj2, new Object[]{"Pipper.Sung"});
						Method method4=cls1.getDeclaredMethod("getName", null);
						System.out.println("method4 name="+method2.invoke(obj2, null));
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值