Data verification method and device, electronic equipment and readable medium
By generating the verifier during the compilation process and storing the mapping relationship between the class and the verifier, the problems of low data verification efficiency and high resource utilization in the prior art are solved, and efficient static data verification is achieved.
Patent Information
- Application Number
- CN202411640249.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-15
- Publication Date
- 2025-03-11
AI Technical Summary
The prior art requires dynamic analysis and reflection calls during the data verification process, resulting in inefficiency and high resource utilization.
By using the first annotation in the source code to mark the class to be verified by data, a verification device is generated during the compilation process, and the mapping relationship between the class and the verification device is stored in the verification device registry, so that the pre-built static verification method is directly called during the operation process for data verification.
Dynamic analysis and reflection calls are avoided, data verification efficiency is improved, and operational resource occupation is reduced.
Smart Images

Figure CN119597618A_ABST
Abstract
Description
Background Art
[0002] Data verification is a key link in the software development process, which can include scenarios such as checking the validity of request parameters or checking data during data processing. At present, reflection mechanisms are usually used or a large amount of data judgment code is written to implement reflection-like functions for data verification operations. Based on code maintainability and extensibility, in C language, reflection-like functions are usually implemented through macro definitions and preprocessors; in Python, the reflection mechanism is implemented using decorators based on the "@" symbol; in Java language, the reflection mechanism is implemented through annotations based on the "@" symbol.
[0003] Using reflection mechanisms or implementing reflection-like functions usually requires parsing the class structure information at runtime, which takes up runtime resources and has a slow runtime parsing speed. In addition, reflection calls usually perform security checks to ensure that the caller has permission to access and operate the target class, affecting data verification efficiency.
[0004] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present disclosure, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention
[0005] The purpose of the present disclosure is to provide a data verification method, a data verification device, an electronic device and a computer-readable storage medium, so that dynamic parsing and reflection calling can be avoided when performing data verification during operation, and a pre-built static verification method can be directly executed, thereby improving data verification efficiency and reducing operation resource usage.
[0006] According to a first aspect of the present disclosure, a data verification method is provided, comprising: during operation, in response to a verification request for a target class, searching a verifier corresponding to the target class in a verifier registry; storing in the verifier registry a mapping relationship between a class and a verifier based on a first annotation mark, the first annotation marks in the source code a class to be data verified during operation, and the verifier is generated by parsing the class marked by the first annotation during compilation; and calling the verification method of the verifier to perform data verification on the target class.
[0007] In an exemplary embodiment, the verifier is generated by a verifier generator executing the following steps: scanning the source code during the compilation process to find at least one class marked with a first annotation; traversing the classes marked with the first annotation, parsing them in turn and generating corresponding verifiers; and storing the mapping relationship between each class and the corresponding verifier in a verifier registry.
[0008] In an exemplary embodiment, the steps of parsing each class marked by the first annotation and generating a corresponding validator include: searching for fields marked by the second annotation in the class; the second annotation defines the content of field data verification; traversing the fields marked by the second annotation, parsing and generating corresponding verification method fragments in turn; when the traversal of the fields marked by the second annotation in the class is completed, generating a validator class fragment corresponding to the class; adding the verification method fragment corresponding to each field to the validator class fragment, and generating a validator corresponding to the class.
[0009] In an exemplary embodiment, the validator generator includes an annotation processor registry, which stores the mapping relationship between the second annotation and the annotation processor. The steps of parsing each field marked by the second annotation and generating a verification method snippet include: obtaining the annotation processor corresponding to the second annotation according to the annotation processor registry; executing the annotation processor, and the annotation processor obtains the corresponding verification code template through the verification code template registry; the verification code template registry stores the mapping relationship between the annotation processor and the verification code template, and the verification code template is obtained by transforming the second annotation; executing the verification code template to generate a verification code snippet for the corresponding field in the class; creating a verification method snippet corresponding to the field, and adding the verification code snippet to the verification method snippet.
[0010] In an exemplary embodiment, creating a verification method fragment corresponding to a field includes: using the field as a prefix, the second annotation as a suffix, adding method input parameters with the value of the field, and adding a verification method scope to create a verification method fragment corresponding to the field.
[0011] In an exemplary embodiment, the verifier generator includes a result temporary storage table, which parses and generates corresponding verification method fragments in sequence, including: adding the verification method fragment generated by each field in the class to the result temporary storage table in sequence; adding the verification method fragment corresponding to each field to the verifier class fragment, and generating a verifier corresponding to the class, including: adding the verification method fragments corresponding to all fields in the class stored in the result temporary storage table to the verifier class fragment; clearing the result temporary storage table.
[0012] In an exemplary embodiment, the check method fragment corresponding to each field is added to the checker class fragment to generate a checker corresponding to the class, including: adding the fully qualified name and the check method fragment corresponding to each field to the checker class fragment to generate the checker source code; and compiling the checker source code to generate the checker corresponding to the class.
[0013] According to a second aspect of the present disclosure, a data verification device is provided, including: a request response module, which is used to search for a verifier corresponding to a target class in a verifier registry in response to a verification request for a target class during operation; the verifier registry stores a mapping relationship between a class and a verifier based on a first annotation mark, the first annotation marks a class to be data verified during operation in the source code, and the verifier is generated by parsing the class marked by the first annotation during compilation; a data verification module, which is used to call the verification method of the verifier to perform data verification on the target class.
[0014] According to a third aspect of the present disclosure, there is provided an electronic device, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to implement the above method by executing the executable instructions.
[0015] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the above method is implemented.
[0016] The data verification method provided by the embodiment of the present disclosure is that the first annotation marks the class to be verified during the operation process in the source code, the class marked by the first annotation in the source code is pre-parsed and a verifier is generated during the compilation process, and the mapping relationship between the class marked by the first annotation and the verifier is stored in the verifier registry; on this basis, during the operation process, in response to the verification request for the target class, the verifier of the target class can be found in the verifier registry, and the verification method of the verifier is called to perform data verification on the target class. Through this method, when executing data verification during the operation process, dynamic parsing and reflection calling of the class can be avoided, and the pre-built static verification method can be directly executed, thereby improving data verification efficiency and reducing operation resource usage.
[0017] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the specification are used to explain the principles of the present disclosure. Obviously, the accompanying drawings described below are only some embodiments of the present disclosure, and for ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without creative work.
[0019] Figure 1 The following is a flowchart schematically showing the steps of a data verification method in an exemplary embodiment of the present disclosure.
[0020] Figure 2A flowchart schematically illustrates the steps of generating a verifier in an exemplary embodiment of the present disclosure.
[0021] Figure 3 The flowchart schematically shows the steps of class parsing in an exemplary embodiment of the present disclosure.
[0022] Figure 4 The flowchart schematically shows the steps of generating a verification method fragment in an exemplary embodiment of the present disclosure.
[0023] Figure 5 A schematic diagram schematically illustrates a data verification framework in an exemplary embodiment of the present disclosure.
[0024] Figure 6 A schematic diagram of the composition of a data verification device that can be applied in an exemplary embodiment of the present disclosure is shown.
[0025] Figure 7 The figure shows a schematic diagram of the composition of an electronic device to which the exemplary embodiments of the present disclosure can be applied. DETAILED DESCRIPTION
[0026] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that the disclosure will be more comprehensive and complete and to fully convey the concepts of the example embodiments to those skilled in the art. The described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
[0027] In addition, the accompanying drawings are only schematic illustrations of the present disclosure and are not necessarily drawn to scale. The same reference numerals in the figures represent the same or similar parts, and their repeated description will be omitted. Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software form, or implemented in one or more hardware modules or integrated circuits, or implemented in different networks and / or processor devices and / or microcontroller devices.
[0028] Figure 1 A flowchart of a data verification method according to an exemplary embodiment of the present disclosure is shown. Figure 1 The data verification method of the embodiment of the present disclosure may include steps 101 to 102:
[0029] In step 101, during the running process, in response to a verification request for the target class, the verifier corresponding to the target class is searched in the verifier registry; the mapping relationship between the class marked based on the first annotation and the verifier is stored in the verifier registry, the first annotation marks the class to be verified during the running process in the source code, and the verifier is generated by parsing the class marked by the first annotation during the compilation process.
[0030] The verification request refers to a request for data verification initiated by the application to at least one class during operation. The target class is the object to which the application initiates the verification request, which may include one class or more than two classes. The device that responds to the verification request may be the execution device running the application, or may be other devices other than the execution device running the application, and the embodiments of the present disclosure do not impose specific restrictions on this.
[0031] In the disclosed embodiment, a verifier registry is provided. The verifier registry stores the mapping relationship between the class and the verifier based on the first annotation mark, so that when responding to the verification request for the target class, the verifier corresponding to the target class can be found in the verifier registry. Annotation is a metadata used to mark elements such as classes, methods and fields, and can provide additional information of the element so that corresponding processing can be performed at compile time or runtime. In a specific implementation, the implementation form of the annotation can include the use of "_" to define macros in C language, the use of "@" to define decorators in Python language, the use of "@" to define annotations in Java, and any other achievable language formats according to different language formats. The first annotation can be defined as marking the class to be verified during the operation process. On this basis, the class to be verified during the operation process can be found through the first annotation during the compilation of the source code, and it can be parsed to generate a verifier that executes the verification method for the class. Parsing and generating the verifier during the compilation process not only avoids the dynamic parsing and reflection call of the class during the operation process, but also can optimize the code based on the compiler, such as reducing the code size, shortening the execution time, etc., to further improve the data verification efficiency. In step 102, the verification method of the verifier is called to perform data verification on the target class.
[0032] In the disclosed embodiment, after determining the verifier corresponding to the target class, the verification method of the verifier can be directly called to perform data verification on the target class. There is no need to dynamically parse and reflectively call the target class, nor is there a need to perform a security check to determine whether the caller has the authority to access and operate the target class. This can effectively improve the efficiency of data verification on the target class.
[0033] The data verification method provided by the embodiment of the present disclosure is that the first annotation marks the class to be verified during the operation process in the source code, the class marked by the first annotation in the source code is pre-parsed and a verifier is generated during the compilation process, and the mapping relationship between the class marked by the first annotation and the verifier is stored in the verifier registry; on this basis, during the operation process, in response to the verification request for the target class, the verifier of the target class can be found in the verifier registry, and the verification method of the verifier is called to perform data verification on the target class. Through this method, when executing data verification during the operation process, dynamic parsing and reflection calling of the class can be avoided, and the pre-built static verification method can be directly executed, thereby improving data verification efficiency and reducing operation resource usage.
[0034] In an exemplary embodiment, a flowchart of the steps of generating a checker is provided. Figure 2 As shown, the verifier is generated by the verifier generator executing steps 201 to 203:
[0035] In step 201, source code is scanned during compilation to find at least one class marked by a first annotation.
[0036] In the embodiment of the present disclosure, during the process of the compiler compiling the source code, the source code can be scanned by the validator generator to find at least one class marked with the first annotation. The form of the first annotation can be customized by those skilled in the art based on actual needs. For example, in the C language, the first annotation can be defined as _FastValidator. Then, during the compilation process, the validator generator can scan all classes marked with _FastValidator in the source code for subsequent validator generation.
[0037] In step 202, the classes marked with the first annotation are traversed, parsed in sequence, and corresponding validators are generated.
[0038] In the disclosed embodiment, after determining the class marked by the first annotation, it can be traversed, and each class marked by the first annotation can be parsed in turn and a corresponding verifier can be generated, until all classes are processed and the traversal ends. After the traversal ends, each class should generate its corresponding verifier, and the verifier can call the verification method of the corresponding class to perform data verification on the class.
[0039] In step 203, a mapping relationship between each class and the corresponding verifier is stored in the verifier registry.
[0040] In the disclosed embodiment, the mapping relationship between classes and verifiers can be stored through the table structure of the verifier registry to improve the search efficiency during the application process. The mapping relationship between each class and its corresponding verifier is stored in the verifier registry. When the application initiates a verification request for any class, the specific verifier can be quickly located for data verification.
[0041] In an exemplary embodiment, a flow chart of the steps of class parsing is provided. Figure 3 As shown, for each class marked with the first annotation, the class is parsed and a corresponding verifier is generated, which can be generated by the verifier generator executing steps 301 to 304:
[0042] In step 301, a field marked by a second annotation in a class is searched; the second annotation defines the content of the field data verification.
[0043] When parsing a single class, the validator generator can search for all fields in the class marked by the second annotation. The second annotation defines the specific operation content of data verification on the field, such as the size, length, format, etc. of the number, set, array, and string of the field. Those skilled in the art can customize the second annotation according to the actual verification requirements and mark it to the field that needs corresponding data verification. The second annotation can also be set using existing standards. The embodiment of the present disclosure does not specifically limit the number and content of the second annotation.
[0044] In step 302, the fields marked with the second annotation are traversed, parsed in sequence, and corresponding verification method fragments are generated.
[0045] In the disclosed embodiment, after determining the fields marked by the second annotation, they can be traversed, and each field marked by the second annotation is parsed in turn and a corresponding verification method fragment is generated, until the traversal ends after all fields are processed. After the traversal ends, each field should generate its corresponding verification method fragment, and executing the verification method fragment can implement data verification of the corresponding field. It can be considered that the verification method fragments corresponding to all the fields marked by the second annotation in the class constitute the verification method of the class.
[0046] In step 303, when the traversal of the fields marked with the second annotation in the class is completed, a validator class fragment corresponding to the class is generated.
[0047] In the disclosed embodiment, when the traversal of the fields marked by the second annotation in the class is completed, it can be considered that all its fields have generated corresponding verification method fragments, which constitute the verification method for data verification of the class. On this basis, the verifier class fragment corresponding to the class can be generated as a preparation for constructing the verifier.
[0048] In step 304, the verification method fragment corresponding to each field is added to the verifier class fragment to generate a verifier corresponding to the class.
[0049] In the disclosed embodiment, the verification method fragment corresponding to each field can be added to the verifier class fragment, so as to generate the verifier corresponding to the class. During the traversal process, the verifier generator can continue to generate the verifier corresponding to the next class until the last class marked by the first annotation in the source code is traversed.
[0050] In an optional method embodiment, the checker generator includes a result temporary storage table, and step 302 may include the following step 302A:
[0051] In step 302A, the verification method fragment generated for each field in the class is added to the result temporary storage table in sequence.
[0052] On this basis, step 304 may include the following steps 304A1 to 304A2:
[0053] In step 304A1, the verification method fragments corresponding to all the fields in the class stored in the temporary result table are added to the verifier class fragment.
[0054] In step 304A2, the temporary result storage table is cleared.
[0055] In the disclosed embodiment, the verifier generator may include a result temporary storage table, and during the verifier generation process of each class, the verification method fragments generated by each field are temporarily stored in the result temporary storage table, and after the field traversal is completed, the verification method fragments generated by all fields are added to the verifier class fragment based on the result temporary storage table. After the field traversal of a class is completed, the result temporary storage table can be further cleared to be applied in the verifier generation process of the next class. The table structure of the result temporary storage table can quickly write, query and extract the temporarily stored verification method fragments, and it can also facilitate the traversal process to check for omissions and fill in the gaps, and by clearing the result temporary storage table, storage space can be effectively saved, which is suitable for the traversal verifier generation process during the traversal process.
[0056] In an optional method embodiment, step 304 may include the following steps 304B1 to 304B2:
[0057] In step 304B1, the fully qualified name and the verification method fragment corresponding to each field are added to the verifier class fragment to generate the verifier source code.
[0058] In step 304B2, a verifier corresponding to the class is generated by compiling the verifier source code.
[0059] In the disclosed embodiment, the fully qualified name is the complete name of the class including the package name in the programming language, which can uniquely identify a class and avoid class name conflicts. On this basis, the fully qualified name of the class can be added to the verifier class fragment, and the verification method fragment corresponding to each field of the class can be added to the verifier class fragment to generate the verifier source code. During the compilation process, the verifier source code is compiled to generate the verifier corresponding to the class.
[0060] In an exemplary embodiment, a flow chart of steps for generating a verification method fragment is provided. Figure 4 As shown, the validator generator includes an annotation processor registry, in which the mapping relationship between the second annotation and the annotation processor is stored, and each field marked with the second annotation is parsed and a validation method fragment is generated, which can be generated by the validator generator executing steps 401 to 404:
[0061] In step 401, the annotation processor corresponding to the second annotation is obtained according to the annotation processor registry.
[0062] In the embodiment of the present disclosure, the verifier generator may include an annotation processor registry, in which the mapping relationship between the second annotation and the annotation processor is stored, so that when the field marked by the second annotation is determined, the annotation processor corresponding to the second annotation can be obtained. The second annotation can correspond to the relevant description of the aforementioned step 301, and will not be repeated here to avoid repetition. The annotation processor corresponds to the second annotation one-to-one, and the verification code template corresponding to the second annotation can be searched for code generation.
[0063] In an exemplary embodiment, the second annotation may include:
[0064] _NotNull: Verifies that the object cannot be null.
[0065] _AssertTrue: Validates that a Boolean field must be true.
[0066] _AssertFalse: Validates that a Boolean field must be false.
[0067] _Min: Verifies that the number must be greater than or equal to the specified minimum value.
[0068] _Max: Verifies that the number must be less than or equal to the specified maximum value.
[0069] _DecimalMin: Verifies that the number must be greater than or equal to the specified minimum value (which can be specified as a decimal).
[0070] _DecimalMax: Verifies that the number must be less than or equal to the specified maximum value (which can be specified as a decimal).
[0071] _Size: The size of the verification element must be within the specified range (can be a string, collection, array, etc.).
[0072] _Digits: Validates the number of integer and decimal places of a number.
[0073] _Past: The validation date must be in the past.
[0074] _PastOrPresent: The validation date must be a date in the past or today.
[0075] _Future: The validation date must be a future date.
[0076] _FutureOrPresent: The validation date must be a future date or today.
[0077] _Pattern: The validation string must match the specified regular expression.
[0078] _Email: The verification string must be a valid email address.
[0079] _Positive: Verify that the number must be positive.
[0080] _PositiveOrZero: Verifies that the number must be positive or zero.
[0081] _Negative: The verification number must be negative.
[0082] _NegativeOrZero: Verifies that the number must be negative or zero.
[0083] _NotEmpty: Validates that a string, set, map, or array is not empty (not null and has a length greater than 0).
[0084] _NotBlank: The verification string must be non-null and its length must be greater than 0, and its length must be greater than 0 after removing the leading and trailing spaces.
[0085] _Null: The verification object must be null.
[0086] On this basis, you can predefine the _NotNull annotation processor, _AssertTrue annotation processor, _AssertFalse annotation processor, _Min annotation processor, _Max annotation processor, _DecimalMin annotation processor, _DecimalMax annotation processor, _Size annotation processor, _Digits annotation processor, _Past annotation processor, _PastOrPresent annotation processor, _Future annotation processor, _FutureOrPresent annotation processor, _Pattern annotation processor, _Email annotation processor, _Positive annotation processor, _PositiveOrZero annotation processor, _Negative annotation processor, _NegativeOrZero annotation processor, _NotEmpty annotation processor, _NotBlank annotation processor and _Nul annotation processor, etc.
[0087] At this time, assuming that the field is marked with _NotNull, the validator generator can obtain the corresponding _NotNull annotation processor according to the annotation processor registry.
[0088] It should be noted that the aforementioned definition of the second annotation with "_" is only an exemplary description in the C language format. Those skilled in the art can also define the decorator with "@" in Python to create the corresponding second annotation, and can use "@" to define the annotation in the Java language to create the corresponding second annotation, such as replacing the _NotNull macro with @NotNull. The embodiment of the present disclosure does not specifically limit the method of implementing the annotation.
[0089] In step 402, the annotation processor is executed, and the annotation processor obtains the corresponding verification code template through the verification code template registry; the verification code template registry stores the mapping relationship between the annotation processor and the verification code template, and the verification code template is obtained by the second annotation transformation.
[0090] In the disclosed embodiment, the verification code template can be obtained by transforming the second annotation, and is a code representation of the data verification rule defined by the second annotation. For example, the second annotation _NotNull: verification object cannot be null can be transformed into a verification code template return value!=null.
[0091] On this basis, a validation code template registry can be provided to store the mapping relationship between annotation processors and validation code templates. The corresponding annotation processor can obtain the validation code template based on the mapping relationship in the validation code template registry. For example, the _NotNull annotation processor can obtain the validation code template return value! = null based on the mapping relationship in the validation code registry.
[0092] In an optional embodiment of the present disclosure, an annotation processor component library composed of predefined annotation processors and a verification code template library composed of verification code templates can be constructed based on the second annotation. The verification code template registry can define the mapping relationship between the elements in the annotation processor component library and the verification code template library. The annotation processor component library and the verification code template library can be dynamically updated and maintained based on the settings of the second annotation, so as to adapt to changes and expansions in verification requirements.
[0093] In step 403, the verification code template is executed to generate a verification code snippet for the corresponding field in the class.
[0094] In the disclosed embodiment, the checker generator may execute a check code template to generate a check code snippet for a corresponding field, and the check code snippet may be executed to perform a data check defined by the second annotation on the field.
[0095] In step 404, a verification method fragment corresponding to the field is created, and the verification code fragment is added to the verification method fragment.
[0096] Furthermore, a verification method snippet corresponding to the field may be created to specifically define relevant parameters of the verification method, and the verification code snippet may be added as an internal code block to the verification method snippet for completion to obtain a complete verification method snippet corresponding to the field.
[0097] In an optional method embodiment, creating a verification method fragment corresponding to the field in step 404 may specifically include the following step 404A:
[0098] In step 404A, the field is used as a prefix and the second annotation is used as a suffix. The method input parameter is added with the value of the field, and the verification method scope is added to create a verification method fragment corresponding to the field.
[0099] In the disclosed embodiment, to create a verification method fragment corresponding to a field, the method definition can be performed with the field as a prefix and the second annotation as a suffix. On this basis, the method input parameter can also be added with the value of the field, and its method scope can be added. The method scope defines the scope of the verification method fragment, which can be defined by public, private, etc. in the programming language.
[0100] The data verification method provided by the embodiment of the present disclosure is that the first annotation marks the class to be verified during the operation process in the source code, the class marked by the first annotation in the source code is pre-parsed and a verifier is generated during the compilation process, and the mapping relationship between the class marked by the first annotation and the verifier is stored in the verifier registry; on this basis, during the operation process, in response to the verification request for the target class, the verifier of the target class can be found in the verifier registry, and the verification method of the verifier is called to perform data verification on the target class. Through this method, when executing data verification during the operation process, dynamic parsing and reflection calling of the class can be avoided, and the pre-built static verification method can be directly executed, thereby improving data verification efficiency and reducing operation resource usage.
[0101] Furthermore, by setting the validator generator to traverse the first annotated class, generate the validators corresponding to the class in turn and store the mapping relationship between the class and the validator in the validator registry, in the specific analysis, you can also configure the annotation processor, verification code template registry and verifier code template and other modules to work together to achieve efficient generation and execution of the verifier. At the same time, it also has configuration scalability and flexibility, and can quickly adapt to different verification requirements by adjusting different modules.
[0102] Figure 5 A schematic diagram of a data verification framework according to an exemplary embodiment of the present disclosure is shown, and the data verification framework is used to implement the aforementioned Figures 1 to 4 Any of the methods described, such as Figure 5 As shown, the data verification framework may include a verifier generator, a verifier registry, a verifier, and a verifier code template registry, wherein the verifier generator includes a result temporary storage table and an annotation processor registry.
[0103] On this basis, the embodiment of the present disclosure also provides an annotation processor component library and a verification code template library. The annotation processor component library stores predefined annotation processors, which are implemented one-to-one for the second annotation, such as _NotNull annotation processor, _AssertTrue annotation processor, _AssertFalse annotation processor, _Min annotation processor, _Max annotation processor, etc. The verification code template library stores predefined verification code templates, which are implemented one-to-one for the second annotation, such as _NotNull verification code template, _AssertTrue verification code template, _AssertFalse verification code template, _Min verification code template, _Max verification code template, etc. On this basis, the annotation processor registry stores the mapping relationship between the annotation processor and the verification code template.
[0104] exist Figure 5In the compilation process of the source code, class 1, class 2, class 3, etc. marked by the first annotation can be scanned, and the classes marked by the first annotation can be traversed by the verifier generator to generate their corresponding verifiers and maintain the mapping relationship between the classes and verifiers stored in the verifier registry.
[0105] The validator generator includes a result temporary storage table and an annotation processor registry. The annotation processor registry is used by the validator generator to determine the corresponding annotation processor in the annotation processor component library based on the second annotation of the field in the class. The validator generator can query the verification code registry through the annotation processor to determine the verification code template corresponding to the annotation processor in the verification code template library, and then generate the verification method fragment corresponding to the field based on the verification code template. The result temporary storage table is used to temporarily store the verification method fragment of each field during the verifier generation process of a class, and clear the result temporary storage table after the verification method fragments of all fields in the class are added to the verifier class fragment, so as to be applied to the verifier generation of the next class.
[0106] On this basis, during operation, in response to a verification request for at least one of class 1, class 2, class 3, etc., the corresponding verifier can be found and executed in the verifier register to call the corresponding verification method for data verification.
[0107] Through the collaborative work of the above-mentioned different modules, efficient generation and execution of the validator can be achieved. At the same time, each module also has configuration scalability and flexibility, such as the definition extension of the second annotation, the annotation processing component library based on this, the verification code template library extension, and the maintenance of the annotation processor registry and the validator registry. By adjusting different modules, it can quickly adapt to different verification requirements.
[0108] At this time, when performing data verification during operation, dynamic parsing and reflection calls of classes can be avoided, and pre-built static verification methods can be directly executed through the verifier, which improves data verification efficiency and further reduces operating resource usage.
[0109] It should be noted that the above figures are only schematic illustrations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended to be limiting. It is easy to understand that the processes shown in the above figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be performed synchronously or asynchronously, for example, in multiple modules.
[0110] For further reference, Figure 6 As shown, in an exemplary embodiment of the present disclosure, a data verification device 600 is provided, including a request response module 601 and a data verification module 602, wherein:
[0111] The request response module 601 is used to search for a verifier corresponding to the target class in a verifier registry in response to a verification request for the target class during operation; the verifier registry stores a mapping relationship between a class and a verifier based on a first annotation mark, the first annotation marks a class to be verified during operation in the source code, and the verifier is generated by parsing the class marked by the first annotation during compilation;
[0112] The data verification module 602 is used to call the verification method of the verifier to perform data verification on the target class.
[0113] In an exemplary embodiment, the verifier in the data verification device 600 is generated by a verifier generator, which is used to scan the source code during the compilation process to find at least one class marked with a first annotation; traverse the classes marked with the first annotation, parse them in turn and generate corresponding verifiers; and store the mapping relationship between each class and the corresponding verifier in a verifier registry.
[0114] In an exemplary embodiment, the validator generator is specifically used to find fields marked by a second annotation in a class; the second annotation defines the content of field data verification; the fields marked by the second annotation are traversed, parsed in turn and corresponding verification method fragments are generated; when the traversal of the fields marked by the second annotation in the class is completed, a validator class fragment corresponding to the class is generated; the verification method fragment corresponding to each field is added to the validator class fragment to generate a validator corresponding to the class.
[0115] In an exemplary embodiment, the validator generator includes an annotation processor registry, which stores a mapping relationship between a second annotation and an annotation processor. The validator generator is specifically used to obtain an annotation processor corresponding to the second annotation according to the annotation processor registry; execute the annotation processor, and the annotation processor obtains the corresponding verification code template through a verification code template registry; the verification code template registry stores a mapping relationship between the annotation processor and the verification code template, and the verification code template is obtained by transforming the second annotation; execute the verification code template to generate a verification code snippet for the corresponding field in the class; create a verification method snippet corresponding to the field, and add the verification code snippet to the verification method snippet.
[0116] In an exemplary embodiment, the validator generator is specifically used to use the field as a prefix and the second annotation as a suffix, add method input parameters with the value of the field, and add a validation method scope to create a validation method fragment corresponding to the field.
[0117] In an exemplary embodiment, the verifier generator includes a result temporary storage table, and the verifier generator is specifically used to add the verification method fragments generated by each field in the class to the result temporary storage table in sequence; and, add the verification method fragments corresponding to all fields in the class stored in the result temporary storage table to the verifier class fragment; and clear the result temporary storage table.
[0118] In an exemplary embodiment, the validator generator is specifically used to add the fully qualified name and the validation method fragment corresponding to each field to the validator class fragment to generate the validator source code; and generate the validator corresponding to the class by compiling the validator source code.
[0119] The data verification device provided by the embodiment of the present disclosure has a first annotation in the source code to mark the class to be verified during operation, pre-parses and generates a verifier for the class marked by the first annotation in the source code during the compilation process, and stores the mapping relationship between the class marked by the first annotation and the verifier in the verifier registry; on this basis, during the operation process, in response to the verification request for the target class, the verifier of the target class can be found in the verifier registry, and the verification method of the verifier can be called to perform data verification on the target class. Through this method, when executing data verification during operation, dynamic parsing and reflection calling of the class can be avoided, and the pre-built static verification method can be directly executed, thereby improving data verification efficiency and reducing operation resource usage.
[0120] Furthermore, by setting the validator generator to traverse the first annotated class, generate the validators corresponding to the class in turn and store the mapping relationship between the class and the validator in the validator registry, in the specific analysis, you can also configure the annotation processor, verification code template registry and verifier code template and other modules to work together to achieve efficient generation and execution of the verifier. At the same time, it also has configuration scalability and flexibility, and can quickly adapt to different verification requirements by adjusting different modules.
[0121] The specific details of each module in the above-mentioned data verification device have been described in detail in the implementation method part. The undisclosed details can be found in the implementation method content of the method part, that is, the explanation and beneficial effects of the data verification method in the above-mentioned embodiment are also applicable to the data verification device 600 of the embodiment of the present disclosure, and will not be elaborated here.
[0122] Those skilled in the art will appreciate that various aspects of the present disclosure may be implemented as systems, methods or program products. Therefore, various aspects of the present disclosure may be specifically implemented in the following forms, namely: complete hardware implementation, complete software implementation (including firmware, microcode, etc.), or a combination of hardware and software, which may be collectively referred to herein as "circuits", "modules" or "systems".
[0123] Refer to the following Figure 7 hereinafter describes an electronic device 700 according to such an embodiment of the present disclosure. Figure 7 The electronic device 700 shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0124] like Figure 7 As shown, the electronic device 700 is in the form of a general computing device. The components of the electronic device 700 may include, but are not limited to: the at least one processing unit 710, the at least one storage unit 720, a bus 730 connecting different system components (including the storage unit 720 and the processing unit 710), and a display unit 740.
[0125] The storage unit stores program codes, which can be executed by the processing unit 710, so that the processing unit 710 executes the steps according to various exemplary embodiments of the present disclosure described in the above “Exemplary Method” section of this specification.
[0126] The storage unit 720 may include a readable medium in the form of a volatile storage unit, such as a random access storage unit (RAM) 7201 and / or a cache storage unit 7202 , and may further include a read-only storage unit (ROM) 7203 .
[0127] The storage unit 720 may also include a program / utility 7204 having a set (at least one) of program modules 7205, such program modules 7205 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0128] Bus 730 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0129] The electronic device 700 may also communicate with one or more external devices 800 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 700, and / or communicate with any device that enables the electronic device 700 to communicate with one or more other computing devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 750. Furthermore, the electronic device 700 may also communicate with one or more networks (e.g., local area networks (LANs), wide area networks (WANs), and / or public networks, such as the Internet) via a network adapter 760. As shown, the network adapter 760 communicates with other modules of the electronic device 700 via a bus 730. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 700, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc.
[0130] In addition, the exemplary embodiments of the present disclosure further provide a computer-readable storage medium on which a program product capable of implementing the above-mentioned method of the present specification is stored. In some possible implementations, various aspects of the present disclosure may also be implemented in the form of a program product, which includes a program code, and when the program product is run on a terminal device, the program code is used to enable the terminal device to execute the steps according to various exemplary embodiments of the present disclosure described in the above-mentioned "Exemplary Method" section of the present specification.
[0131] It should be noted that the computer-readable medium shown in the present disclosure may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0132] In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, apparatus, or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in combination with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0133] In addition, program code for performing the operations of the present disclosure may be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as a separate software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving a remote computing device, the remote computing device may be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computing device (e.g., through the Internet using an Internet service provider).
[0134] Other embodiments of the present disclosure will be readily apparent to those skilled in the art after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary technical means in the art that are not disclosed in the present disclosure. The specification and embodiments are to be considered merely as exemplary, and the true scope and spirit of the present disclosure are indicated by the claims.
Claims
1. A data verification method, characterized in that: include: During operation, in response to a verification request for a target class, a verifier corresponding to the target class is searched in a verifier registry; The verifier registry stores a mapping relationship between a class marked by a first annotation and the verifier, wherein the first annotation marks a class to be verified during operation in the source code, and the verifier is generated by parsing the class marked by the first annotation during compilation; The verification method of the verifier is called to perform data verification on the target class.
2. The method according to claim 1, characterized in that The verifier is generated by the verifier generator executing the following steps: Scan source code during compilation to find at least one class marked by the first annotation; Traversing the classes marked by the first annotation, parsing them in turn and generating corresponding validators; The mapping relationship between each class and the corresponding verifier is stored in the verifier registry.
3. The method according to claim 2, characterized in that The steps of parsing each class marked with the first annotation and generating a corresponding verifier include: Searching for a field in the class that is marked by a second annotation; the second annotation defines content for verifying field data; Traversing the fields marked by the second annotation, parsing them in sequence and generating corresponding verification method fragments; When the traversal of the fields marked by the second annotation in the class is completed, generating a validator class fragment corresponding to the class; The verification method fragment corresponding to each of the fields is added to the verifier class fragment to generate a verifier corresponding to the class.
4. The method according to claim 3, characterized in that The verifier generator includes an annotation processor registry, in which a mapping relationship between the second annotation and the annotation processor is stored. The steps of parsing each field marked by the second annotation and generating a verification method fragment include: Obtain the annotation processor corresponding to the second annotation according to the annotation processor registry; Execute the annotation processor, and obtain the corresponding verification code template through the verification code template registry by the annotation processor; the verification code template registry stores the mapping relationship between the annotation processor and the verification code template, and the verification code template is obtained by converting the second annotation; Execute the verification code template to generate a verification code snippet for the corresponding field in the class; Create a verification method fragment corresponding to the field, and add the verification code fragment to the verification method fragment.
5. The method according to claim 4, characterized in that The verification method fragment corresponding to the field is created including: The field is used as a prefix and the second annotation is used as a suffix. The method input parameter is added with the value of the field, and the verification method scope is added to create a verification method fragment corresponding to the field.
6. The method according to claim 3, characterized in that: The checker generator includes a result temporary storage table, and the sequential parsing and generation of corresponding check method fragments include: Add the verification method fragment generated by each field in the class to the result temporary storage table in sequence; The adding the verification method fragment corresponding to each of the fields to the verifier class fragment to generate a verifier corresponding to the class includes: Add the verification method fragments corresponding to all the fields in the class stored in the temporary result table to the verifier class fragment; Clear the temporary result table.
7. The method according to claim 3, characterized in that The adding the verification method fragment corresponding to each of the fields to the verifier class fragment to generate a verifier corresponding to the class includes: Add the fully qualified name and the verification method fragment corresponding to each of the fields to the verifier class fragment to generate a verifier source code; The verifier corresponding to the class is generated by compiling the verifier source code.
8. A data verification device, characterized in that: include: A request response module, used for searching a verifier corresponding to a target class in a verifier registry in response to a verification request for a target class during operation; the verifier registry stores a mapping relationship between a class marked by a first annotation and the verifier, wherein the first annotation marks a class to be verified during operation in the source code, and the verifier is generated by parsing the class marked by the first annotation during compilation; The data verification module is used to call the verification method of the verifier to perform data verification on the target class.
9. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to perform the method of any one of claims 1 to 7 by executing the executable instructions.
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 method according to any one of claims 1 to 7 is implemented.