函数的反射。
包含了参数的各种参数的情况,返回值的情况,几种访问权限的情况,静态的情况。
其中需要注意的是第5个参数为字符串数组时,需要在invoke时将其修饰为单体对象Object,这是因为jdk1.4之前不支持动态数量的参数列表,所以造成了jdk版本之间的冲突,应该算是一个bug。(jdk误认为数组中的元素为若干个参数)。
package com.xxx;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo3 {
String className="com.xxx.SwordBean";
@Test
public void test1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getMethod("fn1", null);
m.invoke(sw, null);
}
@Test
public void test2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getMethod("fn2", int.class);
m.invoke(sw, 1);
}
@Test
public void test3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getMethod("fn3", int.class,String.class);
m.invoke(sw, 1,"string");
}
@Test
public void test4() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getMethod("fn4", int.class);
String r=(String)m.invoke(sw, 618);
System.out.println(r);
}
@Test
public void test5() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getMethod("fn5",String[].class);
//m.invoke(sw, new String[]{"aa","bb","cc"});
m.invoke(sw, (Object)(new String[]{"aa","bb","cc"}));
}
@Test
public void test6() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getDeclaredMethod("fn6", null);
m.invoke(sw, null);
}
@Test
public void test7() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getDeclaredMethod("fn7", null);
m.setAccessible(true);
m.invoke(sw, null);
}
@Test
public void test8() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
SwordBean sw=new SwordBean();
Class c1=Class.forName(className);
Method m=c1.getDeclaredMethod("fn8", null);
m.invoke(sw, null);
}
@Test
public void test9() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class c1=Class.forName(className);
Method m=c1.getDeclaredMethod("fn9", String.class, PropertyBean.class);
m.invoke(null, "string",new PropertyBean());
}
}
bean定义:
package com.xxx;
public class SwordBean {
String swordName;
float price;
float Attack;
int sid;
PropertyBean propertyOfSword;
public String text="good sword!";
private String secret="bad sword!";
String review="soso sword!";
public String getSwordName() {
return swordName;
}
public void setSwordName(String swordName) {
this.swordName = swordName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public float getAttack() {
return Attack;
}
public void setAttack(float attack) {
Attack = attack;
}
private int getSid() {
return sid;
}
private void setSid(int sid) {
this.sid = sid;
}
public PropertyBean getPropertyOfSword() {
return propertyOfSword;
}
public void setPropertyOfSword(PropertyBean propertyOfSword) {
this.propertyOfSword = propertyOfSword;
}
public SwordBean() {
setAttack(1);
}
public SwordBean(float p) {
setPrice(p);
}
public SwordBean(float p, float a) {
setAttack(a);
setPrice(p);
}
public SwordBean(PropertyBean pro) {
setPropertyOfSword(pro);
}
private SwordBean(String Name)
{
setSwordName(Name);
}
public void fn1()
{
System.out.println("fn1");
}
public void fn2(int p)
{
System.out.println("fn2");
}
public void fn3(int p1,String p2)
{
System.out.println("fn3");
}
public String fn4(int p)
{
System.out.println("fn4");
return "fn4 return "+p;
}
public void fn5(String[] p)
{
System.out.println("fn5");
}
void fn6()
{
System.out.println("fn6");
}
private void fn7()
{
System.out.println("fn7");
}
protected void fn8()
{
System.out.println("fn8");
}
public static int fn9(String a,PropertyBean pb)
{
System.out.println("fn9");
return 100;
}
}