Code annotation adding method and device, computer equipment and storage medium

By defining annotation macros in Objective-C and using the compiler to write annotation information to the executable file, the problem of Objective-C lacking annotation mechanism is solved, and convenient code annotation addition is achieved, which improves development efficiency and code maintainability.

CN120010907APending Publication Date: 2025-05-16CHENGDU SHUANGLIU GAOJIAN INTERNET HOSPITAL CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411986592.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-31
Publication Date
2025-05-16

AI Technical Summary

Technical Problem

In the Objective-C programming language, the lack of a native annotation mechanism makes it difficult to add metadata information to components in scenarios such as component development, which increases maintenance costs and reduces development efficiency.

Method used

By defining annotation macros, the compiler is called to precompile the source code, generate a string containing annotation information, and write the string to the specified storage location of the executable file during the compilation process to realize the addition of code annotations.

Benefits of technology

It realizes the convenient addition of code annotations in Objective-C, which makes up for the lack of annotation mechanism in the language, reduces the need to maintain configuration files, and improves development efficiency and maintainability of code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120010907A_ABST
    Figure CN120010907A_ABST
Patent Text Reader

Abstract

The invention relates to a code annotation adding method and device, computer equipment and a storage medium. The method comprises the steps that a predefined annotation macro is called in a source code needing to be added with annotation information, the annotation macro comprises the annotation information and a storage position corresponding to the annotation information, a compiler is called to pre-compile the source code, a character string containing the annotation information is generated, the source code is compiled into an executable file through the compiler, and the executable file is stored in the executable file. And writing the character string into a storage position of the executable file in the compiling process so as to add annotation information to the source code. By adopting the method, a developer can directly call in the source code to add the annotation information in a manner of defining the annotation macro in some language environments which are originally lack of an annotation mechanism, so that the defects of the annotation macro in the aspect are overcome, and the operation of adding the metadata becomes simple and feasible.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a method, device, computer equipment and storage medium for adding code annotations. Background Art

[0002] Annotations are a type of metadata that provides a safe way to associate information or metadata with program elements (such as classes, methods, fields, etc.) without directly affecting the logic of the program. Annotations are often used to configure tools, frameworks, and compilers so that they can process code based on this metadata. Annotations are supported in many modern programming languages, such as Java, C#, and Python (similar functionality is achieved through decorators). Therefore, you can associate information or metadata with program elements (such as classes, methods, fields, etc.) by adding annotations to the code. Adding annotations helps to better manage and use the code, such as more clearly defining component information in different development scenarios, and facilitating more efficient and precise operations on the code.

[0003] In traditional technology, languages ​​such as Java and C# have built-in annotation mechanisms, which can directly add metadata to program code through corresponding syntax, and bind various additional information that you want to associate with program elements, making it easier to perform corresponding logical operations based on these annotation information during subsequent development, framework application, etc.

[0004] In other programming languages, Objective-C does not have a native annotation mechanism like Java and C#, so it is not possible to add metadata information to program elements directly by writing code. Especially in some specific development scenarios, such as component-based development, it is not convenient to write relevant component information for components in the code, and it is often necessary to rely on separately maintained additional configuration files to record this information, which not only increases maintenance costs, but also affects development efficiency, and the operation is not convenient and efficient enough. Summary of the invention

[0005] Based on this, it is necessary to provide a method, apparatus, computer device and storage medium for adding code annotations to address the above technical issues.

[0006] In a first aspect, the present application provides a method for adding code annotations, comprising:

[0007] Calling a predefined annotation macro in the source code to which annotation information needs to be added, the annotation macro includes the annotation information and the storage location corresponding to the annotation information;

[0008] Call the compiler to precompile the source code and generate a string containing annotation information;

[0009] The source code is compiled into an executable file through a compiler, and a string is written into a storage location of the executable file during the compilation process to add annotation information to the source code.

[0010] In one embodiment, the method for adding code annotations further includes:

[0011] Define basic macros, which include compiler properties. Compiler properties are configured with storage locations corresponding to annotation information.

[0012] Get annotation information and define corresponding parameters for annotation macro according to the annotation information;

[0013] The base macro is called in the annotation macro so that the storage location corresponding to the annotation information is specified through the compiler attributes in the base macro to define the annotation macro.

[0014] In one embodiment, calling a predefined annotation macro in a source code to which annotation information needs to be added includes:

[0015] When the target class needs to be annotated, call the annotation macro in the header of the target class.

[0016] In one embodiment, the storage location includes a segment identifier and a section identifier of the executable file, and writing the string to the storage location of the executable file during the compilation process includes:

[0017] During the compilation process, the string is written to the corresponding section in the corresponding segment of the executable file according to the segment identifier and the section identifier.

[0018] In one embodiment, the method for adding code annotations further includes:

[0019] Register callback function;

[0020] When it is detected that the executable file is loaded, the callback function is executed to parse the annotation information from the executable file through the callback function.

[0021] In one embodiment, executing a callback function to parse annotation information from an executable file through the callback function includes:

