package com.myibs.test.service.reflection;
import lombok.Data;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@Data
public class ReflectionDemo {
String className;
Class class0;
Integer classParamCnt;
Class<?>[] classParam;
Constructor contructor;
Integer instanceParamCnt;
Object[] InstanceParam;
Object objInstance;
Integer methodParamCnt;
Class<?>[] methodParam;
String methodName;
Class[] methodParamTypes;
Object[] methodArgs;
Method method;
Object returnObject;
public void setClass0(String className) throws ClassNotFoundException {
this.className = className;
class0 = Class.forName(className);
}
public void setClassParam(ArrayList<Class> classParamLst) {
classParam = classParamLst.toArray(new Class[classParamLst.size()]);
instanceParamCnt = this.classParam.length;
}
public void setConstructor() throws NoSuchMethodException {
contructor = class0.getConstructor(classParam);
}
public void setInstanceParam(ArrayList<Object> instanceParamLst) {
InstanceParam = instanceParamLst.toArray(new Object[instanceParamLst.size()]);
instanceParamCnt = InstanceParam.length;
}
public void setObjInstance() throws IllegalAccessException, InvocationTargetException, InstantiationException {
objInstance = contructor.newInstance(InstanceParam);
}
public void setMethodParam(ArrayList<Class> methodParamLst) {
methodParam = methodParamLst.toArray(new Class[methodParamLst.size()]);
methodParamCnt = methodParam.length;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public void setMethodParamTypes(ArrayList<Class> methodParamTypesList) {
methodParamTypes = methodParamTypesList.toArray(new Class[methodParamTypesList.size()]);
}
public void setMethodArgs(ArrayList<Object> methodArgsList) {
methodArgs = methodArgsList.toArray(new Object[methodArgsList.size()]);
}
public void setMethod() throws NoSuchMethodException {
method = class0.getMethod(methodName,methodParamTypes);
}
public Object callMethod() throws InvocationTargetException, IllegalAccessException {
returnObject = method.invoke(objInstance, methodArgs);
return returnObject;
}
public static void main(String[] args) {
String className;
ArrayList<Class> classParamLst = new ArrayList();
ArrayList<Object> instanceParamLst = new ArrayList();
String methodName;
ArrayList<Class> methodParamLst = new ArrayList();
ArrayList<Class> methodParamTypesList = new ArrayList();
ArrayList<Object> setMethodArgsList = new ArrayList();
className = "com.myibs.test.service.reflection.Person";
classParamLst.add(String.class);
classParamLst.add(int.class);
classParamLst.add(String.class);
instanceParamLst.add("小明111");
instanceParamLst.add(222);
instanceParamLst.add("哈尔滨111");
methodName = "method4";
methodParamLst.add(String.class);
methodParamTypesList.add(String.class);
setMethodArgsList.add("itcast");
try {
ReflectionDemo reflectionDemo = new ReflectionDemo();
reflectionDemo.setClass0(className);
reflectionDemo.setClassParam(classParamLst);
reflectionDemo.setConstructor();
reflectionDemo.setInstanceParam(instanceParamLst);
reflectionDemo.setObjInstance();
reflectionDemo.setMethodName(methodName);
reflectionDemo.setMethodParam(methodParamLst);
reflectionDemo.setMethodParamTypes(methodParamTypesList);
reflectionDemo.setMethodArgs(setMethodArgsList);
reflectionDemo.setMethod();
reflectionDemo.callMethod();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
@Data
class Person implements Serializable {
public String name;
public int age;
private String address;
private final static long serialVersionUID= -123123612836L;
public Person() {
System.out.println("空参数构造方法");
}
public Person(String name) {
this.name = name;
System.out.println("带有String的构造方法");
}
private Person(String name, int age){
this.name = name;
this.age = age;
System.out.println("带有String,int的构造方法");
}
public Person(String name, int age, String address){
this.name = name;
this.age = age;
this.address = address;
System.out.println("带有String, int, String的构造方法");
}
public void method1(){
System.out.println("没有返回值没有参数的方法");
}
public void method2(String name){
System.out.println("没有返回值,有参数的方法 name= "+ name);
}
public int method3(){
System.out.println("有返回值,没有参数的方法");
return 123;
}
public String method4(String name){
System.out.println("有返回值,有参数的方法");
return "哈哈" + name;
}
private void method5(){
System.out.println("私有方法");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address+ "]";
}
}