Code enhancement method and device, equipment and storage medium

By inserting interception code and establishing listeners at the enhancement points of Java methods, the limitations of dynamic interception capabilities and usage restrictions of Java method interception and enhancement technologies are resolved, enabling flexible code interception and enhancement operations.

CN121579062APending Publication Date: 2026-02-27BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511498603.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-20
Publication Date
2026-02-27

AI Technical Summary

Technical Problem

Existing Java method interception and enhancement technologies lack dynamic interception capabilities and have many limitations. They require pre-coding for each interception and re-coding when adding, deleting, or modifying them.

Method used

By inserting interception code at the enhancement location of Java methods and establishing a listener associated with the enhancement location, the target enhancement action is executed according to the enhancement conditions and actions, thus achieving dynamic code interception and enhancement.

Benefits of technology

It achieves greater flexibility and fewer restrictions in code interception, supports dynamic operations based on enhanced location, conditions, and actions, and simplifies the code configuration process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121579062A_ABST
    Figure CN121579062A_ABST
Patent Text Reader

Abstract

Embodiments of the invention provide a code enhancement method and apparatus, a device and a storage medium. The method comprises the steps of obtaining an enhancement condition, an enhancement action, an enhancement position of a code and an intercepted code; inserting an interception code in the enhancement position of the code; establishing a monitor according to the enhancement condition and the enhancement action, wherein the monitor and the enhancement position of the code have an association relationship; when the intercepted code is executed, determining a target monitor associated with the enhancement position of the code corresponding to the intercepted code from the monitors; and executing the target enhancement action according to the target enhancement condition. According to the embodiment of the invention, a code interception and enhancement implementation mechanism based on the monitor enables the insertion of the intercepted code to have higher flexibility, and has less use limitation compared with the prior art; in addition, when the enhancement position, the enhancement condition and / or the enhancement action are added, deleted and modified, only the corresponding monitor needs to be adjusted, original codes do not need to be rewritten, and therefore dynamic code interception is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a method, apparatus, device, and storage medium for enhancing code. Background Technology

[0002] Intercepting and enhancing Java methods is a crucial technique in modern Java development, primarily applied to cross-cutting concerns such as Aspect-Oriented Programming (AOP), system logging, transaction management, performance monitoring, and security control. This technique inserts specific processing logic before and after Java method calls, enabling developers to manage system-level functionalities uniformly while maintaining the integrity of core business logic. Currently, mainstream solutions include JDK (Java Development Kit) dynamic proxies, CGLIB (Code Generation Library) bytecode enhancement, Java Agent technology, compile-time weaving in AspectJ (a Java-based aspect-oriented programming framework), and the Interceptor pattern, among other implementation methods.

[0003] However, existing Java method interception and enhancement technologies generally suffer from a lack of dynamic interception capabilities and numerous limitations in their use. Summary of the Invention

[0004] The purpose of this invention is to provide a code enhancement method, apparatus, device, and storage medium to address the problems of existing Java method interception and enhancement technologies, which generally lack dynamic interception capabilities and have many limitations. The specific technical solution is as follows: In a first aspect of the invention, a code enhancement method is provided, applied to an electronic device with an agent tool deployed thereon, the method comprising: Get the enhancement conditions, enhancement actions, enhancement locations in the code, and interception code; Insert the intercept code at the enhanced location of the code; A listener is established based on the enhancement conditions and the enhancement actions, and there is an association between the listener and the enhancement location of the code; When the intercept code is executed, based on the enhancement location of the code corresponding to the intercept code and the association between the listener and the enhancement location of the code, a target listener associated with the enhancement location of the code corresponding to the intercept code is determined from the listeners; the target listener has target enhancement conditions and target enhancement actions; The target enhancement action is performed according to the target enhancement conditions to obtain the enhancement result.

[0005] Optionally, according to the method of claim 1, the step of establishing a listener based on the enhancement conditions and the enhancement action includes: Generate an enhancement expression based on the enhancement conditions and the enhancement actions; The listener is built based on the enhanced expression.

[0006] Optionally, performing the target enhancement action according to the target enhancement conditions includes: Obtain the input data and the enhancement expression in the target listener, the enhancement expression containing the variable to be filled; The input data is used to populate the variables to be populated in the enhanced expression of the target listener, thus obtaining the target expression; If the target expression satisfies the target enhancement condition, then the target enhancement action corresponding to the target enhancement condition is executed according to the target expression.

[0007] Optionally, the method includes: If a new enhancement location is added to the code, the interception code is inserted into the new enhancement location, and a new listener is established based on the new enhancement conditions and new enhancement actions corresponding to the new enhancement location. If the enhancement location of the code is deleted, then the listener corresponding to the enhancement location of the code is deleted; If the enhancement location of the code is modified, the listener corresponding to the enhancement location of the code is deleted, and a modified listener is created for the enhancement location of the modified code.

[0008] Optionally, the proxy tool communicates with the server, and the server communicates with the enhancement rule platform. The step of obtaining the enhancement conditions, enhancement actions, and the enhancement location of the code includes: The enhancement rules are obtained from the server. The enhancement rules include the enhancement conditions, the enhancement actions, and the enhancement locations of the code. The enhancement rules are configured by the enhancement rule platform and uploaded to the server.

