A method and device for conversion and assignment between objects

By adding custom annotations to the fields of the source object, the problems of large amount of conversion and assignment codes between objects in the prior art, high coupling degree and inconsistency are solved, and a more convenient and general conversion and assignment effect between objects are achieved.

CN110764770BActive Publication Date: 2025-05-16BEIJING JINGDONG SHANGKE INFORMATION TECH CO LTD +1
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN201810841842.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2018-07-27
Publication Date
2025-05-16
Estimated Expiration
2038-07-27

AI Technical Summary

Technical Problem

The prior art has large code volume, high coupling degree, and is not universal enough when converting and assignment between objects.

Method used

By adding a custom annotation to the field of the source object, assign the field value of the source object to the field of the target object according to the annotation. Annotation classes can customize parameter types, parameter names and parameter default values, and support different types of field conversions, such as collections, classes, dates_strings, enums, etc.

Benefits of technology

It realizes that the conversion and assignment between objects is more convenient, greatly improves the universality of conversion and assignment, reduces the amount of code and reduces the coupling degree.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN110764770B_ABST
    Figure CN110764770B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for object conversion and assignment, and relates to the field of computer technology. A specific implementation of the method includes: adding annotations to the fields of a source object according to a custom annotation class; and assigning the values ​​of the fields of the source object to the fields of a target object according to the annotations. This implementation overcomes the technical problems of the prior art in object conversion and assignment, such as large code size, high coupling, and lack of universality, and achieves a technical effect of more convenient object conversion and assignment and greatly improving the universality of conversion and assignment.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for conversion and assignment between objects. Background Art

[0002] Conversion and assignment between objects involves setting fields in a source object to fields in a target object. However, the field settings between the source and target objects may be the same or different. Common methods for conversion and assignment between objects include hard coding and calling third-party tool classes.

[0003] In the process of implementing the present invention, the inventors discovered that the prior art has at least the following problems:

[0004] 1. Although the hard coding method is simple, the amount of code is large;

[0005] 2. The third-party tool class method requires that the name and field nesting level between the source object and the target object must be the same, which is not universal enough. Summary of the Invention

[0006] In view of this, an embodiment of the present invention provides a method for conversion and assignment between objects, which can overcome the technical problems of the existing technology in conversion and assignment between objects, such as large amount of code, high coupling, and lack of universality, and achieve the technical effect of making conversion and assignment between objects more convenient and greatly improving the universality of conversion and assignment.

[0007] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for conversion and assignment between objects is provided, comprising:

[0008] Add annotations to the fields of the source object according to the custom annotation class;

[0009] According to the annotation, the value of the field of the source object is assigned to the field of the target object.

[0010] Optionally, the custom annotation class includes:

[0011] Customize parameter type and parameter name;

[0012] Or, customize the parameter type, parameter name, and parameter default value.

[0013] Optionally, the parameter name of the annotation class includes at least: attribute name, attribute type;

[0014] The parameter name of the annotation includes: the attribute name and attribute type called from the custom annotation class;

[0015] The value of the attribute name is used to indicate the field name of the target object; the value of the attribute type is used to indicate the field type of the target object.

[0016] Optionally, the value of the attribute name is determined based on a field name of a target object or a field name of a source object.

[0017] Optionally, a method for determining the value of the attribute type includes:

[0018] When the field type of the target object is a collection, the value of the attribute type in the annotation is determined according to the field type of each element in the collection;

[0019] When the field type of the target object is a class, the path and name of the target object are indicated by the value of the attribute class of the annotation class; the value of the attribute type in the annotation is determined according to the value of the attribute class of the annotation class;

[0020] When the field type of the target object is date_string, the value of the attribute type in the annotation is determined by using the value of the combined type of the annotation class and the format string;

[0021] When the field type of the target object is enumeration, the value of the attribute type in the annotation is determined according to the enumeration conversion method;

[0022] When the field type of the target object is a subclass with nested layers, the value of the attribute type in the annotation is determined according to the field type of each nested layer;

[0023] When the field type of the target object is customized, the value of the attribute type in the annotation is determined according to the customized method.

[0024] Optionally, assigning the value of the field of the source object to the field of the target object according to the annotation includes:

[0025] Get the annotations for completing the configuration in the fields of the source object;

[0026] According to the value of the attribute type in the annotation, the value of the field of the source object is assigned to the field of the target object.

