Java反射备忘录

在看spring的时候发现很多都是通过反射实现的,所以也看了看java关于反射的部分,总结如下,方便以后查找
Arithmetic.java

/**
* Class Arithmetic
* 反射目标类
* 水平有限,所以此类纯是为了测试而写,无任何逻辑而言
* @author lgsun
* Date: 2011-3-27
*/
package com.lgsun.target;

@SuppressWarnings("all")
public class Arithmetic
{
private int parmaerOne;
private int parmaerTwo;
public int sum;

private Arithmetic()
{
this.parmaerOne = -1;
this.parmaerTwo = -2;
}

public Arithmetic(int parmaerOne, int parmaerTwo)
{
this.parmaerOne = parmaerOne;
this.parmaerTwo = parmaerTwo;
}

public int getParmaerOne()
{
return parmaerOne;
}

public void setParmaerOne(int parmaerOne)
{
this.parmaerOne = parmaerOne;
}

public int getParmaerTwo()
{
return parmaerTwo;
}

public void setParmaerTwo(int parmaerTwo)
{
this.parmaerTwo = parmaerTwo;
}

public int getSum()
{
sum = sum(parmaerOne, parmaerTwo);
return this.sum;
}

public double getDivision()
{
return (parmaerOne / parmaerTwo);
}

private int sum(int p1, int p2)
{
return (p1 + p2);
}

public void getInfo()
{
System.out.println();
System.out.println("parmaerOne=" + parmaerOne);
System.out.println("parmaerTwo=" + parmaerTwo);
System.out.println("sum=" + sum);
}
}


ReflectConstructor.java

/**
* Class ReflectConstructor
* Constructor getConstructor(Class[] params) -- 获得使用特殊的参数类型的公共构造函数,
* Constructor[] getConstructors() -- 获得类的所有公共构造函数
* Constructor getDeclaredConstructor(Class[] params) -- 获得使用特定参数类型的构造函数(与接入级别无关)
* Constructor[] getDeclaredConstructors() -- 获得类的所有构造函数(与接入级别无关)
* 前2个可以返回继承自父类的构造方法;后2个只能返回当前类定义的,并且包含私有方法
* @author lgsun
* Date: 2011-3-27
*/
package com.lgsun.test;

import java.lang.reflect.Constructor;
import com.lgsun.target.Arithmetic;

public class ReflectConstructor
{
@SuppressWarnings("all")
public static void main(String[] args)
{
try
{
// 返回Arithmetic类全部的构造方法,包含私有构造方法
Constructor[] con = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructors();
for (int i = 0; i < con.length; i++)
{
System.out.println(con[i].toGenericString());
}

// 通过Arithmetic(int p1,int p2)构造方法创建实例
Class[] type = new Class[] { int.class, int.class };
Constructor pCon1 = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructor(type);
Arithmetic arit1 = (Arithmetic) pCon1.newInstance(3, 4);
arit1.getInfo();

// 通过Arithmetic()构造方法创建实例
Constructor pCon2 = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructor();
// 由于此构造方法是私有的,所以必须设置Accessible为true
pCon2.setAccessible(true);
Arithmetic arit2 = (Arithmetic) pCon2.newInstance();
arit2.getInfo();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


ReflectMethod.java

/**
* Class ReflectMethod
* Method getMethod(String name, Class[] params) -- 使用特定的参数类型,获得命名的公共方法
* Method[] getMethods() -- 获得类的所有公共方法
* Method getDeclaredMethod(String name, Class[] params) -- 使用特写的参数类型,获得类声明的命名的方法
* Method[] getDeclaredMethods() -- 获得类声明的所有方法
* 前2个方法会返回所有的属性,包括父类属性;后2个只会返回此类中定义的属性,包含私有属性
* @author lgsun
* Date: 2011-3-28
*/
package com.lgsun.test;

import java.lang.reflect.Method;

import com.lgsun.target.Arithmetic;

@SuppressWarnings("all")
public class ReflectMethod
{
public static void main(String[] args)
{
Arithmetic arithmetic = new Arithmetic(7, 8);
Method[] methods = arithmetic.getClass().getDeclaredMethods();
for (int i = 0; i < methods.length; i++)
{
System.out.println(methods[i].toGenericString());
}

try
{
Class[] types = new Class[] { int.class, int.class };
Method method1 = arithmetic.getClass().getDeclaredMethod("sum", types);
method1.setAccessible(true);
// 这样写是错误的
// int[] parms=new int[]{17,18};
Object[] parms = new Object[] { 17, 18 };
Object result = method1.invoke(arithmetic, parms);

System.out.println(((Integer) result).intValue());
arithmetic.getInfo();
}
catch (Exception e)
{
e.printStackTrace();
}

}
}


ReflectFiled.java

/**
* Class ReflectFiled
* Field getField(String name) -- 获得命名的公共字段
* Field[] getFields() -- 获得类的所有公共字段
* Field getDeclaredField(String name) -- 获得类声明的命名的字段
* Field[] getDeclaredFields() -- 获得类声明的所有字段
* 同样,前2个方法会返回所有的属性,包括父类属性;后2个只会返回此类中定义的属性,包含私有属性
* @author lgsun
* Date: 2011-3-28
*/
package com.lgsun.test;

import java.lang.reflect.Field;

import com.lgsun.target.Arithmetic;

public class ReflectFiled
{
@SuppressWarnings("all")
public static void main(String[] args)
{
// 返回Arithmetic类全部属性,包含私有属性
Arithmetic arithmetic = new Arithmetic(4, 5);
Field[] fields = arithmetic.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
System.out.println(fields[i].toGenericString());
}

try
{
// 直接给public属性sum赋值
Field field1 = arithmetic.getClass().getDeclaredField("sum");
field1.set(arithmetic, 19);
arithmetic.getInfo();

// 给private属性parmaerOne赋值
Field field2 = arithmetic.getClass().getDeclaredField("parmaerOne");
// 必加
field2.setAccessible(true);
field2.set(arithmetic, 14);
arithmetic.getInfo();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


附件为工程文件
参考http://www.ibm.com/developerworks/cn/java/j-dyn0603/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值