[0009] Optionally, the method includes: If a new enhancement rule is added, the interception code is inserted into the enhancement location of the code corresponding to the new enhancement rule, and a new listener is established according to the new enhancement rule; If the enhancement rule is deleted, then the listener corresponding to the enhancement location of the code in the enhancement rule is deleted; If the enhancement rule is modified, the listener corresponding to the enhancement position of the code in the enhancement rule is deleted, and a modified listener is created for the modified enhancement rule.

[0010] Optionally, inserting the intercept code at the enhanced location of the code includes: Obtain the pre-interception point and / or post-interception point corresponding to the enhanced location of the code; The interception code is inserted at the pre-interception point and / or the post-interception point.

[0011] In a second aspect of the invention, a code enhancement apparatus is also provided, applied to an electronic device on which an agent tool is deployed, the apparatus comprising: The rule acquisition module is used to acquire enhancement conditions, enhancement actions, enhancement locations in the code, and interception code; A code insertion module is used to insert the intercept code at the enhanced location of the code; A listener creation module is used to create listeners based on the enhancement conditions and the enhancement actions, wherein there is an association between the listeners and the enhancement locations in the code; The first interception module is used, when executing the interception code, to determine, from the listeners, a target listener associated with the enhancement position of the code corresponding to the interception code, based on the enhancement position of the code corresponding to the interception code and the association relationship between the listener and the enhancement position of the code; the target listener has target enhancement conditions and target enhancement actions; The second interception module is used to execute the target enhancement action according to the target enhancement conditions to obtain the enhancement result.

[0012] In another aspect of the present invention, an electronic device is also provided, characterized in that it includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus. Memory, used to store computer programs; A processor, when executing a program stored in memory, implements any of the above-described methods of code enhancement.

[0013] In another aspect of the present invention, a computer-readable storage medium is also provided, wherein instructions are stored therein, which, when executed on a computer, cause the computer to execute the enhanced method of any of the above-described codes.

[0014] In another aspect of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the code described above, an enhanced method.

[0015] This invention provides a code enhancement method that involves obtaining enhancement conditions, enhancement actions, enhancement locations, and intercepting code; inserting intercepting code into the enhancement locations; establishing listeners based on the enhancement conditions and actions, with an association between the listeners and the enhancement locations; when executing the intercepting code, determining a target listener associated with the enhancement location of the code corresponding to the intercepting code, based on the enhancement location of the code corresponding to the intercepting code and the association between the listeners and the enhancement locations; the target listener is associated with the enhancement location of the code corresponding to the intercepting code, and the target listener has target enhancement conditions and target enhancement actions; and executing the target enhancement actions based on the target enhancement conditions to obtain the enhancement result. This invention, by inserting intercepting code into the enhancement locations and establishing listeners associated with the enhancement locations based on the enhancement conditions and actions, allows for the direct determination of the target listener corresponding to the enhancement location when the code is executed, based on the association between the listeners and the enhancement locations, and the execution of the corresponding target enhancement actions based on the target enhancement conditions in the target listener to obtain the enhancement result. The listener-based code interception and enhancement mechanism of this invention provides greater flexibility in inserting interception code and has fewer usage restrictions compared to existing Java method interception and enhancement techniques. Furthermore, when adding, deleting, and / or modifying enhancement locations, conditions, and / or actions, only the corresponding listeners need to be adjusted; there is no need to rewrite the original code, thus achieving dynamic code interception and significantly improving operational convenience. Attached Figure Description

[0016] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.

[0017] Figure 1 This is a flowchart illustrating the steps of an embodiment of a code enhancement method according to the present invention; Figure 2 This is a system architecture diagram of an embodiment of a code enhancement method according to the present invention; Figure 3 This is a structural block diagram of an embodiment of a code enhancement device according to the present invention; Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation

[0018] The technical solutions of the present invention will now be described with reference to the accompanying drawings in the embodiments of the present invention.

[0019] Java is an object-oriented programming language. In Java, a method is a unit of encapsulation of the behavior of a class or object, used to perform a specific task or operation.

[0020] Intercepting and enhancing Java methods are important techniques in modern Java development, primarily applied to address cross-cutting concerns such as aspect-oriented programming, system logging, transaction management, performance monitoring, and security control. Interception refers to inserting custom logic during method calls, while enhancement involves injecting additional functional logic into existing methods without modifying the original code. The intercepted Java method is the Java method itself. Cross-cutting concerns are those in software development that cannot be effectively encapsulated through traditional modularization methods but affect the functional requirements of multiple modules or components. These concerns typically span multiple layers of the system, leading to code fragmentation and duplication. Therefore, interception and enhancement techniques are needed to address cross-cutting concerns. This technique inserts specific processing logic before and after Java method calls, allowing developers to manage system-level functionalities uniformly while maintaining the integrity of core business code, thus decoupling the logic of cross-cutting concerns. Currently, the mainstream interception and enhancement Java method techniques include the following: 1. JDK dynamic proxy solution: This solution is based on the proxy mechanism implemented by interfaces. It uses the java.lang.reflect.Proxy class (one of the core classes of the Java Reflection API, used to dynamically create proxy classes and proxy objects) to create proxy objects to intercept methods. This solution can only intercept methods defined in interfaces.