[0027] Optionally, assigning the value of the field of the source object to the field of the target object according to the value of the attribute type in the annotation includes:

[0028] When the value of the attribute type in the annotation is "basic", the value of the attribute name in the annotation is obtained; according to the value of the attribute name combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0029] When the attribute type in the annotation is "collection", the value of the source object's field is assigned to the target object's field based on the value of each element's attribute type in the collection and the reflection mechanism;

[0030] When the value of the attribute type in the annotation is "class", the value of the attribute class in the annotation is obtained; according to the value of the attribute class, a class object is created; according to the class object combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0031] When the value of the attribute type in the annotation is "date_string type", the value of the composite type and the value of the formatted string in the annotation are obtained; based on the value of the composite type and the value of the formatted string combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0032] When the value of the attribute type in the annotation is "enumeration", the value of the field of the source object is assigned to the field of the target object according to the attribute name in the annotation and the enumeration conversion method combined with the reflection mechanism;

[0033] When the value of the attribute type in the annotation is a "subclass" with a nested layer, the value of the field of the source object is assigned to the field of the target object by using the iterative call conversion method combined with the reflection mechanism;

[0034] When the value of the attribute type in the annotation is "custom", the value of the field of the source object is assigned to the field of the target object according to the custom method combined with the reflection mechanism.

[0035] According to another aspect of an embodiment of the present invention, a device for converting and assigning values ​​between objects is provided, comprising:

[0036] Field correspondence matching module, used to add annotations to the fields of source objects according to custom annotation classes;

[0037] A general relation conversion module is used to assign the value of the field of the source object to the field of the target object according to the annotation.

[0038] Optionally, the custom annotation class includes:

[0039] Customize parameter type and parameter name;

[0040] Or, customize the parameter type, parameter name, and parameter default value.

[0041] Optionally, the parameter name of the annotation class includes at least: attribute name, attribute type;

[0042] The parameter name of the annotation includes: the attribute name and attribute type called from the custom annotation class;

[0043] The value of the attribute name is used to indicate the field name of the target object; the value of the attribute type is used to indicate the field type of the target object.

[0044] Optionally, the value of the attribute name is determined based on a field name of a target object or a field name of a source object.

[0045] Optionally, a method for determining the value of the attribute type includes:

[0046] When the field type of the target object is a collection, the value of the attribute type in the annotation is determined according to the field type of each element in the collection;

[0047] When the field type of the target object is a class, the path and name of the target object are indicated by the value of the attribute class of the annotation class; the value of the attribute type in the annotation is determined according to the value of the attribute class of the annotation class;

[0048] When the field type of the target object is date_string, the value of the attribute type in the annotation is determined by using the value of the combined type of the annotation class and the format string;

[0049] When the field type of the target object is enumeration, the value of the attribute type in the annotation is determined according to the enumeration conversion method;

[0050] When the field type of the target object is a subclass with nested layers, the value of the attribute type in the annotation is determined according to the field type of each nested layer;

[0051] When the field type of the target object is customized, the value of the attribute type in the annotation is determined according to the customized method.

[0052] Optionally, assigning the value of the field of the source object to the field of the target object according to the annotation includes:

[0053] Get the annotations for completing the configuration in the fields of the source object;

[0054] According to the value of the attribute type in the annotation, the value of the field of the source object is assigned to the field of the target object.

[0055] Optionally, assigning the value of the field of the source object to the field of the target object according to the value of the attribute type in the annotation includes:

[0056] When the value of the attribute type in the annotation is "basic", the value of the attribute name in the annotation is obtained; according to the value of the attribute name combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0057] When the attribute type in the annotation is "collection", the value of the source object's field is assigned to the target object's field based on the value of each element's attribute type in the collection and the reflection mechanism;

[0058] When the value of the attribute type in the annotation is "class", the value of the attribute class in the annotation is obtained; according to the value of the attribute class, a class object is created; according to the class object combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0059] When the value of the attribute type in the annotation is "date_string type", the value of the composite type and the value of the formatted string in the annotation are obtained; based on the value of the composite type and the value of the formatted string combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0060] When the value of the attribute type in the annotation is "enumeration", the value of the field of the source object is assigned to the field of the target object according to the attribute name in the annotation and the enumeration conversion method combined with the reflection mechanism;

