A method and device for enhancing bytecode

By identifying and replacing thread pools and threads created by software development kits, the resource consumption and stability problems caused by bytecode enhancement in the existing technology are solved, and the stability and compatibility of the system are improved.

CN110262841BActive Publication Date: 2024-10-18BEIJING JINGDONG SHANGKE INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN201810187345.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2018-03-07
Publication Date
2024-10-18
Estimated Expiration
2038-03-07

AI Technical Summary

Technical Problem

When the existing technology enhances the thread pool class bytecode of the software development tool kit, non-standard parameters need to be used, resulting in increased system resource consumption and potentially unpredictable problems, and incompatible upgrades bring hidden dangers.

Method used

By identifying the system's own classes, scanning its bytecode and determining whether it was created based on a software development kit, if so, it will be replaced with a custom thread pool, thread or tool class to ensure that bytecode enhancements do not affect compatibility.

Benefits of technology

It reduces system resource consumption, improves system stability, and avoids startup parameter compatibility issues during subsequent upgrades.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN110262841B_ABST
    Figure CN110262841B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for enhancing bytecodes, and relates to the field of computer technology. A specific implementation of the method includes: identifying a system's own class from loaded classes; by scanning the bytecode of the system's own class, determining whether the system's own class is created based on a software development kit; if so, modifying the bytecode of the system's own class. The embodiment of the present invention can enhance the bytecode of a class without considering the compatibility of parameters, and can reduce the consumption of the system and improve the stability of the system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for enhancing bytecode. Background Art

[0002] For some basic frameworks, when out-of-band data transmission tools are needed, such as distributed tracing systems, if they are to be implemented in a non-invasive manner, it is necessary to enhance the bytecode of the system's thread pool or thread when the system starts, so that it has the function of passing the data of the parent thread to the child thread, thereby performing transparent data transmission between threads. In the existing technology, the bytecode of the thread pool class of the software development toolkit is directly enhanced when the system starts.

[0003] The existing technical solutions have the following problems during implementation: directly enhancing the bytecode of the thread pool class of the software development kit requires the use of some non-standard option parameters, which may not be applicable to the updated software development kit, and will have a relatively large impact on the system; and directly enhancing the bytecode of the thread pool class of the software development kit will require the entire operating environment and the third-party packages it depends on to use the enhanced thread pool class, which will increase the resource consumption of the system and easily cause unpredictable problems, thereby bringing hidden dangers to the system. Summary of the invention

[0004] In view of this, an embodiment of the present invention provides a method and device for enhancing bytecodes, which can enhance the bytecodes of a class without considering the compatibility of parameters, and can reduce system consumption and improve system stability.

[0005] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for enhancing bytecode is provided.

[0006] The method for enhancing bytecode in an embodiment of the present invention includes: identifying the system's own classes from the loaded classes; determining whether the system's own classes are created based on a software development kit by scanning the bytecodes of the system's own classes; and if so, modifying the bytecodes of the system's own classes.

[0007] Optionally, the step of identifying the system's own classes from the loaded classes includes: obtaining a package name of the loaded class; and determining whether the loaded class is the system's own class according to the package name.

[0008] Optionally, the step of determining whether the system's own class is created based on a software development kit by scanning the bytecode of the system's own class includes: scanning the bytecode of the system's own class, and determining whether the method corresponding to the scanned bytecode uses a thread pool created by a new method, a thread created by a new method, or a thread pool created by a tool class of a software development kit; if so, the step of modifying the bytecode of the system's own class includes: modifying the scanned bytecode.

[0009] Optionally, the step of modifying the scanned bytecode includes: replacing the scanned bytecode with an enhanced bytecode; wherein the enhanced bytecode is compiled according to a custom thread pool, a custom thread, or a custom tool class, and the custom thread pool, the custom thread, or the custom tool class respectively corresponds to a thread pool created using the new method, a thread created using the new method, or a thread pool created using a tool class of a software development kit.

[0010] Optionally, the method corresponding to the scanned bytecode includes: a construction method, a static variable initialization, a static initialization block or a non-construction method;

[0011] The step of determining whether the method corresponding to the scanned bytecode uses a thread pool created by a new method, a thread created by a new method, or a thread pool created by a tool class of a software development kit comprises:

