当前位置:网站首页>And play the little chestnut of dynamic agent
And play the little chestnut of dynamic agent
2022-07-05 07:16:00 【I am the king of X】
Go straight to the code
List of articles
Premise
This needs to be understood in combination with another blog post :
Code
Interface :
package com.xmonster.demo2;
public interface IService {
public void sayHello();
}
Real implementation class :
package com.xmonster.demo2;
public class RealService implements IService{
@Override
public void sayHello() {
System.out.println("hello i am xmonster!");
}
}
Tools + test :
package com.xmonster.demo2;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class SimpleInvocationHandler implements InvocationHandler {
private Object realObj;
public SimpleInvocationHandler(Object realObj) {
this.realObj = realObj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("entering"+method.getName());
Object invokeResult = method.invoke(realObj, args);
System.out.println("leaving"+method.getName());
return invokeResult;
}
public static void main(String[] args) {
IService realService = new RealService();
IService proxy =(IService) Proxy.newProxyInstance(IService.class.getClassLoader(), new Class<?>[]{
IService.class},
new SimpleInvocationHandler(realService));
proxy.sayHello();
}
}
test result :
newProxyInstance
Let's see newProxyInstance Three parameters of
private static Object newProxyInstance(ClassLoader loader, // null if no SecurityManager
Constructor<?>[] interfaces,
InvocationHandler h)
Parameters :
- ClassLoader loader Represents a class loader , Use the same class loader as the interface here
- interfaces Represents the list of interfaces to be implemented by the proxy class , Is an array ,
The type of element can only be interface , It cannot be a general class
- InvocationHandler It's an interface , Only one... Is defined invoke Method , Calls to all methods of the proxy interface will be transferred to this method for processing
newProxyInstance The return value of Object, Can be forced to interfaces An interface type in the array
, Here, it is directly converted to IService type , Be careful : It cannot be forcibly converted to a class , such as RealService !
InvocationHandler
package java.lang.reflect;
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}
Parameters :
- proxy Represents the agent itself , Be careful , He is not the object of being represented
- method Represents the method being called
- args Parameters representing methods
Object invokeResult = method.invoke(realObj, args);
So this is calling theta method Of invoke Method , What is passed is the actual object realObj
, Can't be proxy Pass as parameter to method.invoke, There will be a dead cycle , Because it has this method itself
Here are method Of invoke Method source code
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
if (!override) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz,
Modifier.isStatic(modifiers) ? null : obj.getClass(),
modifiers);
}
MethodAccessor ma = methodAccessor; // read volatile
if (ma == null) {
ma = acquireMethodAccessor();
}
return ma.invoke(obj, args);
}
边栏推荐
猜你喜欢
Jenkins reported an error. Illegal character: '\ufeff'. Class, interface or enum are required
Negative number storage and type conversion in programs
三体目标管理笔记
Ros2 - workspace (V)
Word import literature -mendeley
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
目标检测系列——Faster R-CNN原理详解
M2DGR 多源多场景 地面机器人SLAM数据集
SOC_SD_DATA_FSM
Three body goal management notes
随机推荐
ModuleNotFoundError: No module named ‘picamera‘
[node] NVM version management tool
SD_CMD_RECEIVE_SHIFT_REGISTER
Special training of C language array
window navicat连接阿里云服务器mysql步骤及常见问题
ROS2——配置开发环境(五)
PowerManagerService(一)— 初始化
The SQL implementation has multiple records with the same ID, and the latest one is taken
三体目标管理笔记
解读最早的草图-图像翻译工作SketchyGAN
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
What is soda?
逻辑结构与物理结构
ROS2——工作空间(五)
[untitled]
Word import literature -mendeley
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
Use of Pai platform
SOC_SD_CMD_FSM
Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换