Method, apparatus, device and readable storage medium for obtaining information of anonymous function
By obtaining the boot function and bytecode constant pool in the bytecode file, and obtaining the context information of the anonymous function of the Lambda expression, the identification problem of anonymous functions in AOP processing is solved, and the effective application of anonymous functions is realized.
Patent Information
- Application Number
- CN201911320658.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-12-19
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2039-12-19
AI Technical Summary
In the Javassist-based AOP scheme, since the interface name and function name in the Lambda expression belong to an unknown state, an anonymous function cannot be recognized, so the AOP processing of the anonymous function cannot be implemented.
By obtaining the boot function and bytecode constant pool in the bytecode file, obtaining function information from the bytecode constant pool according to the boot function, and obtaining interface information through dynamic bytecode instructions, establishing the correspondence between the constant pool index and the attribute table index, thereby obtaining the context information of the anonymous function of the Lambda expression.
It realizes the information acquisition of the anonymous functions of Lambda expressions, solves the problem of identification of anonymous functions in AOP processing, and thus supports the effective application of anonymous functions.
Smart Images

Figure CN113010175B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present application relate to the field of programming, and particularly to a method, device, equipment, and readable storage medium for obtaining information of an anonymous function. Background Art
[0002] An anonymous function is a function form that can be passed in without explicit definition. In the application process of this anonymous function, it contains an expression. In this expression, there is no need to define the name of the anonymous function, but directly use the anonymous function expression, assign the anonymous function to a variable, so as to realize the function of the anonymous function. Schematically, the anonymous function expression includes a Lambda expression.
[0003] In related technologies, taking the interface for implementing click listening in Android development as an example, usually, the interface for implementing click listening is realized by means of an anonymous inner class. The anonymous inner class contains information such as the interface class name and function name, and the context information of the anonymous inner class can be directly obtained for identification and positioning to realize click listening.
[0004] However, when the above-mentioned interface for implementing click listening needs to be realized through a Lambda expression, since in the Lambda expression, the interface class name and function name are in an unknown state and cannot be directly identified, the above-mentioned interface for implementing click listening cannot be realized either. Summary of the Invention
[0005] Embodiments of the present application provide a method, device, equipment, and readable storage medium for obtaining information of an anonymous function, which can obtain the context information of the Lambda expression anonymous function and realize the application of the anonymous function. The technical solution is as follows:
[0006] On the one hand, a method for obtaining information of an anonymous function is provided. The method includes:
[0007] Obtain a bytecode file, where the bytecode file is a file generated after compiling a source code file containing the anonymous function. The bytecode file includes a bootstrap function and a bytecode constant pool, and the bootstrap function includes a constant pool index;
[0008] Obtain the function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool, and obtain a first correspondence between the constant pool index and the function information. The function information includes the function name of the anonymous function;
[0009] Obtain a dynamic bytecode instruction, where the dynamic bytecode instruction includes an attribute table index and an interface information descriptor, and the attribute table index is used to index to the corresponding bootstrap function;
[0010] Obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool, and obtain the second correspondence between the attribute table index and the interface information;
[0011] Combine the first correspondence and the second correspondence to obtain the information of the anonymous function.
[0012] On the other hand, an information acquisition device for an anonymous function is provided. The device includes:
[0013] An acquisition module, configured to acquire a bytecode file, where the bytecode file is a file generated after compilation of a source code file containing the anonymous function, the bytecode file includes a bootstrap function and a bytecode constant pool, and the bootstrap function includes a constant pool index;
[0014] The acquisition module is further configured to obtain the function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool, and obtain the first correspondence between the constant pool index and the function information, where the function information includes the function name of the anonymous function;
[0015] The acquisition module is further configured to acquire a dynamic bytecode instruction, where the dynamic bytecode instruction includes an attribute table index and an interface information descriptor;
[0016] The acquisition module is further configured to obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool, and obtain the second correspondence between the attribute table index and the interface information;
[0017] A determination module, configured to combine the first correspondence and the second correspondence to obtain the information of the anonymous function.
[0018] In an optional embodiment, the acquisition module is further configured to obtain a method handle pointer corresponding to the constant pool index from the bytecode constant pool, where the method handle pointer is used to point to the content in the bytecode constant pool; under the indication of the method handle pointer, obtain the function information of the anonymous function from the bytecode constant pool.
[0019] In an optional embodiment, the bytecode file includes a bootstrap function attribute table;
[0020] The acquisition module includes:
[0021] A traversal unit, configured to traverse the bootstrap function attribute table to obtain the bootstrap function;
[0022] An acquisition unit, configured to obtain the constant pool index from the bootstrap function.
[0023] In an optional embodiment, the obtaining module is further configured to use the constant pool index as a keyword and the function information as a value to obtain a key-value pair between the constant pool index and the function information as the first correspondence relationship.
[0024] In an optional embodiment, the function information further includes at least one of the function signature of the anonymous function and the class name to which the anonymous function belongs.
[0025] In an optional embodiment, the anonymous function is a function expressed by a Lambda expression;
[0026] The obtaining module includes:
[0027] A traversing unit, configured to traverse the bytecode constant pool to obtain the dynamic bytecode instructions in the bytecode constant pool, where each Lambda expression corresponds to one of the dynamic bytecode instructions.
[0028] In an optional embodiment, the interface information includes at least one of an interface class name and an interface function name.
[0029] On the other hand, a computer device is provided, which includes a processor and a memory. At least one instruction, at least one program, a code set or an instruction set is stored in the memory, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement the information obtaining method of the anonymous function as described in any one of the embodiments of the present application above.
[0030] On the other hand, a computer-readable storage medium is provided. At least one instruction, at least one program, a code set or an instruction set is stored in the storage medium, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement the information obtaining method of the anonymous function as described in any one of the embodiments of the present application above.
[0031] On the other hand, a computer program product is provided. When the computer program product runs on a computer, it causes the computer to execute the information obtaining method of the anonymous function as described in any one of the embodiments of the present application above.
[0032] The beneficial effects brought by the technical solutions provided in the embodiments of the present application at least include:
[0033] Through the bootstrap function and the bytecode constant pool in the bytecode file, function information is obtained from the bytecode constant pool according to the bootstrap function, and interface information is obtained from the bytecode constant pool through dynamic bytecode instructions. A corresponding relationship is established between the function information and the interface information according to the constant pool index and the attribute table index, so as to obtain the context information of the Lambda expression anonymous function and realize the application of the anonymous function. Brief Description of the Drawings
[0034] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for describing the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0035] Figure 1 is a flowchart of a method for obtaining information of an anonymous function provided by an exemplary embodiment of the present application;
[0036] Figure 2 is a flowchart of a method for obtaining information of an anonymous function provided by another exemplary embodiment of the present application;
[0037] Figure 3 is a schematic diagram of an architecture for obtaining function information of an anonymous function provided by an exemplary embodiment of the present application;
[0038] Figure 4 is a flowchart of a method for obtaining information of an anonymous function provided by another exemplary embodiment of the present application;
[0039] Figure 5 is a block diagram of the structure of an apparatus for obtaining information of an anonymous function provided by an exemplary embodiment of the present application;
[0040] Figure 6 is a block diagram of the structure of an apparatus for obtaining information of an anonymous function provided by another exemplary embodiment of the present application;
[0041] Figure 7 is a block diagram of the structure of a terminal provided by an exemplary embodiment of the present application. Detailed Embodiments
[0042] To make the objectives, technical solutions, and advantages of the present application clearer, the embodiments of the present application will be further described in detail below in conjunction with the drawings.
[0043] First, a brief introduction to the terms involved in the present application:
[0044] Anonymous function: It refers to a shorthand form of function definition in the programming process. During the use of this anonymous function, there is no need to define a function name. Only by determining the assignment relationship or the triggering of an event can the application of this anonymous function be realized. Schematically, the anonymous function is in the form of: var a = function() {return 1}; This anonymous function does not define a function name and assigns a value to the variable a. Since the anonymous function does not require a function name, there is no function name conflict.
[0045] Lambda expression: Also known as a closure, it is a way of writing an anonymous function. Before Java 8, Java only had the writing method of anonymous classes and did not support the writing method of anonymous functions. Java 8 is a version developed for the Java language, which supports writing anonymous functions with Lambda expressions. Schematically, the syntax format of this Lambda expression is as follows: (parameters) -> expression; or (parameters) -> { statements;}, where parameters represent parameters. When there is only one parameter, there is no need to define it, and when there are multiple parameters, they need to be defined; expression and { statements;} represent the body. When the body contains statements, the curly braces do not need to be used. When the body has only one expression return value, the compiler automatically returns the value, and the curly braces need to specify the state where the expression returns a value.
[0046] Aspect Oriented Programming (AOP): It refers to a technology that realizes program functions through pre-compilation and runtime dynamic proxy. Optionally, this AOP technology is mainly used in functions such as logging, performance statistics, security control, transaction processing, and exception handling, separating the codes for logging, performance statistics, security control, transaction processing, exception handling, etc. from the business logic codes. That is, it extracts the aspects during the business processing process to obtain the isolation effect of low coupling between various parts of the logical process.
[0047] Java programming Assistant, Javassist: It is a class library for editing bytecodes in Java, enabling Java programs to define a new class at runtime. Javassist is a high-level Java bytecode processing class library that can generate classes and modify classes dynamically at runtime.
[0048] Optionally, after the introduction of Lambda expressions in Java 8, developers increasingly use Lambda expressions in programming. However, in the AOP solution based on Javassist, since the interface name and function name of the anonymous function are in an unknown state, the anonymous function cannot be recognized, and thus the AOP processing in the function cannot be implemented.
[0049] Combined with the above noun introduction, the method for obtaining information of the anonymous function provided in the embodiments of this application is described. Figure 1 FIG. is a flowchart of a method for obtaining information of an anonymous function provided by an exemplary embodiment of this application. This method is described by taking it as an example applied to a terminal. As Figure 1 shown, this method includes:
[0050] Step 101, obtain a bytecode file, which is a file generated after compiling the source code file containing the anonymous function. The bytecode file includes a bootstrap function and a bytecode constant pool.
[0051] Optionally, the bootstrap function includes a constant pool index.
[0052] Optionally, the anonymous function is a function expressed by a Lambda expression.
[0053] Optionally, in the embodiments of this application, taking the above source code file as a Java source file as an example, the bytecode file is a file generated after compiling the Java source file. According to the Java Virtual Machine (JVM) specification, in the bytecode file generated after compiling a Java source file containing a Lambda expression, a bootstrap method attribute table will be added to the attribute table set. The bootstrap method attribute table includes at least one bootstrap function, and the bootstrap function includes a constant pool index of the method handle.
[0054] Optionally, the bootstrap method attribute table describes at least one Lambda expression, and the information of the Lambda expression in the Java source file can be obtained by traversing the bootstrap method attribute table.
[0055] Optionally, after compiling the Java source file, a bytecode file is generated. The bytecode file includes an attribute table set, and the attribute table set includes at least one attribute table, including a bootstrap method attribute table. The bootstrap method attribute table includes at least one bootstrap function, and each bootstrap function includes a constant pool index of the method handle.
[0056] Step 102: Obtain the function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool, and obtain the first correspondence between the constant pool index and the function information.
[0057] Optionally, the function information includes at least one of the function name of the anonymous function, the function signature of the anonymous function, and the class name to which the anonymous function belongs.
[0058] Optionally, index through the bytecode constant pool using the constant pool index to obtain the function information of the anonymous function, and establish the first correspondence between the constant pool index and the function information.
[0059] Optionally, what is obtained by indexing through the constant pool index in the bytecode constant pool is a method handle (English: MethodHandle) corresponding to the constant pool index. Through this method handle, the function information of the anonymous function can be obtained from the bytecode constant pool.
[0060] Optionally, store the correspondence between the constant pool index and the function information in the form of key-value pairs (English: key-value), where the constant pool index is the key and the function information of the anonymous function is the value.
[0061] Step 103: Obtain the dynamic bytecode instruction, which includes an attribute table index and an interface information descriptor.
[0062] Optionally, the attribute table index is used to index to the corresponding bootstrap function, that is, through this attribute table index, a one-to-one correspondence can be established with the constant pool index in the bootstrap function.
[0063] Optionally, the attribute table index can also be directly implemented as the above constant pool index, that is, directly correspond the interface information with the function information through the constant pool index.
[0064] Optionally, to better support dynamic type languages, the JVM specification has added the dynamic bytecode instruction invokedynamic, which is the basis for the implementation of Lambda expressions.
[0065] Optionally, by traversing the bytecode constant pool, all the dynamic bytecode instructions in the bytecode constant pool can be obtained. Optionally, each dynamic bytecode instruction corresponds to a Lambda expression.
[0066] Optionally, the bytecode file includes a bootstrap function attribute table, and the attribute table index is used to index to the bootstrap function in the bootstrap function attribute table, so as to match the constant pool index in the bootstrap function. Optionally, since the constant pool index corresponds to obtain function information in the bytecode constant pool, the attribute table index can determine the matching relationship with the function information.
[0067] Optionally, the interface information descriptor (NameAndTypeInfo) gives the name and descriptor of a field or method, and a field or method can be determined through the interface information descriptor.
[0068] Step 104, obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool, and obtain the second corresponding relationship between the attribute table index and the interface information.
[0069] Optionally, through the interface information descriptor, the interface information of the anonymous function can be obtained from the bytecode constant pool, such as: the interface type and interface function name implemented by the anonymous function, etc.
[0070] Optionally, through the attribute table index, it can be indexed to the bootstrap function in the bootstrap function attribute table, and thus corresponding to the constant pool index in the bootstrap function. Therefore, after obtaining the interface information through the interface information descriptor, the corresponding relationship between the interface information and the constant pool index can be obtained.
[0071] Step 105, combine the first corresponding relationship and the second corresponding relationship to obtain the information of the anonymous function.
[0072] Optionally, the first corresponding relationship includes the corresponding relationship between the constant pool index and the function information. Through the above second corresponding relationship, after indexing to the bootstrap function in the bootstrap function attribute table through the attribute table index, the corresponding relationship between the attribute table index and the constant pool index in the bootstrap function is established, so as to obtain the corresponding relationship between the constant pool index and the interface information. That is, the first corresponding relationship includes the corresponding relationship between the constant pool index and the function information, and the second corresponding relationship includes the corresponding relationship between the constant pool index and the interface information. Thus, the function information and the interface information are corresponding through the constant pool index, and the context information of the anonymous function is obtained.
[0073] Optionally, by obtaining the context information of the anonymous function, subsequent applications can be performed on the anonymous function, such as: AOP processing.
[0074] In summary, the method for obtaining information of an anonymous function provided in this embodiment obtains function information from a bytecode constant pool according to a bootstrap function in a bytecode file, and obtains interface information from the bytecode constant pool through dynamic bytecode instructions. A corresponding relationship is established between the function information and the interface information according to the constant pool index and the attribute table index, so as to obtain the context information of the Lambda expression anonymous function and realize the application of the anonymous function.
[0075] In an optional embodiment, when the above function information is obtained, it is implemented by obtaining a method handle through an index. Figure 2 FIG. Figure 2 is a flowchart of a method for obtaining information of an anonymous function provided in another exemplary embodiment of the present application. Taking the application of this method in a terminal as an example, as Figure 2 shown, the method includes:
[0076] Step 201, obtain a bytecode file, which is a file generated after compiling a source code file containing an anonymous function. The bytecode file includes a bootstrap function and a bytecode constant pool.
[0077] Optionally, the bootstrap function includes a constant pool index.
[0078] Optionally, the anonymous function is a function expressed by a Lambda expression.
[0079] Optionally, after compiling a Java source file, a bytecode file is generated. The bytecode file includes a set of attribute tables. The set of attribute tables includes at least one attribute table, including a bootstrap function attribute table. The bootstrap function attribute table includes at least one bootstrap function, and each bootstrap function includes a constant pool index of a method handle.
[0080] Step 202, obtain a method handle pointer corresponding to the constant pool index from the bytecode constant pool.
[0081] Optionally, the bytecode file includes a bootstrap function attribute table. First, traverse the bootstrap function attribute table to obtain all the bootstrap functions in the bootstrap function attribute table, and obtain the constant pool index from the bootstrap function. Each bootstrap function contains a constant pool index of a method handle, so as to obtain a method handle pointer corresponding to the constant pool index from the bytecode constant pool.
[0082] Optionally, the method handle pointer is used to point to the content in the bytecode constant pool.
[0083] Optionally, the method handle is an application programming interface (API) introduced in Java for indirect method calls. The method handle includes two classes: MethodHandle and MethodType. Among them, MethodType is used to describe the return value type and parameter types of the method, and MethodHandle includes a pointer to an object. Optionally, in the embodiments of the present application, the case where the method handle includes a MethodHandle pointer is taken as an example for illustration. That is, through this constant pool index, a MethodHandle pointer can be queried from the bytecode constant pool.
[0084] Step 203: Obtain function information of the anonymous function from the bytecode constant pool according to the indication of the method handle pointer, and obtain a first correspondence relationship between the constant pool index and the function information.
[0085] Optionally, the method handle pointer points to the content in the bytecode constant pool. According to the indication of the method handle pointer, the content pointed to by the method handle pointer is obtained from the bytecode constant pool, that is, the function information of the anonymous function.
[0086] Optionally, the function information includes the function name of the anonymous function. Optionally, the function information further includes at least one of the function signature of the anonymous function and the class name to which the anonymous function belongs.
[0087] Optionally, since the method handle pointer is a pointer obtained through the constant pool index, and the function information of the anonymous function is the content pointed to by the method handle pointer, a first correspondence relationship between the constant pool index and the function information is obtained.
[0088] Step 204: Obtain a dynamic bytecode instruction, where the dynamic bytecode instruction includes an attribute table index and an interface information descriptor.
[0089] Optionally, the attribute table index is used to index to the corresponding bootstrap function. That is, through this attribute table index, a one-to-one correspondence can be established with the constant pool index in the bootstrap function.
[0090] Optionally, by traversing the bytecode constant pool, all dynamic bytecode instructions in the bytecode constant pool can be obtained. Optionally, each dynamic bytecode instruction corresponds to a Lambda expression.
[0091] Optionally, the bytecode file includes a bootstrap function attribute table, and the attribute table index is used to index to the bootstrap function in the bootstrap function attribute table, so as to match with the constant pool index in the bootstrap function.
[0092] Step 205: Obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool to obtain the second correspondence between the attribute table index and the interface information.
[0093] Optionally, through this interface information descriptor, the interface information of the anonymous function can be obtained from the bytecode constant pool. Optionally, the interface information includes at least one of the interface types implemented by the anonymous function and the interface function name.
[0094] Step 206: Combine the first correspondence and the second correspondence to obtain the information of the anonymous function.
[0095] Optionally, the first correspondence includes the correspondence between the constant pool index and the function information. Through the above second correspondence, after obtaining the bootstrap function in the bootstrap function attribute table through the attribute table index, establish the correspondence between the attribute table index and the constant pool index in the bootstrap function, so as to obtain the correspondence between the constant pool index and the interface information. That is, the first correspondence includes the correspondence between the constant pool index and the function information, and the second correspondence includes the correspondence between the constant pool index and the interface information. Thus, the function information and the interface information are corresponded through the constant pool index to obtain the context information of the anonymous function.
[0096] In summary, the method for obtaining the information of the anonymous function provided in this embodiment obtains the function information from the bytecode constant pool according to the bootstrap function through the bootstrap function and the bytecode constant pool in the bytecode file, and obtains the interface information from the bytecode constant pool through the dynamic bytecode instruction. The function information and the interface information are established in correspondence according to the constant pool index and the attribute table index, so as to obtain the context information of the Lambda expression anonymous function and realize the application of the anonymous function.
[0097] In the method provided in this embodiment, first obtain the method handle pointer from the bytecode constant pool according to the constant pool index, so as to obtain the function information of the anonymous function from the bytecode constant pool according to the method handle pointer, and obtain the interface information from the bytecode constant pool through the dynamic bytecode instruction. The function information and the interface information are established in correspondence according to the constant pool index and the attribute table index, so as to obtain the context information of the Lambda expression anonymous function and realize the application of the anonymous function.
[0098] Schematically, the function information acquisition architecture of the anonymous function is as Figure 3 shown:
[0099] First, obtain the bytecode file 310. The bytecode file 310 includes an attribute table set 320 and a bytecode constant pool 330. Among them, the attribute table set 320 includes a bootstrap function attribute table 340, and the bootstrap function attribute table 340 includes at least one bootstrap function 350. The bootstrap function 350 includes a constant pool index 351. Through the constant pool index 351, the function information 360 of the anonymous function can be indexed from the bytecode constant pool 330. The function information 360 includes the function name 361, function signature 362, and class name 363 of the anonymous function.
[0100] The bytecode constant pool 330 includes dynamic bytecode instructions 370. The dynamic bytecode instructions 370 include an attribute table index 371 and an interface information descriptor 372. Through the interface information descriptor 372, the interface information 380 of the anonymous function is obtained from the bytecode constant pool 330. The interface information 380 includes the interface class name 381 and interface function name 382 of the anonymous function.
[0101] Among them, the attribute table index 371 in the dynamic bytecode instruction 370 corresponds one-to-one with the constant pool index 351 in the bootstrap function 350. The attribute table index 371 can index to the bootstrap function 350 in the bootstrap function attribute table 340, so as to correspond to the constant pool index 351; or, the attribute table index 371 and the constant pool index 351 can be implemented as the same index, and the embodiments of the present application do not limit this.
[0102] In an alternative embodiment, the above anonymous function is a function expressed by a Lambda expression. Figure 4 FIG. is a flowchart of a method for obtaining information of an anonymous function provided by another exemplary embodiment of the present application. Taking the application of this method in a terminal as an example, as Figure 4 shown, the method includes:
[0103] Step 401, obtain a bytecode file, which is a file generated after compiling a source code file containing an anonymous function. The bytecode file includes a bootstrap function and a bytecode constant pool.
[0104] Optionally, the bootstrap function includes a constant pool index.
[0105] Optionally, the anonymous function is a function expressed by a Lambda expression.
[0106] Optionally, after compiling a Java source file, a bytecode file is generated. The bytecode file includes an attribute table set, and the attribute table set includes at least one attribute table, including a bootstrap function attribute table. The bootstrap function attribute table includes at least one bootstrap function, and each bootstrap function includes a constant pool index of a method handle.
[0107] Step 402: Obtain the method handle pointer corresponding to the constant pool index from the bytecode constant pool.
[0108] Optionally, the method handle is an application programming interface (API) introduced in Java for indirect method calls. The method handle includes two classes: MethodHandle and MethodType. Among them, MethodType is used to describe the return value type and parameter types of the method, and MethodHandle includes a pointer to an object. Optionally, in the embodiments of the present application, the case where the method handle includes a MethodHandle pointer is taken as an example for description, that is, through this constant pool index, a MethodHandle pointer can be queried from the bytecode constant pool.
[0109] Step 403: Obtain the function information of the anonymous function from the bytecode constant pool according to the indication of the method handle pointer.
[0110] Optionally, the method handle pointer points to the content in the bytecode constant pool. According to the indication of the method handle pointer, the content pointed to by the method handle pointer is obtained from the bytecode constant pool, that is, the function information of the anonymous function.
[0111] Optionally, the function information includes at least one of the function name of the anonymous function. Optionally, the function information further includes the function signature of the anonymous function and the class name to which the anonymous function belongs.
[0112] Step 404: Use the constant pool index as the key and the function information as the value to obtain the key-value pair between the constant pool index and the function information as the first correspondence.
[0113] Optionally, since the method handle pointer is a pointer obtained through the constant pool index, and the function information of the anonymous function is the content pointed to by the method handle pointer, the first correspondence between the constant pool index and the function information is obtained.
[0114] Optionally, the first correspondence between the constant pool index and the function information is stored in the form of a key-value pair (English: key-value), where the constant pool index is the keyword key and the function information of the anonymous function is the value value.
[0115] Step 405: Traverse the bytecode constant pool to obtain the dynamic bytecode instructions in the bytecode constant pool. The dynamic bytecode instructions include an attribute table index and an interface information descriptor.
[0116] Optionally, each dynamic bytecode instruction corresponds to a Lambda expression.
[0117] Optionally, the bytecode file includes a bootstrap function attribute table, and the attribute table index is used to index to the bootstrap function in the bootstrap function attribute table, so as to match the constant pool index in the bootstrap function.
[0118] Step 406: Obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool, and obtain the second corresponding relationship between the attribute table index and the interface information.
[0119] Optionally, through the interface information descriptor, the interface information of the anonymous function can be obtained from the bytecode constant pool. Optionally, the interface information includes at least one of the interface types implemented by the anonymous function and the interface function name.
[0120] Step 407: Combine the first corresponding relationship and the second corresponding relationship to obtain the information of the anonymous function.
[0121] Optionally, the first corresponding relationship includes the corresponding relationship between the constant pool index and the function information. Through the above second corresponding relationship, after indexing to the bootstrap function in the bootstrap function attribute table through the attribute table index, the corresponding relationship between the attribute table index and the constant pool index in the bootstrap function is established, so as to obtain the corresponding relationship between the constant pool index and the interface information. That is, the first corresponding relationship includes the corresponding relationship between the constant pool index and the function information, and the second corresponding relationship includes the corresponding relationship between the constant pool index and the interface information. Thus, the function information and the interface information are corresponded through the constant pool index to obtain the context information of the anonymous function.
[0122] In summary, the method for obtaining information of an anonymous function provided in this embodiment obtains function information from the bytecode constant pool according to the bootstrap function through the bootstrap function and the bytecode constant pool in the bytecode file, and obtains interface information from the bytecode constant pool through dynamic bytecode instructions. The function information and the interface information are established in a corresponding relationship according to the constant pool index and the attribute table index, so as to obtain the context information of the Lambda expression anonymous function and realize the application of the anonymous function.
[0123] Figure 5 It is a structural block diagram of an apparatus for obtaining information of an anonymous function provided by an exemplary embodiment of the present application. Taking the application of the apparatus in a terminal as an example for illustration, as Figure 5 shown, the apparatus includes: an obtaining module 510 and a determining module 520;
[0124] The obtaining module 510 is configured to obtain a bytecode file, where the bytecode file is a file generated after compiling a source code file containing the anonymous function, the bytecode file includes a bootstrap function and a bytecode constant pool, and the bootstrap function includes a constant pool index;
[0125] The obtaining module 510 is further configured to obtain function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool, so as to obtain a first correspondence between the constant pool index and the function information, where the function information includes the function name of the anonymous function;
[0126] The obtaining module 510 is further configured to obtain a dynamic bytecode instruction, where the dynamic bytecode instruction includes an attribute table index and an interface information descriptor;
[0127] The obtaining module 510 is further configured to obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool, so as to obtain a second correspondence between the attribute table index and the interface information;
[0128] The determining module 520 is configured to combine the first correspondence and the second correspondence to obtain information about the anonymous function.
[0129] In an optional embodiment, the obtaining module 510 is further configured to obtain a method handle pointer corresponding to the constant pool index from the bytecode constant pool, where the method handle pointer is used to point to the content in the bytecode constant pool; under the indication of the method handle pointer, obtain the function information of the anonymous function from the bytecode constant pool.
[0130] In an optional embodiment, the bytecode file includes a bootstrap function attribute table;
[0131] As Figure 6 shown, the obtaining module 510 includes:
[0132] A traversing unit 511, configured to traverse the bootstrap function attribute table to obtain the bootstrap function;
[0133] An obtaining unit 512, configured to obtain the constant pool index from the bootstrap function.
[0134] In an optional embodiment, the obtaining module 510 is further configured to use the constant pool index as a keyword and the function information as a value to obtain a key-value pair between the constant pool index and the function information as the first correspondence.
[0135] In an optional embodiment, the function information further includes at least one of a function signature of the anonymous function and a class name to which the anonymous function belongs.
[0136] In an optional embodiment, the anonymous function is a function expressed by a Lambda expression;
[0137] The obtaining module 510 includes:
[0138] A traversal unit 511 is configured to traverse the bytecode constant pool to obtain the dynamic bytecode instructions in the bytecode constant pool, where each Lambda expression corresponds to one of the dynamic bytecode instructions.
[0139] In an optional embodiment, the interface information includes at least one of an interface class name and an interface function name.
[0140] In summary, the information acquisition device for anonymous functions provided in this embodiment obtains function information from the bytecode constant pool according to the bootstrap function in the bytecode file, and obtains interface information from the bytecode constant pool through the dynamic bytecode instructions. The function information and the interface information are associated according to the constant pool index and the attribute table index, so as to obtain the context information of the Lambda expression anonymous function, and realize the application of the anonymous function.
[0141] It should be noted that: the information acquisition device for anonymous functions provided in the above embodiment is only illustrated by the division of the above functional modules. In practical applications, the above functions can be allocated to different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the information acquisition device for anonymous functions provided in the above embodiment and the embodiment of the information acquisition method for anonymous functions belong to the same concept, and the specific implementation process is detailed in the method embodiment, which will not be elaborated here.
[0142] Figure 7 FIG. shows a structural block diagram of a terminal 700 provided by an exemplary embodiment of the present invention. The terminal 700 may be: a smart phone, a tablet computer, an MP3 player (Moving Picture Experts Group Audio Layer III), an MP4 (Moving Picture Experts Group Audio Layer IV) player, a notebook computer, or a desktop computer. The terminal 700 may also be referred to by other names such as a user equipment, a portable terminal, a laptop terminal, a desktop terminal, etc.
[0143] Generally, the terminal 700 includes a processor 701 and a memory 702.
[0144] The processor 701 may include one or more processing cores, such as a quad-core processor, an octa-core processor, etc. The processor 701 may be implemented in at least one of the following hardware forms: DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 701 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the wake state, also known as the CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 701 may be integrated with a GPU (Graphics Processing Unit), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 701 may further include an AI (Artificial Intelligence) processor, which is used to process computational operations related to machine learning.
[0145] The memory 702 may include one or more computer-readable storage media, and the computer-readable storage media may be non-transitory. The memory 702 may further include high-speed random access memory and non-volatile memory, such as one or more disk storage devices and flash storage devices. In some embodiments, the non-transitory computer-readable storage media in the memory 702 is used to store at least one instruction, and the at least one instruction is used to be executed by the processor 701 to implement the information acquisition method of the anonymous function provided in the method embodiments of the present application.
[0146] In some embodiments, the terminal 700 may further optionally include: a peripheral device interface 703 and at least one peripheral device. The processor 701, the memory 702, and the peripheral device interface 703 may be connected by a bus or signal lines. Each peripheral device may be connected to the peripheral device interface 703 through a bus, signal lines, or a circuit board. Specifically, the peripheral devices include at least one of the following: a radio frequency circuit 704, a touch display screen 705, a camera 706, an audio circuit 707, a positioning component 708, and a power supply 709.
[0147] The peripheral device interface 703 can be used to connect at least one I / O (Input / Output) related peripheral device to the processor 701 and the memory 702. In some embodiments, the processor 701, the memory 702, and the peripheral device interface 703 are integrated on the same chip or circuit board; in some other embodiments, any one or two of the processor 701, the memory 702, and the peripheral device interface 703 can be implemented on separate chips or circuit boards, and this embodiment does not limit this.
[0148] The radio frequency circuit 704 is used to receive and transmit RF (Radio Frequency) signals, also known as electromagnetic signals. The radio frequency circuit 704 communicates with the communication network and other communication devices through electromagnetic signals. The radio frequency circuit 704 converts electrical signals into electromagnetic signals for transmission, or converts the received electromagnetic signals into electrical signals. Optionally, the radio frequency circuit 704 includes: an antenna system, an RF transceiver, one or more amplifiers, a tuner, an oscillator, a digital signal processor, a codec chipset, a subscriber identity module card, and so on. The radio frequency circuit 704 can communicate with other terminals through at least one wireless communication protocol. The wireless communication protocol includes but is not limited to: the World Wide Web, a metropolitan area network, an intranet, generations of mobile communication networks (2G, 3G, 4G, and 5G), a wireless local area network, and / or a WiFi (Wireless Fidelity) network. In some embodiments, the radio frequency circuit 704 may also include a circuit related to NFC (Near Field Communication), and this application does not limit this.
[0149] The display screen 705 is used to display the UI (User Interface). The UI may include graphics, text, icons, videos, and any combination thereof. When the display screen 705 is a touch display screen, the display screen 705 also has the ability to collect touch signals on or above the surface of the display screen 705. The touch signals can be input as control signals to the processor 701 for processing. At this time, the display screen 705 can also be used to provide virtual buttons and / or a virtual keyboard, also known as soft buttons and / or a soft keyboard. In some embodiments, there may be one display screen 705, which is provided on the front panel of the terminal 700; in other embodiments, there may be at least two display screens 705, which are respectively provided on different surfaces of the terminal 700 or are in a foldable design; in still other embodiments, the display screen 705 may be a flexible display screen, which is provided on the curved surface or the folding surface of the terminal 700. Even, the display screen 705 can also be set to an irregular non-rectangular shape, that is, a special-shaped screen. The display screen 705 can be prepared using materials such as LCD (Liquid Crystal Display) and OLED (Organic Light-Emitting Diode).
[0150] The camera module 706 is used to capture images or videos. Optionally, the camera module 706 includes a front camera and a rear camera. Generally, the front camera is provided on the front panel of the terminal, and the rear camera is provided on the back of the terminal. In some embodiments, there are at least two rear cameras, which are any one of a main camera, a depth camera, a wide-angle camera, and a telephoto camera respectively, so as to implement the function of background blurring by fusing the main camera and the depth camera, panoramic shooting by fusing the main camera and the wide-angle camera, and VR (Virtual Reality) shooting function or other fusion shooting functions. In some embodiments, the camera module 706 may further include a flash. The flash can be a single-color temperature flash or a dual-color temperature flash. The dual-color temperature flash refers to the combination of a warm light flash and a cold light flash, which can be used for light compensation under different color temperatures.
[0151] The audio circuit 707 may include a microphone and a speaker. The microphone is used to collect sound waves of the user and the environment, and convert the sound waves into electrical signals for input to the processor 701 for processing, or input to the radio frequency circuit 704 to enable voice communication. For the purpose of stereo collection or noise reduction, there may be multiple microphones, which are respectively arranged at different parts of the terminal 700. The microphone may also be an array microphone or an omnidirectional collection microphone. The speaker is used to convert the electrical signal from the processor 701 or the radio frequency circuit 704 into sound waves. The speaker may be a traditional thin film speaker or a piezoelectric ceramic speaker. When the speaker is a piezoelectric ceramic speaker, it can not only convert the electrical signal into sound waves audible to humans, but also convert the electrical signal into sound waves inaudible to humans for uses such as ranging. In some embodiments, the audio circuit 707 may also include a headphone jack.
[0152] The positioning component 708 is used to locate the current geographical location of the terminal 700 to implement navigation or LBS (Location Based Service). The positioning component 708 may be a positioning component based on the GPS (Global Positioning System) of the United States, the Beidou system of China, or the Galileo system of Russia.
[0153] The power supply 709 is used to supply power to each component in the terminal 700. The power supply 709 may be alternating current, direct current, a disposable battery, or a rechargeable battery. When the power supply 709 includes a rechargeable battery, the rechargeable battery may be a wired rechargeable battery or a wireless rechargeable battery. A wired rechargeable battery is a battery charged through a wired line, and a wireless rechargeable battery is a battery charged through a wireless coil. The rechargeable battery can also be used to support fast charging technology.
[0154] In some embodiments, the terminal 700 further includes one or more sensors 710. The one or more sensors 710 include but are not limited to: an acceleration sensor 711, a gyroscope sensor 712, a pressure sensor 713, a fingerprint sensor 714, an optical sensor 715, and a proximity sensor 716.
[0155] The acceleration sensor 711 can detect the magnitude of acceleration on the three coordinate axes of the coordinate system established with the terminal 700. For example, the acceleration sensor 711 can be used to detect the components of the gravitational acceleration on the three coordinate axes. The processor 701 can control the touch display screen 705 to display the user interface in a landscape view or a portrait view according to the gravitational acceleration signal collected by the acceleration sensor 711. The acceleration sensor 711 can also be used for game or collection of the user's motion data.
[0156] The gyroscope sensor 712 can detect the body orientation and rotation angle of the terminal 700. The gyroscope sensor 712 can cooperate with the acceleration sensor 711 to collect the 3D actions of the user on the terminal 700. Based on the data collected by the gyroscope sensor 712, the processor 701 can implement the following functions: motion sensing (such as changing the UI according to the user's tilting operation), image stabilization during shooting, game control, and inertial navigation.
[0157] The pressure sensor 713 can be disposed on the side frame of the terminal 700 and / or the lower layer of the touch display screen 705. When the pressure sensor 713 is disposed on the side frame of the terminal 700, it can detect the holding signal of the user on the terminal 700, and the processor 701 can perform left / right hand recognition or quick operation according to the holding signal collected by the pressure sensor 713. When the pressure sensor 713 is disposed on the lower layer of the touch display screen 705, the processor 701 can control the operable controls on the UI interface according to the pressure operation of the user on the touch display screen 705. The operable controls include at least one of button controls, scroll bar controls, icon controls, and menu controls.
[0158] The fingerprint sensor 714 is used to collect the fingerprint of the user. The processor 701 can identify the user's identity according to the fingerprint collected by the fingerprint sensor 714, or the fingerprint sensor 714 can identify the user's identity according to the collected fingerprint. When the identified user identity is a trusted identity, the processor 701 authorizes the user to perform relevant sensitive operations, and the sensitive operations include unlocking the screen, viewing encrypted information, downloading software, making payments, and changing settings, etc. The fingerprint sensor 714 can be disposed on the front, back, or side of the terminal 700. When there are physical buttons or manufacturer logos on the terminal 700, the fingerprint sensor 714 can be integrated with the physical buttons or manufacturer logos.
[0159] The optical sensor 715 is used to collect the ambient light intensity. In one embodiment, the processor 701 can control the display brightness of the touch display screen 705 according to the ambient light intensity collected by the optical sensor 715. Specifically, when the ambient light intensity is high, the display brightness of the touch display screen 705 is increased; when the ambient light intensity is low, the display brightness of the touch display screen 705 is decreased. In another embodiment, the processor 701 can also dynamically adjust the shooting parameters of the camera module 706 according to the ambient light intensity collected by the optical sensor 715.
[0160] The proximity sensor 716, also known as the distance sensor, is typically disposed on the front panel of the terminal 700. The proximity sensor 716 is used to collect the distance between the user and the front of the terminal 700. In one embodiment, when the proximity sensor 716 detects that the distance between the user and the front of the terminal 700 is gradually decreasing, the processor 701 controls the touch display screen 705 to switch from the lit state to the off state; when the proximity sensor 716 detects that the distance between the user and the front of the terminal 700 is gradually increasing, the processor 701 controls the touch display screen 705 to switch from the off state to the lit state.
[0161] Those skilled in the art can understand that Figure 7 the structure shown in does not constitute a limitation on the terminal 700, and it may include more or fewer components than shown in the figure, or combine certain components, or adopt a different component arrangement.
[0162] Optionally, the computer-readable storage medium may include: read-only memory (ROM, Read Only Memory), random access memory (RAM, Random Access Memory), solid state drives (SSD, Solid State Drives), or optical discs, etc. Among them, the random access memory may include resistive random access memory (ReRAM, Resistance Random Access Memory) and dynamic random access memory (DRAM, Dynamic Random Access Memory). The serial numbers of the embodiments of the present application above are only for description and do not represent the advantages or disadvantages of the embodiments.
[0163] Those of ordinary skill in the art can understand that all or part of the steps of implementing the above embodiments can be completed by hardware, or can be completed by a program instructing relevant hardware. The program can be stored in a computer-readable storage medium. The storage medium mentioned above can be read-only memory, a magnetic disk, or an optical disc, etc.
[0164] The above are only optional embodiments of the present application and are not intended to limit the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A method for obtaining information of an anonymous function, characterized in that, the method includes: Obtaining a bytecode file, which is a file generated after compiling a source code file containing the anonymous function. The bytecode file includes a bootstrap function and a bytecode constant pool. The bootstrap function includes a constant pool index, and the anonymous function is a function expressed by a Lambda expression; Obtaining the function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool, obtaining a first correspondence between the constant pool index and the function information, and the function information includes the function name of the anonymous function; Obtaining dynamic bytecode instructions, which include an attribute table index and an interface information descriptor, and the attribute table index is used to index to the corresponding bootstrap function; Obtaining the interface information corresponding to the interface information descriptor from the bytecode constant pool, obtaining a second correspondence between the attribute table index and the interface information; Combining the first correspondence and the second correspondence to obtain the information of the anonymous function.
2. The method according to claim 1, characterized in that, the obtaining the function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool includes: Obtaining a method handle pointer corresponding to the constant pool index from the bytecode constant pool, and the method handle pointer is used to point to the content in the bytecode constant pool; Under the indication of the method handle pointer, obtaining the function information of the anonymous function from the bytecode constant pool.
3. The method according to claim 2, characterized in that, the bytecode file includes a bootstrap function attribute table; Before obtaining the function information of the anonymous function obtained from the constant pool index from the bytecode constant pool, it further includes: Traversing the bootstrap function attribute table to obtain the bootstrap function; Obtaining the constant pool index from the bootstrap function.
4. The method according to any one of claims 1 to 3, characterized in that, the obtaining the first correspondence between the constant pool index and the function information includes: Using the constant pool index as a keyword and the function information as a value, obtaining a key-value pair between the constant pool index and the function information as the first correspondence.
5. The method according to any one of claims 1 to 3, characterized in that, the function information further includes at least one of the function signature of the anonymous function and the class name to which the anonymous function belongs.
6. The method according to any one of claims 1 to 3, characterized in that, the obtaining the dynamic bytecode instructions includes: Traversing the bytecode constant pool to obtain the dynamic bytecode instructions in the bytecode constant pool, where each Lambda expression corresponds to one dynamic bytecode instruction.
7. The method according to claim 6, characterized in that, the interface information includes at least one of an interface class name and an interface function name.
8. An apparatus for obtaining information of an anonymous function, characterized in that, the apparatus includes: An acquisition module, configured to acquire a bytecode file, where the bytecode file is a file generated after compilation of a source code file containing the anonymous function, the bytecode file includes a bootstrap function and a bytecode constant pool, the bootstrap function includes a constant pool index, and the anonymous function is a function expressed by a Lambda expression; The acquisition module is further configured to obtain function information of the anonymous function corresponding to the constant pool index from the bytecode constant pool, to obtain a first correspondence between the constant pool index and the function information, where the function information includes the function name of the anonymous function; The acquisition module is further configured to obtain dynamic bytecode instructions, where the dynamic bytecode instructions include an attribute table index and an interface information descriptor, and the attribute table index is used to index to the corresponding bootstrap function; The acquisition module is further configured to obtain the interface information corresponding to the interface information descriptor from the bytecode constant pool, to obtain a second correspondence between the attribute table index and the interface information; A determination module, configured to combine the first correspondence and the second correspondence to obtain information about the anonymous function.
9. A computer device, characterized in that, the computer device includes a processor and a memory, and at least one program is stored in the memory, and the at least one program is loaded and executed by the processor to implement the method for obtaining information about an anonymous function according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, at least one program is stored in the storage medium, and the at least one program is loaded and executed by a processor to implement the method for obtaining information about an anonymous function according to any one of claims 1 to 7.
Citation Information
Patent Citations
Business data acquisition method and device thereof, storage medium and computing equipment
CN112306501A
Code hot replacement method and apparatus
WO2024255178A1