[0012] Determine whether the method corresponding to the scanned bytecode is a constructor. If so, determine whether the constructor uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, then

[0013] Determine whether the method corresponding to the scanned bytecode is a static variable initialization. If so, determine whether the static variable initialization uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of the software development kit; if not, then

[0014] Determine whether the method corresponding to the scanned bytecode is a static initialization block. If so, determine whether the static initialization block uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, then

[0015] Determine whether the method corresponding to the scanned bytecode is a non-construction method. If so, determine whether the non-construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit.

[0016] To achieve the above objective, according to another aspect of an embodiment of the present invention, a device for enhancing bytecode is provided.

[0017] The device for enhancing bytecode in an embodiment of the present invention includes: an identification module, which is used to identify the system's own class from the loaded classes; a judgment module, which is used to judge whether the system's own class is created based on a software development kit by scanning the bytecode of the system's own class; and a modification module, which is used to modify the bytecode of the system's own class after the judgment module judges that the system's own class is created based on a software development kit.

[0018] Optionally, the identification module is further used to obtain a package name of the loaded class, and determine whether the loaded class is a class of the system itself according to the package name.

[0019] Optionally, the judgment module is also used to: scan the bytecode of the system's own class, and determine whether the method corresponding to the scanned bytecode uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if so, the modification module modifies the scanned bytecode.

[0020] Optionally, the modification module is also used to replace the scanned bytecode with an enhanced bytecode; wherein the enhanced bytecode is compiled according to a custom thread pool, a custom thread or a custom tool class, and the custom thread pool, the custom thread or the custom tool class respectively corresponds to a thread pool created using the new method, a thread created using the new method or a thread pool created using a tool class of a software development kit.

[0021] Optionally, the method corresponding to the scanned bytecode includes: a construction method, a static variable initialization, a static initialization block or a non-construction method;

[0022] The judgment module is also used to judge whether the method corresponding to the scanned bytecode is a construction method. If so, it is used to judge whether the construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not,

[0023] Determine whether the method corresponding to the scanned bytecode is a static variable initialization. If so, determine whether the static variable initialization uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of the software development kit; if not, then

[0024] Determine whether the method corresponding to the scanned bytecode is a static initialization block. If so, determine whether the static initialization block uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, then

[0025] Determine whether the method corresponding to the scanned bytecode is a non-construction method. If so, determine whether the non-construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit.

[0026] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device for enhancing bytecode is provided.

[0027] An electronic device for enhancing bytecode according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement any one of the above-mentioned methods for enhancing bytecode.

[0028] To achieve the above-mentioned purpose, according to another aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, characterized in that when the program is executed by a processor, any of the above-mentioned methods for enhancing bytecode is implemented.

[0029] One embodiment of the above invention has the following advantages or beneficial effects: it can enhance the bytecode of the classes in the system instead of enhancing the bytecode of the classes of the software development kit itself. In the process of bytecode enhancement, if the constructor, static variable initialization, static initialization block and non-constructor use the thread pool, thread, and tool class for creating threads that comes with the software development kit, the constructor, static variable initialization, static initialization block and non-constructor are modified respectively to make them comply with the standard specifications of the software development kit, so there is no need to worry about the compatibility of the startup parameters during subsequent upgrades. And because no classes inside the software development kit are changed, the consumption of system resources is less and the stability of the system is improved.

[0030] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0032] Figure 1 is a schematic diagram of the main process of a method for enhancing bytecode according to an embodiment of the present invention;

[0033] Figure 2 is a schematic diagram of a method for enhancing bytecode according to an embodiment of the present invention;

[0034] Figure 3 is a schematic diagram of a method for enhancing bytecode according to an embodiment of the present invention;

[0035] Figure 4 is a schematic diagram of the structure of a thread pool customized according to an embodiment of the present invention;

[0036] Figure 5 is a sequence diagram of a thread pool executed according to a customized embodiment of the present invention;

[0037] Figure 6 is a schematic diagram of a structure of a thread customized according to an embodiment of the present invention;

[0038] Figure 7 is a sequence diagram of thread execution customized according to an embodiment of the present invention;

[0039] Figure 8 is a schematic diagram of the structure of a tool class customized according to an embodiment of the present invention;

[0040] Fig. 9 It is a sequence diagram of a tool class customized according to an embodiment of the present invention;

[0041] Fig.10 is a schematic diagram of main modules of an apparatus for enhancing bytecode according to an embodiment of the present invention;

[0042] Fig.11 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0043] Fig.12 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0044] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0045] Figure 1 is a schematic diagram of the main process of the method for enhancing bytecode according to an embodiment of the present invention, such as Figure 1As shown, the method for enhancing bytecode in an embodiment of the present invention mainly includes:

[0046] Step S101: Identify the system's own classes from the loaded classes. The system's own classes refer to classes that are different from software development kits. For the system's own classes, their package names generally have the same beginning. For example, the package name of the class in the JD system may start with com.jd. Therefore, in the process of class loading, the package name of the loaded class is obtained, and then it can be determined whether the loaded class is the system's own class based on the package name. Identifying the system's own classes by package name is simple and easy to operate. Without changing the original information of the class, the system's own classes can be accurately and effectively identified. However, it is also possible to identify whether a class is the system's own class based on other characteristics of the system's own class, such as setting identification information for the system's own class, and identifying the system's own class through the identification information.

[0047] Step S102: By scanning the bytecode of the system's own class, determine whether the system's own class is created based on the software development kit. If yes, execute step S103; if not, it means that the bytecode of the system's own class does not need to be enhanced.

[0048] Specifically, the bytecode of the system's own class is scanned, and it is determined whether the method corresponding to the scanned bytecode uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit. If yes, it means that the system's own class is created based on the software development kit, and then step S103 is executed to modify the scanned bytecode.

[0049] Step S103: Modify the bytecode of the system's own class. If the above steps confirm that the method corresponding to the scanned bytecode uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit, the scanned bytecode is replaced with an enhanced bytecode. The enhanced bytecode is compiled according to a custom thread pool, a custom thread, or a custom tool class, and the custom thread pool, the custom thread, or the custom tool class respectively corresponds to a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit. In addition, the custom tool class depends on the custom thread pool.

[0050] Furthermore, the method corresponding to the scanned bytecode includes: a constructor, a static variable initialization, a static initialization block, or a non-construction method. In step S102, it can be first determined whether the method corresponding to the scanned bytecode is a constructor, a static variable initialization, a static initialization block, or a non-construction method. If it is one of them, it is further determined whether the one of them is a thread pool created using the new method, a thread created using the new method, or a thread pool created using a tool class of a software development kit. For the above-mentioned constructor, it refers to the constructor in the class, and the non-construction method refers to the non-construction method.

[0051] Specifically, determine whether the method corresponding to the scanned bytecode is a constructor. If so, determine whether the constructor uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by the tool class of the software development kit; if not, determine whether the method corresponding to the scanned bytecode is a static variable initialization. If so, determine whether the static variable initialization uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by the tool class of the software development kit; if not, determine whether the method corresponding to the scanned bytecode is a static initialization block. If so, determine whether the static initialization block uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by the tool class of the software development kit; if not, determine whether the method corresponding to the scanned bytecode is a non-construction method. If so, determine whether the non-construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by the tool class of the software development kit. In the embodiment of the present invention, the order of determining whether the method corresponding to the scanned bytecode is a constructor or static variable initialization, static initialization block, or non-construction method, and the order of determining whether one of the methods is a thread pool created using the new method, a thread created using the new method, or a thread pool created using a tool class of a software development kit do not constitute limitations to the present technical solution.

[0052] Figure 2 1 is a schematic diagram of a method for enhancing bytecode according to an embodiment of the present invention, taking the enhancement of Java bytecode as an example. Java bytecode is a virtual instruction format executed by JVM (Java Virtual Machine). JVM is a specification for a computing device, which is a fictional computer that is implemented by simulating various computer functions on an actual computer.

[0053] In the existing technology, Java Instrumentation is used to enhance the bytecode of the thread pool class of JDK (Java language software development kit) itself when the system starts. JDK is the Java language software development kit, and JDK is only a subset of SDK, because JDK is a platform for developing Java programs, and SDK for developing other programs can be developed without JDK. Java Instrumentation is a new feature of Java 5, which frees Java's instrument function from native code so that it can solve problems in the form of Java code. Using Instrumentation, developers can build an agent program independent of the application to monitor and assist programs running on JVM (Java Virtual Machine), and can even replace and modify the definition of certain classes. Specifically, when the JVM starts, the loaded class is judged to determine whether it is

[0054] java.util.concurrent.ThreadPoolExecutor

[0055] Or java.util.concurrent.ScheduledThreadPoolExecutor. If yes, modify the bytecode of these two classes. The specific modification is to replace the places where Runnable and Callable are used in the methods of these two classes with classes with functions similar to MyRunnable (custom Runnable) and MyCallable (custom Callable).

[0056] In the implementation process of the prior art solution, some non-standard option parameters of JVM need to be used. However, since these parameters are non-standard options, they cannot be guaranteed to be supported by all JVM implementations. If these parameters are changed in subsequent versions of JDK, the official will not notify separately. For example, in the process of modifying the thread pool class in JDK, non-JDK classes are used, so it is necessary to use the -Xbootclasspath / a parameter to load these non-JDK classes first, wherein the parameter -Xbootclasspath / a is the non-standard option parameter of JVM. Therefore, if JDK is upgraded, once these parameters are changed, it will have a certain impact on the system, and this impact may be relatively large. The second problem of the prior art is that the bytecode of the thread pool class of JDK itself is directly enhanced, so the entire operating environment and the third-party packages it relies on must use the enhanced thread pool class, such as Tomcat container, Spring framework, MyBatis and other third-party packages, which will increase the resource consumption of the system and cause some unpredictable problems, thereby bringing hidden dangers to the system.

[0057] like Figure 2 As shown, the method for enhancing bytecode according to the embodiment of the present invention includes the following steps:

[0058] Step S201: JVM loads classes. After the system is started, JVM loads classes, and after JVM completes loading classes, the system can be initialized.

[0059] Step S202: The bytecode of the class enters ClassFileTransformer. After the JVM loads the class, the bytecode of the class goes through ClassFileTransformer, and the bytecode of the class can be operated through the ClassFileTransformer.

[0060] Step S203: Determine whether the class is a class of the system itself. By determining the package name of the class, identify the class of the system itself. If it is determined that the class is a class of the system itself, execute step S204, otherwise it means that the class does not need to be enhanced, and execute step S207 to prompt that the bytecode enhancement is successful.

[0061] Step S204: Scan the bytecode of the class of the system itself. During the class loading process, the bytecode of the class is scanned in sequence.

[0062] Step S205: Determine whether the JDK thread pool is used in the scanned bytecode. If yes, execute step S206; otherwise, execute step S207. Through this process, it is determined whether the system bytecode uses the JDK thread pool. If yes, it is replaced with a custom thread pool, and the bytecode is modified to the desired result. In essence, the bytecode of the system itself is modified.

[0063] Step S206: Replace the JDK thread pool used by the scanned bytecode with a custom thread pool. In this process, the scanned bytecode is replaced with an enhanced bytecode, which is compiled according to the custom thread pool, thereby completing the enhancement of the scanned bytecode. Before the class is loaded, the scanning of the class bytecode is continuously carried out. After the enhancement of the scanned bytecode is completed, the bytecode scanned again can be judged and modified until all the bytecodes of a class are scanned.

[0064] Step S207: prompting that bytecode enhancement is completed. This prompting process may not be performed, and after determining that the class is not a class of the system itself or the scanned bytecode does not use the JDK thread pool, and replacing the JDK thread pool used by the scanned bytecode with a custom thread pool, step S208 may be directly executed.

[0065] Step S208: Determine whether class loading is complete. After the system is started, multiple classes are generally loaded. For each class, the above steps S201 to S207 are executed to process the bytecode of the next class of the system itself until all the classes of the system itself loaded by the JVM are processed.

[0066] Figure 2 The method for enhancing bytecode shown in FIG. 1 only illustrates the case where the JDK thread pool is used in the scanned bytecode. For the case where the JDK thread or tool class is used in the scanned bytecode, the operation is similar to step S205 and step S206. The specific process is as follows: Figure 3 As shown:

[0067] Step S301: Determine whether it is a construction method. If yes, execute step S301a; otherwise, execute step S302.

[0068] Step S301a: Determine whether the new method is used to create a Java thread pool in the construction method. If yes, replace the thread pool with a corresponding custom thread pool; otherwise, execute step S301b.

