Code calling method and device
Technical Field
The present disclosure relates to the field of computer technologies, and in particular, to a code calling method and apparatus.
Background
With the popularization of the internet, content information is more and more complex, the number of users and the access amount are more and more, and services and scenes are more and more. For each service scene, developers often need to develop various codes, and when the same function code is referred to by different service codes, the function code and each service code are often required to be coupled in the same method body, so that the method has strong invasiveness, and the risk of bug occurrence during service logic modification is increased. Moreover, when the function code needs to be deleted, the service code still needs to be changed, further increasing the risk of bug occurrence in the complex system.
It is to be noted that the information disclosed in the above background section is only for enhancement of understanding of the background of the present disclosure, and thus may include information that does not constitute prior art known to those of ordinary skill in the art.
Disclosure of Invention
The present disclosure is directed to a code calling method and a code calling apparatus for overcoming, at least to some extent, the problems of high intrusiveness and increased system reliability risk in a code reference process due to limitations and disadvantages of the related art.
According to a first aspect of the embodiments of the present disclosure, there is provided a code calling method, including: responding to a method calling message, and detecting whether a called Java method comprises a preset annotation, wherein the preset annotation records executable logic; if the called method comprises preset annotations, executing all the executable logics in all the preset annotations; and executing the service code in the called method according to the execution result of the executable logic.
In an exemplary embodiment of the disclosure, the executable logic includes caching logic, and the caching logic includes a caching key, a caching expiration time, a caching type, a caching operation type, a caching execution order, and a custom parameter.
In an exemplary embodiment of the present disclosure, the preset annotation includes a preset annotation group, and the preset annotation group includes a plurality of preset annotations having a set execution order.
In an exemplary embodiment of the disclosure, the executing the service code in the called method according to the execution result of the executable logic includes:
when the executable logic is used for reading cache data, executing the service code according to the cache data;
and when the executable logic is used for storing cache data, executing the service code to obtain the cache data and storing the cache data into a cache.
In an exemplary embodiment of the present disclosure, the preset annotations are manually added from annotation templates in the custom annotation library.
In an exemplary embodiment of the disclosure, the executing the service code in the called method according to the execution result of the executable logic includes:
and determining whether to execute a part or all of the service codes according to the execution result.
In an exemplary embodiment of the present disclosure, said executing all of said executable logic of all of said preset annotations includes:
executing the executable logic in multiple preset annotations simultaneously.
According to a second aspect of the embodiments of the present disclosure, there is provided a code calling apparatus including:
the calling interception module is set to respond to the method calling message and detect whether the called Java method comprises a preset annotation, and the preset annotation records executable logic; an annotation execution module configured to execute all of the executable logic in all of the preset annotations if the called method includes a preset annotation; and the service code execution module is set to execute the service code in the called method according to the execution result of the executable logic.
According to a third aspect of the present disclosure, there is provided an electronic device comprising: a memory; and a processor coupled to the memory, the processor configured to perform the method of any of the above based on instructions stored in the memory.
According to a fourth aspect of the present disclosure, there is provided a computer-readable storage medium having stored thereon a program which, when executed by a processor, implements a code calling method as recited in any one of the above.
According to the code calling method provided by the embodiment of the disclosure, the executable code modified according to the template is arranged in the annotation, and the executable code influences the business code by adding the annotation, so that the intrusion of the code calling to the business code can be avoided, the function code can be flexibly added, deleted and modified, and the reliability of the system is improved.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and together with the description, serve to explain the principles of the disclosure. It is to be understood that the drawings in the following description are merely exemplary of the disclosure, and that other drawings may be derived from those drawings by one of ordinary skill in the art without the exercise of inventive faculty.
FIG. 1 is a flow chart of a code invocation method in an exemplary embodiment of the present disclosure.
Fig. 2A and 2B are schematic diagrams of implementing caching logic in exemplary embodiments of the disclosure.
Fig. 3 is a block diagram of a code invocation device in an exemplary embodiment of the present disclosure.
Fig. 4 is a block diagram of an electronic device in an exemplary embodiment of the present disclosure.
FIG. 5 is a schematic diagram of a computer-readable storage medium in an exemplary embodiment of the disclosure.
Detailed Description
Example embodiments will now be described more fully with reference to the accompanying drawings. Example embodiments may, however, be embodied in many different forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the concept of example embodiments to those skilled in the art. The described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments. In the following description, numerous specific details are provided to give a thorough understanding of embodiments of the disclosure. One skilled in the relevant art will recognize, however, that the subject matter of the present disclosure can be practiced without one or more of the specific details, or with other methods, components, devices, steps, and the like. In other instances, well-known technical solutions have not been shown or described in detail to avoid obscuring aspects of the present disclosure.
Further, the drawings are merely schematic illustrations of the present disclosure, in which the same reference numerals denote the same or similar parts, and thus, a repetitive description thereof will be omitted. Some of the block diagrams shown in the figures are functional entities and do not necessarily correspond to physically or logically separate entities. These functional entities may be implemented in the form of software, or in one or more hardware modules or integrated circuits, or in different networks and/or processor devices and/or microcontroller devices.
The following detailed description of exemplary embodiments of the disclosure refers to the accompanying drawings.
Fig. 1 schematically shows a flowchart of a code calling method in an exemplary embodiment of the present disclosure. Referring to fig. 1, a code invocation method 100 may include:
step S102, responding to a method calling message, and detecting whether a called Java method comprises a preset annotation, wherein the preset annotation records an executable logic;
step S104, if the called method comprises preset annotations, all executable logics in all the preset annotations are executed;
and step S106, executing the service code in the called method according to the execution result of the executable logic.
According to the code calling method provided by the embodiment of the disclosure, the executable code modified according to the template is arranged in the annotation, and the executable code influences the business code by adding the annotation, so that the intrusion of the code calling to the business code can be avoided, the function code can be flexibly added, deleted and modified, and the reliability of the system is improved.
The steps of the code calling method 100 are explained in detail below.
In step S102, in response to the method invocation message, it is detected whether the invoked Java method includes a preset annotation recorded with executable logic.
The method provided by the present disclosure can be completed by an independently provided code controller. The code controller can monitor Java method call messages, intercept the called Java methods, and detect whether the Java methods include preset annotations.
While annotations are data in Java that describes data, typically used to describe methods, in embodiments of the present disclosure the annotations serve as access interfaces to executable logic to invoke executable logic of non-business code with the ease of annotation addition, modification, and deletion. In the disclosed embodiments, the annotations are rewritten or, as it were, the executable code is presented in the form of annotations. After intercepting the method call message, the code controller detects whether the method body includes the preset annotation recorded with the executable logic, and if so, the step S104 is entered.
Such pre-set code including executable logic may be pre-programmed as templates and stored in a custom annotation library for different business code calls. When the preset annotation is added, the template in the annotation library is only required to be called, and the annotation is rewritten according to the specific service code.
The addition of the calling function can be realized by adding the preset annotation, and due to the non-invasiveness of the annotation, the process of adding the annotation does not influence the business code structure, so that the non-invasiveness of code calling is realized. When the calling function needs to be deleted, the deletion of the function can be conveniently realized only by deleting the preset annotation, the service code does not need to be changed, and the system reliability risk is reduced. If the called function needs to be modified, only the annotation needs to be modified, and the business code structure is not affected.
In step S104, if the called method includes a preset annotation, all the executable logic in all the preset annotations is executed.
Different from a conventional code calling method, in the embodiment of the present disclosure, all the preset annotations are detected before the business code is run, and all executable codes in all the preset annotations are executed before the business code is run, so as to determine the execution mode of the business logic according to the execution result of the executable logic.
In one embodiment of the present disclosure, the executable logic comprises caching logic comprising caching keys, caching expiration time, caching type, caching operation type, caching execution order, custom parameters, and the like. That is, in this embodiment, the preset annotations may be used to perform cache operations of the type, e.g., store data or fetch data. At this time, one preset annotation may be set to perform only the caching operation, or may be set to perform a series of executable logics including the caching operation. When the preset annotation is only used for executing the caching operation, the caching operations of different operation types, different expiration times and different data types can be executed in sequence.
In order to further improve the code calling efficiency, a preset annotation group can be further arranged, and the executable codes of a plurality of functions are sequentially executed through the preset annotation group so as to realize complex functions.
The preset annotation group can be added through a template in the annotation library, and can also be formed by temporarily combining a plurality of annotation templates. The preset annotation group comprises a plurality of preset annotations, and the execution sequence of the preset annotations is set when the preset annotation group is generated. When a plurality of preset annotations in the preset annotation group are all set to execute caching operation, the preset annotation group can realize the function of the caching group.
In addition, because a plurality of preset annotations or preset annotation groups are searched at the same time, executable logics in different preset annotation groups can be executed at the same time, so that the code calling efficiency is improved.
In step S106, the service code in the called method is executed according to the execution result of the executable logic.
When the preset annotation is used for executing the cache logic, the way that the business code is influenced by the execution result of the preset annotation can be controlled according to the actual situation. For example, when the executable logic is used to read cached data, the business code is executed according to the cached data; when the executable logic is used for storing the cache data, the service code is executed to obtain the cache data and store the cache data into the cache. That is, if the executable code needs to use the service code to store data, the data may be stored in the cache after the service code is executed and the data is related.
Fig. 2A and 2B are schematic diagrams of implementing caching logic in an embodiment of the disclosure.
Referring to fig. 2A, when the data reading operation is performed with the preset annotation, the service code does not need to be modified, and only the data obtained by performing the preset annotation in advance needs to be directly taken when the service code is executed.
Referring to fig. 2B, when the operation of storing data is performed with the preset annotations, once the operation of storing data is involved in the process of executing the business logic, the code controller executes the operation of storing data in the preset annotations in parallel while the business logic is normally executed, without affecting the execution of the business logic. At this time, adding, deleting and modifying the relevant cache logic code will not affect the service code.
Whether to execute a portion or all of the business code may be determined based on the execution results of the executable logic. For example, it may be set that a condition judgment is required for execution of a certain module in the service code, and an execution result of the executable logic in the preset annotation is set as a judgment key, so as to implement overall control of the service code. In addition, the execution sequence of each module in the service code can be controlled according to the execution result of the executable logic.
In summary, the code calling method provided by the embodiment of the present disclosure can implement flexible and non-intrusive function calling, control the function and execution of the service code on the premise of not modifying the service code, and flexibly add and delete functions, thereby greatly improving the reliability and development efficiency of the system.
Corresponding to the above method embodiment, the present disclosure also provides a code invoking device, which may be used to execute the above method embodiment.
Fig. 3 schematically illustrates a block diagram of a code invoking device in an exemplary embodiment of the present disclosure.
Referring to fig. 3, the code calling apparatus 300 may include:
a call interception module 302 configured to respond to a method call message and detect whether a called Java method includes a preset annotation, where the preset annotation records an executable logic;
an annotation execution module 304 configured to execute all of the executable logic of all of the preset annotations if the called method includes a preset annotation;
a service code execution module 306 configured to execute the service code in the called method according to the execution result of the executable logic.
In an exemplary embodiment of the disclosure, the executable logic includes caching logic, and the caching logic includes a caching key, a caching expiration time, a caching type, a caching operation type, a caching execution order, and a custom parameter.
In an exemplary embodiment of the present disclosure, the preset annotation includes a preset annotation group, and the preset annotation group includes a plurality of preset annotations having a set execution order.
In an exemplary embodiment of the disclosure, the service code execution module 306 is configured to:
when the executable logic is used for reading cache data, executing the service code according to the cache data;
and when the executable logic is used for storing cache data, executing the service code to obtain the cache data and storing the cache data into a cache.
In an exemplary embodiment of the present disclosure, the preset annotations are manually added from annotation templates in the custom annotation library.
In an exemplary embodiment of the disclosure, the service code execution module 306 is configured to:
and determining whether to execute a part or all of the service codes according to the execution result.
In an exemplary embodiment of the disclosure, the annotation execution module 304 is arranged to:
executing the executable logic in multiple preset annotations simultaneously.
Since the functions of the apparatus 300 have been described in detail in the corresponding method embodiments, the disclosure is not repeated herein.
It should be noted that although in the above detailed description several modules or units of the device for action execution are mentioned, such a division is not mandatory. Indeed, the features and functionality of two or more modules or units described above may be embodied in one module or unit, according to embodiments of the present disclosure. Conversely, the features and functions of one module or unit described above may be further divided into embodiments by a plurality of modules or units.
In an exemplary embodiment of the present disclosure, an electronic device capable of implementing the above method is also provided.
As will be appreciated by one skilled in the art, aspects of the present invention may be embodied as a system, method or program product. Thus, various aspects of the invention may be embodied in the form of: an entirely hardware embodiment, an entirely software embodiment (including firmware, microcode, etc.) or an embodiment combining hardware and software aspects that may all generally be referred to herein as a "circuit," module "or" system.
An electronic device 400 according to this embodiment of the invention is described below with reference to fig. 4. The electronic device 400 shown in fig. 4 is only an example and should not bring any limitation to the function and the scope of use of the embodiments of the present invention.
As shown in fig. 4, electronic device 400 is embodied in the form of a general purpose computing device. The components of electronic device 400 may include, but are not limited to: the at least one processing unit 410, the at least one memory unit 420, and a bus 430 that couples various system components including the memory unit 420 and the processing unit 410.
Wherein the storage unit stores program code that is executable by the processing unit 410 to cause the processing unit 410 to perform steps according to various exemplary embodiments of the present invention as described in the above section "exemplary methods" of the present specification. For example, the processing unit 410 may execute step S102 as shown in fig. 1: responding to a method calling message, and detecting whether a called Java method comprises a preset annotation, wherein the preset annotation records executable logic; step S104: if the called method comprises preset annotations, executing all the executable logics in all the preset annotations; step S104: and executing the service code in the called method according to the execution result of the executable logic.
The storage unit 420 may include readable media in the form of volatile storage units, such as a random access memory unit (RAM)4201 and/or a cache memory unit 4202, and may further include a read only memory unit (ROM) 4203.
The storage unit 420 may also include a program/utility 4204 having a set (at least one) of program modules 4205, such program modules 4205 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which, or some combination thereof, may comprise an implementation of a network environment.
Bus 430 may be any bus representing one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
The electronic device 400 may also communicate with one or more external devices 700 (e.g., keyboard, pointing device, bluetooth device, etc.), with one or more devices that enable a user to interact with the electronic device 400, and/or with any devices (e.g., router, modem, etc.) that enable the electronic device 400 to communicate with one or more other computing devices. Such communication may occur via input/output (I/O) interfaces 450. Also, the electronic device 400 may communicate with one or more networks (e.g., a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network, such as the internet) via the network adapter 460. As shown, the network adapter 460 communicates with the other modules of the electronic device 400 over the bus 430. It should be appreciated that although not shown in the figures, other hardware and/or software modules may be used in conjunction with electronic device 400, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, among others.
Through the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein may be implemented by software, or by software in combination with necessary hardware. Therefore, the technical solution according to the embodiments of the present disclosure may be embodied in the form of a software product, which may be stored in a non-volatile storage medium (which may be a CD-ROM, a usb disk, a removable hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which may be a personal computer, a server, a terminal device, or a network device, etc.) to execute the method according to the embodiments of the present disclosure.
In an exemplary embodiment of the present disclosure, there is also provided a computer-readable storage medium having stored thereon a program product capable of implementing the above-described method of the present specification. In some possible embodiments, aspects of the invention may also be implemented in the form of a program product comprising program code means for causing a terminal device to carry out the steps according to various exemplary embodiments of the invention described in the above section "exemplary methods" of the present description, when said program product is run on the terminal device.
Referring to fig. 5, a program product 500 for implementing the above method according to an embodiment of the present invention is described, which may employ a portable compact disc read only memory (CD-ROM) and include program code, and may be run on a terminal device, such as a personal computer. However, the program product of the present invention is not limited in this regard and, in the present document, a readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium include: an electrical connection having one or more wires, a portable disk, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
A computer readable signal medium may include a propagated data signal with readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A readable signal medium may also be any readable medium that is not a readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Program code for carrying out operations for aspects of the present invention may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C + + or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device and partly on a remote computing device, or entirely on the remote computing device or server. In the case of a remote computing device, the remote computing device may be connected to the user computing device through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computing device (e.g., through the internet using an internet service provider).
Furthermore, the above-described figures are merely schematic illustrations of processes involved in methods according to exemplary embodiments of the invention, and are not intended to be limiting. It will be readily understood that the processes shown in the above figures are not intended to indicate or limit the chronological order of the processes. In addition, it is also readily understood that these processes may be performed synchronously or asynchronously, e.g., in multiple modules.
Other embodiments of the disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This application is intended to cover any variations, uses, or adaptations of the disclosure following, in general, the principles of the disclosure and including such departures from the present disclosure as come within known or customary practice within the art to which the disclosure pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the disclosure being indicated by the following claims.