Code generation method, computing device and storage medium

By identifying and converting the parameter types of extension functions to nullable types in Kotlin source files, and generating corresponding function signatures and body code, the problem of chained call interruption is solved, improving development efficiency and code conciseness.

CN121387254APending Publication Date: 2026-01-23BEIJING CHENGSHI WANGLIN INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511544732.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-27
Publication Date
2026-01-23

AI Technical Summary

Technical Problem

In Kotlin application development, the problem of passing null parameters when chaining extension functions can cause the call to be interrupted is usually solved by adding cumbersome null check code on the calling side.

Method used

By identifying multiple primitive extension functions used for chaining in Kotlin source files, converting their parameter types to nullable types, and generating corresponding function signatures and function body code, it ensures that the current object is returned when the parameter is null, thereby avoiding interruption during chaining.

Benefits of technology

It enables chained calls to execute continuously when the parameters are empty, reducing the null check code on the calling side and improving development efficiency and code conciseness.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121387254A_ABST
    Figure CN121387254A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code generation method, computing equipment and a storage medium. The method comprises the following steps of: identifying function information respectively corresponding to a plurality of original expansion functions for chained calling from a to-be-processed packet which is generated by compiling a source file based on a Kotlin language; the function information comprises function signature information; for each original extension function, converting the parameter type of each parameter in the function signature information from a non-null type to a null type to generate converted function signature information; on the basis of the converted function signature information, a function signature code and a function body code are generated, and the function body code is configured to call the original spread function under the condition that the parameter value is judged not to be empty, and return to the current object under the condition that the parameter value of any parameter is empty; and writing the function signature code and the function body code into a source code file of the target extension function and storing. According to the technical scheme provided by the embodiment of the invention, the chained calling of the target extension function can be realized without interruption.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to the technical field of computer, and particularly relate to a code generation method, a computing device and a storage medium. BACKGROUND

[0002] In modern Kotlin application development, chain invocation of extension functions is widely used, for example, it can be applied to modern Android (Android) native UI (User Interface, UI) development framework such as Jetpack Compose, which is written in Kotlin language, allowing developers to build more flexible and responsive UI with less code. Extension functions are a feature of the Kotlin language, which are new functions added to existing classes without inheritance. Chain invocation of extension functions refers to connecting multiple extension functions through return values to form a call chain, where the return value of the previous extension function can be used as the call object of the next extension function.

[0003] However, the parameters of some extension functions are non-null types, and passing null parameters when chain invoking extension functions will cause the chain invocation to be interrupted, so a solution is needed to safely handle null parameters to ensure smooth chain invocation. SUMMARY

[0004] Embodiments of the present application provide a code generation method, a computing device and a storage medium to solve the problem of interruption of chain invocation of extension functions in the prior art.

[0005] In a first aspect, the embodiments of the present application provide a code generation method, comprising: From the to-be-processed package generated by compiling the source file based on the Kotlin language, identify function information corresponding to a plurality of original extension functions used for chain invocation; the function information includes function signature information; For each original extension function, convert the parameter type of each parameter in the function signature information from a non-null type to a nullable type to generate converted function signature information; Generate function signature code based on the converted function signature information; Generate function body code based on the converted function signature information, the function body code is configured to: call the original extension function if the parameter value is not null, and return the current object if the parameter value of any parameter is null; Write the function signature code and the function body code into the source code file of the target extension function; The storage module is configured to store source code files corresponding to the plurality of original extension functions respectively, so as to form a calling chain by combination of the plurality of target extension functions, and execute executable codes generated by compiling the source code files corresponding to the plurality of target extension functions respectively when the plurality of target extension functions are called in a chain.

[0006] In a second aspect, an embodiment of the present application provides a code generation apparatus, which comprises: The identification module is configured to identify function information corresponding to a plurality of original extension functions used for chain calling from a to-be-processed package generated by compiling a source file based on a Kotlin language. The conversion module is configured to convert, for each original extension function, a parameter type of each parameter in the function signature information from a non-null type to a nullable type, to generate converted function signature information. The first generation module is configured to generate function signature code based on the converted function signature information. The second generation module is configured to generate function body code based on the converted function signature information, and the function body code is configured to call the original extension function when a parameter value is not null, and return a current object when a parameter value of any parameter is null. The writing module is configured to write the function signature code and the function body code into a source code file of a target extension function. The storage module is configured to store source code files corresponding to the plurality of original extension functions respectively, so as to form a calling chain by combination of the plurality of target extension functions, and execute executable codes generated by compiling the source code files corresponding to the plurality of target extension functions respectively when the plurality of target extension functions are called in a chain.

[0007] In a third aspect, an embodiment of the present application provides a computing device, which comprises a processing component and a storage component. The storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component, to implement the code generation method in the first aspect.

[0008] In a fourth aspect, an embodiment of the present application provides a computer storage medium, which stores a computer program; the computer program is executed by a computer, to implement the code generation method in the first aspect.

[0009] In a fifth aspect, an embodiment of the present application provides a computer program product, which comprises a computer program or instructions; the computer program or instructions are executed by a processing component, to implement the code generation method in the first aspect.

