CN113791767B - Byte code modification method and device - Google Patents

Byte code modification method and device Download PDF

Info

Publication number
CN113791767B
CN113791767B CN202111100933.4A CN202111100933A CN113791767B CN 113791767 B CN113791767 B CN 113791767B CN 202111100933 A CN202111100933 A CN 202111100933A CN 113791767 B CN113791767 B CN 113791767B
Authority
CN
China
Prior art keywords
byte code
task
modification
code modification
modification task
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202111100933.4A
Other languages
Chinese (zh)
Other versions
CN113791767A (en
Inventor
秦光辉
李涛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhengzhou Apas Digital Cloud Information Technology Co ltd
Original Assignee
Zhengzhou Apas Digital Cloud Information Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhengzhou Apas Digital Cloud Information Technology Co ltd filed Critical Zhengzhou Apas Digital Cloud Information Technology Co ltd
Priority to CN202111100933.4A priority Critical patent/CN113791767B/en
Publication of CN113791767A publication Critical patent/CN113791767A/en
Application granted granted Critical
Publication of CN113791767B publication Critical patent/CN113791767B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management
    • G06F8/73Program documentation

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Library & Information Science (AREA)
  • Computing Systems (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The application discloses a method and a device for modifying byte codes, wherein the method comprises the following steps: acquiring and compiling the object codes added with the annotations to obtain a plurality of class files containing the annotations, wherein the annotation content comprises a modification task type and byte code information to be modified; analyzing the annotations in the plurality of class files, determining the adding position information and the annotation content of the annotations in the plurality of class files, and generating a byte code modification task configuration file according to the adding position information and the annotation content; generating a plurality of bytecode modification tasks based on the bytecode modification task configuration file; traversing the plurality of class files and performing a first specifying step on a current class file, the first specifying step comprising: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file. The method and the device can improve the byte code modification efficiency.

Description

Byte code modification method and device
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method and an apparatus for modifying a byte code.
Background
Bytecode modification generally refers to a technique of inserting or replacing a piece of code into an original code after compiling the source code into a bytecode (Class) before Android generates a dex file. The purpose of applying this technique is to modify a Class file to modify or enhance the logic of the original code. The technology is commonly used in the fields of thermal repair, plug-in, bug repair of third-party jar files, performance monitoring and the like.
Currently, a compiler is often used for byte code modification. However, this modification requires a large amount of manual operations, and is inefficient.
Disclosure of Invention
The embodiment of the application provides a method and a device for modifying byte codes, which are used for improving the efficiency of modifying byte codes.
In a first aspect, an embodiment of the present application provides a method for modifying a bytecode, including:
Acquiring and compiling target codes added with annotations to obtain a plurality of class files containing the annotations, wherein the annotation content of the annotations comprises a modification task type and byte code information to be modified;
analyzing the annotations in the plurality of class files, determining the adding position information and the annotation content of the annotations in the plurality of class files, and generating a byte code modification task configuration file based on the adding position information and the annotation content of the annotations in the plurality of class files;
Generating a plurality of byte code modification tasks based on the byte code modification task configuration file;
Traversing the plurality of class files and executing a first designating step on the current class file, wherein the first designating step comprises: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file.
In a second aspect, an embodiment of the present application further provides a device for modifying a bytecode, including:
The compiling module is used for acquiring and compiling the object codes added with the annotations to obtain a plurality of class files containing the annotations, wherein the annotation content of the annotations comprises a modification task type and byte code information to be modified;
The annotation analysis module is used for analyzing the annotations in the plurality of class files, determining the adding position information and the annotation content of the annotations in the plurality of class files, and generating a byte code modification task configuration file based on the adding position information and the annotation content of the annotations in the plurality of class files;
The task generating module is used for generating a plurality of byte code modification tasks based on the byte code modification task configuration file;
The task execution module is used for traversing the plurality of class files and executing a first appointed step on the current class file, wherein the first appointed step comprises the following steps: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file.
In a third aspect, an embodiment of the present application further provides an electronic device, including: a memory, a processor and a computer program stored on the memory and executable on the processor, which when executed by the processor performs the steps of the method according to the first aspect.
In a fourth aspect, embodiments of the present application also provide a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, implements the steps of the method according to the first aspect.
The above at least one technical scheme adopted by the embodiment of the application can automatically analyze the notes in the object code to generate the byte code modification task after the notes in the object code are compiled to obtain a plurality of class files of the object code, and automatically execute the byte code modification task to complete the byte code modification, so that the whole process has high automation degree, the byte code modification process can be simplified, the byte code modification efficiency can be improved, and the development threshold of the application corresponding to the object code can be reduced.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this specification, illustrate embodiments of the application and together with the description serve to explain the application and do not constitute a limitation on the application. In the drawings:
Fig. 1 is a schematic diagram of a modification scheme of byte codes according to an embodiment of the present application.
Fig. 2 is a flowchart of a method for modifying a bytecode according to an embodiment of the present application.
Fig. 3 is a schematic structural diagram of a byte code modification device according to an embodiment of the present application.
Fig. 4 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be clearly and completely described below with reference to specific embodiments of the present application and corresponding drawings. It will be apparent that the described embodiments are only some, but not all, embodiments of the application. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
A client often contains a large number of dependent libraries, and the client often has multiple versions, each of which may have multiple teams integrating a large amount of code into the client, and which are difficult to trace directly to source code, which creates significant challenges for the development and maintenance of the client. For example, when collecting Log information on line, all places calling the system log.i method in the client are required to be replaced by the self-encapsulated xlog.i method (the method has the same function as the system Log, can write the Log information into a file, and can upload the Log information to a server when the system crashes). For such a requirement, if a compiler is used for modification, huge workload is required, modification efficiency is low, and jar files cannot be used.
In order to solve the above problems, the embodiment of the application provides an automatic byte code modification scheme based on a code annotation form. As shown in fig. 1, the bytecode modification scheme may include three links: the annotation adding link 11, the annotation analyzing link 12 and the task executing link 13, wherein the annotation adding link 11 generally occurs in a development period of an object code, the annotation analyzing link 12 and the task executing link 13 occur in a period of a client corresponding to the object code constructed by a client constructing system, and the object code can be a code of any client (or a certain application) of any version, and the content realized by each link is specifically described below.
In the annotation adding step 11, a developer can state an annotation in the object code, specifically, the annotation can be added on the method in the class, and the annotation content can comprise the name of the class to be modified, the name of the method to be modified, the description information of the method to be modified and the like; after adding the annotation, the annotated object code may be compiled to generate the product 21: a plurality of class files containing annotations.
In the annotation parsing step 12, annotations in a plurality of class files containing annotations may be parsed to obtain the added location information and annotation content of the annotations in the plurality of class files, and a product 22 may be generated based on the information: the bytecode modifies the configuration file.
In the task execution link 13, a byte code modification task can be generated based on the byte code modification configuration file, and when the class file is processed by callback, the class file is matched with the byte code modification task, if the class file is matched, the byte code modification is performed on the class file, otherwise, the modification on the class file is skipped, and finally the product 23 is obtained: and the modified class file.
Annotating, which can be seen as an extended template for a class/method, each annotates a different parameter for the class/method according to rules in the annotating class, where used, the various parameters and values annotated in the different class/method can be obtained. Annotations are somewhat different from annotations, which can be understood as special tags in code that can be read at compile, class load, runtime, and perform the corresponding processing. By annotating the developer can embed supplemental information in the source code without changing the original code and logic.
On the basis of the technical conception, the embodiment of the application provides a byte code modification method and a byte code modification device, which can be executed by electronic equipment or software installed in the electronic equipment, wherein the electronic equipment can comprise a terminal and a server.
It should be noted that, the method and the device for modifying the byte code provided by the embodiment of the application can automatically access the SDK (Software Development Kit ).
The following describes a method for modifying byte codes provided in the embodiment of the present application.
As shown in fig. 2, a method for modifying a bytecode according to an embodiment of the present application may include:
And 201, acquiring and compiling the object codes added with the annotations to obtain a plurality of class files containing the annotations, wherein the annotation content of the annotations comprises a modification task type and byte code information to be modified.
As described above, annotations in object code may be added by a developer in a code development link, typically in a method in a class (class), and object code may be code of any client (or some application) of any version, a class file.
The modification task type refers to a task type for modifying a class file, or a type of a bytecode modification task, and the modification task type may be preset, for example, the modification task type may include one or more types of substitution method (method) call, modification method implementation, and exception capture (try catch) added to a method.
The bytecode information to be modified may include, but is not limited to, at least one of a class name to be modified, a method name to be modified, method description information to be modified, and whether a method to be modified is static or not.
When adding annotations to object code, this can be achieved by:
First, a java library project is created by a compiler in response to a java library project creation request of a developer.
Then, in response to the developer's annotation creation request, an annotation is created in the object code, and the modification task type and the bytecode information to be modified are declared in the annotation, for example, the creation method protects the task annotation, and the pseudo code is as shown in table 1.
TABLE 1
Finally, the annotated object code may be packaged and published to a preset repository, such as a maven repository, for subsequent calls, such as for application development dependency calls, based on the developer's publication request.
And 202, analyzing the annotations in the plurality of class files, determining the adding position information and the annotation content of the annotations in the plurality of class files, and generating a byte code modification task configuration file based on the adding position information and the annotation content of the annotations in the plurality of class files.
The adding location information of the annotations in the class file may include: class name where the annotation is located, method name where the annotation is located, descriptive information of the method where the annotation is located, and the like.
As described above, the annotation content may include the modification task type and the bytecode information to be modified, wherein the bytecode information to be modified may include, but is not limited to, a class name to be modified, a method name to be modified, method description information to be modified, whether a method to be modified is static, and the like.
Specifically, step 202 may include the steps of: firstly, creating a java library project; secondly, creating an annotation analyzer which analyzes the annotations in the plurality of class files respectively to obtain the adding position information and the annotation content of the annotations in the plurality of class files; the obtained addition location information and annotation content of the annotations in the plurality of class files are then written into a bytecode modification task profile, e.g. as shown in table 2, generating a bytecode modification task profile app/roboasm configggen. And finally, packaging the configuration file of the byte code modification task, and not sending the configuration file to a preset warehouse, such as a maven warehouse, so as to supply development dependency calling.
TABLE 2
Step 203, generating a plurality of byte code modification tasks based on the byte code modification task configuration file.
Since a plurality of annotations may be generally included in the plurality of class files, a bytecode modification task configuration file generated based on the addition location information of the annotations and the annotation content in the plurality of class files correspondingly includes a plurality of pieces of bytecode modification task information, generally, one piece of the bytecode modification task information corresponds to one piece of the bytecode modification task information, and one piece of the bytecode modification task information includes a modification task type and a target parameter of the modification task, wherein the target parameter of the modification task is determined according to the addition location information of the corresponding annotation and the annotation content, and for example, the target parameter may include a modification task type, a class name to be modified, a method name to be modified, and method description information to be modified, and other parameters determined according to the addition location information of the annotation.
Further, as an example, step 203 may include: generating a plurality of pieces of byte code modification task information based on the byte code modification task configuration file, and generating a plurality of byte code modification tasks aiming at the plurality of class files based on the plurality of pieces of byte code modification task information, wherein one piece of byte code modification task information corresponds to one byte code modification task.
Further, as another example, step 203 may include: generating a plurality of bytecode modification tasks for the plurality of class files based on at least one bytecode modification task template and the bytecode modification task configuration file. Specifically, a plurality of pieces of byte code modification task information can be generated based on the byte code modification task configuration file, wherein one annotation in the plurality of class files corresponds to one piece of byte code modification task information; acquiring at least one byte code modification task template corresponding to the plurality of pieces of byte code modification task information; and respectively and correspondingly filling the plurality of pieces of byte code task information into corresponding byte code modification task templates to generate a plurality of byte code modification tasks aiming at the plurality of class files, wherein one piece of byte code modification task information corresponds to one piece of byte code modification task.
It can be understood that the byte code modification task template is created in advance, and byte code modification task information is filled into the corresponding byte code modification task template to generate the byte code modification task, so that the process of generating the byte code task can be simplified, the technical threshold of byte code modification is reduced, details of byte code modification task generation are not concerned, and finally byte code modification efficiency is improved.
Typically, one type of bytecode modification task corresponds to creating one bytecode modification task template.
Step 204, traversing the plurality of class files and executing a first appointed step on the current class file, wherein the first appointed step comprises: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file.
And matching the plurality of class files generated by compiling the target codes with the plurality of byte code modification tasks generated in the step 203 one by one, determining whether the target byte code modification tasks matched with the class files exist, and executing the target byte code modification tasks if the target byte code modification tasks exist so as to modify the byte codes of the current class files.
Specifically, when traversing to a certain class of file, the string pool information of the current class of file can be read in advance, and then the string pool information of the current class of file is respectively compared with target parameters in the plurality of byte code tasks to determine whether a target byte code modification task matched with the current class of file exists.
Optionally, if the target bytecode modification task matching the current class file does not exist in the plurality of bytecode modification tasks, skipping the current class file and executing the first designating step on a next class file in the plurality of class files until the traversal of the plurality of class files is finished.
More specifically, comparing the string pool information of the current class file with the target parameters in the plurality of byte code modification tasks, and determining whether there is a target byte code modification task matching the current class file may include: traversing the plurality of bytecode modification tasks and performing a second specifying step on the current bytecode modification task. Wherein the second specifying step includes: determining whether the target parameters in the character string pool information of the current class file are consistent with the target parameters in the current byte code modification task; if the current byte code modification task is consistent with the target byte code modification task, determining the current byte code modification task as the target byte code modification task; and if the two byte code modification tasks are inconsistent, taking the next byte code modification task in the byte code modification tasks as the current byte code modification task until the traversal of the byte code modification tasks is finished.
Optionally, before executing step 204, a create transfrom task may be created, then, when executing the target bytecode modification task to perform bytecode modification on the current class file, the target bytecode modification task may be executed by transfrom task to perform bytecode modification on the current class file, and finally, the bytecode modification on the plurality of class files is completed by the transfrom task.
It can be understood that the construction time of the client corresponding to the target code can be greatly reduced by processing a plurality of byte code modification tasks through one transformation task and processing the specified class file through pre-reading matching (comparing the character string pool of the pre-reading class file with the information in the byte code modification task).
The following describes the implementation of the steps 203 and 204 by way of an example:
First, a android gradle plugin library project is created.
Next, a transfrom task is created that will callback triggers handling of the class files when the client build system builds the client.
And thirdly, reading information in the byte code modification task configuration file to generate a plurality of pieces of byte code modification task information.
Then, one or more pre-created byte code modification task templates are read, the byte code modification task information is filled into the corresponding byte code modification task templates, and a plurality of byte code modification tasks are generated.
And finally, when the client building system calls back the plurality of class files, pre-reading the character string pool information of the plurality of class files, respectively comparing the character string pool information of the plurality of class files with the plurality of byte code modification tasks, executing the byte code modification tasks when the character string pool information of a certain class file is matched with a certain byte code modification task, otherwise, skipping the current class file or skipping the current byte code modification task, and continuing to perform the next comparison.
Wherein performing the bytecode modification task includes: according to the target parameters of the byte code modification task, the byte code modification is performed through ASM (one byte code manipulation framework of Java), and the effect is shown in table 3.
TABLE 3 Table 3
Optionally, the modified plurality of class files can be packaged and issued to a preset warehouse, such as a maven warehouse, to supply development dependency calls.
According to the byte code modification method provided by the embodiment of the application, the byte code modification task can be automatically generated by analyzing the notes in the object code based on the notes in the object code after the plurality of class files of the object code are compiled, and the byte code modification task is automatically executed to complete the byte code modification, so that the whole process is high in automation degree, the byte code modification process can be simplified, the byte code modification efficiency is improved, and the development threshold of the application corresponding to the object code is reduced.
Optionally, before executing the bytecode modification method provided by the embodiment of the present application, an APP (Application program) item corresponding to the object code may be created by a compiler, annotation dependencies may be added, annotation analysis dependencies may be added, and task execution plug in library dependencies may be added, so that preparation is made for executing the method provided by the embodiment of the present application.
In brief, if the bytecode modification is to be performed on the class file compiled by the object code, the annotation can be added when the object code is developed, for example, as shown in table 4, the annotation about the class a can be added in the class C; then, the method for modifying the byte code provided by the embodiment of the application is executed aiming at the target code, so that when the object code which is packed by the compiler is executed to construct APP to generate apk, the annotation is automatically parsed, the byte code modification task is automatically generated, and the byte code modification task is automatically executed to complete the byte code modification, and the effect can be seen by comparing the table 5 with the table 6, wherein the table 5 lists class files of class A before the byte code modification method provided by the embodiment of the application is executed, and the table 6 lists class files of class A after the byte code modification method provided by the embodiment of the application is executed.
TABLE 4 Table 4
TABLE 5
TABLE 6
The foregoing describes a method for modifying a bytecode provided by an embodiment of the present application, and corresponding to the foregoing method embodiment, the embodiment of the present application further provides a device for modifying a bytecode, which is described below.
As shown in fig. 3, a bytecode modification apparatus 300 according to an embodiment of the present application, the apparatus 300 may include: a compiling module 301, an annotation parsing module 302, a task generating module 303 and a task executing module 304.
The compiling module 301 is configured to obtain and compile the object code added with the annotation, so as to obtain a plurality of class files containing the annotation, where the annotation content of the annotation includes a modification task type and byte code information to be modified.
The annotation analysis module 302 is configured to analyze the annotations in the plurality of class files, determine addition location information and annotation content of the annotations in the plurality of class files, and generate a bytecode modification task configuration file based on the addition location information and the annotation content of the annotations in the plurality of class files.
The adding location information of the annotations in the class file may include: class name where the annotation is located, method name where the annotation is located, descriptive information of the method where the annotation is located, and the like.
The annotation content may include a modification task type and bytecode information to be modified, where the bytecode information to be modified may include, but is not limited to, a class name to be modified, a method name to be modified, method description information to be modified, whether a method to be modified is static, etc.
Specifically, annotation parsing module 302 may be used to: analyzing the annotations in the plurality of class files respectively to obtain the adding position information and the annotation content of the annotations in the plurality of class files; then, the obtained addition location information and annotation content of the annotations in the plurality of class files are written into the bytecode modification task configuration file.
The task generating module 303 is configured to generate a plurality of bytecode modification tasks based on the bytecode modification task configuration file.
As one example, the task generation module 303 may be configured to: generating a plurality of pieces of byte code modification task information based on the byte code modification task configuration file, and generating a plurality of byte code modification tasks aiming at the plurality of class files based on the plurality of pieces of byte code modification task information, wherein one piece of byte code modification task information corresponds to one byte code modification task.
Further, as another example, the task generation module 303 may be configured to: generating a plurality of bytecode modification tasks for the plurality of class files based on at least one bytecode modification task template and the bytecode modification task configuration file. Specifically, a plurality of pieces of byte code modification task information can be generated based on the byte code modification task configuration file, wherein one annotation in the plurality of class files corresponds to one piece of byte code modification task information; acquiring at least one byte code modification task template corresponding to the plurality of pieces of byte code modification task information; and respectively and correspondingly filling the plurality of pieces of byte code task information into corresponding byte code modification task templates to generate a plurality of byte code modification tasks aiming at the plurality of class files, wherein one piece of byte code modification task information corresponds to one piece of byte code modification task.
It can be understood that the byte code modification task template is created in advance, and byte code modification task information is filled into the corresponding byte code modification task template to generate the byte code modification task, so that the process of generating the byte code task can be simplified, the technical threshold of byte code modification is reduced, details of byte code modification task generation are not concerned, and finally byte code modification efficiency is improved.
Typically, one type of bytecode modification task corresponds to creating one bytecode modification task template.
The task execution module 304 is configured to traverse the plurality of class files and execute a first specifying step on a current class file, where the first specifying step includes: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file.
And matching the plurality of class files generated by compiling the target codes with the plurality of byte code modification tasks generated in the step 203 one by one, determining whether the target byte code modification tasks matched with the class files exist, and executing the target byte code modification tasks if the target byte code modification tasks exist so as to modify the byte codes of the current class files.
Specifically, when traversing to a certain class of file, the string pool information of the current class of file can be read in advance, and then the string pool information of the current class of file is respectively compared with target parameters in the plurality of byte code tasks to determine whether a target byte code modification task matched with the current class of file exists.
More specifically, comparing the string pool information of the current class file with the target parameters in the plurality of byte code modification tasks, and determining whether there is a target byte code modification task matching the current class file may include: traversing the plurality of bytecode modification tasks and performing a second specifying step on the current bytecode modification task. Wherein the second specifying step includes: determining whether the target parameters in the character string pool information of the current class file are consistent with the target parameters in the current byte code modification task; if the current byte code modification task is consistent with the target byte code modification task, determining the current byte code modification task as the target byte code modification task; and if the two byte code modification tasks are inconsistent, taking the next byte code modification task in the byte code modification tasks as the current byte code modification task until the traversal of the byte code modification tasks is finished.
Optionally, the apparatus shown in fig. 3 may further include: the task creation module is configured to create a creation transfrom task before traversing the plurality of class files and performing the first specifying step on the current class file, and then, when performing the target bytecode modification task to perform the bytecode modification on the current class file, perform the target bytecode modification task through transfrom task to perform the bytecode modification on the current class file, and finally complete the bytecode modification on the plurality of class files through transfrom task.
It can be understood that the construction time of the client corresponding to the target code can be greatly reduced by processing a plurality of byte code modification tasks through one transformation task and processing the specified class file through pre-reading matching (comparing the character string pool of the pre-reading class file with the information in the byte code modification task).
The byte code modification device provided by the embodiment of the application can automatically analyze the notes in the object code to generate byte code modification tasks based on the notes in the object code after the plurality of class files of the object code are compiled, and automatically execute the byte code modification tasks to complete the byte code modification, so that the whole process has high automation degree, the byte code modification process can be simplified, the byte code modification efficiency is improved, and the development threshold of the application corresponding to the object code is reduced.
It should be noted that, since the apparatus embodiment performs similar to the method embodiment, the apparatus embodiment is described in a simplified manner, and reference is made to the method embodiment.
Fig. 4 is a schematic structural diagram of an electronic device according to an embodiment of the present application. Referring to fig. 4, at the hardware level, the electronic device includes a processor, and optionally an internal bus, a network interface, and a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a non-volatile Memory (non-volatile Memory), such as at least 1 disk Memory. Of course, the electronic device may also include hardware required for other services.
The processor, network interface, and memory may be interconnected by an internal bus, which may be an ISA (Industry Standard Architecture ) bus, a PCI (PERIPHERAL COMPONENT INTERCONNECT, peripheral component interconnect standard) bus, or EISA (Extended Industry Standard Architecture ) bus, among others. The buses may be classified as address buses, data buses, control buses, etc. For ease of illustration, only one bi-directional arrow is shown in FIG. 4, but not only one bus or type of bus.
And the memory is used for storing programs. In particular, the program may include program code including computer-operating instructions. The memory may include memory and non-volatile storage and provide instructions and data to the processor.
The processor reads the corresponding computer program from the nonvolatile memory into the memory and then runs, and the byte code modification device is formed on the logic level. The processor executes the program stored in the memory and is specifically used for executing the byte code modification method provided by the embodiment of the application.
The method performed by the byte code modification device disclosed in the embodiment of fig. 3 of the present application may be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or by instructions in the form of software. The processor may be a general-purpose processor, including a central processing unit (Central Processing Unit, CPU), a network processor (Network Processor, NP), etc.; but may also be a digital signal Processor (DIGITAL SIGNAL Processor, DSP), application SPECIFIC INTEGRATED Circuit (ASIC), field-Programmable gate array (Field-Programmable GATE ARRAY, FPGA) or other Programmable logic device, discrete gate or transistor logic device, discrete hardware components. The disclosed methods, steps, and logic blocks in the embodiments of the present application may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present application may be embodied directly in the execution of a hardware decoding processor, or in the execution of a combination of hardware and software modules in a decoding processor. The software modules may be located in a random access memory, flash memory, read only memory, programmable read only memory, or electrically erasable programmable memory, registers, etc. as well known in the art. The storage medium is located in a memory, and the processor reads the information in the memory and, in combination with its hardware, performs the steps of the above method.
The embodiment of the application also provides a computer readable storage medium, which stores one or more programs, the one or more programs including instructions, which when executed by an electronic device including a plurality of application programs, enable the electronic device to execute the method executed by the bytecode modification device in the embodiment shown in fig. 3, and is specifically configured to execute the bytecode modification method provided by the embodiment of the application.
It will be appreciated by those skilled in the art that embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It should be noted that, in the present application, each embodiment is described in a related manner, and identical and similar parts of each embodiment are all referred to each other, and each embodiment is mainly described in a different point from other embodiments. In particular, for the device embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments in part.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises an element.
The foregoing is merely exemplary of the present application and is not intended to limit the present application. Various modifications and variations of the present application will be apparent to those skilled in the art. Any modification, equivalent replacement, improvement, etc. which come within the spirit and principles of the application are to be included in the scope of the claims of the present application.

Claims (10)

1. A method of modifying bytecode, the method comprising:
Acquiring and compiling target codes added with annotations to obtain a plurality of class files containing the annotations, wherein the annotation content of the annotations comprises a modification task type and byte code information to be modified;
analyzing the annotations in the plurality of class files, determining the adding position information and the annotation content of the annotations in the plurality of class files, and generating a byte code modification task configuration file based on the adding position information and the annotation content of the annotations in the plurality of class files;
Generating a plurality of byte code modification tasks based on the byte code modification task configuration file;
Traversing the plurality of class files and executing a first designating step on the current class file, wherein the first designating step comprises: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file.
2. The method according to claim 1, wherein the method further comprises:
If the target byte code modification task matched with the current class file does not exist, skipping the current class file and executing the first appointed step on the next class file in the class files until the traversal of the class files is finished.
3. The method of claim 1, wherein the generating a plurality of bytecode modification tasks based on the bytecode modification task profile comprises:
Generating a plurality of bytecode modification tasks for the plurality of class files based on at least one bytecode modification task template and the bytecode modification task configuration file.
4. The method of claim 3, wherein the generating a plurality of bytecode modification tasks for the plurality of class files based on at least one bytecode modification task template and the bytecode modification task profile comprises:
Generating a plurality of pieces of byte code modification task information based on the byte code modification task configuration file, wherein one annotation in the plurality of class files corresponds to one piece of byte code modification task information, one piece of byte code modification task information comprises a modification task type and target parameters of the modification task, and the target parameters of the modification task are determined according to the addition position information and the annotation content of the corresponding annotation;
acquiring at least one byte code modification task template corresponding to the plurality of pieces of byte code modification task information;
And respectively and correspondingly filling the plurality of pieces of byte code task information into corresponding byte code modification task templates to generate a plurality of byte code modification tasks aiming at the plurality of class files, wherein one piece of byte code modification task information corresponds to one piece of byte code modification task.
5. The method of claim 1, wherein prior to said traversing said plurality of class files and performing a first specifying step on a current class file, said method further comprises:
Creating transfrom tasks;
Wherein the executing the target bytecode modification task to perform bytecode modification on the current class file includes:
Executing the target byte code modification task through transfrom tasks to modify the byte code of the current class file.
6. The method of claim 5, wherein the matching the current class file with the plurality of bytecode modification tasks, respectively, and determining whether there is a target bytecode modification task that matches the current class file comprises:
pre-reading character string pool information of the current class file, comparing the character string pool information with target parameters in the plurality of byte code modification tasks respectively, and determining whether a target byte code modification task matched with the current class file exists.
7. The method of claim 6, wherein the comparing the string pool information with the target parameters in the plurality of bytecode modification tasks, respectively, and determining whether there is a target bytecode modification task that matches the current class file comprises:
Traversing the plurality of bytecode modification tasks and performing a second specifying step on the current bytecode modification task; wherein the second specifying step includes:
determining whether the target parameters in the character string pool information are consistent with the target parameters in the current byte code modification task;
if the current byte code modification task is consistent with the target byte code modification task, determining the current byte code modification task as the target byte code modification task;
and if the two byte code modification tasks are inconsistent, taking the next byte code modification task in the byte code modification tasks as the current byte code modification task until the traversal is finished.
8. The method according to any one of claims 1 to 7, wherein,
The annotated addition location information includes: the class name where the annotation is located, the method name where the annotation is located and the description information of the method where the annotation is located;
The byte code information to be modified in the annotation content includes: class name to be modified, method name to be modified, and method description information to be modified.
9. A byte code modification device, the device comprising:
The compiling module is used for acquiring and compiling the object codes added with the annotations to obtain a plurality of class files containing the annotations, wherein the annotation content of the annotations comprises a modification task type and byte code information to be modified;
The annotation analysis module is used for analyzing the annotations in the plurality of class files, determining the adding position information and the annotation content of the annotations in the plurality of class files, and generating a byte code modification task configuration file based on the adding position information and the annotation content of the annotations in the plurality of class files;
The task generating module is used for generating a plurality of byte code modification tasks based on the byte code modification task configuration file;
The task execution module is used for traversing the plurality of class files and executing a first appointed step on the current class file, wherein the first appointed step comprises the following steps: and respectively matching the current class file with the plurality of byte code modification tasks, determining whether a target byte code modification task matched with the current class file exists, and if so, executing the target byte code modification task to modify the byte code of the current class file.
10. The apparatus of claim 9, wherein the apparatus further comprises:
And the jump module is used for skipping the current class file and executing the first appointed step on the next class file in the class files when the target byte code modification task matched with the current class file does not exist, and the traversal of the class files is finished.
CN202111100933.4A 2021-09-18 2021-09-18 Byte code modification method and device Active CN113791767B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111100933.4A CN113791767B (en) 2021-09-18 2021-09-18 Byte code modification method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111100933.4A CN113791767B (en) 2021-09-18 2021-09-18 Byte code modification method and device

Publications (2)

Publication Number Publication Date
CN113791767A CN113791767A (en) 2021-12-14
CN113791767B true CN113791767B (en) 2024-08-30

Family

ID=79184160

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111100933.4A Active CN113791767B (en) 2021-09-18 2021-09-18 Byte code modification method and device

Country Status (1)

Country Link
CN (1) CN113791767B (en)

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US10146515B1 (en) * 2015-03-10 2018-12-04 Twitter, Inc. Live code updates
CN112114810A (en) * 2020-08-13 2020-12-22 浙江大搜车融资租赁有限公司 Compiling processing method and device

Family Cites Families (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110716715B (en) * 2019-10-18 2024-01-19 恒生电子股份有限公司 Application program development method, device, equipment and medium
US10963275B1 (en) * 2019-10-31 2021-03-30 Red Hat, Inc. Implementing dependency injection via direct bytecode generation
CN113391811B (en) * 2020-03-13 2023-11-03 抖音视界有限公司 Function compiling method, function compiling device, electronic equipment and computer readable storage medium
CN111796865B (en) * 2020-06-28 2024-04-16 北京字节跳动网络技术有限公司 Byte code file modification method, device, terminal equipment and medium
CN112698867A (en) * 2020-12-28 2021-04-23 平安科技(深圳)有限公司 Method and device for dynamically modifying annotation information, electronic equipment and medium

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US10146515B1 (en) * 2015-03-10 2018-12-04 Twitter, Inc. Live code updates
CN112114810A (en) * 2020-08-13 2020-12-22 浙江大搜车融资租赁有限公司 Compiling processing method and device

Also Published As

Publication number Publication date
CN113791767A (en) 2021-12-14

Similar Documents

Publication Publication Date Title
Madsen et al. Static analysis of event-driven Node. js JavaScript applications
EP3234851B1 (en) A system and method for facilitating static analysis of software applications
US8141035B2 (en) Method for accessing internal states of objects in object oriented programming
EP2939111A1 (en) Extending a development environment
US9626170B2 (en) Method and computer program product for disassembling a mixed machine code
US20180203676A1 (en) Removing library objects from a static library
JP2004164554A (en) Device and method for executing and monitoring program
CN107203535B (en) Information query method and device
CN111222141B (en) Automobile electronic control unit code vulnerability analysis method and system
CN112965720B (en) Component compiling method, device, equipment and computer readable storage medium
CN111427578B (en) Data conversion method, device and equipment
Kellogg et al. Verifying object construction
CN110347588B (en) Software verification method, device, computer equipment and storage medium
CN115357898A (en) Dependency analysis method, device and medium for JAVA component
US9116714B2 (en) Methods and systems for file processing
US9747085B2 (en) Source code generation from prototype source
CN110765008B (en) Data processing method and device
CN112445706A (en) Program abnormal code acquisition method and device, electronic equipment and storage medium
CN113791767B (en) Byte code modification method and device
CN117149209A (en) Code increment compiling method and device, computer equipment and storage medium
KR20220047928A (en) source modification engine
CN116578282A (en) Code generation method, device, electronic equipment and medium
CN108614704B (en) Code compiling method and device
CN114490370A (en) Multi-language compatible test method and device and electronic equipment
CN111273944B (en) Method and device for generating program file and uploading program file to code warehouse

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant