当前位置:网站首页>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);
}
边栏推荐
- Do you choose pandas or SQL for the top 1 of data analysis in your mind?
- What is sodium hydroxide?
- Qu'est - ce que l'hydroxyde de sodium?
- Concurrent programming - deadlock troubleshooting and handling
- ROS2——工作空间(五)
- 【软件测试】04 -- 软件测试与软件开发
- Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
- D2L installation
- [vscode] search using regular expressions
- Tshydro tool
猜你喜欢

Docker installs MySQL and uses Navicat to connect

Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui

HDU1231 最大连续子序列(分治or动规or双指针)

Machine learning Seaborn visualization

1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS

【软件测试】04 -- 软件测试与软件开发

Ros2 - ros2 vs. ros1 (II)

SOC_ SD_ CMD_ FSM

Concurrent programming - deadlock troubleshooting and handling

Ros2 topic (VIII)
随机推荐
Interpretation of the earliest sketches - image translation work sketchygan
Docker installs MySQL and uses Navicat to connect
睿智的目标检测59——Pytorch Focal loss详解与在YoloV4当中的实现
MySQL setting trigger problem
ROS2——常用命令行(四)
Ros2 - first acquaintance with ros2 (I)
ROS2——ROS2对比ROS1(二)
DataGrid offline installation of database driver
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
[software testing] 03 -- overview of software testing
PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
【软件测试】06 -- 软件测试的基本流程
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
[node] NVM version management tool
Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
并发编程 — 死锁排查及处理
Powermanagerservice (I) - initialization
氢氧化钠是什么?
The SQL implementation has multiple records with the same ID, and the latest one is taken