[0021] 2. CGLIB bytecode enhancement solution: This solution achieves method interception by generating a subclass of the target class. It can intercept methods of non-final classes (i.e., methods that can be inherited and overridden) and can be applied to frameworks such as Spring AOP (one of the modules of the Spring framework).

[0022] 3. Java Agent technology: This technology utilizes the Java Instrumentation API (a core API provided by Java that allows dynamic modification or monitoring of loaded classes at JVM runtime) to modify bytecode during class loading to intercept methods. It is powerful but complex to implement.

[0023] 4. AspectJ compile-time weaving solution: This solution uses a special compiler or bytecode processing tool to inject interception logic at compile time or class loading time to achieve method interception. It has high performance but poor flexibility.

[0024] 5. Interceptor pattern solution, commonly found in web frameworks (a type of software framework), such as Spring MVC (a web module in the Spring framework), this solution achieves the interception of method calls by configuring an interceptor chain.

[0025] However, existing Java method interception and enhancement technologies generally have the following drawbacks: First, if existing Java method interception and enhancement technologies are needed to implement method interception, each interception needs to be pre-coded. Furthermore, if the code related to method interception and enhancement needs to be added, deleted, or modified later, it needs to be re-coded. In other words, most existing Java method interception and enhancement technologies are static interception technologies and lack dynamic interception capabilities. Second, existing Java method interception and enhancement technologies have many limitations, such as being supported only in the Spring framework and being limited to intercepting interface methods.

[0026] To address this, the present invention proposes a code enhancement method to solve, or at least partially solve, the problems existing in the prior art.

[0027] This invention provides a code enhancement method applied to an electronic device. The electronic device is equipped with an agent tool, which can be a Java Agent. The Java Agent can enhance specified locations based on Java's Instrumentation and ClassFileTransformer (an interface used to dynamically modify bytecode during class loading). However, existing Java Agent technology can only achieve static enhancement. This invention implements dynamic enhancement of the Java Agent by establishing a listener. The specific process is as follows: Reference Figure 1 The above is a flowchart illustrating the steps of an embodiment of a code enhancement method according to the present invention, including the following steps: Step 101: Obtain the enhancement conditions, enhancement actions, enhancement locations of the code, and interception code.

[0028] Step 102: Insert the intercept code at the enhanced location of the code.

[0029] Step 103: Establish a listener based on the enhancement conditions and the enhancement actions. The listener is associated with the enhancement location of the code.

[0030] Step 104: When executing the intercept code, based on the enhancement location of the code corresponding to the intercept code and the association between the listener and the enhancement location of the code, determine the target listener associated with the enhancement location of the code corresponding to the intercept code from the listeners; the target listener has target enhancement conditions and target enhancement actions.

[0031] Step 105: Execute the target enhancement action according to the target enhancement conditions to obtain the enhancement result.

[0032] In step 101 of this embodiment, the code is Java code containing Java methods. The proxy tool first obtains the enhancement location corresponding to the Java method code that needs to be intercepted, as well as the enhancement conditions, enhancement actions, and interception code corresponding to the enhancement location.

[0033] Here, "enhancement location" refers to the specific Java method in the Java code where the interceptor code needs to be inserted; "enhancement condition" refers to the condition that triggers the execution of the enhancement action; and "enhancement action" refers to the specific logic executed after the method is intercepted. Interceptor code, on the other hand, refers to code snippets used to insert custom logic before or after a Java method call during program execution.

[0034] In step 102, after determining which specific Java method in the Java code needs to have the interception code inserted, that is, after determining the enhancement location, the interception code is inserted at the corresponding Java method.

[0035] In one example, the intercept code format can be as follows, both of which are used to insert custom logic before or after the execution of the target Java method: `Inteceptor.interceptBefore(Object objOrCls, Object[]params)` and `Inteceptor.interceptAfter(Object objOrCls, Object[] params, Object result)`. The parameter `Object objOrCls` represents the object or class to which the enhancement point belongs, the parameter `Object[]params` represents the original parameters of the enhancement point, and the parameter `Object result` represents the original return value of the enhancement point. Here, a class is the most basic programming unit in Java, and an object is an instance of a class.

[0036] In step 103, listeners are created for the enhancement conditions and actions corresponding to the Java method where the intercepting code is inserted. The listeners are associated with the enhancement location; that is, there is a relationship between the listener and the enhancement location in the code. In one embodiment, the listeners can be stored in a Map. <String, List <listener>>Middle. Map <String, List <listener>> is a nested collection type in Java that represents a key-value pair mapping where the keys (i.e., Strings) are information related to the augmented location, thus storing the association between listeners and augmented locations.

[0037] In step 104, since a listener is already associated with the enhancement location, during the execution of the Java code, when interception code is detected, it can be confirmed that the code has reached the enhancement location. Based on the enhancement location of the code corresponding to the interception code, and the association between the listener and the enhancement location, a target listener associated with that enhancement location is searched from the listeners. If a target listener associated with that enhancement location is found, it indicates that a user intends to intercept and execute the corresponding target enhancement conditions and target enhancement actions at this location; if no target listener associated with that enhancement location is found, there is no need to execute the enhancement conditions or enhancement actions.