[0061] When the value of the attribute type in the annotation is a "subclass" with a nested layer, the value of the field of the source object is assigned to the field of the target object by using the iterative call conversion method combined with the reflection mechanism;

[0062] When the value of the attribute type in the annotation is "custom", the value of the field of the source object is assigned to the field of the target object according to the custom method combined with the reflection mechanism.

[0063] According to another aspect of an embodiment of the present invention, an electronic device for converting and assigning values ​​between objects is provided, including:

[0064] one or more processors;

[0065] a storage device for storing one or more programs,

[0066] When the one or more programs are executed by the one or more processors, the one or more processors implement the method for conversion and assignment between objects of the present invention.

[0067] According to another aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored. When the program is executed by a processor, the method for converting and assigning values ​​between objects of the present invention is implemented.

[0068] One embodiment of the above invention has the following advantages or beneficial effects: because annotations are added to the fields of the source object to assist in the conversion and assignment between objects, the technical problems of the existing technology in the conversion and assignment between objects, such as the large amount of code, high coupling, and lack of universality, are overcome, thereby achieving the technical effect of making the conversion and assignment between objects more convenient and greatly improving the universality of the conversion and assignment.

[0069] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0070] The accompanying drawings are provided for a better understanding of the present invention and are not intended to limit the present invention.

[0071] Figure 1 is a schematic diagram of the main process of a method for conversion and assignment between objects according to an embodiment of the present invention;

[0072] Figure 2 is a flowchart with annotations in a method for conversion and assignment between objects according to an embodiment of the present invention;

[0073] Figure 3 This is a flowchart of assigning the value of a field of a source object to a field of a target object in a method for inter-object conversion and assignment according to an embodiment of the present invention;

[0074] Figure 4 is a schematic diagram of main modules of an apparatus for inter-object conversion and assignment according to an embodiment of the present invention;

[0075] Figure 5 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;

[0076] Figure 6 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0077] The following description of exemplary embodiments of the present invention is made in conjunction with the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding. These details should be considered as merely exemplary. Therefore, it should be appreciated by those skilled in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.

[0078] Figure 1 FIG. 1 is a schematic diagram of the main flow of the method for conversion and assignment between objects according to an embodiment of the present invention. Figure 1 As shown, including:

[0079] Step S101: Add annotations to the fields of the source object according to the custom annotation class;

[0080] Step S102: assign the value of the field of the source object to the field of the target object according to the annotation.

[0081] The purpose of the present invention is to solve the technical defects that the conversion and assignment between objects are complicated and have poor versatility due to the possible differences in field names, numbers, and field nesting levels between the fields of the source object and the target object.

[0082] Annotations are added to source objects to match fields in the source object with those in the target object. By setting annotations on the source object, you can determine the corresponding target object and other information about the target object. By assigning values ​​to the target object's fields based on the annotations, you can avoid the problem of different field names and types between the source and target objects.

[0083] The present invention assigns the value of the source object's field to the target object's field after writing the target object and its information in the annotation, thereby overcoming the technical problems of the existing technology in realizing conversion and assignment between objects, such as large amount of code, high coupling, and lack of universality, thereby achieving the technical effect of greatly reducing the amount of code, making conversion and assignment between objects more convenient, and significantly improving the universality of conversion and assignment.

[0084] If annotations are added to the fields of the source object, annotation classes can be predefined. The annotation classes of the present invention are self-defined according to actual needs, thereby facilitating the conversion and assignment between objects and providing more appropriate annotation classes for annotations.

[0085] Optionally, the custom annotation class includes:

[0086] Customize parameter type and parameter name;

[0087] Or, customize the parameter type, parameter name, and parameter default value.

[0088] Defining parameter types and names in a custom annotation class allows the source object to know that each field in the target object is stored as a parameter type and name, making it easier to assign values ​​to the target object's fields. Defining parameter types in the annotation class prevents assignment errors when assigning values ​​from fields in the source object to fields in the target object due to different field types between objects. Optionally, you can add default parameter values. Parameter types include integers, strings, floating-point numbers, and other types, without limitation.

[0089] Optionally, the parameter name of the annotation class includes at least: attribute name, attribute type;

[0090] The parameter name of the annotation includes: the attribute name and attribute type called from the custom annotation class;