[0022] When the executable is detected to be loaded, an array of mutable type is created;

[0023] Obtain the memory pointer and data size corresponding to the storage location according to the operating system running the source code;

[0024] Determine the number of elements in the data based on the data size;

[0025] Traverse each element in the storage location in turn according to the number of elements;

[0026] Convert each element and add it to the array;

[0027] When the traversal is complete, an array is returned to parse out the annotation information.

[0028] In one embodiment, when the component class to which annotation information needs to be added includes the component name and the priority of the component, after executing the callback function to parse the annotation information from the executable file through the callback function, the method further includes:

[0029] Find the corresponding component code module according to the component name;

[0030] Register components according to component code module and component name;

[0031] Components are loaded according to priority so that they can be initialized after loading.

[0032] In a first aspect, the present application provides a device for adding code annotations, comprising:

[0033] A calling module, used for calling a predefined annotation macro in a source code to which annotation information needs to be added, wherein the annotation macro includes the annotation information and a storage location corresponding to the annotation information;

[0034] The compilation module is used to call the compiler to precompile the source code and generate a string containing annotation information;

[0035] Add a module for compiling source code into an executable file through a compiler, and write a string to the storage location of the executable file during the compilation process to add annotation information to the source code.

[0036] In a third aspect, the present application provides a computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the method for adding code annotations provided in any embodiment of the present application in the first aspect are implemented.

[0037] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method for adding code annotations provided in any embodiment of the first aspect of the present application.

[0038] The above-mentioned method, device, computer device and storage medium for adding code annotations can realize convenient addition of annotations. Specifically, in a language environment such as Objective-C that originally lacks an annotation mechanism similar to Java and C#, by defining annotation macros, developers can call them directly in the source code to easily add annotation information, which makes up for its shortcomings in this regard and makes the operation of adding metadata simple and easy.

[0039] In addition, the present application can also integrate the addition of annotation information into the compilation process of the compiler. First, a string containing annotation information is generated in the pre-compilation stage, and then when it is subsequently compiled into an executable file, the annotation information can be accurately written into the specified storage location. The entire process is closely integrated, and there is no need to maintain additional configuration files separately as in traditional practices, avoiding maintenance difficulties caused by problems such as the configuration file and the code being out of sync, so that the code and related annotation information can be processed uniformly during the compilation process. If there are subsequent adjustments, the code can be operated through annotation macros, which is easier to maintain and manage, and reduces the risk of errors caused by configuration file-related issues. In addition, developers do not need to spend extra energy to manage complex configuration files to record annotation-related information. The annotation addition can be completed directly using the compiler compilation process. The code and annotation information are more closely related, and the operation is smoother, thereby effectively saving time and improving overall development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 A flowchart of a method for adding code annotations in some embodiments;

[0041] Figure 2 A structural block diagram of an apparatus for adding code annotations in some embodiments;

[0042] Figure 3 1 is a diagram of the internal structure of a computer device in some embodiments. DETAILED DESCRIPTION

[0043] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0044] In a first aspect, the present application provides a method for adding code annotations, such as Figure 1 As shown, this method is applied to Figure 1 The server in the example is used to illustrate the following steps:

[0045] Step S11, calling a predefined annotation macro in the source code to which annotation information needs to be added, wherein the annotation macro includes the annotation information and a storage location corresponding to the annotation information.

[0046] Among them, the present application can be applied to the Objective-C language. Objective-C is an object-oriented programming language, which is an extension of the C language. Objective-C was originally developed by Brad Cox and Tom Love in the early 1980s, and was adopted by NeXT in 1988. It later became the main programming language for Apple to develop macOS and iOS (Input Output System) applications.

[0047] An annotation macro is a predefined code structure (implemented based on macro programming in Objective-C). It contains two key contents: one is the annotation information, which is the metadata content that developers want to associate with program elements, such as component-related descriptions, etc. The other is the storage location corresponding to the annotation information, which is used to specify where the annotation information will eventually be stored. By calling this annotation macro, you can easily implement functions similar to the annotation mechanism of other languages ​​in the code and add metadata to program elements.

[0048] Annotation information is the specific content of metadata. It can be various descriptions that supplement program elements and facilitate subsequent processing. For example, in component-based development, it may be the function introduction, version number, dependency relationship and other information of the component. By adding these annotation information, configuration tools, frameworks or compilers can know more characteristics of program elements and then perform corresponding processing according to this information.

[0049] The storage location corresponding to the annotation information refers to a place determined in the executable file or related code structure to store the annotation information. When compiling the code, the compiler will accurately write the corresponding annotation information into the storage location specified in the annotation macro, so that these metadata information can be closely associated with the program elements and can be correctly obtained and used later, such as a specific memory segment in the executable file.

[0050] Specifically, developers write annotation macros in advance according to actual needs, in which they clearly define the specific content of the annotation information to be added and determine the corresponding storage location. Find the source code location of the program element where the annotation information needs to be added, such as class definition, method definition, etc., and call the previously defined annotation macro through the appropriate syntax, which is equivalent to telling the compiler that the corresponding annotation information and its storage arrangement need to be associated here. When the compiler performs subsequent pre-compilation, formal compilation and other operations, it will first generate a string containing the annotation information according to the content set in the annotation macro, and then accurately write it to the specified storage location, thereby completing the entire process of adding annotation information to the code.

