当前位置:网站首页>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);
}
边栏推荐
- ROS2——初识ROS2(一)
- Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
- Ros2 topic (VIII)
- GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
- M2dgr slam data set of multi-source and multi scene ground robot
- Reading literature sorting 20220104
- PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
- SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
- C#学习笔记
- testing framework
猜你喜欢
并发编程 — 如何中断/停止一个运行中的线程?
睿智的目标检测59——Pytorch Focal loss详解与在YoloV4当中的实现
Machine learning Seaborn visualization
数学分析_笔记_第8章:重积分
window navicat连接阿里云服务器mysql步骤及常见问题
Netease to B, soft outside, hard in
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
ROS2——功能包(六)
The problem of configuring opencv in qt5.13.2 is solved in detail
Ros2 - function package (VI)
随机推荐
The SQL implementation has multiple records with the same ID, and the latest one is taken
[node] differences among NPM, yarn and pnpm
Reading literature sorting 20220104
Powermanagerservice (I) - initialization
能量守恒和打造能量缺口
SOC_ SD_ CMD_ FSM
ROS2——配置开发环境(五)
【无标题】
In C language, int a= 'R'
纯碱是做什么的?
*P++, (*p) + +, * (p++) differences
现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
PostMessage communication
[nvidia] CUDA_ VISIBLE_ DEVICES
IPage能正常显示数据,但是total一直等于0
ROS2——工作空间(五)
Raspberry pie 4B arm platform aarch64 PIP installation pytorch
Initialization of global and static variables
Matlab在线性代数中的应用(四):相似矩阵及二次型
Course learning accumulation ppt