[0069] Step S301b: Determine whether the new method is used to create a Java thread in the construction method. If yes, replace the thread with a corresponding custom thread; otherwise, execute step S301c.

[0070] Step S301c: Determine whether the construction method uses the JDK tool class to create a Java thread pool. If yes, replace the Java thread pool created using the JDK tool class with the corresponding custom tool class; otherwise, execute step S302.

[0071] Step S302: Determine whether it is static variable initialization. If yes, execute step S302a; otherwise, execute step S303.

[0072] Step S302a: Determine whether the new method is used to create a Java thread pool in the static variable initialization. If yes, replace the thread pool with a corresponding custom thread pool; otherwise, execute step S302b.

[0073] Step S302b: Determine whether the new method is used to create a Java thread in the static variable initialization. If yes, replace the thread with a corresponding custom thread; otherwise, execute step S302c.

[0074] Step S302c: Determine whether the JDK tool class is used to create a Java thread pool in the static variable initialization. If yes, replace the Java thread pool created by the JDK tool class with the corresponding custom tool class; otherwise, execute step S303.

[0075] Step S303: Determine whether it is a static initialization block. If yes, execute step S303a; otherwise, execute step S304.

[0076] Step S303a: Determine whether the new method is used to create a Java thread pool in the static initialization block. If yes, replace the thread pool with a corresponding custom thread pool; otherwise, execute step S303b.

[0077] Step S303b: Determine whether the new method is used to create a Java thread in the static initialization block. If yes, replace the thread with a corresponding custom thread; otherwise, execute step S303c.

[0078] Step S303c: Determine whether the static initialization block uses the JDK tool class to create a Java thread pool. If yes, replace the Java thread pool created using the JDK tool class with a corresponding custom tool class; otherwise, execute step S304.

[0079] Step S304: Determine whether it is a non-construction method. If yes, execute step S304a; otherwise, scan the bytecode of the class.

[0080] Step S304a: Determine whether the non-construction method uses the new method to create a Java thread pool. If yes, replace the thread pool with a corresponding custom thread pool; otherwise, execute step S304b.

[0081] Step S304b: Determine whether the non-construction method uses the new method to create a Java thread. If yes, replace the thread with a corresponding custom thread; otherwise, execute step S3034.

[0082] Step S304c: Determine whether the non-construction method uses the JDK tool class to create a Java thread pool. If yes, replace the Java thread pool created using the JDK tool class with the corresponding custom tool class; otherwise, scan the bytecode of the class.

[0083] According to the technical solution of the embodiment of the present invention, the bytecode of the classes in the system can be enhanced instead of the bytecode of the classes of the JDK itself. And in the process of bytecode enhancement, if the constructor, static variable initialization, static initialization block and non-constructor use the thread pool, thread, and tool class for creating threads that come with the software development kit, the constructor, static variable initialization, static initialization block and non-constructor can be modified respectively to make them conform to the standard specifications of the JDK, so when upgrading to a higher version of the JDK later, there is no need to worry about the compatibility of the startup parameters. And because no classes inside the JDK are changed, the consumption of system resources is less and the stability of the system is improved.

[0084] Figure 4 Schematic diagram of the structure of a thread pool customized according to an embodiment of the present invention. Figure 4 As shown in the figure, the custom thread pool MyThreadPoolExecutor inherits the existing thread pool ThreadPoolExecutor of JDK, and depends on JDK's own Runnable / Callable and custom MyRunnable / MyCallable. MyRunnable / MyCallable is the proxy of JDK's Runnable / Callable, which is responsible for copying the data of the current request context, which will be used in the child thread, so MyRunnable / MyCallable implements JDK's Runnable / Callable interface and holds the reference of JDK's Runnable / Callable.

[0085] Figure 5 is a sequence diagram of a thread pool executed according to a customized embodiment of the present invention, such as Figure 5As shown in the figure, during the execution of MyThreadPoolExecutor, the client first creates a Runnable / Callable object, and then calls the submit / execute method of MyThreadPoolExecutor. The input parameter of the method is Runnable / Callable, and the MyRunnable / MyCallable object is created through these two methods. Among them, the client refers to the code that uses the thread pool, thread, and thread pool tool class. In the constructor of MyRunnable / MyCallable, copy the data of the current thread context to the MyRunnable / MyCallable object. After that, when the MyRunnable / MyCallable thread (definitely in the child thread) works, it can use the data just copied in the MyRunnable / MyCallable object, and this data will be used in the child thread, which is equivalent to copying the data of the parent thread to the child thread, that is, realizing the data transfer between threads.