[0091] The value of the attribute name is used to indicate the field name of the target object; the value of the attribute type is used to indicate the field type of the target object.

[0092] The value of the attribute name (attrName) is the field name of the target object;

[0093] The value of the attribute type (attrType) is the field type of the target object, wherein the value of the attribute type includes but is not limited to: collection (LIST), class (CLASS), date_string (DATE_STRING), enumeration (ENUM), subclass (SUBCLASS), and custom (CUSTOM).

[0094] Optionally, the parameter name may also include: sub-attribute type, attribute class, combination type, format string

[0095] The sub-attribute type (eleAttrType) is used when the value of the attribute name is a collection (that is, the field types of the target object are nested). The attribute type of each element in the collection is called a sub-attribute type.

[0096] The value of the attribute class (attrClass) is the class path corresponding to the target object field type;

[0097] The combination type (group) represents that the field type of the corresponding target object is generated by combining multiple fields, and the value of the combination type is the field name of the source object;

[0098] The formatting string is format, which is used to set the fields of the target object to a preset format.

[0099] By defining these common parameter names in the annotation class, it is possible to easily determine field names and types during object conversion and assignment. Taking into account that the field type of the target object may differ from the field type of the source object, the present invention defines the parameter names required to determine the field type of the target object in the annotation class, thereby overcoming the technical shortcomings of the prior art in handling different types of field conversion and assignment, such as large code size and strong limitations.

[0100] When using annotations, you need to call the annotation class to facilitate annotation. Since object conversion and assignment require finding the target object's fields, you need to add attribute names to the annotation to achieve this. Field formats may differ between objects, so specifying the target object's attribute type in the annotation facilitates conversion and assignment.

[0101] Optionally, the value of the attribute name is determined based on a field name of a target object or a field name of a source object.

[0102] Adding an attribute name to the annotation specifies the field name of the target object. If the attribute name does not specify the field name of the target object, the field name of the source object is used as the attribute name.

[0103] Optionally, a method for determining the value of the attribute type includes:

[0104] When the field type of the target object is a collection, the value of the attribute type in the annotation is determined according to the field type of each element in the collection;

[0105] When the field type of the target object is a class, the path and name of the target object are indicated by the value of the attribute class of the annotation class; the value of the attribute type in the annotation is determined according to the value of the attribute class of the annotation class;

[0106] When the field type of the target object is date_string, the value of the attribute type in the annotation is determined by using the value of the combined type of the annotation class and the format string;

[0107] When the field type of the target object is enumeration, the value of the attribute type in the annotation is determined according to the enumeration conversion method;

[0108] When the field type of the target object is a subclass with nested layers, the value of the attribute type in the annotation is determined according to the field type of each nested layer;

[0109] When the field type of the target object is customized, the value of the attribute type in the annotation is determined according to the customized method.

[0110] Since the field types of the target object may be different, it is necessary to configure the attribute type according to the corresponding field type when configuring the annotation, so as to facilitate the subsequent conversion and assignment of the value of the source object's field to the field of the target object.

[0111] Optionally, when configuring the attribute type of an element in a collection according to the sub-attribute type (eleAttrType), if the sub-attribute type is basic (BASIC), no other configuration is required; if the sub-attribute type is class (CLASS), set the attribute class (attrType) to configure the path and name of the target object corresponding to the element in the collection; according to the path and name, iteratively configure the attribute type in the annotation.

[0112] Figure 2This is a flowchart of adding annotations in the method of object conversion and assignment according to an embodiment of the present invention. The following describes the process of adding annotations in detail with a specific embodiment. First, determine that the annotation is @FieldConvertAnnot; then, Figure 2 As shown, the attribute name of the target object is set first, and then the attribute type in the annotation class is set according to the field type of the target object. When setting the attribute type, the present invention needs to set different attribute types according to the different field types of the target object.

[0113] Taking the field type of the target object as a set as an example, the steps of adding annotations are explained in detail. Since the field type of the target object is a set, it means that there are multiple elements in the character type of the target object. The sub-attribute type can be used to determine the attribute type of each element in the set. If the value of the sub-attribute type is basic (BASIC), no other configuration is required. Because "BASIC" is a well-known type, the field of the source object can be easily set to the field of the target object according to the value of the sub-attribute type. Unlike the value of the sub-attribute type as "basic", when the field type of the target object is "class", the path and name of the target object need to be set in the annotation. The present invention defines the processing method of the above-mentioned set in the annotation, which solves the technical defect of the prior art that the attribute types between objects need to be the same when using third-party tools for conversion and assignment, and greatly reduces the amount of code.

