public class DynamicProxy {
public static void main(String[] args) {
ProxyHandler handler = new ProxyHandler();
DynamicBaseView view = (DynamicBaseView) handler.setProxyObject(new ProxyDynamicView());
view.showText();
}
}
interface DynamicBaseView {
void showText();
}
class ProxyDynamicView implements BaseView {
public static final String TAG = ProxyDynamicView.class.getSimpleName();
@Override
public void showText() {
Log.d(TAG, "niubility Dynamic");
}
}
class ProxyHandler implements InvocationHandler {
public static final String TAG = ProxyHandler.class.getSimpleName();
private Object mObject;
public Object setProxyObject(Object o) {
mObject = 0;
return Proxy.newProxyInstance(mObject.getClass().getClassLoader(),
mObject.getClass().getInterfaces()
, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// AOP
// 切入前操作
Object obj = method.invoke(mObject, args);
Log.d(TAG, "niubility Dynamic +1");
// 切入后操作
return obj;
}
}