[0010] In the embodiments of the present application, first, function signature information corresponding to each of the plurality of original extension functions used for chain calling is identified, so that the parameter type of each parameter is converted from a non-null type to a nullable type based on the function signature information, the function signature code and the function body code of the target extension function are generated according to the converted function signature information, and the function signature code and the function body code of the target extension function are written into the source code file of the target extension function. When the target extension function is used for chain calling, the logic of the function body code of the target extension function can be executed, the original extension function corresponding to the target extension function is called when the parameter value is not null, and the current object is returned when the parameter value of any parameter is null, so that the call chain can continue to execute under the condition of passing null parameters, and will not be interrupted due to passing null parameters.

[0011] These aspects or other aspects of the present application will be more apparent in the following description of the embodiments. BRIEF DESCRIPTION OF DRAWINGS

[0012] In order to more clearly illustrate the technical solutions of the embodiments of the present application or the prior art, the drawings needed in the description of the embodiments or the prior art will be briefly introduced. Obviously, the drawings in the following description are some embodiments of the present application, and other drawings can also be obtained by those skilled in the art without creative labor.

[0013] Figure 1 A flow chart of one embodiment of a code generation method provided by the present application is shown; Figure 2 A flow chart of one embodiment of a function information acquisition method provided by the present application is shown; Figure 3 A structural schematic diagram of one embodiment of a code generation device provided by the present application is shown; Figure 4 A structural schematic diagram of one embodiment of a function information acquisition device provided by the present application is shown; Figure 5 A structural schematic diagram of one embodiment of a computing device provided by the present application is shown. DETAILED DESCRIPTION

[0014] In order to enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application.

[0015] In some of the flowcharts described in this specification and the accompanying drawings, and in the description of the above-mentioned figures, a plurality of operations are included in a specific order, but it should be clear that these operations can be executed in the order in which they appear in this text or in parallel, and the serial numbers of the operations, such as 101, 102, etc., are only used to distinguish different operations, and the serial numbers themselves do not represent any execution order. In addition, these flowcharts can include more or fewer operations, and these operations can be executed in sequence or in parallel. It should be noted that the descriptions of "first", "second", etc. in this text are used to distinguish different messages, devices, modules, etc., and do not represent the order of precedence, nor do "first" and "second" represent different types.

[0016] The technical solutions in the embodiments of the present application will be described clearly and completely in the specification of the present application in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, not all. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of protection of the present application.

[0017] As described in the background, there is currently a problem that the parameters of the extension function are non-empty types, and passing in an empty parameter when chaining the extension function will cause the chain to be interrupted.

[0018] The inventor found that when facing the problem of chain interruption, external control logic is usually added on the calling side, that is, when calling the extension function, write the null judgment code of the conditional judgment, such as executing the next extension function in the chain when the return value of any extension function is empty, but this will cause code redundancy and tediousness.

[0019] To this end, the present application provides a code generation method, wherein the method comprises: identifying function information corresponding to a plurality of original extension functions for chain calling from a to-be-processed package generated by compiling a source file based on Kotlin language; the function information includes function signature information; for each original extension function, converting the parameter type of each parameter in the function signature information from a non-empty type to a nullable type to generate converted function signature information; based on the converted function signature information, generating function signature code and function body code, the function body code is configured to: call the original extension function when the parameter value is not empty, and return the current object when the parameter value of any parameter is empty; write the function signature code and the function body code into a source code file of a target extension function and store.

[0020] By accurately identifying the function signature information corresponding to each of the plurality of original extension functions used for chain calling, the parameter type of each parameter is converted from a non-null type to a nullable type based on the function signature information, the function signature code and the function body code of the target extension function are generated according to the converted function signature information, and the function signature code and the function body code of the target extension function are written into the source code file of the target extension function. When the target extension function is used for chain calling, the logic of the function body code of the target extension function can be executed, the original extension function corresponding to the target extension function is called when the parameter value is not null, and the current object is returned when the parameter value of any parameter is null, so that the call chain can continue to execute under the condition of parameter passing null, and the chain will not be interrupted due to parameter passing null.

[0021] The present application identifies function information, converts function signature information, and generates a source code file of a target extension function by full automation. The parameter null handling logic of the target extension function is defined on the function definition side, the chain is not interrupted when the target extension function is called in chain, and there is no need for manual coding of tedious null judgment code on the calling side, which can improve the simplicity of the calling side code and improve the development efficiency.

[0022] The technical solutions provided by the embodiments of the present application will be described in detail below with reference to the accompanying drawings.

[0023] Figure 1 A flowchart of one embodiment of the code generation method provided by the embodiments of the present application can include the following steps: 101: Identify the function signature information corresponding to each of the plurality of original extension functions used for chain calling from the to-be-processed package generated by compiling the source file based on the Kotlin language.

[0024] The to-be-processed package can be generated by compiling one or more source files based on the Kotlin language. Of course, the to-be-processed package can also be generated by compiling one or more source files based on the Kotlin language and one or more source files based on other programming languages such as the Java language. The classes defined by one or more class files generated by compiling one or more source files constitute the to-be-processed package.

[0025] The compiler can compile one or more source files based on the Kotlin language into one or more class files in bytecode format, and the class files contain definition information of the classes, such as information about the package to which the classes belong. The class file can be a.class file, etc. The classes defined by the one or more class files can belong to a package, and the package can refer to the namespace of the classes defined by the one or more class files. The class file is the physical carrier of the class, and the package is the logical carrier of the class.