[0051] Step S12, calling a compiler to precompile the source code and generate a character string containing annotation information.

[0052] This application can be applied to the Objective-C programming language. Specifically, this application can configure the compiler environment. Ensure that the compiler has been correctly installed and the relevant parameters have been configured so that it can recognize and process Objective-C code and corresponding macro definitions, etc., to ensure that the pre-compilation process can be started normally.

[0053] Furthermore, in the development environment, the compiler is called through appropriate compilation instructions or operations to start pre-compiling the source code containing the annotation macro call.

[0054] The compiler will perform operations according to the pre-defined annotation macro rules during the pre-compilation phase. It will parse the annotation information and related logic set in the annotation macro, and convert it into a specific string based on the macro syntax and definition. This string contains the annotation information to be added, such as component-related descriptions and other specific metadata content.

[0055] After the above-mentioned macro expansion and other processing, a complete string containing annotation information is finally generated, so that it can be further processed in the compilation process, such as being accurately written to the corresponding storage location, completing the entire process of adding annotations to the code.

[0056] For example, suppose there is a custom UIViewControlle subclass called MyViewController. Now you need to add annotation information to this class to indicate the functional module it belongs to and version related information. The specific steps may include:

[0057] Step 1. Define the annotation macro

[0058] First, define such an annotation macro (simplified illustration, the actual syntax is more rigorous):

[0059] objc

[0060] #define ANNOTATION_MODULE_VERSION(moduleName,version)\

[0061] "@module:"#moduleName";@version:"#version";"

[0062] This macro accepts two parameters, one is the module name moduleName, and the other is the version number version. It defines the format of the final expanded string and will concatenate the passed parameters according to the specified format to form a string containing annotation information.

[0063] Step 2. Call the annotation macro in the source code

[0064] Call this annotation macro near the definition of the MyViewController class, as follows:

[0065] objc

[0066] @interface MyViewController:UIViewController

[0067] ANNOTATION_MODULE_VERSION(ProfileModule,1.0)

[0068] @end

[0069] Here, the annotation macro is called at the class definition, passing ProfileModule as the module name and 1.0 as the version number, telling the compiler to associate such annotation information with this class.

[0070] Step 3. Expand the precompilation phase

[0071] When the compiler performs precompilation, there are macro calls like this:

[0072] ANNOTATION_MODULE_VERSION(ProfileModule,1.0)

[0073] The compiler will expand according to the macro definition. It will replace ProfileModule and 1.0 with the corresponding positions and concatenate the strings. The actual content after expansion is as follows:

[0074] "@module:ProfileModule;@version:1.0;"

[0075] In this way, the original annotation macro is expanded into a string containing specific annotation information (module name and version number). Later in the compilation process, based on this string, these annotation information can be written into the corresponding storage location of the executable file according to the rules, achieving an effect similar to adding annotations to the class, so that there is additional metadata in the code to describe the relevant properties of the class.

[0076] Step S13, compiling the source code into an executable file through a compiler, and writing the character string into the storage location of the executable file during the compilation process to add annotation information to the source code.

[0077] Among them, the present application can utilize the compilation command in the development environment or the corresponding compilation operation options in the integrated development environment to allow the compiler to start performing a complete compilation operation on the source code containing annotation-related content and process it in the direction of generating an executable file.

[0078] The compiler accurately finds the target location to write the annotation information during the compilation process based on the storage location corresponding to the annotation information defined in the previous annotation macro (for example, a specific memory segment in the executable file, a specific field location in a data structure, etc.).

[0079] Furthermore, the string containing the annotation information generated in the pre-compilation stage is accurately written into the storage location of the executable file located previously according to the established compilation process and rules. For example, if the storage location is a specific field of a data structure, the string content is filled in through the corresponding memory write operation, so that the annotation information officially becomes part of the executable file, and the purpose of adding annotation information to the source code is achieved.

[0080] After the annotation information is successfully written, the compiler continues to perform subsequent compilation operations, such as code optimization, linking other libraries, etc., and finally generates a complete executable file containing the annotation information. In this way, when the file is subsequently run or analyzed by related tools and frameworks, it can perform corresponding processing based on the annotation information.

[0081] In one of the embodiments, the method for adding code annotations may further include defining a basic macro, the basic macro including compiler properties, the compiler properties being configured with a storage location corresponding to the annotation information, obtaining the annotation information and defining corresponding parameters for the annotation macro according to the annotation information, and calling the basic macro in the annotation macro so as to specify the storage location corresponding to the annotation information through the compiler properties in the basic macro to define the annotation macro.

[0082] Among them, the basic macro is a lower-level macro definition, which is mainly used to encapsulate some common, compiler-related properties and settings. In this application, the basic macro contains compiler properties, which are used to determine important information such as the storage location of annotation information in the executable file, providing basic configuration support for annotation macros.