[0114] When the target object's field type is Date_String, you need to set the group type in the annotation to indicate the target object's field generated by combining multiple source object fields. Then, set a formatting string for the target field. This formatting string ensures that the target object's date format matches the source object's format during conversion and assignment.

[0115] When the value of the attribute type of the target object is an enumeration, the attribute type in the annotation can be configured using the "field name of the source object + ToENUM" method.

[0116] When the target object's field type is a subclass or custom field, the annotation is also set according to the above method, which will not be repeated here. It is worth noting that because the target object's fields may be nested or in the form of collections, this method needs to configure each element or nested layer of the target object when determining the attribute type.

[0117] Optionally, assigning the value of the field of the source object to the field of the target object according to the annotation includes:

[0118] Get the annotations for completing the configuration in the fields of the source object;

[0119] According to the value of the attribute type in the annotation, the value of the field of the source object is assigned to the field of the target object.

[0120] Alternatively, using reflection, you can transform and assign the value of a source object's field to a target object's field through annotations. Because the target object's attribute types defined in the annotations are different, the assignment steps during reflection are determined based on the value of the annotation's attribute type.

[0121] Figure 3 The present invention is a flowchart of assigning the value of a field of a source object to a field of a target object in a method for conversion and assignment between objects according to an embodiment of the present invention.

[0122] Specifically, first obtain all the fields declared by the source object; loop through each field one by one so that each field enters the conversion and assignment operation; then obtain the annotation of the field of the source object; and finally, determine the specific operation steps based on the value of the attribute type in the annotation.

[0123] Since annotations are determined according to certain steps, when using annotations to convert and assign the values ​​of the fields of the source object to the fields of the target object, the different attribute type values ​​need to be reflected using corresponding steps.

[0124] Optionally, assigning the value of the field of the source object to the field of the target object according to the value of the attribute type in the annotation includes:

[0125] When the value of the attribute type in the annotation is "basic", the value of the attribute name in the annotation is obtained; according to the value of the attribute name combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0126] When the attribute type in the annotation is "collection", the value of the source object's field is assigned to the target object's field based on the value of each element's attribute type in the collection and the reflection mechanism;

[0127] When the value of the attribute type in the annotation is "class", the value of the attribute class in the annotation is obtained; according to the value of the attribute class, a class object is created; according to the class object combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0128] When the value of the attribute type in the annotation is "date_string type", the value of the composite type and the value of the formatted string in the annotation are obtained; based on the value of the composite type and the value of the formatted string combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0129] When the value of the attribute type in the annotation is "enumeration", the value of the field of the source object is assigned to the field of the target object according to the attribute name in the annotation and the enumeration conversion method combined with the reflection mechanism;

[0130] When the value of the attribute type in the annotation is a "subclass" with a nested layer, the value of the field of the source object is assigned to the field of the target object by using the iterative call conversion method combined with the reflection mechanism;

[0131] When the value of the attribute type in the annotation is "custom", the value of the field of the source object is assigned to the field of the target object according to the custom method combined with the reflection mechanism.

[0132] Optionally, set the source object's fields to the target object's fields based on the annotation's sub-property type, including:

[0133] If the value of the sub-attribute type is BASIC, the field of the target object is obtained according to the attribute name in the annotation;

[0134] If the value of the element attribute type is class (CLASS), create a collection target collection type in the field of the target object; obtain the attribute class (attrClazz) in the annotation, create a class object (targetObj) based on the attribute class and add it to the object collection (targetList); set the field of the target object according to the elements of the source object field collection and the target object.

[0135] According to another aspect of an embodiment of the present invention, a device 400 for converting and assigning values ​​between objects is provided, comprising:

[0136] Module 401, a field correspondence matching module, is used to add annotations to the fields of the source object according to the custom annotation class;

[0137] Module 402, a general relationship conversion module, is used to assign the value of the field of the source object to the field of the target object according to the annotation.

[0138] Optionally, the custom annotation class includes:

[0139] Customize parameter type and parameter name;

[0140] Or, customize the parameter type, parameter name, and parameter default value.

