Java公式バージョン動的エージェント
1246 ワード
package org.sun.sample.pojo;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
*
*/
public class ReflectProxySample implements IReflectProxy{
public ReflectProxySample() {
LogInvocationHandler handler = new LogInvocationHandler(this);
IReflectProxy sample = (IReflectProxy) handler.dynamicProxy();
sample.register();
sample.login();
sample.logout();
}
public void register() {
}
public void login() {
}
public void logout() {
}
}
class LogInvocationHandler implements InvocationHandler {
protected Object target;
public LogInvocationHandler(Object target) {
this.target = target;
}
public Object dynamicProxy() {
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args) {
Object result = null;
try {
result = method.invoke(target, args);
} catch (Exception exp) {
}
System.out.println("Invoke:" + method.getName());
return result;
}
}