[0083] Compiler properties refer to some settings used or referenced by the compiler during the compilation process. Here, compiler properties include the storage location corresponding to the annotation information, which specifies where the annotation information should be placed in the compiled executable file, such as a specific data segment, a specific symbol table location, or other memory area.

[0084] Specifically, the application can write a basic macro definition according to the requirements of the compiler and system. This macro definition contains compiler attributes and clearly specifies the storage location corresponding to the annotation information. For example, a basic macro similar to the following can be defined (this is a simplified example):

[0085] #define CRAnnotationDATA(sectname)__attribute((used,section("__DATA,"#sectname"")))

[0086] This is a macro definition. It defines a macro named CRAnnotationDATA, which receives a parameter sectname (the parameter type is a string).

[0087] Furthermore, the content of the annotation information to be added is determined, such as the name of the component, the version number, the author, etc. According to the content of the annotation information, corresponding parameters are defined for the annotation macro.

[0088] Define parameters for the annotation macro according to the annotation information to be recorded. These parameters are used to receive specific annotation content, such as module name, class name, etc., and are the basic elements for constructing annotation data.

[0089] Furthermore, the basic macro is called inside the annotation macro, and the annotation information constructed according to the parameters is stored in the specified location using the compiler attributes in the basic macro. In this way, the definition of an annotation macro that can store specific annotation information in the specified location is completed.

[0090] Exemplarily, the basic macro may be in the following format:

[0091] #define CRAnnotationDATA(sectname)__attribute((used,section("__DATA,"#sectname"")))

[0092] The main purpose of this basic macro is to define a mechanism so that the string content represented by the passed macro parameter sectname can be placed in the __DATA data area in the Mach-O file executable by iOSApp (Application) during the compilation process. It uses compiler-related attributes to control the specific location of code or data in the memory layout of the executable file.

[0093] __attribute__ is a mechanism provided by the compiler to add additional attributes or instructions to objects such as variables, functions, types, etc., thereby affecting the behavior of the compiler. Here it is used to convey special requirements such as storage locations to the compiler.

[0094] used is a specific attribute in __attribute__, which tells the compiler that even if the symbol associated with this macro definition (such as the defined variable, etc.) does not seem to be actually used in the compiled code (that is, it is not explicitly referenced elsewhere), the compiler cannot optimize it away. It is necessary to ensure that the relevant content is still compiled and placed according to the specified storage location requirements to ensure that the corresponding data can be obtained normally when needed later.

[0095] Section attribute: This explicitly specifies that the storage "section" attribute of the data is to be set, that is, it specifies the specific storage area division of the data in the executable file.

[0096] __DATA: It represents a standard data area in an executable file (Mach-O format). This area is usually used to store various data when the program is running. Global variables, static variables, etc. are placed here. It is a very important data storage space. "__DATA,"#sectname"" is equivalent to the storage location in this application.

[0097] #sectname: The string operator # is used here, which converts the passed macro parameter sectname into a string. In this way, according to the different sectname values ​​passed in by the developer, the specific sub-area or subdivided "section" in the large data area __DATA can be flexibly determined to store the corresponding content. For example, if my_annotations is passed in as the sectname, a small storage section will be opened in the __DATA area specifically for the my_annotations related content to store the corresponding data (such as annotation information, etc.).

[0098] In general, the basic macro CRAnnotationDATA(sectname) uses __attribute__ and its related property settings to flexibly place specific data in a specific location in the __DATA data area of ​​the executable file according to the sectname specified by the developer, providing an effective control method for subsequent code annotations and other related data storage needs.

[0099] For example, the annotation macro customized based on the basic macro may be in the following form:

[0100] #define ModuleRegister(moduleName,moduleClass,async)\

[0101] class ComponentRegister; char*k##moduleClass##_module CRAnnotationDATA(Module)="{\"name\":\""#moduleName"\",\"class\":\""#moduleClass"\",\"async\":\""#async"\"}";

[0102] This annotation macro is mainly used to create a convenient way to define and store module-related information, so that the module information can be placed in a specific location of the iOS executable file according to established rules, so that subsequent programs can obtain and use this information to process module-related logic, such as understanding the basic properties and execution characteristics of the module.

[0103] The CRAnnotationDATA(Module) part calls the previously defined basic macro CRAnnotationDATA(sectname) and passes in Module as a parameter. According to the function of the basic macro, this means that the relevant content defined by this annotation macro (that is, the module information string below) will be compiled into the __DATA data area of ​​the executable file and the specific sub-area related to Module (equivalent to the storage location of this application), ensuring that its storage location meets expectations and facilitates subsequent positioning and use.

[0104] ="{\"name\":\""#moduleName"\",\"class\":\""#moduleClass"\",\"async\":\""#async"\"}";: This part initializes and assigns a value to the character pointer variable defined earlier, and constructs a string in JSON format.