[0141] Optionally, the parameter name of the annotation class includes at least: attribute name, attribute type;

[0142] The parameter name of the annotation includes: the attribute name and attribute type called from the custom annotation class;

[0143] The value of the attribute name is used to indicate the field name of the target object; the value of the attribute type is used to indicate the field type of the target object.

[0144] Optionally, the value of the attribute name is determined based on a field name of a target object or a field name of a source object.

[0145] Optionally, a method for determining the value of the attribute type includes:

[0146] When the field type of the target object is a collection, the value of the attribute type in the annotation is determined according to the field type of each element in the collection;

[0147] When the field type of the target object is a class, the path and name of the target object are indicated by the value of the attribute class of the annotation class; the value of the attribute type in the annotation is determined according to the value of the attribute class of the annotation class;

[0148] When the field type of the target object is date_string, the value of the attribute type in the annotation is determined by using the value of the combined type of the annotation class and the format string;

[0149] When the field type of the target object is enumeration, the value of the attribute type in the annotation is determined according to the enumeration conversion method;

[0150] When the field type of the target object is a subclass with nested layers, the value of the attribute type in the annotation is determined according to the field type of each nested layer;

[0151] When the field type of the target object is customized, the value of the attribute type in the annotation is determined according to the customized method.

[0152] Optionally, assigning the value of the field of the source object to the field of the target object according to the annotation includes:

[0153] Get the annotations for completing the configuration in the fields of the source object;

[0154] According to the value of the attribute type in the annotation, the value of the field of the source object is assigned to the field of the target object.

[0155] Optionally, assigning the value of the field of the source object to the field of the target object according to the value of the attribute type in the annotation includes:

[0156] When the value of the attribute type in the annotation is "basic", the value of the attribute name in the annotation is obtained; according to the value of the attribute name combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0157] When the attribute type in the annotation is "collection", the value of the source object's field is assigned to the target object's field based on the value of each element's attribute type in the collection and the reflection mechanism;

[0158] When the value of the attribute type in the annotation is "class", the value of the attribute class in the annotation is obtained; according to the value of the attribute class, a class object is created; according to the class object combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0159] When the value of the attribute type in the annotation is "date_string type", the value of the composite type and the value of the formatted string in the annotation are obtained; based on the value of the composite type and the value of the formatted string combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object;

[0160] When the value of the attribute type in the annotation is "enumeration", the value of the field of the source object is assigned to the field of the target object according to the attribute name in the annotation and the enumeration conversion method combined with the reflection mechanism;

[0161] When the value of the attribute type in the annotation is a "subclass" with a nested layer, the value of the field of the source object is assigned to the field of the target object by using the iterative call conversion method combined with the reflection mechanism;

[0162] When the value of the attribute type in the annotation is "custom", the value of the field of the source object is assigned to the field of the target object according to the custom method combined with the reflection mechanism.

[0163] Figure 5 An exemplary system architecture 500 is shown to which the method or apparatus for inter-object conversion and assignment according to an embodiment of the present invention may be applied.

[0164] like Figure 5 As shown, system architecture 500 may include terminal devices 501, 502, 503, a network 504, and a server 505. Network 504 is used to provide a medium for communication links between terminal devices 501, 502, 503 and server 505. Network 504 may include various connection types, such as wired or wireless communication links or fiber optic cables.

[0165] Users can interact with server 505 via network 504 using terminal devices 501, 502, and 503 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 501, 502, and 503, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).

[0166] The terminal devices 501 , 502 , and 503 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.

[0167] Server 505 may be a server that provides various services, such as a backend management server (for example only) that supports shopping websites browsed by users using terminal devices 501, 502, and 503. The backend management server may analyze and process received data such as product information query requests, and feed back processing results (for example, target push information and product information—for example only) to the terminal device.

[0168] It should be noted that the method for inter-object conversion and assignment provided by the embodiment of the present invention is generally executed by the server 505 , and accordingly, the apparatus for inter-object conversion and assignment is generally provided in the server 505 .

[0169] It should be understood that Figure 5 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.

[0170] Reference below Figure 6 , which shows a schematic structural diagram of a computer system 600 of a terminal device suitable for implementing an embodiment of the present invention. Figure 6 The terminal device shown is only an example and should not bring any limitation to the functions and scope of the embodiments of the present invention.