[0086] Figure 6 is a schematic diagram of the structure of a thread customized according to an embodiment of the present invention, such as Figure 6 As shown, the custom thread MyThread inherits the existing JDK thread Thread.

[0087] Figure 7 is a sequence diagram of thread execution customized according to an embodiment of the present invention, such as Figure 7 As shown in the figure, during the execution of MyThread, the client will first create a Runnable / Callable object, and then call the constructor of MyThread. The input parameter of this method is Runnable / Callable. In the constructor of MyThread, it is responsible for copying the data of the current request context, which will be used in the child thread. This is equivalent to copying the data of the parent thread to the child thread, that is, realizing the data transfer between threads.

[0088] Figure 8 is a schematic diagram of the structure of a tool class customized according to an embodiment of the present invention, such as Figure 8 As shown, the custom tool class MyExecutors depends on the custom thread pool MyThreadPoolExecutor.

[0089] Fig. 9 is a sequence diagram executed according to a tool class customized according to an embodiment of the present invention, such as Fig. 9As shown in the figure, during the execution of MyExecutors, the client calls the newFixedThreadPool / newCachedThreadPool method of MyExecutors, which returns the MyThreadPoolExecutor object. Then the client calls the execute / submit method of MyThreadPoolExecutor to start the thread. The following process is the same as the execution process of MyThreadPoolExecutor, and finally realizes the data transfer between threads.

[0090] Fig.10 is a schematic diagram of the main modules of the device for enhancing bytecode according to an embodiment of the present invention, such as Fig.10 As shown, the device 1000 for enhancing bytecode according to the embodiment of the present invention mainly includes an identification module 1001 , a judgment module 1002 and a modification module 1003 .

[0091] The identification module 1001 is used to identify the system's own classes from the loaded classes. The identification module is also used to obtain the package name of the loaded class and determine whether the loaded class is the system's own class according to the package name.

[0092] The judgment module 1002 is used to judge whether the system's own class is created based on the software development kit by scanning the bytecode of the system's own class. The judgment module is also used to: scan the bytecode of the system's own class, and judge whether the method corresponding to the scanned bytecode uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of the software development kit; if so, the modification module modifies the scanned bytecode.

[0093] The modification module 1003 is used to modify the bytecode of the system's own class after the judgment module determines that the system's own class is created based on the software development kit. The modification module is also used to replace the scanned bytecode with an enhanced bytecode; wherein the enhanced bytecode is compiled according to a custom thread pool, a custom thread, or a custom tool class, and the custom thread pool, the custom thread, or the custom tool class respectively corresponds to a thread pool created using the new method, a thread created using the new method, or a thread pool created using the tool class of the software development kit.

[0094] The method corresponding to the scanned bytecode includes: a construction method, static variable initialization, a static initialization block or a non-construction method. The judgment module is also used to judge whether the method corresponding to the scanned bytecode is a construction method. If so, it is judged whether the thread pool created by the new method, the thread created by the new method, or the thread pool created by the tool class of the software development kit is used in the construction method; if not, it is judged whether the method corresponding to the scanned bytecode is static variable initialization. If so, it is judged whether the thread pool created by the new method, the thread created by the new method, or the thread pool created by the tool class of the software development kit is used in the static variable initialization; if not, it is judged whether the method corresponding to the scanned bytecode is a static initialization block. If so, it is judged whether the thread pool created by the new method, the thread created by the new method, or the thread pool created by the tool class of the software development kit is used in the static initialization block; if not, it is judged whether the method corresponding to the scanned bytecode is a non-construction method. If so, it is judged whether the thread pool created by the new method, the thread created by the new method, or the thread pool created by the tool class of the software development kit is used in the non-construction method.

