java反射

package com.openv.spring.reflectTest;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
/**
* 构造函数
*/
public Test() {

}

/**
* 构造函数2
*
* @param a
* @param b
*/
public Test(int a, int b) {
System.out.println("a = " + a + " b = " + b);
}

/**
* @param args
*/
public static void main(String[] args) {
Son1 son1 = (Son1) Test.convertStr2ServiceBean(
"com.openv.spring.reflectTest.Son1", "setName", "wang da sha");
System.out.println("son1.getName() :" + son1.getName());

testClass();
test2();
test3();
test4();
}

/**
* 多态性测试
* 对象.方法 是根据多态性调用,对象.实例变量 是根据对象的类型执行调用
* 多态性仅仅适用于方法的调用,而不适合于实例变量
*/
public static void test4() {
Base base = new Son1();
System.out.println("base.getX()==" + base.getX());
System.out.println("base.x==" + base.x);
System.out.println("base.s==" + base.s);
}

/**
*
* @param beanName
* @param fieldSetter
* @param paraValue
* @return
*/
public static Base convertStr2ServiceBean(String beanName,
String fieldSetter, String paraValue) {
Base base = null;
try {
Class cls = Class.forName(beanName);
base = (Base) cls.newInstance();
Class[] paraTypes = new Class[] { String.class };
Method method = cls.getMethod(fieldSetter, paraTypes);
String[] paraValues = new String[] { paraValue };
method.invoke(base, paraValues);
} catch (Throwable e) {
e.printStackTrace();
}
return base;
}

public static void testClass() {
try {
Class cls = Class.forName("java.lang.String");
Method[] method = cls.getDeclaredMethods();
for (int i = 0; i < method.length; i++) {
System.out.println(method[i].toString());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* 根据方法名执行方法
*
*/
public static void test2() {
try {
Class cls = Class.forName("com.openv.spring.reflectTest.Son2");
// 参数类型
Class paraTypes[] = new Class[2];
paraTypes[0] = Integer.TYPE;
paraTypes[1] = Integer.TYPE;
String methodName = "add";
Method meth = cls.getMethod(methodName, paraTypes);
Object instance = cls.newInstance();
// method2 methobj = new method2();
// 参数列表
Object argList[] = new Object[2];
argList[0] = new Integer(37);
argList[1] = new Integer(47);
// 返回值retobj
Object retobj = meth.invoke(instance, argList);
Integer retval = (Integer) retobj;
System.out.println(retval.intValue());

// 改变属性的值
Field fld = cls.getField("salary");
fld.setDouble(instance, 6000);
System.out.println("new salary == "
+ cls.getMethod("getSalary").invoke(instance));
System.out
.println("new salary == " + ((Son2) instance).getSalary());
} catch (Throwable e) {
System.err.println(e);
}
}

/**
* 根据指定的参数类型找到相应的构造函数并执行它, 程序运行时动态地创建对象
*/
public static void test3() {
try {
Class cls = Class.forName("com.openv.spring.reflectTest.Test");
Class[] paraTypes = new Class[2];
paraTypes[0] = Integer.TYPE;
paraTypes[1] = Integer.TYPE;
Constructor ct = cls.getConstructor(paraTypes);
// 参数列表
Object[] argList = new Object[2];
argList[0] = new Integer(37);
argList[1] = new Integer(47);
Object o = ct.newInstance(argList);
} catch (Throwable e) {
System.err.println(e);
}
}
}

你可能感兴趣的:(java,spring)