[0171] like Figure 6 As shown, the computer system 600 includes a central processing unit (CPU) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage unit 608 into a random access memory (RAM) 603. Various programs and data required for the operation of the system 600 are also stored in the RAM 603. The CPU 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0172] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, and the like; an output section 607 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage section 608 including a hard disk; and a communication section 609 including a network interface card such as a LAN card or a modem. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, or a semiconductor memory, is installed in the drive 610 as needed, so that computer programs read therefrom can be installed into the storage section 608 as needed.

[0173] In particular, according to the embodiments disclosed in the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program comprising program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 609, and / or installed from a removable medium 611. When the computer program is executed by the central processing module (CPU) 601, the above-mentioned functions defined in the system of the present invention are performed.

[0174] It should be noted that the computer-readable medium described in the present invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of computer-readable storage media can include, but are not limited to, an electrical connection having one or more conductors, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In the present invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. This propagated data signal can take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. Program code embodied on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wireline, optical fiber cable, RF, or any suitable combination thereof.

[0175] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the above-mentioned module, program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0176] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be provided in a processor. For example, they may be described as follows: a processor includes a sending module, an acquisition module, a determination module, and a first processing module. The names of these modules do not, in some cases, constitute limitations on the modules themselves. For example, the sending module may also be described as a "module for sending a request for obtaining an image to a connected server."

[0177] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiments, or may exist independently without being incorporated into the device. The computer-readable medium carries one or more programs, and when the one or more programs are executed by the device, the device includes:

[0178] Add annotations to the fields of the source object;

[0179] According to the annotation, the value of the field of the source object is assigned to the field of the target object.

[0180] According to the technical solution of the embodiment of the present invention, the following beneficial effects can be achieved:

[0181] The present invention adopts the technical means of adding annotations to the fields of the source object to assist in the conversion and assignment between objects, thereby overcoming the technical problems of the existing technology in the conversion and assignment between objects, such as the large amount of code, high coupling, and lack of universality, thereby achieving the technical effect of making the conversion and assignment between objects more convenient and greatly improving the universality of the conversion and assignment.

[0182] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.

Claims

1. A method for conversion and assignment between objects, characterized in that: include: Annotate the fields of the source object according to the custom annotation class; wherein the custom annotation class includes a custom parameter type and a parameter name; the parameter name of the annotation class includes at least: an attribute name and an attribute type; the parameter name of the annotation includes: an attribute name and an attribute type called from the custom annotation class; the value of the attribute name is used to indicate the field name of the target object; the value of the attribute type is used to indicate the field type of the target object; The value determination method of the attribute type includes: When the field type of the target object is a collection, the value of the attribute type in the annotation is determined according to the field type of each element in the collection; When the field type of the target object is a class, the path and name of the target object are indicated by the value of the attribute class of the annotation class; the value of the attribute type in the annotation is determined according to the value of the attribute class of the annotation class; When the field type of the target object is date_string, the value of the attribute type in the annotation is determined by using the value of the combined type of the annotation class and the format string; When the field type of the target object is an enumeration, the value of the attribute type in the annotation is determined according to the enumeration conversion mode; When the field type of the target object is a subclass with a nested layer, the value of the attribute type in the annotation is determined according to the field type of each nested layer; When the field type of the target object is customized, the value of the attribute type in the annotation is determined according to the customized method; According to the annotation, the value of the field of the source object is assigned to the field of the target object.

2. The method according to claim 1, characterized in that The custom annotation class includes: custom parameter type, parameter name and parameter default value.

3. The method according to claim 1, characterized in that The value of the attribute name may be determined by: determining it according to the field name of the target object, or determining it according to the field name of the source object.

4. The method according to claim 1, characterized in that: According to the annotation, assigning the value of the field of the source object to the field of the target object includes: Get the annotations that complete the configuration in the fields of the source object; According to the value of the attribute type in the annotation, the value of the field of the source object is assigned to the field of the target object.

