package com.pp.test01;
import java.lang.reflect.Proxy;
public class MainClass {
public static void main(String[] args) {
UserDao userDao = (UserDao) Proxy.newProxyInstance(MainClass.class.getClassLoader(),
new Class[] {UserDao.class}, new MyHandler());
userDao.findBySex("男","湖人");
}
}
package com.pp.test01;
public interface UserDao {
public void findBySex(String sex,String dept);
}
package com.pp.test01;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class MyHandler implements InvocationHandler{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
Parameter[] paramArr = method.getParameters();
String params="";
for(Parameter p:paramArr) {
params+=p.getName()+",";
}
System.out.println("params:"+params);
System.out.println("methodName:"+methodName);
return null;
}
}
Window->Preference->Compiler->勾选最后一个把输出参数名从arg0,arg1变为参数名