[0105] In this string, the macro parameters moduleName, moduleClass, and async passed in are embedded into the corresponding key-value pairs through the stringization operator #. For example, if MyModule is passed in as moduleName, MyModuleClass as moduleClass, and true as the async parameter, the constructed string is {"name":"MyModule","class":"MyModuleClass","async":"true"}, which records the three important module information: the module name, the implementation class name, and whether it is executed synchronously, so as to facilitate the subsequent identification, configuration, and operation of the module.

[0106] In general, this annotation macro uses the storage location specification function of the basic macro to organize the key information related to the module in the form of a string in a specific format and store it in the appropriate location of the executable file, providing a convenient code-level implementation method for module-based development and module-related information management.

[0107] The beneficial effects of this embodiment are:

[0108] By defining basic macros to encapsulate compiler properties such as storage locations, if the storage location or other compiler-related settings need to be changed, only the definition of the basic macro needs to be modified, without having to modify it in each annotation macro. This greatly improves the maintainability of the code.

[0109] The base macro can be reused by multiple different annotation macros, as long as these annotation macros require the same compiler properties (such as the same storage location). This can reduce the duplication of code and improve code reusability.

[0110] Better organization and management of annotation information: Annotation macros and their related basic settings can be defined more systematically, making the processing of annotation information more orderly during the compilation process, facilitating developers to have more precise control over the addition and management of annotations, ensuring that annotation information is accurately stored in the desired location, and facilitating subsequent tools, frameworks, etc. to utilize this information.

[0111] In one of the embodiments, calling a predefined annotation macro in a source code to which annotation information needs to be added may include: when the target class to which annotation needs to be added is a target class, calling the annotation macro in the header of the target class.

[0112] First, clearly identify the target class to which annotation information should be added in the source code of the project. For example, in an iOS application development project, identify a custom view controller class or business logic processing class as the target class to which annotations should be added, and clearly know its class name and the location of the code file.

[0113] Open the source code file containing the target class and locate the definition header of the target class. In the target class header, call it according to the syntax requirements of the annotation macro. Assuming that an annotation macro named ModuleAnnotation has been defined before (used to add module-related annotation information), the calling form may be as follows:

[0114] objc

[0115] @interface MyViewController:UIViewController

[0116] ModuleAnnotation("ModuleName","ModuleVersion",YES)

[0117] @end

[0118] Here, the ModuleAnnotation annotation macro is called in the interface declaration header of the MyViewController class, and the corresponding parameters (module name, module version number, and a Boolean value indicating whether a certain feature is enabled, etc., and the parameter content depends on the specific definition of the annotation macro). Through such a calling operation, the source code containing the annotation macro is obtained. The subsequent compiler can generate and process the corresponding annotation information based on this annotation macro and its parameters in the compilation process, such as storing it in the specified location of the executable file according to the settings.

[0119] If you add annotation macros to the class implementation part, the operation is similar. Just put it in the corresponding position after the @implementation keyword and call it.

[0120] In one embodiment, the storage location includes a segment identifier and a section identifier of the executable file, and writing the string to the storage location of the executable file during the compilation process includes: writing the string to a corresponding section in a corresponding segment of the executable file according to the segment identifier and the section identifier during the compilation process.

[0121] This application can divide the executable file into multiple logical parts or segments. These segments contain different information, and each segment has a specific purpose and attributes. This segmentation mechanism helps the operating system and loader to manage and use the data in the executable file more efficiently. iOS App executable files belong to the Mach-O format.

[0122] A segment is a continuous area in an executable file, usually containing data with the same characteristics. A segment can contain different types of information such as code, data, and symbol tables.

[0123] Segments can be further subdivided into sections. Sections are smaller units within segments, and each section also has a specific purpose. For example, in the ELF (Executable and Linkable Format) file format, the .text section usually contains the program code, while the .data section contains initialized data.

[0124] Specifically, you need to know the segment identifier of the executable file where the string needs to be written (for example, the common __DATA is a segment identifier, representing the data segment) and the section identifier (for example, there may be different sections under the __DATA segment, such as __const, which is a specific section identifier). These identifier information is usually pre-set when defining basic macros or annotation macros. Developers need to obtain the accurate content of these two identifiers from the code or related configurations to clearly identify the segment and section where the target location to write the string is located.

[0125] During the compilation process, the compiler starts to process the source code. The compiler itself has the ability to parse the executable file structure and understand the meaning of segment identifiers and section identifiers. It will find the corresponding segment in the memory layout of the compiled executable file based on the obtained segment identifier. For example, when the segment identifier is __DATA, the compiler will locate the memory area where the data segment is located.

[0126] After determining the specific segment, the compiler then uses the section identifier to further accurately find the corresponding section in the located segment. For example, in the found __DATA segment, according to the section identifier __const, this specific sub-area (equivalent to the section) can be determined, which is the exact target location where the string is to be written.

[0127] The compiler uses appropriate memory writing mechanisms and compilation-related operation methods to accurately write the previously generated string containing annotation information into the corresponding section that has been located. This may involve steps such as operating the memory address and filling the data to ensure that the string can be completely placed in the memory space corresponding to the section as required, so that the annotation information is correctly stored in the executable file, which is convenient for subsequent program running or other tools to obtain and use these annotation information when analyzing the executable file.