[0038] In step 105, after identifying the target listener, based on the object or class to which the enhancement location belongs, the parameters of the intercepted Java method, the execution result of the intercepted Java method, and user input data, the corresponding target enhancement action is executed according to the target enhancement conditions. If the above parameters meet the target enhancement conditions, the corresponding target enhancement action is executed to enhance the target Java method, resulting in an enhancement result, which is the execution result of the target enhancement action. If the above parameters do not meet the target enhancement conditions, the corresponding target enhancement action is not executed. In one example, the target enhancement action may include actions such as printing, statistics, and delivery.

[0039] This invention, through the insertion of intercepting code at enhancement locations, establishes listeners associated with these locations based on enhancement conditions and actions. When the code executes, the target listener corresponding to the enhancement location is determined directly based on the association between the listener and the enhancement location. Then, the corresponding target enhancement action is executed according to the target enhancement conditions in the target listener, resulting in the enhancement outcome. This listener-based code interception and enhancement mechanism provides greater flexibility in code insertion and fewer limitations compared to existing Java method interception and enhancement techniques. Furthermore, when adding, deleting, or modifying enhancement locations, conditions, and / or actions, only the corresponding listeners need adjustment; rewriting the original code is unnecessary. This achieves dynamic code interception and significantly improves operational convenience.

[0040] Optionally, the proxy tool communicates with the server, and the server communicates with the enhanced rules platform. Step 101 specifically includes: The enhancement rules are obtained from the server. The enhancement rules include the enhancement conditions, the enhancement actions, and the enhancement locations of the code. The enhancement rules are configured by the enhancement rule platform and uploaded to the server.

[0041] In this embodiment of the invention, the enhancement rule is a rule that includes information necessary for code interception, such as enhancement conditions, enhancement actions, and enhancement locations of the code.

[0042] Reference Figure 2 This is a system architecture diagram of an embodiment of a code enhancement method according to the present invention.

[0043] like Figure 2 As shown, this embodiment of the invention provides an enhanced rule platform and a corresponding server, with the enhanced rule platform and server communicating with each other. The enhanced rule platform provides users with a visual graphical interface, allowing users to configure enhanced rules and perform operations such as adding, deleting, modifying, and querying them.

[0044] In one example, the configuration of the enhanced rules of the enhanced rules platform can be represented as Table 1. Table 1 Configuration details of the enhanced rules

[0045] In the enhanced rules platform, multiple enhanced rules can belong to a single enhanced function. The function description and ncform form configuration are at the enhanced function level. The function description is a detailed description of the enhanced function; ncform is a form tool that, upon configuration, generates a form UI (User Interface) and its interactions. The generated form is used to match and populate user input data with the variables to be filled in the enhanced conditions and actions. The target class is the class of the method to be intercepted. The interception type refers to the stage of execution of the method to be intercepted, including pre-interception and post-interception. Pre-interception is executed as soon as the method is entered, while post-interception is executed before the method returns.

[0046] The rules configured and modified on the enhanced rules platform are synchronously uploaded to the server's database for storage. The server also provides the interfaces required by the proxy tools, which can then access data from the server through these interfaces. In one embodiment, the server can provide three interfaces: the first interface provides the enhanced location to the proxy tool; the second interface provides the user input data corresponding to the enhanced function to the proxy tool; and the third interface provides the enhanced conditions and actions corresponding to the enhanced location to the proxy tool.

[0047] The proxy tool is deployed within a business service, which is a business JVM (Java Virtual Machine). This business service can be deployed on electronic devices. In one embodiment, to prevent the dependencies used by the proxy tool from disrupting the original class loading of the business JVM, a separate class loader is needed to load the proxy tool. Simultaneously, to avoid issues such as the proxy tool not finding the business class during interception, the class loader loading the proxy tool needs to be a subclass of the business class loader. The class loader is a core component of the Java Virtual Machine, specifically responsible for loading bytecode into memory and converting it into JVM-executable objects.

[0048] In one embodiment, the proxy tool may perform the following steps upon initial startup: First, the proxy tool calls the server's interface providing enhancement locations to obtain the enhancement locations of the code and inserts the interception code at the enhancement locations. Next, the proxy tool calls the server's interface providing user input data corresponding to the enhancement functions, establishing a mapping between enhancement functions and user input data, and storing it in memory. This mapping can be used to determine the enhancement functions corresponding to the user input data, thereby enabling operations such as matching user input data with enhancement rules in the enhancement functions and filling in variables to be filled. Then, the proxy tool calls the server's interface providing enhancement conditions and actions corresponding to the enhancement locations, establishes a listener corresponding to the enhancement locations based on the enhancement conditions and actions, and associates the listener with the enhancement locations.

[0049] This invention provides users with an enhanced rule platform where they can directly configure enhanced rules. The enhanced rule platform, server, and proxy tools work together to execute the enhanced rules. Users do not need to write interception code at the enhancement location, which reduces the difficulty of operation for users and simplifies the configuration process of code interception.

