Detailed Description
The following describes in detail the apparatus and method provided by the embodiments of the present invention with reference to the accompanying drawings. While the preferred embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art. The following describes the concepts and embodiments of the present invention using Java programming as an example.
The Java language is now widely used, particularly in Java language version 5.0, to support special syntax metadata added to source code. Classes, methods, variables, parameters, packages, etc. in the Java language can be annotated and custom Java annotations are supported. Unlike Java doc, java labels can obtain label content through reflection. During the compilation of Java source code, annotations may be processed by the annotation processor and generated as messages containing information or as additional Java source code files or resources, while newly added Java source code files may also be compiled and processed and modify the annotated code itself. Furthermore, the Java language provides Java reflection mechanisms for observing and modifying the behavior of programs at runtime. In Java, reflection allows classes, interfaces, fields and methods to be checked at runtime without knowing the name of the interface, fields (i.e. member variables), method during compilation. It also allows calls to instantiate new objects and different methods based on the determination. One or more embodiments of the present invention utilize the annotation interpretation and reflection mechanism of Java to achieve unified programming for multi-database frameworks.
Fig. 1 illustrates a program component 100 according to one embodiment of the invention, where the program component 100 may be a stand-alone SDK component or plug-in that may be embedded in an application APP to invoke functionality preprogrammed by the SDK component, such as access to a database. In accordance with one or more embodiments of the present invention, the configuration device 102 is integrated within the program component 100. When it is desired to integrate the program component 100 into the application program APP, the configuration device 102 may adaptively configure the program and operating mechanism of the program component 100 according to the characteristics of the application program APP. The application APP here may be any application known to run on a smart terminal, which may be a computing device including, for example, a computer, a cell phone, a tablet, etc.
As shown in fig. 1, the program component 100 includes a program 101 and a configuration device 102. The program 101 stores therein an Original data model Class origin_class labeled with a unified annotation for realizing a predetermined function. The unified annotation here may be a custom annotation, such as a custom Java annotation. One example of the raw data model Class origin_class annotated with custom annotations is shown below:
in this example, a database model class Message class is annotated with a unified annotation IAPDatabaseTable, IAPDatabaseField, in which the operational parameters such as methods, attributes, etc. are defined.
The configuration device 102 includes a type determination module 104, an annotation processor 106, a database operation interface 108, and a reflection processor 110. When the SDK component 100 is integrated into the application APP and compiled, the type determination module 104 determines the database Frame type db_frame employed by the application APP that loads the SDK component 100. The database framework type here may be any type known in the art that is common across APPs, such as SQlite, ormLite, greenDAO, etc. As one example, the type determination module 104 may further determine whether the Original data model Class origin_class in the SDK component matches the determined database Frame type db_frame, where the matching is whether the Original data model Class and its unified annotation are supported by the database Frame. For example, when the database Frame db_frame supports the unified annotation or when the database Frame db_frame does not require an annotation at runtime, then the original data model class is considered to match the database Frame type db_frame.
If the Original data model Class origin_class in the program 101 does not match the database Frame type DB_frame, the annotation processor 106 generates a New annotation model Class New_class matching the database Frame type DB_frame based on the Original data model Class origin_class and stores the New annotation model Class New_class into the program 101, where the Original data model Class and the New annotation model Class implement the same predetermined function of the SDK component. In generating the New annotation model Class New_class, according to one example of the invention, the annotation processor 106 scans the unified annotation of the Original data model Class original_class to convert the unified annotation into a specific annotation conforming to the database Frame type DB_frame for annotating the New annotation model Class New_class. At the same time, a new annotation model class is associated with the original data class using an identifier field, e.g., an identifier is added to the class name of the original data class.
By way of example, FIG. 2A illustrates class conversion implemented by annotation processor 106 when application APP employs an ormLite database framework. As shown, in this example, the SDK component 100 is a Message box component for storing messages having an original data model class Message in which the title (title) and content (content) of the Message are defined, and further, the Message is annotated with custom uniform annotations @ IAPDatabaseTable, @ IAPDatabaseField to the databases and method attributes involved in the class. Because the custom unified annotation @ IAPDatabaseTable, @ IAPDatabaseField is not an annotation supported by the database framework ormLite employed by the current application APP, during compilation, the annotation processor 106 scans the Message box assembly with the unified annotation model class Message class, converts the unified annotation @ IAPDatabaseTable, @ IAPDatabaseField to an annotation @ databaseTable, @ databaseField supported by the ormLite, respectively, and creates a new annotation model class GenMessage class whose name consists of the original model class name Message plus the prefix "Gen".
When the application APP runs and wishes to Call the SDK component to implement a predetermined function, the database operation interface 108 receives a call_sdk from the application APP for the Call of the SDK component, and transmits a database operation specific request, such as storage, of the database framework used by the application APP through the component database operation interface 108.
It should be noted here that the SDK component 100 is implemented using the Original model Class original_class when performing a specific database operation, but the actual database storage is implemented by an application APP database framework such as (orm lite). To this end, the reflection processor 110 transmits a class object of a New annotation model class new_class corresponding to the database frame type employed by the application APP to the database frame of the application APP in response to the Call request call_sdk. Taking an application APP that uses the orm lite database framework as an example, as shown in fig. 2B, the reflection processor 110 receives a Call request call_sdk of the application APP by using the database operation interface 108, and may determine that the annotation model class supported by the current APP is an orm lite data model class. Thus, the reflection processor 110 traverses the original data model class to obtain class objects of the model class, which include parameters in the class and parameter values for each parameter that the SDK component generates when performing a particular database operation. Taking the above message box component as an example, through the reflection processing, the reflection processor 110 may obtain a class object (message. Class) of the original data model class and specific values of a message title (title) and a content (content) in the class object, and the reflection processor 110 encapsulates the obtained specific values of parameters into corresponding characteristic parameters title and content in the new annotation model class GenMessage. The reflection processor 110 then transmits the class object genmessage class of the new annotation model class to the orm lite database framework in the application APP to implement the database storing the message.
It should be noted that, the Message Class is only used as an example for illustration, but the Original data model Class origin_class is not limited to one entity Class, and may include other types of classes and a greater number of entity classes, and in operation, the reflection processor 110 may traverse all entity classes and objects in the classes and switch to the New annotation model Class new_class.
Furthermore, it should be noted that the type determining module 104 is not an essential component in the configuration device 102 in this example, for example, the configuration device 102 may directly receive the database Frame type db_frame employed by the application program APP specified by the user from the outside, and further determine whether the Original data model Class original_class in the program component 100 matches the specified database Frame type db_frame.
Fig. 3 shows an example in which the Original data model Class original_class in the SDK component matches the database Frame type db_frame, with a message box component as an example of the SDK component. As shown, the APP here employs the SQLite database framework, which does not require annotations at runtime, so the annotation processor 106 does not need to make any transformations of the unified annotations during the compilation phase. When the APP is operated, each object in the original data model class Message can be directly used through a database operation interface.
Fig. 4 shows a schematic diagram of the operation between the application APP, the SDK component, the annotation processor and the reflection processor in the compiling and executing stage, where the SDK component, the annotation processor and the reflection processor are separately provided for the purpose of illustration.
As shown in FIG. 1.1, the Original data model Class, labeled with a uniform annotation, is pre-programmed in the program 101 of the SDK component. For example, for the Message box component shown in FIG. 2A, the Message class of the original data model class is annotated with custom uniform annotations @ IAPDatabase Table, @ IAPDatabase Field. As shown in figure 1.2, in the compilation phase, when it is desired to integrate an SDK component into an APP, the database framework types db_frames supported by the APP may be transferred to the annotation processor. As shown in FIG. 1.3, the annotation processor generates a corresponding new annotation model class supporting the database Frame DB_frame from the database Frame type DB_frame. As shown in fig. 2A, when the database Frame type db_frame is orm lite, the annotation processor scans the model class Message class with unified annotation in the SDK component, converts the unified annotation @ IAPDatabaseTable, @ IAPDatabaseField therein into a new annotation @ DatabaseTable, @ DatabaseField supporting orm lite, and creates a data model class GenMessage class, the new annotation model class name of which is composed of the model class name Message plus a prefix identifier "Gen". The annotation processor then returns the created data model class GenMessage class to the SDK component, e.g., stored in program 101, at 1.4.
When the APP runs, for example, it wants to call the SDK component to implement a predetermined function, for example, message storage, as shown in fig. 2.1, the APP issues a call request call_sdk to the SDK component through the database operation interface, and the SDK component operates the original data model class, for example, the Message class, to perform a Message storage operation, and the operation result is that, for example, the title and content of the Message are stored in the title and content attributes of the Message class. Meanwhile, as shown in 2.2, the reflection processor searches for a New annotation model class new_class corresponding to the type db_frame from the program 101 based on the database Frame type db_frame of the current APP that issued the call request. For example, as shown in FIG. 2B, when the APP supports an ormLite database framework, the reflection processor may determine that the corresponding new annotation model class is an ormLite. Subsequently, as shown in 2.3, the reflection processor obtains a Class object, e.g., message Class, of the original model Class to generate a Class object, e.g., genMessage Class, of the new annotation model Class. As one example, the process of generating a Class object of a new annotation model Class by a reflection processor is as follows:
(1) And obtaining a class (class) object of the new annotation model class from a mapping relation cache of the class object of the original model class and the class object of the new annotation model class. If the cache does not exist, the class name of the model class is obtained from the class object of the original model class through reflection, an identifier such as Gen is added to form the class name of the new annotation model class, then the class object of the new annotation model class is generated based on the class object of the original model class through reflection, and the mapping relation between the class object of the original model class and the class object of the new annotation model class is stored in the mapping cache.
(2) Obtaining the mapping relation between the member variable names of the original model class and the new annotation model class and the Field (Field) object from the cache, wherein any cache does not exist, and obtaining the mapping relation between the member variable names on the original model class or the new annotation model class and the Field object through reflection and writing the mapping relation into the cache;
(3) Traversing the member variable names of the new annotation model class, obtaining the field object from the mapping relation cache of the member variable names of the original model class and the field object for each member variable name, obtaining the values of the same-name member variables on the original model class object through reflection, and setting the member variable values of the original model class object to the same-name member variables of the new annotation model class object through reflection.
Returning to FIG. 4, after reflection processing, the reflection processor returns a class object GenMessage. Class of the new annotation model class, as shown in FIG. 2.4. Thus, as shown in 2.5 and 2.6, the actual operation on the database is realized based on the class object GenMessage.class of the new annotation model class under the ormLite database framework of the APP, and the APP can return the result of the operation such as message storage.
Fig. 5 shows a flowchart for configuring an SDK component according to one embodiment of the present invention, in the following example, a message box component is taken as an example, and APP uses both the orm lite and SQLite database frameworks to perform database operations, respectively, for convenience of explanation. The message box component encapsulates the raw data model class with a unified annotation markup that is custom in the Java language, e.g.,
as shown in fig. 5, when it is desired to integrate the message box combination in the APP, at step 502, a database Frame type db_frame employed by the application APP that is desired to load the message box component is determined at the APP compilation stage, as described above, wherein determining the Frame type db_frame may be implemented either by user specification or by self-judgment. The process then proceeds to step 504 where it is determined whether the database Frame type DB_frame matches the Original data model Class original_class. If the database Frame DB_frame employed by the current APP does not match the Original data model Class original_class, e.g., the current APP employs the orm Lite Frame, then the process proceeds to step 506.
At step 506, the unified annotation of the Original data model Class original_class is scanned with the annotation processor to convert the unified annotation into a specific annotation conforming to the database Frame type DB_frame of the current APP for annotating the New annotation model Class New_class. At the same time, a new annotation model class is associated with the original data class using an identifier field. As illustrated in the example of FIG. 2A, when the current APP employs the ormLite database framework, the annotation processor 106 scans the Message box component, converting the unified annotation @ IAPDatabase Table, @ IAPDatabase Field to the annotations @ Database Table, @ Database Field supported by the ormLite, respectively, while creating a new annotation model class GenMessage class whose name is composed of the model class name Message prefixed by the prefix "Gen". Furthermore, the compiling process of the whole APP is completed. If it is determined in step 504 that the database framework adopted by the current APP matches the Original data model Class origin_class, for example, when the current APP adopts the SQLite framework, the whole APP compiling process is completed directly without annotating the conversion process.
In step 508, a call request, such as a message store operation, is received from the APP through the database operation interface during the application APP run phase. At step 510, a data model class is determined that matches the database framework of the current application APP based on the call request. As described in the previous example, if the current APP adopts the SQLite framework, the data storage operation can be directly performed based on the Original data model Class original_class, as shown in FIG. 3; and if the current APP employs the orm lite framework, proceed to step 512. As previously described, in the compilation stage, the new annotation model class, the ormLite data model class, has been previously converted in the SDK component, as shown in FIG. 2B, so in step 512, the ormLite new annotation model class corresponding to the ormLite framework employed by the application is transferred to the ormLite database framework in the application APP in response to the call request using the reflection mechanism. For the message box component in this example, through reflection processing, the reflection processor may obtain a class object (message. Class) of the original data model class and specific values of a message title (title) and content (content) in the class object, and the reflection processor encapsulates the obtained specific values of parameters into corresponding feature parameters title and content in a new annotation model class GenMessage. Then, in step 514, the reflection processor transmits the class object genmessage/class of the new annotation model class to the orm lite database framework in the application APP to implement the operation of the database in storing the message.
By utilizing the scheme of the invention, the new annotation model class aiming at different database frameworks (for example SQlite, ormLite) can be automatically generated through the annotation processor, and then the generated new annotation class object is acquired by using a Java reflection mechanism in the actual database operation, so that the actual database operation is carried out under different database frameworks by using the original data model class on the basis of the same set of unified annotation.
According to the invention, the application APP can be installed on any terminal, such as a computer, a mobile phone, etc. Fig. 6 illustrates an intelligent terminal comprising a memory storing an application APP and a processor, wherein the processor can execute the stored application to enable database access. In accordance with one or more embodiments of the present invention, an application APP is programmed to include a plug-in to implement a predetermined function, wherein the plug-in includes: the system comprises a database operation interface and a reflection processor, wherein the database operation interface is used for receiving a call request from an application program to a plugin, and then responding to the call request, the reflection processor transmits a new annotation model class corresponding to a database framework type adopted by an application program APP in the plugin to a database framework used by the application program, wherein the new annotation model class is generated based on an original data model class marked by unified annotation in the plugin so as to realize the function of the plugin. In one example, a reflection processor obtains feature parameters and parameter values thereof in an original data model class, wherein the parameter values in the original data model class are generated by the program component operating the original data model class, and encapsulates the obtained parameter values as parameter values of corresponding feature parameters in the new annotation model class. It should be noted that, in addition to the processor, the memory, and the nonvolatile memory shown in fig. 6, the intelligent terminal implementing the execution of the program component in the embodiment generally may further include other hardware according to its actual functions, which will not be described herein.
It should be noted here that while the invention has been described in connection with the preferred embodiments described above, it is obvious that the invention is not limited thereto. Further, the modules in fig. 1 may include processors, electronics devices, hardware devices, electronics components, logic circuits, memories, software codes, firmware codes, etc., or any combination thereof. Those of skill would further appreciate that the various illustrative logical blocks, modules, and method steps described in connection with the disclosure herein may be implemented as electronic hardware, computer software, or combinations of both. Taking a software implementation as an example, as a device in a logic sense, the device is formed by a processor executing corresponding computer program instructions in a nonvolatile memory to read the corresponding computer program instructions from the memory. Another embodiment of the invention provides a machine-readable medium having stored thereon machine-readable instructions which, when executed by a computer, cause the computer to perform any of the methods disclosed herein. In particular, a system or apparatus may be provided that is equipped with a machine-readable medium on which is stored software program code that implements the functionality of any of the above embodiments, and causes a computer of the system to read and execute machine-readable instructions stored in the machine-readable medium. In this case, the program code itself, read from a machine-readable medium, may implement the functions of any of the above-described embodiments, and thus the machine-readable code and the machine-readable medium storing the machine-readable code form part of the present invention.
It should be noted that not all steps or modules in the above-described flow and structure of the apparatus are necessary, and some steps or modules may be omitted according to actual needs. The execution sequence of the steps is not fixed and can be adjusted as required. The system structure described in the above embodiments may be a physical structure or a logical structure, that is, some modules may be implemented by the same physical entity, or some modules may be implemented by multiple physical entities, or may be implemented jointly by some components in multiple independent devices.
While the invention has been illustrated and described in detail in the drawings and in the preferred embodiments, the invention is not limited to the disclosed embodiments, and it will be appreciated by those skilled in the art that the code audits of the various embodiments described above may be combined to produce further embodiments of the invention, which are also within the scope of the invention.