package com.example.pojo;
public interface IExample {
public void setName(String name);
public String getName();
public void setDesc(String description);
public String getDesc();
}
package com.example.pojo;
public interface IUser {
public void setUserID(String userID);
public String getUserID();
public void setUserName(String userName);
public String getUserName();
}
package com.example.reflection;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
public class MyProxyView implements InvocationHandler {
private Map<Object, Object> map = null;
public static Object newInstance(Class[] interfaces) {
return Proxy.newProxyInstance(MyProxyView.class.getClassLoader(),
interfaces, new MyProxyView());
}
private MyProxyView() {
this.map = new HashMap<Object, Object>();
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
String methodName = method.getName();
if (methodName.startsWith("get")) {
String name = methodName.substring(methodName.indexOf("get") + 3);
return map.get(name);
} else if (methodName.startsWith("set")) {
String name = methodName.substring(methodName.indexOf("set") + 3);
map.put(name, args[0]);
return null;
} else if (methodName.startsWith("is")) {
String name = methodName.substring(methodName.indexOf("is") + 2);
return (map.get(name));
}
return result;
}
}
package com.example.reflection;
import com.example.pojo.IExample;
import com.example.pojo.IUser;
public class TextProxy {
public static void main(String[] args)
{
IExample example = (IExample)MyProxyView.newInstance(new Class[]{IExample.class});
IUser user = (IUser)MyProxyView.newInstance(new Class[]{IUser.class});
// aduit bean 1
example.setName("my example");
example.setDesc("my proxy example");
// aduit bean 2
user.setUserID("jia20003");
user.setUserName("gloomyfish");
System.out.println("exmaple name : " + example.getName());
System.out.println("exmaple desc : " + example.getDesc());
System.out.println();
System.out.println("user ID : " + user.getUserID());
System.out.println("user name : " + user.getUserName());
}
}
exmaple name : my example exmaple desc : my proxy example user ID : jia20003 user name : gloomyfish
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有