[0128] The beneficial effects of this embodiment are:

[0129] By writing strings with explicit segment and section identifiers, very accurate data storage can be achieved. Just like in a complex file warehouse (executable file), segment identifiers are large partitions (such as different warehouse areas), and section identifiers are more detailed small grids within the partitions. In this way, when storing annotation information, it can be accurately placed in the appropriate "small grid", making it easier to find this information accurately during runtime or when analyzing files.

[0130] In addition, this method helps to build an orderly executable file structure. Different types of information can be stored in different segments and sections according to their nature and purpose. For example, the code part is stored in the code segment, constants are stored in the constant section, and annotation information is stored in specific segments and sections according to settings, making the organizational structure of the executable file more reasonable and improving the overall performance and maintainability of the file.

[0131] Since annotation information has a clear storage location, when the program needs to be expanded or modified, such as adding a new annotation type or updating the storage method of annotations, it only needs to adjust the relevant settings of the segment and section identifiers. This is like re-planning the use of small grids in a warehouse without affecting the basic structure of the entire warehouse, making the program more flexible in the face of changes, and easier to expand and maintain.

[0132] For some tools that analyze executable files (such as debugging tools, performance analysis tools, etc.), clear segment and section storage locations make it easier for them to find annotation information. These tools can quickly locate and read annotations based on the known segment and section structure, thereby better understanding the characteristics of the program and assisting developers in debugging, optimization, and other tasks.

[0133] In one of the embodiments, the method for adding code annotations may further include: registering a callback function, and executing the callback function when it is detected that the executable file is loaded, so as to parse the annotation information from the executable file through the callback function.

[0134] Specifically, the present application can register the callback function by using the relevant mechanism provided by the programming language at the appropriate code location (such as the initialization code part related to program startup, or in a specific module loading code, depending on the usage scenario and program architecture).

[0135] Furthermore, the executable file loading is detected. This step usually depends on the relevant mechanisms of the operating system or runtime environment. For example, in iOS or macOS systems, when the application is started, the system will be responsible for loading the executable file. During this process, there will be a corresponding internal mechanism to check whether there is a callback function registered for the executable file loading event. If it is detected that there is, the corresponding callback function will be prepared to trigger. Different platforms and development environments have different underlying implementation methods to implement this detection and triggering mechanism.

[0136] When the executable file is detected to be loaded, the callback function will be executed. Inside this function, you need to write code to read the string containing the annotation information from the specified storage location of the executable file (the location corresponding to the segment and section where the annotation information string was previously written during the compilation process). For example, you can use the file reading related system functions or a specific executable file parsing library (if any) to locate the corresponding segment and section, and then read the string data stored in it.

[0137] The beneficial effects of this embodiment are:

[0138] By registering a callback function to read the annotation information string when the executable file is loaded, the program can dynamically obtain the annotation information written during the previous compilation during the running stage. In this way, even if the program is run in different scenarios or the annotation content is updated later, this information can be obtained and used in real time without recompiling the entire program, which enhances the flexibility of the program to cope with changes.

[0139] In addition, the annotation information string read can be used for a variety of runtime operations. For example, if the annotation information contains the configuration parameters of the module, then when the program starts, after reading and parsing this information through the callback function, the behavior of the module can be dynamically adjusted according to the configuration, such as deciding whether to enable certain functions, setting different initialization parameters, etc., to better meet diverse business needs.

[0140] This method makes it easy to manage annotation information in the subsequent expansion and maintenance of the program. Newly added annotation content can be read and applied at runtime through the same mechanism. Developers can more easily expand and optimize the program based on annotations. And because it is dynamically obtained at runtime, it is relatively less invasive to the existing code and easier to maintain the entire code system.

[0141] Using the key node of executable file loading to trigger the operation of reading annotation information allows the program to work closely with the runtime environment. It conforms to the life cycle characteristics of program operation and allows the program to obtain important annotation data at the right time, so as to better integrate into the entire runtime ecosystem and ensure the normal operation of the program and the effective use of its functions.

[0142] In one embodiment, parsing annotation information from an executable file through a callback function may include: when it is detected that the executable file is loaded, creating an array of a variable type, obtaining a memory pointer and a data size of a storage location according to the operating system running the source code, determining the number of elements in the data according to the data size, traversing each element in the storage location in turn according to the number of elements, converting each element and adding it to the array, and returning the array when the traversal is completed to parse the annotation information.

[0143] Among them, in programming languages, a memory pointer is a variable type that stores a memory address. This address points to another data object in the memory (such as a variable, array element, structure, etc.). It is like a "signpost" that tells the program where to find specific data in the memory.

[0144] Specifically, an empty variable array is first created inside the callback function for subsequent storage of the read string objects, and the variable data size is initialized to 0. It will be assigned the actual size of the corresponding area data when the data is subsequently obtained.

