Annotation inheritance method, device and electronic device
By detecting the relationships and strategies between the target class and the parent class in Java, determine whether the target class inherits the target annotation, solve the problem that the parent annotation in the Java cannot be inherited by the subclasses, and improved the convenience of development and the inheritance mechanism of the annotation inheritance. Essence
Patent Information
- Application Number
- CN201910895016.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-09-20
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2039-09-20
AI Technical Summary
In Java, the annotations of the parent class cannot be inherited by the child class, resulting in less convenience in component and business code development.
Through the target annotation of the target class, if the target class is not detected, the parent class of the target class is obtained, and the target class will inherit the target class according to the relationship between the counting value and the target class and the parent class.
It improves the flexibility of the target inheritance target annotation, ensures the accuracy of inheritance, expands the inheritance mechanism of Java annotations, and enables the exposure to the development of components and business code, which improves the convenience of development.
Smart Images

Figure CN112540746B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of program development, and in particular to an annotation inheritance method, device and electronic equipment. Background Art
[0002] Java is an object-oriented programming language. It not only absorbs various advantages of C++ language, but also abandons the difficult-to-understand concepts such as multiple inheritance and pointers in C++. Therefore, Java language has two characteristics: powerful functions and simple and easy to use.
[0003] Java Development Kit or Java Development Kit (JDK) is the core of Java, and Annotation is a code-level description in Java. It is a feature introduced in JDK1.5 and later versions, and is at the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc. to explain and annotate these elements.
[0004] One of the main features of Java is inheritance. Inheritance is a cornerstone of object-oriented programming technology. It allows the creation of hierarchical classes. Furthermore, inheritance means that a subclass inherits the characteristics and behaviors of a parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class. However, in Java, some parent class annotations cannot be inherited by subclasses, which reduces the convenience of component and business code development. Summary of the invention
[0005] In order to at least overcome the above-mentioned deficiencies in the prior art, one of the objectives of the present invention is to provide an annotation inheritance method, device and electronic device.
[0006] A first aspect of an embodiment of the present invention provides an annotation inheritance method, including:
[0007] Detect whether the target class has a target annotation, and if the target class is not detected to have the target annotation, obtain the parent class of the target class; increase the set count value by one, wherein the initial value of the set count value is zero;
[0008] Determine whether the parent class has the target annotation;
[0009] If the target annotation exists in the parent class, it is determined whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class.
[0010] In an optional manner, determining whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class includes:
[0011] If the first relationship is to implement an interface and the first strategy is a first setting strategy, obtaining an inheritance depth value of the target annotation;
[0012] Determine whether the difference between the inheritance depth value and the set count value is greater than or equal to zero. If the difference is greater than or equal to zero, determine that the target class inherits the target annotation. If the difference is less than zero, determine that the target class does not inherit the target annotation.
[0013] In an optional manner, determining whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class includes:
[0014] If the first relationship is to implement an abstract class and the first strategy is a second setting strategy, obtaining an inheritance depth value of the target annotation;
[0015] Determine whether the difference between the inheritance depth value and the set count value is greater than or equal to zero. If the difference is greater than or equal to zero, determine that the target class inherits the target annotation. If the difference is less than zero, determine that the target class does not inherit the target annotation.
[0016] In an optional manner, the method further includes:
[0017] If the target annotation does not exist in the parent class, the parent class is determined as the current class, and the set count value is increased by one;
[0018] Determine whether the parent class of the current class is a super parent class;
[0019] If so, determining that the target class does not inherit the target annotation;
[0020] Otherwise, when the target annotation exists in the parent class of the current class, determining the relationship and strategy between the current class and the parent class of the current class;
[0021] Determine whether the target class can inherit the target annotation according to the consistency of all relationships and all strategies between the target class and the parent class of the current class, and the difference between the inheritance depth value and the set count value;
[0022] If the target annotation does not exist in the parent class of the current class, the parent class of the current class is determined as the current class, the set count value is increased by one, and the step of determining whether the parent class of the current class is a super parent class is executed again.
[0023] In an optional manner, if the target class is a target method, determining whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class includes:
[0024] If the first relationship is that the subclass overrides the parent class method and the first strategy is the third setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation;
[0025] If the first relationship is that the subclass implements the parent class abstract method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation;
[0026] If the first relationship is that the subclass implements the interface method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero; if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation.
[0027] A second aspect of an embodiment of the present invention provides an annotation inheritance device, including:
[0028] A parent class acquisition module is used to detect whether a target class has a target annotation, and if the target class is not detected to have the target annotation, acquire the parent class of the target class; increase a set count value by one, wherein the initial value of the set count value is zero;
[0029] The annotation inheritance determination module is used to determine whether the parent class has the target annotation; if the parent class has the target annotation, determine whether the target class inherits the target annotation based on the set count value and the first relationship and first strategy between the target class and the parent class.
[0030] In an optional manner, the annotation inheritance determination module is further used to:
[0031] If the target annotation does not exist in the parent class, the parent class is determined as the current class, and the set count value is increased by one;
[0032] Determine whether the parent class of the current class is a super parent class;
[0033] If so, determining that the target class does not inherit the target annotation;
[0034] Otherwise, when the target annotation exists in the parent class of the current class, determining the relationship and strategy between the current class and the parent class of the current class;
[0035] Determine whether the target class can inherit the target annotation according to the consistency of all relationships and all strategies between the target class and the parent class of the current class, and the difference between the inheritance depth value and the set count value;
[0036] If the parent class of the current class does not have a target annotation, the parent class of the current class is determined as the current class, the set count value is increased by one, and the step of determining whether the parent class of the current class is a super parent class is executed again.
[0037] In an optional manner, if the target class is a target method, the annotation inheritance determination module is used to:
[0038] If the first relationship is that the subclass overrides the parent class method and the first strategy is the third setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation;
[0039] If the first relationship is that the subclass implements the parent class abstract method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation;
[0040] If the first relationship is that the subclass implements the interface method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero; if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation.
[0041] According to a third aspect of an embodiment of the present invention, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned annotation inheritance method when executing the program.
[0042] According to a fourth aspect of an embodiment of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the steps of the above-mentioned annotation inheritance method are implemented.
[0043] The annotation inheritance method, device and electronic device provided by the embodiments of the present invention can obtain the parent class of the target class when the target annotation is not detected in the target class, increase the set count value by one, and determine whether the target class inherits the target annotation based on the set count value and the first relationship and the first strategy between the target class and the parent class when the target annotation exists in the parent class. In this way, it is possible to determine whether the target class inherits the target annotation when different relationships and strategies exist between the target class and the parent class, thereby improving the flexibility of the target class in inheriting the target annotation, and the inheritance depth value of the target annotation can be determined based on the set count value, thereby ensuring the accuracy of the target class inheriting the target annotation, and expanding the inheritance mechanism of Java annotations, so that annotations can be widely used in the development process of components and business codes, thereby improving the convenience of component and business code development. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0045] Figure 1 The present invention provides a flowchart of an annotation inheritance method.
[0046] Figure 2 A functional module block diagram of an annotation inheritance device provided by an embodiment of the present invention.
[0047] icon:
[0048] 20-annotation inheritance device; 21-parent class acquisition module; 22-annotation inheritance determination module. DETAILED DESCRIPTION
[0049] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.
[0050] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0051] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0052] In the description of the present invention, it is also necessary to explain that, unless otherwise clearly specified and limited, the terms "set", "install", "connect", and "connect" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two elements. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0053] The inventors have found through investigation that the classes in Java include ordinary classes, abstract classes and interfaces. For ordinary classes, there is no problem for subclasses to inherit the annotations of parent classes, but abstract classes and interfaces do not support the inheritance of annotations. For example, if an animal is defined as an abstract class or interface, then the annotation "smart" of the animal cannot be inherited by a dog, which is a subclass of the animal. In addition, Java does not support the inheritance of annotations of methods. For another example, an animal defines a behavior of "eating" and labels it "things", and things contain attributes such as name and weight. Then the dog cannot inherit the annotation of "things" when implementing the behavior of eating. It can be seen that in Java, some annotations of parent classes cannot be inherited by subclasses, which reduces the convenience of component and business code development.
[0054] In view of this, the embodiments of the present invention provide an annotation inheritance method, device and electronic device, which can determine whether a target class inherits a target annotation when different relationships and strategies exist between the target class and the parent class, thereby improving the flexibility of the target class in inheriting the target annotation, and can determine the inheritance depth value of the target annotation based on a set count value, thereby ensuring the accuracy of the target class inheriting the target annotation, and expanding the inheritance mechanism of Java annotations, so that annotations can be widely used in the development process of components and business codes, thereby improving the convenience of component and business code development.
[0055] Figure 1 A flowchart of an annotation inheritance method provided by an embodiment of the present invention is shown below. Figure 1 The steps shown are explained in detail:
[0056] S20, detecting whether the target class has a target annotation.
[0057] In the specific implementation process, the annotation modified by the meta-annotation @Inherited can be determined as the target annotation. Among them, the meta-annotation refers to the annotation of the annotation, including the following four categories: @Retention, @Target, @Document and @Inherited. In the embodiment of the present application, the meta-annotation is @Inherited.
[0058] In the embodiment of the present application, the meta-annotation @Inherited includes the attribute value and the inheritance depth value depth. The attribute value is the default attribute for all annotations, the inheritance depth value depth is used to characterize the depth to which the target annotation can inherit, and the inheritance depth value depth is the scope of the target annotation. In the embodiment of the present application, the inheritance depth value depth is a positive integer.
[0059] Specifically, if it is detected that the target class has a target annotation, the target class itself has the target annotation, and the depth-th layer subclass of the target class can inherit the target annotation.
[0060] For example, if the depth is 2, when the target class C1 has a target annotation A, if the subclass C11 of C1 can inherit the target annotation A of C1, and the subclass C111 of C11 can inherit the target annotation A inherited by C11, then it can be determined that C111 can inherit the target annotation A of C1.
[0061] Specifically, if C11 is understood as the lower layer of C1, then a depth of 2 can be understood as the target annotation A can act 2 layers downward.
[0062] For another example, when judging whether C1111, a subclass of C111, can inherit the target annotation A inherited by C111, since C1111 is at the third level relative to C1, C1111 cannot inherit the target annotation A of C1. This is because the target annotation A exists in C1, and C11 inherits the target annotation A inherited by C11, and C111 inherits the target annotation A inherited by C11. It can also be understood that C111 inherits the target annotation A of C1. The depth of 2 indicates that the target annotation A can act 2 layers downward, so the last layer where the target annotation A can act is the layer where C111 is located. Therefore, even if C1111 is a subclass of C111, the inheritance depth value of the target annotation A indicates that the target annotation A cannot act on C1111 through C111, so C1111 cannot inherit the target annotation A of C1.
[0063] In the subsequent description of the embodiments of the present application, the inheritance mechanism of the target annotation will be described in detail, so no further explanation is given here.
[0064] Furthermore, if no target annotation is detected for the target class, go to S21.
[0065] S21, obtaining the parent class of the target class, and increasing the set count value by one.
[0066] In the embodiment of the present application, if the target class is not detected to have a target annotation, the parent class of the target class is obtained, and the set count value is increased by 1. The initial value of the set count value is 0, and the set count value is used to compare with the inheritance depth value of the target annotation to determine whether the target class can inherit the target annotation. How to determine whether the target class can inherit the target annotation based on the set count value and the inheritance depth value will be described in the subsequent description.
[0067] For the convenience of subsequent explanation, the target annotation can be represented as A, the target class can be represented as C1, the parent class of C1 can be represented as C2, the parent class of C2 can be represented as C3, the parent class of C3 can be represented as C4, and so on. The set count value can be k, and the initial value of k is 0.
[0068] Specifically, when C2 is obtained, k is incremented from 0 to 1, and k=1 at this time.
[0069] S22, determine whether the parent class of the target class has a target annotation.
[0070] Further, after obtaining C2, it is determined whether C2 has the target annotation A. If so, turn to S23; otherwise, turn to S24.
[0071] S23, determining whether the target class inherits the target annotation according to the set count value and the first relationship between the target class and the parent class and the first strategy.
[0072] In the specific implementation process, according to the set count value and the first relationship and the first strategy between the target class and the parent class, it is determined whether the target class inherits the target annotation. Specifically, it can be implemented in the following two ways. Of course, in the specific implementation process, it is not limited to the following two ways:
[0073] First, if the first relationship between C1 and C2 is to implement an interface and the first strategy is a first setting strategy, the inheritance depth value depth of the target annotation A is obtained.
[0074] Determine whether the difference between depth and k is greater than or equal to zero. If so, determine that the scope of the target annotation A can reach C1 from C2, and then determine that C1 can inherit the target annotation A in C2. Otherwise, determine that the scope of the target annotation A cannot reach C1 from C2, and then determine that C1 cannot inherit the target annotation A in C2.
[0075] Specifically, since the content of judging whether the difference between depth and k is greater than or equal to zero is similar to the content shown in S20, it will not be described again with examples.
[0076] Second, if the first relationship between C1 and C2 is to implement an abstract class and the first strategy is the second setting strategy, determine whether C1 can inherit the target annotation A in C2 according to the set count value k and the obtained inheritance depth value depth. Since the method of determining whether C1 can inherit the target annotation A in C2 according to the set count value k and the obtained inheritance depth value depth is similar to the first case, no further explanation is given here.
[0077] In the embodiment of the present application, the first setting strategy is the TYPE_INTERFACE interface strategy, and the TYPE_INTERFACE interface strategy indicates that when a certain interface is implemented, the subclass of the interface can inherit the annotation of the interface. More specifically, if the first relationship between C1 and C2 is to implement the interface and the first strategy between C1 and C2 is the TYPE_INTERFACE interface strategy, then it can be determined whether C1 can inherit the target annotation A in C2 based on the set count value k and the acquired inheritance depth value depth.
[0078] In the embodiment of the present application, the second setting strategy is the TYPE_ABSTRACT abstract class strategy, which indicates that when implementing an abstract class, the subclass of the abstract class can inherit the annotation of the abstract class. More specifically, if the first relationship between C1 and C2 is to implement an interface and the first strategy between C1 and C2 is the TYPE_ABSTRACT abstract class strategy, then it can be determined whether C1 can inherit the target annotation A in C2 based on the set count value k and the acquired inheritance depth value depth, specifically:
[0079] If the difference between the set count value k and the inheritance depth value depth is greater than or equal to zero, it is determined that C1 can inherit the target annotation A in C2;
[0080] If the difference between the set count value k and the inheritance depth value depth is less than zero, it is determined that C1 cannot inherit the target annotation A in C2.
[0081] Optionally, if the first relationship between C1 and C2 is inheritance and the first strategy is DEFAULT, it can be determined whether C1 can inherit the target annotation A in C2 according to the set count value k and the acquired inheritance depth value depth. Specifically:
[0082] If the difference between the set count value k and the inheritance depth value depth is greater than or equal to zero, it is determined that C1 can inherit the target annotation A in C2;
[0083] If the difference between the set count value k and the inheritance depth value depth is less than zero, it is determined that C1 cannot inherit the target annotation A in C2.
[0084] Among them, DEFAULT indicates that the Java inheritance rules are used by default.
[0085] It can be understood that, based on the set count value k, the inheritance depth value depth, and the first relationship and the first strategy between C1 and C2, it is determined whether C1 can inherit the target annotation A of C2. It is possible to determine whether C1 inherits the target annotation A when different relationships and strategies exist between C1 and C2, thereby improving the flexibility of C1 inheriting the target annotation A. Based on the set count value k, the inheritance depth value depth of the target annotation A can be judged, and the effective scope of the target annotation A can be determined, thereby ensuring the accuracy of C1 inheriting the target annotation A and expanding the inheritance mechanism of Java annotations.
[0086] It can be understood that if it is determined according to S23 that the target class does not inherit the target annotation, the process goes to S29.
[0087] S24, determining the parent class of the target class as the current class, and increasing the set count value by one.
[0088] In a specific implementation process, if the target annotation A does not exist in C2, C2 is determined as the current class, and the set count value k is incremented from 1 to 2.
[0089] It can be understood that if the target annotation A does not exist in C2, it is necessary to determine whether the target annotation A exists in C3, so as to determine whether C2 can inherit the target annotation A, and then determine whether C1 can inherit the target annotation A. Specifically, when determining whether the target annotation A exists in C3, it is first necessary to determine whether C3 is a super parent class, and then make subsequent judgments. In this way, the number of judgments can be effectively reduced, thereby reducing the computing load of the electronic device.
[0090] In specific implementation, to facilitate the subsequent explanation logic, if the parent class does not have a target annotation, the parent class is determined as the current class, and then the parent class of the current class is further analyzed.
[0091] S25, whether the parent class of the current class is a super parent class.
[0092] In the embodiment of the present application, the super parent class is the Object class, which is the root parent class of all Java classes. There is no annotation in the super parent class, and the super parent class has no parent class.
[0093] In S25:
[0094] If the parent class C3 of the current class C2 is a super parent class, go to S29;
[0095] If the parent class C3 of the current class C2 is not a super parent class, go to S26.
[0096] S26, whether the parent class of the current class has a target annotation.
[0097] In S26:
[0098] If the parent class C3 of the current class C2 has the target annotation A, go to S28;
[0099] If the parent class C3 of the current class C2 does not have the target annotation A, go to S27.
[0100] S27, determining the parent class of the current class as the current class, and increasing the set count value by one.
[0101] In S27, if the parent class C3 of the current class C2 does not have the target annotation A, C3 is determined as the current class, k is increased by 1, and then the process returns to S25 to continue to perform relevant judgments on the current class.
[0102] S28, determining whether the target class inherits the target annotation according to the consistency of all relationships and all strategies between the target class and the parent class of the current class, and the difference between the inheritance depth value and the set count value.
[0103] In the specific implementation process, determine whether the target class inherits the target annotation, which can be achieved in the following ways:
[0104] Determine whether the target class can inherit the target annotation based on the consistency of all relationships and all strategies between the target class and the parent class of the current class, as well as the difference between the inheritance depth value and the set count value.
[0105] Specifically, based on the above description, the target class is C1, the parent class of the target class C1 is the current class C2, the parent class of the current class C2 is C3, and further, the first relationship and the first strategy between C1 and C2 can be represented as H12, and the second relationship and the second strategy between C2 and C3 can be represented as H23.
[0106] More specifically, if H12 is the same as H23, it indicates that all relationships and all strategies among C1, C2 and C3 are consistent. In this case, it is possible to continue to determine whether the target class can inherit the target annotation based on the difference between the inheritance depth value and the set count value.
[0107] In the above description, whether all the relationships and all the strategies between C1, C2 and C3 are consistent can be illustrated by the following example:
[0108] When the first relationship between C1 and C2 is to realize an interface and the first strategy between C1 and C2 is the first set strategy, or the first relationship between C1 and C2 is to realize an abstract class and the first strategy between C1 and C2 is the second set strategy, if the second relationship between C2 and C3 is the same as the first relationship between C1 and C2, and the second strategy between C2 and C3 is the same as the first strategy between C1 and C2, then the representation H12 is the same as H23, and then it is determined that all relationships and all strategies between C1, C2 and C3 are consistent.
[0109] On the premise that all relationships and all strategies between C1, C2 and C3 are consistent, it is further determined whether the difference between the inheritance depth value and the set count value is greater than or equal to zero. If the difference between the inheritance depth value and the set count value is greater than or equal to zero, it is determined that the target class inherits the target annotation. If the difference between the inheritance depth value and the set count value is less than zero, it is determined that the target class does not inherit the target annotation.
[0110] For example, assuming that all relationships and all strategies between C1, C2, and C3 are consistent:
[0111] If the difference between the inheritance depth value depth and the set count value k is greater than or equal to zero, it is determined that C2 can inherit the target annotation A from C3, and C1 can inherit the target annotation A from C2, and thus it is determined that C1 can inherit the target annotation A;
[0112] If the difference between the inheritance depth value depth and the set count value k is less than zero, it is determined that C2 can inherit the target annotation A from C3, but C1 cannot inherit the target annotation A from C2, and then it is determined that C1 cannot inherit the target annotation A, and turn to S29.
[0113] S29, determining that the target class does not inherit the target annotation.
[0114] In S27, C3 is determined as the current class, k is increased by 1, and then the process returns to S25 to continue to make relevant judgments about the current class, which can be explained by the following example:
[0115] For example, C3 is determined as the current class, and the set count value k is incremented from 2 to 3.
[0116] Furthermore, return to S25 to determine whether the parent class C4 of the current class C3 is a super parent class:
[0117] If C4 is a super parent class, it is determined that C1 cannot inherit the target annotation A, and the process goes to S29;
[0118] If C4 is not a super parent class, go to S26.
[0119] In this embodiment, when S26 is executed in the cycle C4, it is specifically implemented in the following manner:
[0120] If the target annotation A exists in C4, turn to S28: determine the third relationship and the third strategy between C4 and C3, and then determine the consistency of all relationships and all strategies between C1 to C4 according to the first relationship, the first strategy, the second relationship, the second strategy, the third relationship and the third strategy, and then determine whether C1 inherits the target annotation A based on the set count value. Since the above determination method of whether C1 inherits the target annotation A is similar to the content in the above S28, no further explanation is given here;
[0121] If the target annotation A does not exist in C4, the process turns to S27. After executing S27, the process returns to S25 to continue to perform relevant judgments on the current class.
[0122] In this way, the loop determination of whether C1 inherits the target annotation A can be implemented with the super parent class as the loop termination condition, and the loop determination of whether C1 inherits the target annotation A can also be implemented with the difference between the inheritance depth value and the set count value as the loop termination condition.
[0123] In this way, the loop determination of whether C1 inherits the target annotation A can be avoided from entering an infinite loop process, thereby reducing the computing load of the electronic device.
[0124] In the specific implementation process, if the target class C1 is a target method, according to the set count value and the first relationship and the first strategy between the target class and the parent class, it is determined whether the target class inherits the target annotation, including:
[0125] If the first relationship is that the child class overrides the parent class method and the first strategy is the third setting strategy, the inheritance depth value of the target class is obtained. It is determined whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero. If so, it is determined that the target class inherits the target annotation; otherwise, it is determined that the target class does not inherit the target annotation.
[0126] If the first relationship is that the subclass implements the parent class abstract method and the first strategy is the fourth setting strategy, the inheritance depth value of the target class is obtained. It is determined whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero. If so, it is determined that the target class inherits the target annotation; otherwise, it is determined that the target class does not inherit the target annotation.
[0127] If the first relationship is that the subclass implements the interface method and the first strategy is the fourth setting strategy, the inheritance depth value of the target class is obtained. It is determined whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero. If so, it is determined that the target class inherits the target annotation; otherwise, it is determined that the target class does not inherit the target annotation.
[0128] In the embodiment of the present application, the third setting strategy is the METHOD_OVERRIDE method overriding strategy. The METHOD_OVERRIDE method overriding strategy indicates that when a subclass overwrites a parent class method, the subclass can inherit the annotation of the parent class method. More specifically, if the first relationship between C1 and C2 is that the subclass overwrites the parent class method and the first strategy between C1 and C2 is the METHOD_OVERRIDE method overriding strategy, it is determined that C1 inherits the target annotation A.
[0129] In the embodiment of the present application, the fourth setting strategy is the METHOD_ABSTRACT method abstraction strategy. The METHOD_ABSTRACT method abstraction strategy indicates that when a subclass implements a parent class abstract method, the subclass can inherit the annotation of the parent class method. More specifically, if the first relationship between C1 and C2 is that the subclass implements the parent class abstract method and the first strategy between C1 and C2 is the METHOD_ABSTRACT method abstraction strategy, then it is determined that C1 inherits the target annotation A. Optionally, if the first relationship between C1 and C2 is that the subclass implements the interface method and the first strategy between C1 and C2 is the METHOD_ABSTRACT method abstraction strategy, then it is determined that C1 inherits the target annotation A.
[0130] Optionally, if the first relationship between C1 and C2 is inheritance and the first strategy is DEFAULT, it is determined that C1 inherits the target annotation A; otherwise, it is determined that C1 does not inherit the target annotation A. DEFAULT indicates that the inheritance rule of Java is used by default.
[0131] It should be understood that when C1 is the target method, the subsequent target annotation determination process is consistent with the contents shown in S20 to S29, so no further explanation is given here.
[0132] It can be understood that through the above-mentioned annotation inheritance method, the annotation inheritance function of Java can be expanded. It can determine whether the target class can inherit the target annotation based on the inheritance depth value of the target annotation and the relationship and strategy between the subclass and the parent class. It can effectively improve the problem of poor scalability of Java native annotations, so that annotations can be widely used in the development process of components and business codes, thereby improving the convenience of component and business code development.
[0133] Based on the above, please refer to Figure 2 The embodiment of the present application also provides an annotation inheritance device 20, including a parent class acquisition module 21 and an annotation inheritance determination module 22.
[0134] The parent class acquisition module 21 is used to detect whether the target class has a target annotation, and if the target class is not detected to have the target annotation, acquire the parent class of the target class; increase the set count value by one, wherein the initial value of the set count value is zero;
[0135] The annotation inheritance determination module 22 is used to determine whether the parent class has the target annotation; if the parent class has the target annotation, determine whether the target class inherits the target annotation based on the set count value and the first relationship and first strategy between the target class and the parent class.
[0136] In an optional manner, the annotation inheritance determination module 22 is used to:
[0137] If the first relationship is to implement an interface and the first strategy is a first setting strategy, obtaining an inheritance depth value of the target annotation;
[0138] Determine whether the difference between the inheritance depth value and the set count value is greater than or equal to zero. If the difference is greater than or equal to zero, determine that the target class inherits the target annotation. If the difference is less than zero, determine that the target class does not inherit the target annotation.
[0139] In an optional manner, the annotation inheritance determination module 22 is used to:
[0140] If the first relationship is to implement an abstract class and the first strategy is a second setting strategy, obtaining an inheritance depth value of the target annotation;
[0141] Determine whether the difference between the inheritance depth value and the set count value is greater than or equal to zero. If the difference is greater than or equal to zero, determine that the target class inherits the target annotation. If the difference is less than zero, determine that the target class does not inherit the target annotation.
[0142] In an optional manner, the annotation inheritance determination module 22 is further used to:
[0143] If the target annotation does not exist in the parent class, the parent class is determined as the current class, and the set count value is increased by one;
[0144] Determine whether the parent class of the current class is a super parent class;
[0145] If so, determining that the target class does not inherit the target annotation;
[0146] Otherwise, when the target annotation exists in the parent class of the current class, determining the relationship and strategy between the current class and the parent class of the current class;
[0147] Determine whether the target class can inherit the target annotation according to the consistency of all relationships and all strategies between the target class and the parent class of the current class, and the difference between the inheritance depth value and the set count value;
[0148] If the target annotation does not exist in the parent class of the current class, the parent class of the current class is determined as the current class, the set count value is increased by one, and the step of determining whether the parent class of the current class is a super parent class is executed again.
[0149] In an optional manner, if the target class is a target method, the annotation inheritance determination module 22 is used to:
[0150] If the first relationship is that the subclass overrides the parent class method and the first strategy is the third setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation;
[0151] If the first relationship is that the subclass implements the parent class abstract method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation;
[0152] If the first relationship is that the subclass implements the interface method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero; if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation.
[0153] An embodiment of the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned annotation inheritance method when executing the computer program.
[0154] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium includes a computer program, and when the computer program is executed, the simulation simulator where the computer-readable storage medium is located is controlled to execute the above-mentioned annotation inheritance method.
[0155] In summary, the annotation inheritance method, device and electronic device provided by the embodiment of the present invention can obtain the parent class of the target class when the target annotation is not detected, increase the set count value by one, and when the target annotation exists in the parent class, determine whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class. In this way, it is possible to determine whether the target class inherits the target annotation when there are different relationships and strategies between the target class and the parent class, thereby improving the flexibility of the target class inheriting the target annotation, and based on the set count value, the inheritance depth value of the target annotation can be determined, ensuring the accuracy of the target class inheriting the target annotation, expanding the inheritance mechanism of Java annotations, so that annotations can be widely used in the development process of components and business codes, and improving the convenience of component and business code development. Further, by setting the loop termination conditions of whether the difference between the inheritance depth value and the set count value is zero and whether there is a super parent class, it is possible to avoid the loop determination of whether the target class inherits the target annotation from entering an infinite loop process, reducing the computing load of the computer device, and increasing the convenience of annotation inheritance determination.
[0156] The above are only various embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. An annotation inheritance method, characterized in that: include: Detect whether the target class has a target annotation. If the target class does not have the target annotation, obtain the parent class of the target class. Increasing a set count value by one, wherein an initial value of the set count value is zero; Determine whether the parent class has the target annotation; If the target annotation exists in the parent class, determine whether the target class inherits the target annotation according to the set count value and the first relationship between the target class and the parent class and the first strategy, the first relationship being any one of the following: implementing an interface, implementing an abstract class, and when the target class is a target method, the first relationship being any one of the following: the subclass overwrites the parent class method, the subclass implements the parent class abstract method, and the subclass implements the interface method; The step of determining whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class includes: Determining whether the target class inherits the target annotation according to a difference between the inheritance depth value of the target annotation and the set count value, the first relationship, and the first strategy; The determining, according to the set count value and the first relationship and the first strategy between the target class and the parent class, whether the target class inherits the target annotation includes: If the first relationship is to implement an interface and the first strategy is a first setting strategy, obtain the inheritance depth value of the target annotation; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation, if the difference is less than zero, determine that the target class does not inherit the target annotation; the first setting strategy is the TYPE_INTERFACE interface strategy, and the TYPE_INTERFACE interface strategy indicates that when a single interface is implemented, the subclass of the interface can inherit the annotation of the interface; If the first relationship is to implement an abstract class and the first strategy is the second setting strategy, obtain the inheritance depth value of the target annotation; determine whether the difference between the inheritance depth value and the set count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation, if the difference is less than zero, determine that the target class does not inherit the target annotation, and the second setting strategy is the TYPE_ABSTRACT abstract class strategy, and the TYPE_ABSTRACT abstract class strategy indicates that when a single abstract class is implemented, the subclass of the abstract class can inherit the annotation of the abstract class.
2. The annotation inheritance method according to claim 1, characterized in that: The method further comprises: If the target annotation does not exist in the parent class, the parent class is determined as the current class, and the set count value is increased by one; Determine whether the parent class of the current class is a super parent class; If so, determining that the target class does not inherit the target annotation; Otherwise, when the target annotation exists in the parent class of the current class, determining the relationship and strategy between the current class and the parent class of the current class; Determine whether the target class can inherit the target annotation according to the consistency of all relationships and all strategies between the target class and the parent class of the current class, and the difference between the inheritance depth value and the set count value; If the target annotation does not exist in the parent class of the current class, the parent class of the current class is determined as the current class, the set count value is increased by one, and the step of determining whether the parent class of the current class is a super parent class is executed again.
3. The annotation inheritance method according to claim 1, characterized in that: If the target class is a target method, determining whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class includes: If the first relationship is that the subclass overrides the parent class method and the first strategy is the third setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation, and the third setting strategy is the METHOD_OVERRIDE method overriding strategy, and the METHOD_OVERRIDE method overriding strategy indicates that when a subclass overrides a parent class method, the subclass can inherit the annotation of the parent class method; If the first relationship is that the subclass implements the parent class abstract method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation, and the fourth setting strategy is the METHOD_ABSTRACT method abstract strategy, and the METHOD_ABSTRACT method abstract strategy indicates that when the subclass implements the parent class abstract method, the subclass can inherit the annotation of the parent class method; If the first relationship is that the subclass implements the interface method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero; if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; if the difference is less than zero, determine that the target class does not inherit the target annotation.
4. An annotation inheritance device, characterized in that: include: A parent class acquisition module is used to detect whether a target class has a target annotation, and if the target class is not detected to have the target annotation, acquire the parent class of the target class; Increasing a set count value by one, wherein an initial value of the set count value is zero; An annotation inheritance determination module is used to determine whether the parent class has the target annotation; if the parent class has the target annotation, determine whether the target class inherits the target annotation according to the set count value and the first relationship and the first strategy between the target class and the parent class, the first relationship being any one of the following: implementing an interface, implementing an abstract class, and when the target class is a target method, the first relationship is any one of the following: the subclass overrides the parent class method, the subclass implements the parent class abstract method, and the subclass implements the interface method; the annotation inheritance determination module is specifically used to determine whether the target class inherits the target annotation according to the difference between the inheritance depth value of the target annotation and the set count value, the first relationship, and the first strategy; The annotation inheritance determination module is also specifically used for: if the first relationship is to implement an interface and the first strategy is a first setting strategy, obtaining the inheritance depth value of the target annotation; judging whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, if the difference is greater than or equal to zero, determining that the target class inherits the target annotation, if the difference is less than zero, determining that the target class does not inherit the target annotation; the first setting strategy is a TYPE_INTERFACE interface strategy, and the TYPE_INTERFACE interface strategy indicates that when a single interface is implemented, the subclass of the interface can inherit the annotation of the interface; If the first relationship is to implement an abstract class and the first strategy is the second setting strategy, obtain the inheritance depth value of the target annotation; determine whether the difference between the inheritance depth value and the set count value is greater than or equal to zero, if the difference is greater than or equal to zero, determine that the target class inherits the target annotation, if the difference is less than zero, determine that the target class does not inherit the target annotation, and the second setting strategy is the TYPE_ABSTRACT abstract class strategy, and the TYPE_ABSTRACT abstract class strategy indicates that when a single abstract class is implemented, the subclass of the abstract class can inherit the annotation of the abstract class.
5. The annotation inheritance device according to claim 4, characterized in that: The annotation inheritance determination module is also used for: If the target annotation does not exist in the parent class, the parent class is determined as the current class, and the set count value is increased by one; Determine whether the parent class of the current class is a super parent class; If so, determining that the target class does not inherit the target annotation; Otherwise, when the target annotation exists in the parent class of the current class, determining the relationship and strategy between the current class and the parent class of the current class; Determine whether the target class can inherit the target annotation according to the consistency of all relationships and all strategies between the target class and the parent class of the current class, and the difference between the inheritance depth value and the set count value; If the parent class of the current class does not have a target annotation, the parent class of the current class is determined as the current class, the set count value is increased by one, and the step of determining whether the parent class of the current class is a super parent class is executed again.
6. The annotation inheritance device according to claim 5, characterized in that: If the target class is a target method, the annotation inheritance determination module is used to: If the first relationship is that the subclass overrides the parent class method and the first strategy is the third setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, and if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; If the difference is less than zero, it is determined that the target class does not inherit the target annotation, and the third setting strategy is the METHOD_OVERRIDE method overriding strategy, which indicates that when a subclass overrides a method of a parent class, the subclass can inherit the annotation of the method of the parent class; If the first relationship is that the subclass implements the parent class abstract method and the first strategy is the fourth setting strategy, obtain the inheritance depth value of the target class; determine whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, and if the difference is greater than or equal to zero, determine that the target class inherits the target annotation; If the difference is less than zero, it is determined that the target class does not inherit the target annotation, and the fourth setting strategy is the METHOD_ABSTRACT method abstract strategy, which indicates that when a subclass implements a parent class abstract method, the subclass can inherit the annotation of the parent class method; If the first relationship is that the subclass implements the interface method and the first strategy is the fourth setting strategy, obtaining the inheritance depth value of the target class; determining whether the difference between the inheritance depth value and the setting count value is greater than or equal to zero, and if the difference is greater than or equal to zero, determining that the target class inherits the target annotation; If the difference is less than zero, it is determined that the target class does not inherit the target annotation.
7. An electronic device, characterized in that: The method comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the method according to any one of claims 1 to 3 when executing the program.
8. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the program is executed by a processor, the steps of the method described in any one of claims 1 to 3 are implemented.