[0095] For the code that uses the new method to create a Java thread pool in the class constructor, the code that uses the new method to create a Java thread, and the code that uses the tool class provided by Java to create a Java thread pool, they can be replaced with a custom thread pool, a custom thread, and a custom tool class respectively. For the code that uses the new method to create a Java thread pool when doing static initialization of static member variables, the code that uses the new method to create a Java thread, and the code that uses the tool class provided by Java to create a Java thread pool, they can be replaced with a custom thread pool, a custom thread, and a custom tool class respectively. For the code that uses the new method to create a Java thread pool in the static initialization block of a class, the code that uses the new method to create a Java thread, and the code that uses the tool class provided by Java to create a Java thread pool, they can be replaced with a custom thread pool, a custom thread, and a custom tool class respectively. For the code that uses the new method to create a Java thread pool in the non-construction method of a class, the code that uses the new method to create a Java thread, and the code that uses the tool class provided by Java to create a Java thread pool, they can be replaced with a custom thread pool, a custom thread, and a custom tool class respectively. Therefore, it is possible to enhance the bytecode of the classes in the system instead of enhancing the bytecode of the classes of the JDK itself. In the process of bytecode enhancement, if the constructor, static variable initialization, static initialization block, and non-constructor use the thread pool, thread, and thread creation tool class that comes with the software development kit, the constructor, static variable initialization, static initialization block, and non-constructor can be modified to meet the JDK standard specifications. Therefore, when upgrading to a higher version of JDK, there is no need to worry about the compatibility of startup parameters. And because no internal classes of JDK are modified, the consumption of system resources is reduced and the stability of the system is improved.

[0096] Fig.11 An exemplary system architecture 1100 is shown to which the method for enhancing bytecode or the apparatus for enhancing bytecode according to the embodiments of the present invention can be applied.

[0097] like Fig.11 As shown, system architecture 1100 may include terminal devices 1101, 1102, 1103, a network 1104, and a server 1105. Network 1104 is used to provide a medium for communication links between terminal devices 1101, 1102, 1103 and server 1105. Network 1104 may include various connection types, such as wired, wireless communication links, or optical fiber cables, etc.

[0098] Users can use terminal devices 1101, 1102, 1103 to interact with server 1105 through network 1104 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 1101, 1102, 1103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).

[0099] The terminal devices 1101 , 1102 , and 1103 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.

[0100] The server 1105 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 1101, 1102, and 1103. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results to the terminal device.

[0101] It should be noted that the method for enhancing bytecodes provided in the embodiment of the present invention is generally executed by the server 1105 , and accordingly, the device for enhancing bytecodes is generally arranged in the server 1105 .

[0102] It should be understood that Fig.11 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0103] Reference below Fig.12 , which shows a schematic diagram of the structure of a computer system 1200 of a terminal device suitable for implementing an embodiment of the present invention. Fig.12 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0104] like Fig.12 As shown, the computer system 1200 includes a central processing unit (CPU) 1201, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1202 or a program loaded from a storage part 1208 into a random access memory (RAM) 1203. In the RAM 1203, various programs and data required for the operation of the system 1200 are also stored. The CPU 1201, the ROM 1202, and the RAM 1203 are connected to each other via a bus 1204. An input / output (I / O) interface 1205 is also connected to the bus 1204.

[0105] The following components are connected to the I / O interface 1205: an input section 1206 including a keyboard, a mouse, etc.; an output section 1207 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 1208 including a hard disk, etc.; and a communication section 1209 including a network interface card such as a LAN card, a modem, etc. The communication section 1209 performs communication processing via a network such as the Internet. A drive 1210 is also connected to the I / O interface 1205 as needed. A removable medium 1211, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 1210 as needed, so that a computer program read therefrom is installed into the storage section 1208 as needed.

[0106] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 1209, and / or installed from the removable medium 1211. When the computer program is executed by the central processing unit (CPU) 1201, the above-mentioned functions defined in the system of the present invention are executed.

[0107] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer 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 disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0108] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0109] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor, for example, it may be described as: a processor includes an identification module, a judgment module and a modification module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the identification module may also be described as "a module for identifying the system's own classes from the loaded classes".

[0110] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: identifying the system's own class from the loaded classes; by scanning the bytecode of the system's own class, determining whether the system's own class is created based on a software development kit; if so, modifying the bytecode of the system's own class.