[0145] Furthermore, the architecture of the operating system running the source code is first determined. If it is a non-64-bit architecture, the getsectiondata function is directly called, and the corresponding parameters are passed in to obtain the data pointer and assign it to the memory pointer. In the Objective-C environment, the getsectiondata function is usually used to obtain data in a specific memory segment.

[0146] If it is a 64-bit architecture, the pointer type conversion is performed first, and then the getsectiondata function is called to obtain the data pointer and assign it to the memory pointer, so as to determine where to start reading the data stored in a specific area.

[0147] Further, calculate the number of data elements: determine the number of elements in the data by dividing the data size by sizeof(void*). Here, it is assumed that the data consists of a series of pointer-sized elements. For example, if the data is an array and each element in the array is a pointer, then sizeof(void*) represents the size of each element.

[0148] After calculating the number of data elements, enter the for loop, take out the data pointed to by the pointer corresponding to each element from the memory in turn, convert it to a string object of type NSString, and add it to the array if the conversion is successful, and repeat this cycle until all elements are processed. In Objective-C, NSString is a class used to represent and process strings.

[0149] Finally, the array containing the annotation information is returned to the caller. The caller (such as the callback function and other related subsequent logic parts) can get the string in this array and then parse the specific annotation content, such as parsing out the module name, class name, execution method and other specific information to perform corresponding module management and other operations.

[0150] The beneficial effects of this embodiment are:

[0151] By obtaining the memory pointer and data size of the storage location according to the operating system running the source code, it can adapt to the differences in the memory layout of executable files under different operating systems, ensuring that the corresponding data can be accurately found in various systems. For example, when running the same logic code on different operating systems such as iOS and Android, data can be obtained in a targeted manner, making the program more cross-platform adaptable.

[0152] Create an array of variable type to collect data. Regardless of the source of the data, it can be dynamically integrated into the array to facilitate subsequent unified management and use without limiting the size and form of the data in advance, which enhances the flexibility of the program in processing data at runtime.

[0153] Adding the converted elements to the array brings the scattered data together, making it easier for other modules of the subsequent program to batch analyze and display the data or further execute the corresponding business logic based on the data, thus improving the overall data processing efficiency. In addition, the collected data is stored in the array, which facilitates the subsequent function expansion. For example, if you want to add new data analysis functions or display forms based on these data, you can easily get data from the array to achieve it, making the program easier to expand.

[0154] In one of the embodiments, the method for adding code annotations may also include: when the component class needs to add annotation information, the annotation information includes the component name and the component priority, searching for the corresponding component code module according to the component name, registering the component according to the component code module and the component name, and loading the component according to the priority so that the component can be initialized after loading.

[0155] Specifically, the present application extracts two key contents, namely, component name and component priority, from the added annotation information. This may involve parsing the corresponding annotation string (if the annotation is stored in a specific string format), or obtaining the accurate component name and the numerical value or identifier representing the priority through the corresponding code structure (such as the attributes of the custom annotation class, etc., depending on the specific implementation method).

[0156] Generally, in the code organization structure of a project, the corresponding code file or code module is located by the component name. This can be done by traversing the project's file directory (if there is an agreed naming convention, such as a correspondence between component names and file names), or by using the module search mechanism provided by code management tools and frameworks (for example, some componentized frameworks have built-in module index tables) to accurately find the file, class, and other code module entities that contain the specific implementation code of the component.

[0157] After finding the component code module, call the corresponding registration method or mechanism to complete the component registration. Different development environments and component frameworks have different registration methods. The most common method is to add a record to a global component registry (which may be a data structure, such as a dictionary, etc.), with the component name as the key and the reference related to the component code module (such as a class object or function pointer, etc., depending on the specific implementation) as the value, so that the system can know that the component exists and can be called subsequently.

[0158] Arrange the loading order of components according to the obtained component priorities. Usually there is a loading mechanism (which may be handled uniformly at the program startup stage, or by a specific component management module), which first sorts the priorities of all components to be loaded, and then calls the component loading method in order from high to low (or other agreed order rules), triggering the execution of the component initialization code to make it usable.

[0159] The beneficial effects of this embodiment are:

[0160] The component name in the annotation information is the key content, which can accurately identify the corresponding component in many code modules. The annotation information of component priority provides a key basis for the reasonable allocation of resources during the operation of the system, especially in the startup phase. High-priority components can be loaded and initialized first, obtain more system resources and enter the working state first, just like dividing the components into "orders of priority", making the allocation of the entire system resources more scientific and orderly, avoiding performance problems caused by disordered loading. If such priority annotations are missing, the disordered loading order of components may cause adverse consequences such as resource competition and slow system startup.

[0161] In a second aspect, the present application provides a device for adding code annotations, such as Figure 2 As shown, the device for adding code annotations includes: a calling module 21, a compiling module 22 and an adding module 23, wherein:

[0162] A calling module 21 is used to call a predefined annotation macro in a source code to which annotation information needs to be added, wherein the annotation macro includes annotation information and a storage location corresponding to the annotation information;

[0163] A compiling module 22, used for calling a compiler to precompile the source code and generate a string containing annotation information;