[0050] Optionally, step 102 specifically includes: Obtain the pre-interception point and / or post-interception point corresponding to the enhanced location of the code; The interception code is inserted at the pre-interception point and / or the post-interception point.

[0051] When inserting interception code in this embodiment of the invention, both interception before (pre-interception) and interception after (post-interception) can be performed at each enhancement location, or only pre-interception or post-interception can be performed. Different interception types can also be set for each enhancement location. Specifically, based on Java's Instrumentation and ClassFileTransformer, the interception code is inserted into the pre-interception point and / or post-interception point corresponding to the enhancement location, that is, the interception code is inserted into the pre-interception point and / or post-interception point corresponding to the target Java method.

[0052] In this embodiment of the invention, when both pre-interception and post-interception are performed at each enhancement location, by setting both pre-interception and post-interception for each enhancement location, it is possible to simplify the operation of adding new enhancement rules without inserting new interception code when adding enhancement rules of different enhancement types to the same enhancement location in the future. At the same time, setting pre-interception and post-interception directly at each enhancement location also makes it easy to determine whether interception code has already been inserted at that enhancement location, avoiding the repeated insertion of interception code at the enhancement location.

[0053] Optionally, step 103 specifically includes: Generate an enhancement expression based on the enhancement conditions and the enhancement actions; The listener is built based on the enhanced expression.

[0054] For the embodiments of the present invention, dynamic code interception also needs to meet requirements such as executing different enhancement actions for the same enhancement location due to different enhancement conditions, and allowing different users to use it as needed. Therefore, in the embodiments of the present invention, enhancement conditions and enhancement actions can be configured as enhancement expressions based on the Java expression language, or as enhancement scripts based on a scripting language supported by the JVM. When executing enhancement actions according to enhancement conditions, the enhancement expression or enhancement script can be executed directly. In one embodiment, the MVEL (MVFLEX Expression Language) expression language can be selected, and MVEL expression execution code corresponding to the enhancement conditions and enhancement actions can be established in the listener.

[0055] In existing technologies, if an enhanced expression requires constant values, it is typically hard-coded to directly write fixed, non-dynamically changing constant values ​​into the enhanced expression. Since these constant values ​​are fixed, this type of enhanced expression cannot meet the needs of different users for dynamic, on-demand usage. In this embodiment of the invention, a variable to be filled can be set at the original position of the constant value in the enhanced expression. This variable can be a variable from the ncform form configuration. Therefore, when a user inputs data on the enhanced rules platform, the variable to be filled in the enhanced expression of the corresponding enhanced function is matched and filled, achieving the goal of allowing different users to set enhanced expressions as needed.

[0056] This invention provides an embodiment of the invention that sets corresponding enhancement expressions containing enhancement conditions and enhancement actions for enhancement locations. This allows for the execution of differentiated enhancement actions based on different enhancement conditions in the enhancement expressions at the same enhancement location, achieving dynamic code interception at the same enhancement location. Furthermore, by setting variables to be filled in the enhancement expressions, it enables different users to set enhancement expressions as needed, thereby improving the flexibility of enhancement expressions.

[0057] Optionally, step 105 specifically includes: Obtain the input data and the enhancement expression in the target listener, the enhancement expression containing the variable to be filled; The input data is used to populate the variables to be populated in the enhanced expression of the target listener, thus obtaining the target expression; If the target expression satisfies the target enhancement condition, then the target enhancement action corresponding to the target enhancement condition is executed according to the target expression.

[0058] In this embodiment of the invention, since the target enhancement conditions and target enhancement actions of the target listener have been configured as enhancement expressions, after the proxy tool determines the target listener, it calls the server's interface to obtain the input data corresponding to the enhancement function to which the target listener belongs, based on the enhancement function to which the target listener belongs and the mapping between the enhancement function and the user input data. The input data is then used to fill the variables to be filled in the enhancement expression in the target listener to obtain the target expression.

[0059] Specifically, when the interception type at the enhancement location is pre-interception, there is no data on the execution result of the intercepted Java method. The input data may include the object or class to which the enhancement location belongs, the parameters of the intercepted Java method, user input data, etc. When the interception type at the enhancement location is post-interception, the intercepted Java method already has an execution result when the interception is executed. The input data may include the object or class to which the enhancement location belongs, the parameters of the intercepted Java method, the execution result of the intercepted Java method, user input data, etc.

[0060] Determine whether the padded target expression meets the target enhancement conditions. If it does, execute the target enhancement action based on the padded target expression to intercept the target Java method.

[0061] In this embodiment of the invention, after determining the target listener, the input data is filled into the enhancement expression of the target listener. Thus, the enhancement conditions are determined and the enhancement actions are executed based on the input data and the enhancement expression. Since the input data can be determined by the user as needed, dynamic code interception at the same enhancement location is achieved.

[0062] Optionally, the method includes: If a new enhancement location is added to the code, the interception code is inserted into the new enhancement location, and a new listener is established based on the new enhancement conditions and new enhancement actions corresponding to the new enhancement location. If the enhancement location of the code is deleted, then the listener corresponding to the enhancement location of the code is deleted; If the enhancement location of the code is modified, the listener corresponding to the enhancement location of the code is deleted, and a modified listener is created for the enhancement location of the modified code.