5. The method according to claim 4, characterized in that Assigning the value of the field of the source object to the field of the target object according to the value of the attribute type in the annotation, including: When the value of the attribute type in the annotation is "basic", the value of the attribute name in the annotation is obtained; according to the value of the attribute name combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object; When the value of the attribute type in the annotation is "collection", the value of the field of the source object is assigned to the field of the target object according to the value of the attribute type of each element in the collection combined with the reflection mechanism; When the value of the attribute type in the annotation is "class", the value of the attribute class in the annotation is obtained; according to the value of the attribute class, a class object is created; according to the class object combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object; When the value of the attribute type in the annotation is "date_string type", the value of the composite type and the value of the formatted string in the annotation are obtained; according to the value of the composite type and the value of the formatted string combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object; When the value of the attribute type in the annotation is "enumeration", the value of the field of the source object is assigned to the field of the target object according to the attribute name in the annotation and the enumeration conversion method combined with the reflection mechanism; When the value of the attribute type in the annotation is "subclass" with a nested layer, the value of the field of the source object is assigned to the field of the target object by using the iterative call conversion method combined with the reflection mechanism; When the value of the attribute type in the annotation is "custom", the value of the field of the source object is assigned to the field of the target object according to the custom method combined with the reflection mechanism.

6. A device for converting and assigning values ​​between objects, characterized in that: include: A field correspondence matching module is used to add annotations to the fields of the source object according to the custom annotation class; wherein the custom annotation class includes a custom parameter type and a parameter name; the parameter name of the annotation class includes at least: an attribute name and an attribute type; the parameter name of the annotation includes: an attribute name and an attribute type called from the custom annotation class; the value of the attribute name is used to indicate the field name of the target object; the value of the attribute type is used to indicate the field type of the target object; the value determination method of the attribute type includes: When the field type of the target object is a collection, the value of the attribute type in the annotation is determined according to the field type of each element in the collection; When the field type of the target object is a class, the path and name of the target object are indicated by the value of the attribute class of the annotation class; the value of the attribute type in the annotation is determined according to the value of the attribute class of the annotation class; When the field type of the target object is date_string, the value of the attribute type in the annotation is determined by using the value of the combined type of the annotation class and the format string; When the field type of the target object is an enumeration, the value of the attribute type in the annotation is determined according to the enumeration conversion mode; When the field type of the target object is a subclass with a nested layer, the value of the attribute type in the annotation is determined according to the field type of each nested layer; When the field type of the target object is customized, the value of the attribute type in the annotation is determined according to the customized method; The general relation conversion module is used to assign the value of the field of the source object to the field of the target object according to the annotation.

7. The device according to claim 6, characterized in that The custom annotation class includes: custom parameter type, parameter name and parameter default value.

8. The device according to claim 6, characterized in that The value of the attribute name may be determined by: determining it according to the field name of the target object, or determining it according to the field name of the source object.

9. The device according to claim 6, characterized in that According to the annotation, assigning the value of the field of the source object to the field of the target object includes: Get the annotations that complete the configuration in the fields of the source object; According to the value of the attribute type in the annotation, the value of the field of the source object is assigned to the field of the target object.

10. The device according to claim 9, characterized in that Assigning the value of the field of the source object to the field of the target object according to the value of the attribute type in the annotation, including: When the value of the attribute type in the annotation is "basic", the value of the attribute name in the annotation is obtained; according to the value of the attribute name combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object; When the value of the attribute type in the annotation is "collection", the value of the field of the source object is assigned to the field of the target object according to the value of the attribute type of each element in the collection combined with the reflection mechanism; When the value of the attribute type in the annotation is "class", the value of the attribute class in the annotation is obtained; according to the value of the attribute class, a class object is created; according to the class object combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object; When the value of the attribute type in the annotation is "date_string type", the value of the composite type and the value of the formatted string in the annotation are obtained; according to the value of the composite type and the value of the formatted string combined with the reflection mechanism, the value of the field of the source object is assigned to the field of the target object; When the value of the attribute type in the annotation is "enumeration", the value of the field of the source object is assigned to the field of the target object according to the attribute name in the annotation and the enumeration conversion method combined with the reflection mechanism; When the value of the attribute type in the annotation is "subclass" with a nested layer, the value of the field of the source object is assigned to the field of the target object by using the iterative call conversion method combined with the reflection mechanism; When the value of the attribute type in the annotation is "custom", the value of the field of the source object is assigned to the field of the target object according to the custom method combined with the reflection mechanism.

11. An electronic device for converting and assigning values ​​between objects, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.

12. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.

Citation Information

Patent Citations

  • Network communication data packet analysis method based on Java language

    CN106302442A