[0111] According to the technical solution of the embodiment of the present invention, it is possible to enhance the bytecode of the classes in the system instead of enhancing the bytecode of the classes of the software development kit itself. And in the process of bytecode enhancement, if the constructor, static variable initialization, static initialization block and non-constructor use the thread pool, thread, and tool class for creating threads that comes with the software development kit, the constructor, static variable initialization, static initialization block and non-constructor can be modified respectively to make them comply with the software development kit standard specifications, so there is no need to worry about the compatibility of the startup parameters during subsequent upgrades. And because no classes inside the software development kit are changed, the consumption of system resources is less and the stability of the system is improved.

[0112] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A method for enhancing bytecode, It is characterized in that include: Identify the system's own classes from the loaded classes; By scanning the bytecode of the class of the system itself, determining whether the class of the system itself is created based on the software development kit comprises: by scanning the bytecode of the class of the system itself, the step of determining whether the class of the system itself is created based on the software development kit comprises: scanning the bytecode of the class of the system itself, and determining whether a method corresponding to the scanned bytecode uses a thread pool created by a new method, a thread created by a new method, or a thread pool created by a tool class of the software development kit; If yes, the bytecode of the class of the system itself is modified, including: replacing the scanned bytecode with an enhanced bytecode, wherein the enhanced bytecode is compiled according to a custom thread pool, a custom thread or a custom tool class.

2. The method according to claim 1, It is characterized in that The steps to identify the system's own classes from the loaded classes include: Get the package name of the loaded class; It is determined whether the loaded class is a class of the system itself according to the package name.

3. The method according to claim 1, It is characterized in that The custom thread pool, custom thread or custom tool class corresponds to a thread pool created using the new method, a thread created using the new method or a thread pool created using a tool class of a software development kit.

4. The method according to claim 1, It is characterized in that The method corresponding to the scanned bytecode includes: a construction method, a static variable initialization, a static initialization block or a non-construction method; The step of determining whether the method corresponding to the scanned bytecode uses a thread pool created by a new method, a thread created by a new method, or a thread pool created by a tool class of a software development kit comprises: Determine whether the method corresponding to the scanned bytecode is a constructor. If so, determine whether the constructor uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, then Determine whether the method corresponding to the scanned bytecode is a static variable initialization. If so, determine whether the static variable initialization uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of the software development kit; if not, then Determine whether the method corresponding to the scanned bytecode is a static initialization block. If so, determine whether the static initialization block uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, then Determine whether the method corresponding to the scanned bytecode is a non-construction method. If so, determine whether the non-construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit.

5. A device for enhancing bytecode, It is characterized in that include: Identification module, used to identify the system's own classes from the loaded classes; A judgment module, used for judging whether the class of the system itself is created based on a software development kit by scanning the bytecode of the class of the system itself; A modification module, used for modifying the bytecode of the class of the system itself after the judgment module determines that the class of the system itself is created based on the software development kit; The judgment module is also used to scan the bytecode of the class of the system itself, and judge whether the method corresponding to the scanned bytecode uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; The modification module is also used to replace the scanned bytecode with an enhanced bytecode; wherein the enhanced bytecode is compiled according to a custom thread pool, a custom thread or a custom tool class.

6. The device according to claim 5, It is characterized in that The identification module is also used to obtain the package name of the loaded class, and determine whether the loaded class is the system's own class according to the package name.

7. The device according to claim 5, It is characterized in that The custom thread pool, custom thread or custom tool class corresponds to a thread pool created using the new method, a thread created using the new method or a thread pool created using a tool class of a software development kit.

8. The device according to claim 5, It is characterized in that The method corresponding to the scanned bytecode includes: a construction method, a static variable initialization, a static initialization block or a non-construction method; The judgment module is also used to judge whether the method corresponding to the scanned bytecode is a construction method. If so, it is used to judge whether the construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, Determine whether the method corresponding to the scanned bytecode is a static variable initialization. If so, determine whether the static variable initialization uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of the software development kit; if not, then Determine whether the method corresponding to the scanned bytecode is a static initialization block. If so, determine whether the static initialization block uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit; if not, then Determine whether the method corresponding to the scanned bytecode is a non-construction method. If so, determine whether the non-construction method uses a thread pool created by the new method, a thread created by the new method, or a thread pool created by a tool class of a software development kit.

9. An electronic device for enhancing bytecodes, It is characterized in that include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 4.

10. A computer readable medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.

Citation Information

Patent Citations

  • Method, device and system for acquiring program performance consumption information

    CN104346148A

  • Code operation method and device

    CN105808266A