[0063] This invention enables dynamic enhancement of positions by manipulating listeners, supporting the addition, deletion, and modification of enhancement positions. After the code tool starts, it monitors changes in enhancement positions. When a change occurs, it calls the corresponding server interface to obtain information about the changed enhancement position and then operates on the listener corresponding to that enhancement position.

[0064] Specifically, since each enhancement location that needs to perform interception has a corresponding listener, when adding a new enhancement location, in addition to inserting interception code in the newly added enhancement location according to the pre-interception and post-interception, it is also necessary to create a new listener according to the new enhancement conditions and new enhancement actions corresponding to the new enhancement location, and associate the new listener with the new enhancement location.

[0065] Regarding the deletion of enhancement positions, since deleting interceptor code usually requires rollback or recoding of the class to which the intercepted method belongs, in this embodiment of the invention, after deleting the enhancement position, the interceptor code will remain at the original enhancement position. The deletion of the enhancement position is achieved only by deleting the listener corresponding to that enhancement position. When the listener at that enhancement position is deleted, although the interceptor code can be detected during code execution, the associated listener cannot be found from the listeners based on that enhancement position, and therefore the target enhancement action will not be executed according to the target enhancement condition, thus achieving the deletion of the enhancement position.

[0066] For modifications to the enhancement location, the interceptor code at the original enhancement location is not modified or deleted. Instead, the listener corresponding to the enhancement location that needs to be modified is deleted, and then a new modified listener is created for the enhancement location of the modified code. This invention enables the addition, deletion, and modification of enhancement locations in the code by adding, deleting, and modifying listeners. It supports dynamic operations such as adding, deleting, and modifying enhancement locations without recoding the code.

[0067] Optionally, the method includes: If a new enhancement rule is added, the interception code is inserted into the enhancement location of the code corresponding to the new enhancement rule, and a new listener is established according to the new enhancement rule; If the enhancement rule is deleted, then the listener corresponding to the enhancement location of the code in the enhancement rule is deleted; If the enhancement rule is modified, the listener corresponding to the enhancement position of the code in the enhancement rule is deleted, and a modified listener is created for the modified enhancement rule.

[0068] This invention enables dynamic enhancement rules through the operation of listeners, supporting the addition, deletion, and modification of enhancement rules. After the code tool starts, it needs to monitor changes in enhancement rules. When a rule changes, it calls the corresponding server interface to obtain information about the changed rule and then operates on the listener corresponding to that rule.

[0069] Specifically, similar to the dynamic operation of enhancement positions, if a new enhancement rule is added, interceptor code is inserted into the enhancement position corresponding to the new enhancement rule. A new listener is created based on the enhancement conditions and actions in the new enhancement rule, and the new listener is associated with the enhancement position corresponding to the new enhancement rule. If an enhancement rule is deleted, the listener corresponding to the enhancement position in the deleted enhancement rule is directly deleted, and the interceptor code at that enhancement position is not processed. If an enhancement rule is modified, the listener corresponding to the enhancement position in the enhancement rule before the modification is directly deleted, the interceptor code at that enhancement position is not processed, and a new listener is created based on the enhancement conditions and actions in the modified enhancement rule. This new listener is associated with the enhancement position in the modified enhancement rule.

[0070] This invention enables dynamic operations such as adding, deleting, and modifying enhancement rules within the code without requiring recoding, by adding, deleting, and modifying listeners.

[0071] In one embodiment of the present invention, the code tool can also achieve dynamic input data processing. After the code tool starts, it needs to monitor changes in the input data. When the input data changes, it calls the corresponding interface of the server to obtain information about the changed input data and overwrites and saves the changed input data in memory, thereby achieving dynamic operation on the input data. If input data becomes invalid due to modifications such as enhanced functions or rules, the server processes the input data first to ensure that the interface only returns usable input data.

[0072] This invention, through the insertion of intercepting code at enhancement locations, establishes listeners associated with these locations based on enhancement conditions and actions. When the code executes, the target listener corresponding to the enhancement location is determined directly based on the association between the listener and the enhancement location. Then, the corresponding target enhancement action is executed according to the target enhancement conditions in the target listener, resulting in the enhancement outcome. This listener-based code interception and enhancement mechanism provides greater flexibility in code insertion and fewer limitations compared to existing Java method interception and enhancement techniques. Furthermore, when adding, deleting, or modifying enhancement locations, conditions, and / or actions, only the corresponding listeners need adjustment; rewriting the original code is unnecessary. This achieves dynamic code interception and significantly improves operational convenience.

[0073] It should be noted that, for the sake of simplicity, the method embodiments are all described as a series of actions. However, those skilled in the art should understand that the embodiments of the present invention are not limited to the described order of actions, because according to the embodiments of the present invention, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions involved are not necessarily essential to the embodiments of the present invention.

[0074] Based on the above embodiments, this embodiment also provides a code enhancement device that can be applied to electronic devices such as terminal devices and servers.

[0075] Reference Figure 3 The diagram illustrates a structural block diagram of an embodiment of a code enhancement device according to the present invention, which may specifically include the following modules: Rule acquisition module 301 is used to acquire enhancement conditions, enhancement actions, enhancement locations of code, and interception code; The code insertion module 302 is used to insert the interception code at the enhanced location of the code; The listener creation module 303 is used to create a listener based on the enhancement conditions and the enhancement actions, wherein there is an association between the listener and the enhancement location of the code; The first interception module 304 is used to, when executing the interception code, determine, from the listeners, a target listener associated with the enhancement position of the code corresponding to the interception code, based on the enhancement position of the code corresponding to the interception code and the association relationship between the listener and the enhancement position of the code; the target listener has target enhancement conditions and target enhancement actions; The second interception module 305 is used to perform the target enhancement action according to the target enhancement conditions to obtain the enhancement result.

[0076] Optionally, the listener establishment module 303 includes: An expression generation submodule is used to generate an enhancement expression based on the enhancement conditions and the enhancement actions; The listener generation submodule is used to build the listener based on the enhanced expression.

[0077] Optionally, the second interception module 305 includes: The input data acquisition submodule is used to acquire input data and the enhancement expression in the target listener, wherein the enhancement expression contains variables to be filled. The variable filling submodule is used to fill the variables to be filled in the enhanced expression in the target listener with the input data to obtain the target expression; The enhancement condition judgment submodule is used to execute the target enhancement action corresponding to the target enhancement condition according to the target expression if the target expression satisfies the target enhancement condition.

[0078] Optionally, the device includes: The location addition module is used to insert the interception code into the newly added enhancement location of the code if a new enhancement location of the code is added, and to create a new listener according to the new enhancement conditions and new enhancement actions corresponding to the newly added enhancement location of the code. The location deletion module is used to delete the listener corresponding to the enhancement location of the code if the enhancement location of the code is deleted. The location modification module is used to delete the listener corresponding to the enhancement location of the code if the enhancement location of the code is modified, and to create a modified listener for the enhancement location of the modified code.

[0079] Optionally, the proxy tool is connected to the server, the server is connected to the enhanced rule platform, and the rule acquisition module 301 includes: The platform rule acquisition submodule is used to obtain enhancement rules from the server. The enhancement rules include the enhancement conditions, the enhancement actions, and the enhancement location of the code. The enhancement rules are configured by the enhancement rule platform and uploaded to the server.

[0080] Optionally, the device includes: The rule addition module is used to insert the interception code into the enhancement position of the code corresponding to the new enhancement rule if a new enhancement rule is added, and to create a new listener according to the new enhancement rule; The rule deletion module is used to delete the listener corresponding to the enhancement position of the code in the enhancement rule if the enhancement rule is deleted. The rule modification module is used to delete the listener corresponding to the enhancement position of the code in the enhancement rule if the enhancement rule is modified, and to create a modified listener for the modified enhancement rule.

[0081] Optionally, the code insertion module 302 includes: The interception point acquisition submodule is used to acquire the pre-interception point and / or post-interception point corresponding to the enhancement position of the code; An interception point insertion submodule is used to insert the interception code at the pre-interception point and / or the post-interception point.

[0082] This invention also provides an electronic device, such as... Figure 4 As shown, it includes a processor 401, a communication interface 402, a memory 403, and a communication bus 404, wherein the processor 401, the communication interface 402, and the memory 403 communicate with each other through the communication bus 404. Memory 403 is used to store computer programs; When processor 401 executes the program stored in memory 403, it performs the following steps: Get the enhancement conditions, enhancement actions, enhancement locations in the code, and interception code; Insert the intercept code at the enhanced location of the code; A listener is established based on the enhancement conditions and the enhancement actions, and there is an association between the listener and the enhancement location of the code; When the intercept code is executed, based on the enhancement location of the code corresponding to the intercept code and the association between the listener and the enhancement location of the code, a target listener associated with the enhancement location of the code corresponding to the intercept code is determined from the listeners; the target listener has target enhancement conditions and target enhancement actions; The target enhancement action is performed according to the target enhancement conditions to obtain the enhancement result.

[0083] Optionally, establishing a listener based on the enhancement conditions and the enhancement action includes: Generate an enhancement expression based on the enhancement conditions and the enhancement actions; The listener is built based on the enhanced expression.

[0084] Optionally, performing the target enhancement action according to the target enhancement conditions includes: Obtain the input data and the enhancement expression in the target listener, the enhancement expression containing the variable to be filled; The input data is used to populate the variables to be populated in the enhanced expression of the target listener, thus obtaining the target expression; If the target expression satisfies the target enhancement condition, then the target enhancement action corresponding to the target enhancement condition is executed according to the target expression.

[0085] Optionally, the method includes: If a new enhancement location is added to the code, the interception code is inserted into the new enhancement location, and a new listener is established based on the new enhancement conditions and new enhancement actions corresponding to the new enhancement location. If the enhancement location of the code is deleted, then the listener corresponding to the enhancement location of the code is deleted; If the enhancement location of the code is modified, the listener corresponding to the enhancement location of the code is deleted, and a modified listener is created for the enhancement location of the modified code.

[0086] Optionally, the proxy tool communicates with the server, and the server communicates with the enhancement rule platform. The step of obtaining the enhancement conditions, enhancement actions, and the enhancement location of the code includes: The enhancement rules are obtained from the server. The enhancement rules include the enhancement conditions, the enhancement actions, and the enhancement locations of the code. The enhancement rules are configured by the enhancement rule platform and uploaded to the server.