[0164] The adding module 23 is used to compile the source code into an executable file through a compiler, and write the character string into the storage location of the executable file during the compilation process to add annotation information to the source code.

[0165] In one of the embodiments, the calling module 21 can also define a basic macro, the basic macro includes compiler attributes, the compiler attributes are configured with a storage location corresponding to the annotation information, the annotation information is obtained and corresponding parameters are defined for the annotation macro according to the annotation information, and the basic macro is called in the annotation macro so as to specify the storage location corresponding to the annotation information through the compiler attributes in the basic macro to define the annotation macro.

[0166] In one embodiment, when the target class needs to be annotated, the calling module 21 can call the annotation macro in the header of the target class.

[0167] In one embodiment, the adding module 23 may write the string into a corresponding section in a corresponding segment of the executable file according to the segment identifier and the section identifier during the compilation process.

[0168] In one of the embodiments, the adding module 23 may also register a callback function, and when it is detected that the executable file is loaded, the callback function is executed to parse the annotation information from the executable file through the callback function.

[0169] In one of the embodiments, the adding module 23 can also create an array of a variable type when it detects that the executable file is loaded, obtain the memory pointer and data size corresponding to the storage location according to the operating system running the source code, determine the number of elements in the data according to the data size, traverse each element in the storage location in turn according to the number of elements, convert each element and add it to the array, and return the array when the traversal is completed to parse out the annotation information.

[0170] In one of the embodiments, when the component class needs to add annotation information, the annotation information includes the component name and the component priority. The adding module 23 can also search for the corresponding component code module according to the component name, register the component according to the component code module and the component name, and load the component according to the priority so that the component can be initialized after loading.

[0171] In a third aspect, the present application provides a computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the steps of the method for adding code annotations provided in any embodiment of the present application in the first aspect are implemented.

[0172] In one embodiment, the computer device may be a server, and its internal structure diagram may be as follows: Figure 3 As shown. The computer device includes a processor, a memory, a network interface and a database connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for adding code annotations is implemented.

[0173] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method for adding code annotations provided in any embodiment of the first aspect of the present application.

[0174] The computer readable storage medium may be Figure 3 A computer-readable storage medium in the computer device shown.

[0175] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing related hardware through a computer program, and the above-mentioned computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0176] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0177] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.

Claims

1. A method for adding code annotations, characterized in that: The method comprises: Invoke a predefined annotation macro in a source code to which annotation information needs to be added, wherein the annotation macro includes annotation information and a storage location corresponding to the annotation information; Invoking a compiler to precompile the source code to generate a character string containing the annotation information; The source code is compiled into an executable file by the compiler, and the character string is written into the storage location of the executable file during the compilation process, so as to add the annotation information to the source code.

2. The method according to claim 1, characterized in that The method further comprises: defining a basic macro, wherein the basic macro includes a compiler attribute, and a storage location corresponding to the annotation information is configured in the compiler attribute; Acquire the annotation information and define corresponding parameters for the annotation macro according to the annotation information; The basic macro is called in the annotation macro, so as to specify the storage location corresponding to the annotation information through the compiler attribute in the basic macro to define the annotation macro.

3. The method according to claim 1, characterized in that The calling of the predefined annotation macro in the source code to which annotation information needs to be added includes: When the target class needs to be annotated, the annotation macro is called in the header of the target class.

4. The method according to claim 1, characterized in that The storage location includes a segment identifier and a section identifier of the executable file, and writing the string to the storage location of the executable file during the compiling process includes: During the compiling process, the character string is written into a corresponding section in a corresponding segment of the executable file according to the segment identifier and the section identifier.

5. The method according to claim 1, characterized in that The method further comprises: Register callback function; When it is detected that the executable file is loaded, the callback function is executed to parse the annotation information from the executable file through the callback function.

6. The method according to claim 1, characterized in that The executing the callback function to parse the annotation information from the executable file through the callback function includes: When it is detected that the executable file is loaded, creating an array of a variable type; Obtaining a memory pointer and a data size corresponding to the storage location according to an operating system running on the source code; Determining the number of elements in the data according to the data size; Traversing each element in the storage location in sequence according to the number of the elements; Convert each element and add it to the array; When the traversal is completed, the array is returned to parse out the annotation information.

7. The method according to claim 1, characterized in that When the component class to which annotation information needs to be added includes the component name and the priority of the component, after executing the callback function to parse the annotation information from the executable file through the callback function, the method further includes: Searching for a corresponding component code module according to the component name; Registering a component according to a component code module and the component name; The components are loaded according to the priorities so that the components are initialized after loading.

8. A device for adding code annotations, characterized in that: The device comprises: A calling module, used for calling a predefined annotation macro in a source code to which annotation information needs to be added, wherein the annotation macro includes annotation information and a storage location corresponding to the annotation information; A compiling module, used for calling a compiler to precompile the source code and generate a character string containing the annotation information; An adding module is used to compile the source code into an executable file through the compiler, and write the string into the storage location of the executable file during the compilation process to add the annotation information to the source code.

9. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.