[0026] The function information corresponding to each of the plurality of original extension functions can be obtained by scanning the to-be-processed package and screening the plurality of original extension functions for the chain invocation.

[0027] The function signature information of the original extension function can include, for example, a function name, a receiver type, a return type, and information about each parameter of the original extension function, such as a parameter name and a parameter type.

[0028] The plurality of chain extension functions for the chain invocation can be identified, for example, according to the return type and / or the receiver type. The original extension functions with the same return type and receiver type can be used for the chain invocation.

[0029] 102: For each original extension function, convert the parameter type of each parameter in the function signature information from a non-null type to a nullable type to generate converted function signature information.

[0030] For example, the parameter type of a certain parameter can be converted from a non-null type such as “Dp” to a nullable type such as “Dp?”.

[0031] 103: Generate function signature code based on the converted function signature information.

[0032] The function signature information before conversion is the function signature information of the original extension function, the converted function signature information can be the function signature information of a target extension function corresponding to the original extension function, and the function signature code generated based on the converted function signature information can be the function signature code of the target extension function.

[0033] The function name in the function signature information of the original extension function can be used as the function name of the target extension function, so that the target extension function has the same name as the original extension function, and the developer can automatically invoke the target extension function and run the logic of the target extension function when invoking the function through the function name without awareness. Of course, the function name in the function signature information of the original extension function can also be converted, and the converted function name is the function name of the target extension function. The function signature code of the target extension function is generated based on the converted function name, so that the target extension function has a different name from the original extension function.

[0034] The parameter name of each parameter in the function signature information of the original extension function can not be converted, so that the parameter name of each parameter in the function signature information of the target extension function is the same as the parameter name of each parameter in the original extension function, thereby realizing that the parameters of the original extension function are used as the parameters of the target extension function.

[0035] Further, the function signature information of the original extension function can further include a return type name of a return type and a return type package name. The return type package name is used to locate and identify the return type. The return type name of the original extension function can be used as the receiver type name and the return type name in the function signature information of the target extension function, so that the return type of the original extension function can be used as the receiver type and the return type of the target extension function. Since the return type of the original extension function can also be the receiver type, the receiver type and the return type of the original extension function are the same as the receiver type and the return type of the target extension function.

[0036] Generating the function signature code based on the converted function signature information can be generating the function signature code of the target extension function according to the receiver type, the function name, the parameter name and the parameter type of each parameter, and the return type of the target extension function.

[0037] For example, the receiver type and the return type of the target extension function are both Modifier, the function name is padding, the parameter name is all, and the parameter type is Dp?. The generated function signature code is: public fun Modifier.padding(all:Dp?):Modifier 104: Generating the function body code based on the converted function signature information, the function body code is configured to: calling the original extension function when the parameter value is not null, and returning the current object when the parameter value of any parameter is null.

[0038] For example, the function body generation method can be used to generate the function body code based on the function name and the parameter name of any parameter in the converted function signature information.

[0039] For example, the function body generation method getCodeBlock() is: private fun getCodeBlock(funName:String,paramName: String):CodeBlock{ returnCodeBlock.builder() .addStatement("return if ($paramName!= null) $funName($paramName)else this") .build() } For example, the generated function body code of the target extension function is: public fun Modifier.padding(all:Dp?):Modifier{ return if (all != null) padding(all) else this / / function body code } Here, `funName` is the function name, and `paraName` is the parameter name. If the parameter is not empty, the original extension function is called; if the parameter is empty, the current object `this` is returned. For example, if multiple target extension functions form a call chain, when executing the current target extension function, the current object `this` can refer to the receiver object that called the current target extension function itself. Returning the current object `this` does not modify the object state of the receiver object, allowing the call chain to continue executing without being interrupted by an empty parameter.

[0040] In this case, since the function name (parameter name) in the function body code is like `padding(all)`, and the parameter `all` is not explicitly declared as a nullable type, it defaults to a non-nullable type. Because a non-nullable type is a subtype of a nullable type, the compiler will prioritize calling the more specific extension function. Therefore, even if both the target extension function and the original extension function have the name `padding`, when the parameter value of `all` is non-nullable, the compiler will prioritize calling the more specific original extension function `padding(all)`, and will not execute the target extension function `padding(all:Dp?)`, thus avoiding program recursion.

[0041] 105: Write the function signature code and function body code into the source code file of the target extended function.

[0042] KotlinPoet (a code generation tool for the Kotlin language) can be used to construct the FunSpec of the target extension function, generate the function signature code and function body code of the target extension function, and then generate a complete .kt source code file through FileSpec, thus writing the function signature code and function body code of the target extension function into the source code file of the target extension function.

[0043] 106: Store the source code files corresponding to the multiple target extension functions corresponding to the multiple original extension functions, so that the multiple target extension functions can be combined to form a call chain, and when the multiple target extension functions are called in a chain, the executable code generated by compiling the source code files corresponding to the multiple target extension functions is executed.

[0044] This allows for the storage of source code files corresponding to multiple target extension functions on disk. These source code files can then be compiled by a compiler into machine-executable code.

[0045] For example, the target extension functions padding() and size() can be combined to form a call chain such as Modifier.padding().size().

[0046] In this embodiment, the function signature information corresponding to the multiple original extension functions used for chained calls is first accurately identified. Based on the function signature information, the parameter type of each parameter is converted from a non-nullable type to a nullable type. The function signature code and function body code of the target extension function are generated according to the converted function signature information. After the function signature code and function body code of the target extension function are written into the source code file of the target extension function, when the target extension function is used for chained calls, the logic of the function body code of the target extension function can be executed. If the parameter value is not null, the original extension function corresponding to the target extension function is called. If the parameter value of any parameter is null, the current object is returned. Thus, the call chain can continue to execute even if the parameter is null, without being interrupted due to the parameter being null.

[0047] This embodiment automatically identifies function information, converts function signature information, and generates the source code file of the target extended function. It defines the parameter passing null handling logic of the target extended function on the function definition side, so that the chain of chained calls to the target extended function is not interrupted, without the need to manually write cumbersome null check code on the calling side. This can improve the simplicity of the calling side code and improve development efficiency.

[0048] In this embodiment, instead of using non-nullable parameter types as much as possible in Kotlin, the parameter type of the target extension function is set to a nullable type. This ensures that no error will occur when the parameter value passed to the target extension function is empty, and the chained call will not be interrupted.

[0049] In some embodiments, the function information includes the original package name of the original extended function, and the function signature information includes the function name.

[0050] The method may further include: generating a first import statement that imports the original extended function in the original package based on the original package name and function name; generating a package declaration statement for the target extended function based on the pre-configured target package name of the target extended function; and writing the first import statement and the package declaration statement into the source code file of the target extended function.

[0051] The original package name of the original extension function can be the package name of the above-mentioned to-be-processed package. For example, the original package name is androidx.compose.foundation.layout, the function name is padding, and the first import statement is import androidx.compose.foundation.layout.padding. The first import statement is used to realize that when the target extension function is called by passing a non-null parameter to the target extension function, the compiler can locate the original extension function.

[0052] The target package name of the target extension function is pre-configured so that the function signature code and the function body code of the target extension function, and the first import statement can be attributed to the target package. For example, the target package name is com.example.modifier, and the package declaration statement can be package com.example.modifier. The original package name and the target package name can be different, which realizes that the original extension function and the target extension function are attributed to different packages, realizes functional isolation, and avoids conflicts when the original extension function and the target extension function with the same name are called.

[0053] In some embodiments, the function signature information can include the parameter type package name and the parameter type name of each parameter.

[0054] Converting the parameter type of each parameter in the function signature information from a non-null type to a nullable type can include: converting the parameter type of each parameter from a non-null type to a nullable type according to the parameter type package name and the parameter type name corresponding to the parameter type of each parameter in the function signature information.

[0055] For example, paramType = ClassName(paramPackage, paramTypename), a class type ClassName object paramType can be created according to the parameter type package name such as paramPackage and the parameter type name paraTypename, and the parameter type can be converted from a non-null type to a nullable type by paramType.copy(nuLlable = true). Among them, the parameter type package name is the package name of the package to which the parameter type belongs, which is used to locate the parameter type.

[0056] The method can further include: generating a second import statement for importing the parameter type of each parameter according to the parameter type package name and the parameter type name corresponding to the parameter type of each parameter; and writing the second import statement into the source code file of the target extension function.

[0057] For example, the parameter type package name is androidx.compose.ui.unit, the parameter type name is Dp, and the second import statement is import androidx.compose.ui.unit.Dp. Through the second import statement, the compiler can locate the parameter type.

[0058] According to the foregoing description, the function signature information can include the return type name of the return type and the return type package name. In some embodiments, the method can further include: generating a third import statement for importing the return type according to the return type package name and the return type name; and writing the third import statement into the source code file. For example, the return type name is Modifier, the return type package name is the package name of the package to which the return type belongs, such as androidx.compose.ui, and the third import statement is importandroidx.compose.ui.Modifier. Through the third import statement, the compiler can locate the return type.

[0059] At present, the source file of the Kotlin language can be compiled by the Java virtual machine (JVM), and when the Kotlin language is compatible with the Java virtual machine (JVM) ecosystem, the language characteristics unique to the Kotlin language will be presented in a specific form after compilation. For example, an extension function defined at the top layer of the source file will not exist as a member method of its extension receiver type after compilation, but will be compiled into a static method in a container class (such as StringUtilsKt.class) with a name ending in "Kt". The existing Java reflection mechanism such as (java.lang.reflect) loses the meta information of the object extended by the extension function, and the extension function and the normal tool method in the source file are both presented as static methods after compilation, which makes it impossible to distinguish the extension function from the static method. In addition, the class file of the bytecode can be decompiled to restore the source code, manually distinguish the extension function, and then obtain the function information, but the source code is unstructured text that needs manual intervention or complex natural language processing to identify the function information, which cannot achieve automatic, programmed and accurate function information acquisition, and the process is tedious and has low reliability. Therefore, there is an urgent need for a technical solution that can automatically and accurately identify and extract function information of an extension function from Kotlin bytecode.

[0060] To illustrate in detail the function information corresponding to each of the plurality of original extension functions for chain invocation from the to-be-processed package generated by compiling the source file based on the Kotlin language. Figure 2 A flowchart of one embodiment of a function information acquisition method provided by an embodiment of the present application is shown, and the method includes the following steps: 201: Obtain a package to be processed, which is generated based on source file compilation of the Kotlin language.

[0061] The embodiment can be applied to Android development, since the Android system uses a Dalvik virtual machine (or ART, Android runtime), and Dalvik / ART does not directly run class files. Therefore, the Android compilation tool chain uses the dx tool to combine one or more class files such as.class files into an Android executable file such as a.dex file, and the Android executable file contains the definition information of the classes contained in the original class files. If the classes in the original multiple class files belong to different packages, the Android executable file can be combined from classes contained in multiple different packages.

[0062] The Android executable file can be obtained in advance, and the Android executable file can be combined from classes contained in multiple different packages.

[0063] Obtaining the package to be processed can include receiving a target package name input by a user, and determining the package to be processed from the Android executable file based on the target package name using the DexFile class.

[0064] The DexFile is a class provided by Android, which can scan an Android executable file such as a.dex file, and extract class information, method information, etc. For example, the class information can include a class name.

[0065] The class file name of the class file retains the complete package name, such as the class file name being prefixed with the package name of the package to which the class file belongs, such as "com.example", and combined with the class name "MyKotlinClass" of the class file to form "com.example.MyKotlinClass". The class name can refer to the simple name of the class defined by the class file, and the class file name of the class file is the class name of the class defined in the class file. The class name can refer to the fully qualified name of the class.

[0066] The class name of the class file retains the complete package name, such as the class file name being prefixed with the package name of the package to which the class file belongs, such as "com.example", and combined with the class name "MyKotlinClass" of the class file to form "com.example.MyKotlinClass". The class name can refer to the simple name of the class defined by the class file, and the class file name of the class file is the class name of the class defined in the class file. The class name can refer to the fully qualified name of the class.

[0067] 202: Scan the package to be processed to filter out target classes with "Kt" at the end of the class name.

[0068] Under the background of Android development, the DexFile class can be used to determine the target package to be scanned according to the target package name input by the user, and the target class with the class name ending with "Kt" is filtered from the target package.

[0069] The source file of the Kotlin language can be compiled into a.class file. The extension function is unique in the Kotlin language, and the source file containing the extension function is compiled into a class file named *Kt.class, and the extension function can be defined at the top level of the source file, that is, it can be a top-level function. After the class file is merged into the.dex file, the.dex file will contain the definition information of the class whose class name ends with "Kt" corresponding to the class file. For example, the class file named "StringUtilsKt.class" is merged into the.dex file, and the class name of the class corresponding to the class file is "StringUtilsKt". The class name of the class corresponding to the class file compiled from the source file not containing the extension function will not end with "Kt". Therefore, the target class whose class name ends with "Kt" contains the extension function.

[0070] 203: Load the target class and build the class object corresponding to the target class.

[0071] The Class.forName() method can be used, and the class name of each filtered target class can be filled in the parentheses to dynamically load each filtered target class and build the class object corresponding to each target class. The definition information of the target class can be obtained through the class object.

[0072] 204: From the multiple methods declared in the class object, filter the Kotlin methods that can be mapped to KFunction instances by the Kotlin reflection API, and filter the multiple original extension functions from the Kotlin methods whose extensionReceiverParameter attribute is true.

[0073] All methods declared in the class object can be obtained through the declaredMethods method.

[0074] If the extensionReceiverParameter attribute of multiple methods declared in the class object is true, it can be determined that it is an original extension function.

[0075] Among them, the Kotlin methods that can be mapped to KFunction instances by Kotlin reflection API can be screened out by judging whether the kotlinFunction attribute is true first. The kotlinFunction attribute is true, that is, the Kotlin method. Thus, the functions of other programming languages can be excluded, and then the multiple original extension functions with the extensionReceiverParameter attribute being true are screened out from the Kotlin methods.

[0076] 205: Identify multiple original extension functions for chain invocation and obtain function information corresponding to the multiple original extension functions respectively.

[0077] Among them, the multiple original extension functions for chain invocation can be identified at the same time as the multiple original extension functions are screened in step 204.

[0078] The multiple original extension functions for chain invocation can be identified according to the target return type. The original extension functions with the same return type and receiver type can be used for chain invocation. The target return type can be Modifier, etc. For example, the original extension function with the target return type Modifier has the receiver type Modifier. Of course, in addition to Modifier, there can be other original extension functions with the target return type MutableList<String> and the receiver type MutableList<String>.

[0079] In this embodiment, Kotlin extension functions are accurately identified through multi-step screening by screening target classes including extension functions with class names ending with "Kt", screening Kotlin methods from multiple methods of the target classes, and then screening multiple original extension functions with the extensionReceiverParameter attribute being true from the Kotlin methods. The extensionReceiverParameter attribute can effectively distinguish between ordinary utility methods and extension functions, thereby obtaining function information of the original extension functions, and achieving the acquisition of function information such as return type information of the original extension functions. The problem that the traditional Java reflection mechanism cannot distinguish between extension functions and ordinary utility methods, and thus cannot obtain function information such as return type information of the extension functions is overcome. In this embodiment, the target classes including extension functions are first screened, and then the original extension functions are screened, which can improve the screening efficiency compared with directly screening the original extension functions from all classes included in the to-be-processed package. Compared with distinguishing extension functions from unstructured source files and extracting function information by manual work, the present application realizes function information acquisition through programmatic analysis of bytecode, and the whole process does not require manual intervention, which is efficient and reliable.

[0080] The acquired function information such as return type information of the original extension functions can be used for code development plugins to display matching function information of the original extension functions according to the expected cursor position during code editing, so as to improve the code development efficiency. The function information of the original extension functions can be used for code generation tools to automatically generate codes for calling the original extension functions. The function information of the original extension functions can also be used for automatically generating API (Application Programming Interface, application programming interface) documents and the like.

[0081] In order to further narrow the screening range and improve the screening efficiency of the original extension functions, in some embodiments, the screening of the original extension functions with the extensionReceiverParameter attribute being true from the Kotlin methods can include: filtering methods with a method name containing a $default suffix from the Kotlin methods, and screening the original extension functions with the extensionReceiverParameter attribute being true.

[0082] As can be determined by judging whether the method name contains the $default suffix, the method with the method name containing the $default suffix is a method for implementing the default parameter function. The method for implementing the default parameter function is filtered to narrow the screening range of the original extension functions.

[0083] The identifying the plurality of original extension functions for the chained invocation according to the target return type can be identifying the plurality of original extension functions for the chained invocation according to the target return type. In some embodiments, after obtaining the function information corresponding to the plurality of original extension functions respectively, the method can further include: storing the function information corresponding to the original extension functions according to the target return type.

[0084] The embodiment classifies the original extension functions according to the target return type, and stores the function information corresponding to the original extension functions according to different target return types. This facilitates analysis and query according to a specific target return type. For example, an index library can be established according to the target return type and the corresponding target. A code development plug-in can dynamically recommend a matched original extension function according to an expected return type of a cursor position when code is edited. A code generation tool can query the index library according to a corresponding return type according to a requirement, and automatically generate code for calling the original extension functions according to the function information of the original extension functions queried, to realize automatic and type-safe logical splicing. The original extension functions can be quickly found and displayed according to the target return type in an automatic document, so that the original extension functions are clearer and more readable.

[0085] In order to make the function information of the stored original extension functions clearer and more organized, in some embodiments, the function information can include a function name and parameter information. The storing the function information corresponding to the original extension functions according to the target return type can include: storing the name of the target return type and the function name and parameter information of the original extension function corresponding to the target return type according to a predetermined format.

[0086] For example, the target return type is a “Modifier” type, and the name of the target return type is “Modifier”.

[0087] The storing the name of the target return type and the function name and parameter information of the original extension function according to the predetermined format can be storing in the format of the return type name of the target return type.function name(parameter information).

[0088] Figure 3 An embodiment of a code generation device provided by the embodiment of the present application has a structure diagram as shown in the figure. The device can include: The identification module 301 is configured to identify function signature information corresponding to a plurality of original extension functions for a chained invocation from a to-be-processed package generated by compiling a source file based on a Kotlin language. The conversion module 302 is configured to convert a parameter type of each parameter in the function signature information from a non-null type to a nullable type for each original extension function, to generate converted function signature information. The first generation module 303 is configured to generate function signature code based on the converted function signature information. The second generation module 304 is configured to generate a function body code based on the converted function signature information, the function body code being configured to: call the original extension function in a case where the parameter value is not empty, and return the current object in a case where the parameter value of any parameter is empty; The writing module 305 is configured to write the function signature code and the function body code into a source code file of the target extension function. The storage module 306 is configured to store a plurality of source code files corresponding to a plurality of target extension functions corresponding to a plurality of original extension functions, respectively, so as to form a call chain by combining the plurality of target extension functions, and execute code blocks corresponding to the plurality of target extension functions respectively when the plurality of target extension functions are called in a chain.

[0089] In some embodiments, the function information includes an original package name of the original extension function, and the function signature information includes a function name.

[0090] The apparatus can further include: The third generation module is configured to generate a first import statement for importing the original extension function in the original package according to the original package name and the function name, generate a package declaration statement of the target extension function according to a target package name of the target extension function preconfigured, and write the first import statement and the package declaration statement into a source code file of the target extension function.

[0091] In some embodiments, the function signature information can include a parameter type package name and a parameter type name of each parameter.

[0092] The conversion module can convert the parameter type of each parameter in the function signature information from a non-null type to a nullable type by: converting the parameter type of each parameter from the non-null type to the nullable type according to a parameter type package name and a parameter type name corresponding to the parameter type of each parameter in the function signature information.

[0093] The third generation module can further be configured to: generate a second import statement for importing the parameter type of each parameter according to a parameter type package name and a parameter type name corresponding to the parameter type of each parameter, and write the second import statement into the source code file of the target extension function.

[0094] In some embodiments, the third generation module can further be configured to: generate a third import statement for importing the return type according to a return type package name and a return type name, and write the third import statement into the source code file.

[0095] Figure 3 The code generation apparatus can perform Figure 1The code generation method of the illustrated embodiment, the implementation principle and technical effect will not be repeated. For the specific manner in which each module, unit in the code generation device in the above embodiment performs an operation, a detailed description has been made in the embodiment related to the method, which will not be described in detail here.

[0096] Figure 4 An embodiment of a function information acquisition device provided in the present application provides a structural schematic diagram of the device, which can include: The first acquisition module 401 is configured to acquire a to-be-processed package, which is generated based on source file compilation of the Kotlin language. The scanning module 402 is configured to scan the to-be-processed package to screen a target class with a class name ending with "Kt". The loading module 403 is configured to load the target class and construct a class object corresponding to the target class. The screening module 404 is configured to screen, from a plurality of methods declared in the class object, a Kotlin method that can be mapped into a KFunction instance by a Kotlin reflection API, and screen a plurality of original extension functions with the extensionReceiverParameter attribute being true from the Kotlin method.

[0097] The second acquisition module 405 is configured to identify a plurality of original extension functions for chain calling and acquire function information corresponding to the plurality of original extension functions respectively.

[0098] The first acquisition module acquires the to-be-processed package, which can include: receiving a target package name input by a user, and determining the to-be-processed package from an Android executable file based on the target package name by using a DexFile class.

[0099] In some embodiments, the scanning module scans the to-be-processed package to screen the target class with the class name ending with "Kt", which can include: scanning the to-be-processed package by using a DexFile class to screen the target class with the class name ending with "Kt".

[0100] In some embodiments, the screening module screens, from a plurality of methods declared in the class object, a Kotlin method that can be mapped into a KFunction instance by a Kotlin reflection API, which can include: screening, from a plurality of methods declared in the class object, a Kotlin method that can be mapped into a KFunction instance by a Kotlin reflection API by judging whether the kotlinFunction attribute is true.

[0101] In some embodiments, the screening module screening the multiple original extension functions whose extensionReceiverParameter attribute is true from the Kotlin method can include: filtering the methods whose method name contains a $default suffix from the Kotlin method to screen the multiple original extension functions whose extensionReceiverParameter attribute is true.

[0102] In some embodiments, the loading module loading the target class and constructing the class object corresponding to the target class can include: loading the target class and constructing the class object corresponding to the target class; and obtaining the multiple methods declared in the class object through the declaredMethods method.

[0103] Figure 4 The function information acquisition device can perform Figure 2 The function information acquisition method of the illustrated embodiments will not be described again in detail. The specific manner in which each of the modules, units of the function information acquisition device in the above-described embodiments performs operations has been described in detail in the embodiments related to the method, and will not be described in detail here.

[0104] Figure 5 A structural schematic diagram of one embodiment of a computing device provided in the present application is shown. As Figure 5 In actuality, the computing device can include a storage component 501 and a processing component 502, as shown.

[0105] The storage component 501 is configured to store computer programs and can be configured to store other various data to support operations on the computing device. Examples of these data include instructions of any application program or method for operating on the computing device, data structures, contact data, phonebook data, messages, pictures, videos, etc.

[0106] The processing component 502 is coupled to the storage component 501 and is configured to execute the computer programs in the storage component 501, for implementing the code generation method as Figure 1 described in the illustrated embodiments or the function information acquisition method as Figure 2 described in the illustrated embodiments.

[0107] Further, as Figure 5 shown, the computing device can further include a communication component 503, a display component 504, a power component 505, an audio component 506, and other components. Figure 5 Only some components are shown schematically in the figure, and this does not mean that the computing device only includes Figure 5 the components shown. In addition, Figure 5The components in the dashed box are optional components, not mandatory components, and are determined by the product form of the computing device. The computing device of the present embodiment can be implemented as a terminal device such as a desktop computer, a notebook computer, a smart phone, or an IOT (Internet of Things) device, or as a server device such as a general server, a cloud server, or a server array. If the computing device of the present embodiment is implemented as a terminal device such as a desktop computer, a notebook computer, or a smart phone, it can include Figure 5 components in the dashed box; if the computing device of the present embodiment is implemented as a server device such as a general server, a cloud server, or a server array, it can not include Figure 5 components in the dashed box.

[0108] The processing component described above includes one or more processors to execute computer instructions to complete all or part of the steps in the methods described above. Of course, the processing component can also be one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors or other electronic components, for executing the methods described above.

[0109] The storage component described above can be implemented by any type of volatile or non-volatile storage devices or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk.

[0110] The communication component described above is configured to facilitate wired or wireless communication between the device where the communication component is located and other devices. The device where the communication component is located can access a wireless network based on a communication standard, such as a mobile communication network, or a combination thereof. In an exemplary embodiment, the communication component receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel.

[0111] The display component can include a screen, which can include a Liquid Crystal Display (LCD) and a Touch Panel (TP). If the screen includes a touch panel, the screen can be implemented as a touch screen to receive an input signal from a user. The touch panel includes one or more touch sensors to sense a touch, a slide and a gesture on the touch panel. The touch sensor can not only sense a boundary of a touching or a sliding movement, but also detect a duration and a pressure associated with the touching or the sliding movement.

[0112] The power component provides power to various components of the device in which the power component is located. The power component can include a power management system, one or more power sources, and other components associated with generating, managing and distributing power to the device in which the power component is located.

[0113] The audio component can be configured to output and / or input audio signals. For example, the audio component includes a microphone (MIC) that is configured to receive an external audio signal when the device in which the audio component is located is in an operational mode, such as a call mode, a recording mode and a voice recognition mode. The received audio signal can be further stored in a memory or transmitted via the communication component. In some embodiments, the audio component also includes a speaker for outputting audio signals.

[0114] Accordingly, the embodiments of the present application also provide a computer readable storage medium storing a computer program, when the computer program is executed by a processor, the processor is enabled to implement each step in the above-mentioned method embodiments. The computer readable storage medium includes volatile or non-volatile or a combination thereof, and can be removable or non-removable. Examples of the computer readable storage medium include, but are not limited to, phase-change random access memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital video disc (DVD) or other optical storage, magnetic cassette, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store computer program instructions Accordingly, the embodiments of the present application also provide a computer program product, which comprises computer programs or instructions, and when the computer programs or instructions are executed by a processor, the processor can realize each step in the above-mentioned method embodiments. It should be understood that each process or combination of multiple processes in the above-mentioned method flow can be realized by the computer programs or instructions. In addition, these computer programs or instructions can be applied to the processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing devices, so that the processor of the general-purpose computer, the special-purpose computer, the embedded processor or other programmable data processing devices can be realized as a device for realizing the corresponding functions in the above-mentioned method embodiments.

[0115] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-mentioned system, device and unit can refer to the corresponding processes in the above-mentioned method embodiments, which will not be described here.

[0116] It should also be noted that the terms "comprising", "including", or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed, or further includes elements inherent to such a process, method or device. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, method or device including the element.

[0117] Finally, it should be noted that the above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. within the spirit and principles of the present application shall be included in the scope of the claims of the present application.

Claims

1. A code generation method, characterized in that, include: From the package to be processed generated by compiling source files based on the Kotlin language, identify the function information corresponding to the multiple original extended functions used for chained calls; The function information includes function signature information; For each original extended function, the parameter type of each parameter in the function signature information is converted from a non-nullable type to a nullable type to generate the converted function signature information; Based on the converted function signature information, generate function signature code; The function body code is generated based on the converted function signature information. The function body code is configured to: call the original extended function if the parameter value is not empty, and return the current object if the parameter value of any parameter is empty. Write the function signature code and the function body code into the source code file of the target extended function; The source code files corresponding to the multiple target extension functions corresponding to the multiple original extension functions are stored, so that the multiple target extension functions are combined to form a call chain, and when the multiple target extension functions are called in a chain, the executable code generated by compiling the source code files corresponding to the multiple target extension functions is executed.

2. The method according to claim 1, characterized in that, The function information includes the original package name of the original extended function; The function signature information includes the function name; The method further includes: Based on the original package name and function name, generate the first import statement that imports the original extended function in the original package; Generate the package declaration statement for the target extension function based on the pre-configured target package name; The first import statement and the package declaration statement are used to write the source code file of the target extension function.

3. The method according to claim 1, characterized in that, The function signature information includes the parameter type package name and parameter type name for each parameter; The step of converting the parameter type of each parameter in the function signature information from a non-nullable type to a nullable type includes: Based on the parameter type package name and parameter type name corresponding to the parameter type of each parameter in the function signature information, the parameter type of each parameter is converted from a non-nullable type to a nullable type; The method further includes: Based on the parameter type package name and parameter type name corresponding to the parameter type of each parameter, generate a second import statement that imports the parameter type of each parameter; Write the second import statement into the source code file of the target extension function.

4. The method according to claim 1, characterized in that, The function information identified from the package to be processed, compiled from source files based on the Kotlin language, for the multiple original extended functions used in chained calls includes: Obtain the package to be processed; the package to be processed is compiled from source files in the Kotlin language; Scan the package to be processed to filter target classes whose class names end with "Kt"; Load the target class and construct the class object corresponding to the target class; From the multiple methods declared in the class object, select the Kotlin methods that can be mapped to KFunction instances by the Kotlin reflection API, and from the Kotlin methods, select multiple primitive extension functions whose extensionReceiverParameter property is true; From the class object, identify multiple original extension functions for chained calls, and obtain the function information corresponding to each of the multiple original extension functions.

5. The method according to claim 4, characterized in that, The identification of multiple original extension functions used for chained calls includes: Based on the target return type, identify multiple original extension functions for chained calls.

6. The method according to claim 4, characterized in that, Also includes: Obtain the Android executable file; The acquisition of the packet to be processed includes: Receive the target package name input by the user, and use the DexFile class to determine the package to be processed from the Android executable file based on the target package name; The scanning of the package to be processed to filter target classes whose class names end with "Kt" includes: The DexFile class is used to scan the package to be processed to filter target classes whose class names end with "Kt".

7. The method according to claim 4, characterized in that, Among the multiple methods declared from the class object, the Kotlin methods that can be mapped to KFunction instances by the Kotlin reflection API include: From the multiple methods declared in the class object, Kotlin methods that can be mapped to KFunction instances by checking whether the kotlinFunction property is true are selected.

8. The method according to claim 4, characterized in that, The multiple original extension functions that filter the extensionReceiverParameter property to be true from the Kotlin method include: Filter out methods from the Kotlin methods whose names contain the suffix $default, and then filter out multiple original extension functions whose extensionReceiverParameter property is true.

9. The method according to claim 4, characterized in that, The step of loading the target class and constructing the class object corresponding to the target class includes: Load the target class and construct the class object corresponding to the target class; The declaredMethods method retrieves multiple methods declared in the class object.

10. A computing device, characterized in that, This includes processing components and storage components; The storage component stores a computer program; the computer program is invoked and executed by the processing component to implement the code generation method as described in any one of claims 1 to 9.

11. A computer-readable storage medium, characterized in that, It stores a computer program, which, when executed by a processing component, implements the code generation method as described in any one of claims 1 to 9.