[0087] Optionally, the method includes: If a new enhancement rule is added, the interception code is inserted into the enhancement location of the code corresponding to the new enhancement rule, and a new listener is established according to the new enhancement rule; If the enhancement rule is deleted, then the listener corresponding to the enhancement location of the code in the enhancement rule is deleted; If the enhancement rule is modified, the listener corresponding to the enhancement position of the code in the enhancement rule is deleted, and a modified listener is created for the modified enhancement rule.

[0088] Optionally, inserting the intercept code at the enhanced location of the code includes: Obtain the pre-interception point and / or post-interception point corresponding to the enhanced location of the code; The interception code is inserted at the pre-interception point and / or the post-interception point.

[0089] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.

[0090] The communication interface is used for communication between the aforementioned terminal and other devices.

[0091] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.

[0092] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.

[0093] In another embodiment of the present invention, a computer-readable storage medium is also provided, which stores instructions that, when executed on a computer, cause the computer to perform the enhanced method of the code described in any of the above embodiments.

[0094] In yet another embodiment of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute the enhanced method of the code described in any of the above embodiments.

[0095] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium accessible to a computer or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state disk (SSD)).

[0096] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0097] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.

[0098] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.< / listener> < / listener>

Claims

1. A method for enhancing code, characterized in that, The method, applied to an electronic device with deployed agent tools, includes: Get the enhancement conditions, enhancement actions, enhancement locations in the code, and interception code; Insert the intercept code at the enhanced location of the code; A listener is established based on the enhancement conditions and the enhancement actions, and there is an association between the listener and the enhancement location of the code; When the intercept code is executed, based on the enhancement location of the code corresponding to the intercept code and the association between the listener and the enhancement location of the code, a target listener associated with the enhancement location of the code corresponding to the intercept code is determined from the listeners; the target listener has target enhancement conditions and target enhancement actions; The target enhancement action is performed according to the target enhancement conditions to obtain the enhancement result.

2. The method according to claim 1, characterized in that, The step of establishing a listener based on the enhancement conditions and the enhancement actions includes: Generate an enhancement expression based on the enhancement conditions and the enhancement actions; The listener is built based on the enhanced expression.

3. The method according to claim 2, characterized in that, The step of performing the target enhancement action according to the target enhancement conditions includes: Obtain the input data and the enhancement expression in the target listener, the enhancement expression containing the variable to be filled; The input data is used to populate the variables to be populated in the enhanced expression of the target listener, thus obtaining the target expression; If the target expression satisfies the target enhancement condition, then the target enhancement action corresponding to the target enhancement condition is executed according to the target expression.

4. The method according to claim 1, characterized in that, The method includes: If a new enhancement location is added to the code, the interception code is inserted into the new enhancement location, and a new listener is established based on the new enhancement conditions and new enhancement actions corresponding to the new enhancement location. If the enhancement location of the code is deleted, then the listener corresponding to the enhancement location of the code is deleted; If the enhancement location of the code is modified, the listener corresponding to the enhancement location of the code is deleted, and a modified listener is created for the enhancement location of the modified code.

5. The method according to claim 1, characterized in that, The proxy tool communicates with the server, and the server communicates with the enhancement rule platform. The process of obtaining enhancement conditions, enhancement actions, and the enhancement location of the code includes: The enhancement rules are obtained from the server. The enhancement rules include the enhancement conditions, the enhancement actions, and the enhancement location of the code. The enhancement rules are configured by the enhancement rule platform and uploaded to the server.

6. The method according to claim 5, characterized in that, The method includes: If a new enhancement rule is added, the interception code is inserted into the enhancement location of the code corresponding to the new enhancement rule, and a new listener is established according to the new enhancement rule; If the enhancement rule is deleted, then the listener corresponding to the enhancement location of the code in the enhancement rule is deleted; If the enhancement rule is modified, the listener corresponding to the enhancement position of the code in the enhancement rule is deleted, and a modified listener is created for the modified enhancement rule.

7. The method according to claim 1, characterized in that, Inserting the intercept code at the enhanced location of the code includes: Obtain the pre-interception point and / or post-interception point corresponding to the enhanced location of the code; The interception code is inserted at the pre-interception point and / or the post-interception point.

8. A code enhancement device, characterized in that, An electronic device deployed with agent tools, the device comprising: The rule acquisition module is used to acquire enhancement conditions, enhancement actions, enhancement locations in the code, and interception code; A code insertion module is used to insert the intercept code at the enhanced location of the code; A listener creation module is used to create listeners based on the enhancement conditions and the enhancement actions, wherein there is an association between the listeners and the enhancement locations in the code; The first interception module is used, when executing the interception code, to determine, from the listeners, a target listener associated with the enhancement position of the code corresponding to the interception code, based on the enhancement position of the code corresponding to the interception code and the association relationship between the listener and the enhancement position of the code; the target listener has target enhancement conditions and target enhancement actions; The second interception module is used to execute the target enhancement action according to the target enhancement conditions to obtain the enhancement result.

9. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the steps of the method described in any one of claims 1-7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.