CN108376066B - Code processing device and code processing method - Google Patents
Code processing device and code processing method Download PDFInfo
- Publication number
- CN108376066B CN108376066B CN201611051637.9A CN201611051637A CN108376066B CN 108376066 B CN108376066 B CN 108376066B CN 201611051637 A CN201611051637 A CN 201611051637A CN 108376066 B CN108376066 B CN 108376066B
- Authority
- CN
- China
- Prior art keywords
- code
- layer
- class
- java
- calling
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000012545 processing Methods 0.000 title claims abstract description 32
- 238000003672 processing method Methods 0.000 title claims abstract description 17
- 238000000034 method Methods 0.000 claims abstract description 231
- 230000004044 response Effects 0.000 claims abstract description 18
- 230000003068 static effect Effects 0.000 claims description 29
- 238000011423 initialization method Methods 0.000 claims description 27
- 238000012423 maintenance Methods 0.000 abstract description 8
- 230000006870 function Effects 0.000 description 46
- 238000010586 diagram Methods 0.000 description 12
- 238000011161 development Methods 0.000 description 8
- 238000004891 communication Methods 0.000 description 6
- 230000000694 effects Effects 0.000 description 5
- 238000005516 engineering process Methods 0.000 description 4
- 230000010365 information processing Effects 0.000 description 3
- 230000003287 optical effect Effects 0.000 description 3
- 230000008569 process Effects 0.000 description 3
- 230000008878 coupling Effects 0.000 description 2
- 238000010168 coupling process Methods 0.000 description 2
- 238000005859 coupling reaction Methods 0.000 description 2
- 230000014509 gene expression Effects 0.000 description 2
- 239000003999 initiator Substances 0.000 description 2
- 238000006467 substitution reaction Methods 0.000 description 2
- 108091029480 NONCODE Proteins 0.000 description 1
- 230000002159 abnormal effect Effects 0.000 description 1
- 230000001133 acceleration Effects 0.000 description 1
- 230000006399 behavior Effects 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000008901 benefit Effects 0.000 description 1
- 230000002457 bidirectional effect Effects 0.000 description 1
- 230000010267 cellular communication Effects 0.000 description 1
- 230000006835 compression Effects 0.000 description 1
- 238000007906 compression Methods 0.000 description 1
- 239000012141 concentrate Substances 0.000 description 1
- 238000009795 derivation Methods 0.000 description 1
- 238000004806 packaging method and process Methods 0.000 description 1
- 238000009877 rendering Methods 0.000 description 1
- 239000011800 void material Substances 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/36—Software reuse
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Devices For Executing Special Programs (AREA)
Abstract
The invention discloses a code processing device and a code processing method; the device comprises: a first code layer; a second code layer; an intermediate layer coupled with the first code layer and the second code layer; a first code layer implemented based on a first code running in a host environment of the virtual machine, including an application installed in the code processing apparatus, for calling a second code method formed by a second code to the intermediate layer; the intermediate layer is realized based on the second code, is used for connecting the first code layer and the second code layer, responds to the call aiming at the second code method and calls the second code method to the second code layer; a second code layer, implemented based on a second code capable of being compiled running directly on a specific processor, for invoking to the intermediate layer a second code method formed by the second code; the first code method is executed in response to a call by the middle tier to the second code method. The method and the device can simplify the calling logic of the method realized by calling the code and reduce the code maintenance difficulty.
Description
Technical Field
The present invention relates to computer technologies, and in particular, to a code processing apparatus and a code processing method.
Background
The internet, especially the mobile internet, is rapidly developed, terminals such as smart phones and tablet computers are widely used as an entrance of the internet to access the internet, the terminals provide various applications for users to select, install and use, and computer languages used by codes of the applications are often different from computer languages used by codes of an operating system of the terminals. Taking a terminal of an Android (Android) system as an example, the application installed in the Android system is realized based on Java codes, and the Android system is realized based on C/C + + codes.
As the Java code runs in the host environment based on the Java virtual machine and can easily run in the terminals of different processors, developers can pay attention to the development of the functions of the application by ignoring the difference caused by the processor platform of the terminal, and the development efficiency of the application is improved.
When an application is developed using Java code, all development requirements cannot be met, and in order to improve the development efficiency of the application or implement a function that Java code does not have, a method implemented by calling Native code of the android system (for the android system, the Native code is C/C + + code), also called calling Native (Native) method, is required in Java code of the application. The Java platform provides a Java Native Interface (JNI) to support invoking Native methods in the Java code of an application.
Meanwhile, in order to meet the requirement of realizing the functions of some Java methods in the local code, a requirement of calling the Java methods also exists in the local code, and for this reason, the Java local interface also supports the reverse calling of the Java methods in the local code of the android system, so that the intercommunication between the Java code and the local code is realized.
However, in order to realize the intercommunication between the Java code and the native code, the native code is required to respond to the call from the Java code and the native call from the android system, so that the call logic of the code is complicated, and the code maintenance is difficult.
In summary, for how to simplify the call logic between different codes and reduce the code maintenance difficulty when two types of codes exist in the terminal, an effective solution is not available in the related art.
Disclosure of Invention
Embodiments of the present invention provide a code processing apparatus and a code processing method, which can simplify a call logic of a method implemented by calling a code, and reduce code maintenance difficulty.
The technical scheme of the embodiment of the invention is realized as follows:
in a first aspect, an embodiment of the present invention provides a code processing apparatus:
a first code layer; a second code layer; an intermediate layer coupled with the first code layer and coupled with the second code layer; wherein,
the first code layer is realized based on first codes running in a host environment of the virtual machine, comprises an application installed in the code processing device and is used for calling a second code method formed by second codes to the middle layer;
the middle layer is realized based on the second code and is used for connecting the first code layer and the second code layer and responding to the calling of the second code method to call the second code method to the second code layer;
the second code layer is implemented based on the second code capable of being compiled and run directly on a specific processor and is used for calling the second code method formed by the second code to the middle layer; and, in response to the invocation of the second code method by the intermediary layer, executing the second code method.
In a second aspect, an embodiment of the present invention provides a code processing method, including:
the middle layer is coupled with the first code layer and coupled with the second code layer;
wherein the first code layer is implemented based on first code running in a host environment of a virtual machine, including an installed application; the second code layer is implemented based on a second code capable of being compiled running directly on a particular processor; the middle layer is implemented based on the second code;
the first code layer calls a second code method formed by the second code to a middle layer;
said intermediate layer connecting said first code layer and said second code layer, calling said second code method to said second code layer in response to a call for said second code method;
the second code layer calling the second code method formed by the second code to the middle layer; and, in response to the invocation of the second code method by the intermediary layer, executing the second code method.
In a third aspect, an embodiment of the present invention provides a code processing apparatus, including a memory and a processor; the memory has stored therein executable instructions for causing the processor to:
the middle layer is coupled with the first code layer and coupled with the second code layer;
wherein the first code layer is implemented based on first code running in a host environment of a virtual machine, including an installed application; the second code layer is implemented based on a second code capable of being compiled running directly on a particular processor; the middle layer is implemented based on the second code;
the first code layer calls a second code method formed by the second code to a middle layer;
said intermediate layer connecting said first code layer and said second code layer, calling said second code method to said second code layer in response to a call for said second code method;
the second code layer calling the second code method formed by the second code to the middle layer; and, in response to the invocation of the second code method by the intermediary layer, executing the second code method.
In a fourth aspect, an embodiment of the present invention provides a storage medium, which stores executable instructions for executing the code processing method provided by the embodiment of the present invention.
The embodiment of the invention has the following beneficial effects:
by arranging the intermediate layer in the first code layer and the second code layer, the intermediate layer is utilized to take over the call of the second code method, the second code layer is only used for responding to the call of the second code method from the intermediate layer, and the logic for executing the function for realizing the second code method is not distinguished from the call of the first code layer or the internal call of the second code layer, so that the logic related to the call of the second code method is decoupled from the logic for executing the function for realizing the second code method, the frame structure of the code is simple and clear, the code reusability of the second code method is higher, and the later maintenance and management are convenient.
Drawings
Fig. 1 is an optional structural schematic diagram of a software and hardware architecture of a terminal operating an android system provided in an embodiment of the present invention;
FIG. 2 is a schematic diagram of an abstract representation of the terminal software architecture shown in FIG. 1 according to an embodiment of the present invention;
fig. 3 is an alternative implementation diagram for implementing inter-access between Java code and Native code based on a Java Native interface according to an embodiment of the present invention;
FIG. 4 is an alternative structure diagram of the software structure of the code processing apparatus according to the embodiment of the present invention;
FIG. 5-1 is a schematic diagram of another alternative structure of a code processing apparatus according to an embodiment of the present invention;
FIG. 5-2 is an alternative diagram of a method call between a first code layer, an intermediate layer, and a second code layer provided by an embodiment of the present invention;
FIG. 6 is a schematic flow chart of an alternative code processing method according to an embodiment of the present invention;
fig. 7 is an optional flowchart illustrating that the Java layer calls a C + + method of the C system implementation layer according to an embodiment of the present invention.
Detailed Description
The present invention will be described in further detail below with reference to the accompanying drawings and examples. It should be understood that the examples provided herein are merely illustrative of the present invention and are not intended to limit the present invention. In addition, the following embodiments are provided as some embodiments for implementing the invention, not all embodiments for implementing the invention, and those skilled in the art will not make creative efforts to recombine technical solutions of the following embodiments and other embodiments based on implementing the invention all belong to the protection scope of the invention.
It should be noted that, in the embodiments of the present invention, the terms "comprises", "comprising" or any other variation thereof are intended to cover a non-exclusive inclusion, so that a method or apparatus including a series of elements includes not only the explicitly recited elements but also other elements not explicitly listed or inherent to the method or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other related elements in a method or apparatus that comprises the element (e.g., steps in a method or elements in an apparatus).
For example, the code processing method provided by the embodiment of the present invention includes a series of steps, but the code processing method provided by the embodiment of the present invention is not limited to the described steps, and similarly, the code processing apparatus provided by the embodiment of the present invention includes a series of units, but the code processing apparatus provided by the embodiment of the present invention is not limited to include the explicitly described units, and may also include units that are required to be configured to acquire related information or perform processing based on the information.
Before further detailed description of the present invention, terms and expressions referred to in the embodiments of the present invention are described, and the terms and expressions referred to in the embodiments of the present invention are applicable to the following explanations.
1) Native Code, which is related to the operating system of a device, refers to Code for implementing the kernel and libraries underlying the operating system.
In the android system as an example, the native code is code written in a computer language such as C-system language (including C/C + + language) and is configured to run on a processor (e.g., intel x 86-level processor) of the device, and since the native code is written in the processor, the native code has good execution efficiency when compiled and executed.
2) Native methods (functions) refer to a set of Native codes that are designed to perform a specific function (e.g., graphics processing, database, or any extended function), and are associated with a processor and therefore depend on a specific platform for execution. The local method is stored in a library file mode, and the local method realized by C/C + + codes is stored in a C/C + + class library by taking an android system as an example.
3) Java code, processor independent code running in a host environment (hostnvironment) based on a Java Virtual Machine (JVM).
4) Java methods (functions), which refer to a series of Java codes implementing specific functions for running in a Java virtual machine, are independent of a processor and have a cross-platform execution characteristic, and are stored in a class library of the Java virtual machine in a library file.
5) Java classes, which include Java methods for defining certain behavior or functionality of a Java class and properties for describing objects for defining data included by instances of the class or class.
6) Java Native Interface (JNI), one or a series of Application Program Interfaces (API), which implement communication between Java code and Native code (including C code and any code of a type supported by a convention), and the communication mode mainly includes:
6.1) calling Native methods in Java codes through a Java local interface;
6.2) calling a Java method in the Native code through a Java local interface;
native libraries of the Java virtual machine are linked in Native code, so that Java methods can be executed in the host environment of the Java virtual machine using Java Native interfaces. For example, a browser implemented based on C code may execute web-downloaded applets (applets) in an embedded Java virtual machine.
Taking a code processing device as an example, referring to fig. 1, fig. 1 is an optional structural schematic diagram of a software and hardware architecture of a terminal for running an android system provided in an embodiment of the present invention, including several main structures of an application, an application framework, a library, a Runtime (Runtime) library in the library, a kernel, and a hardware layer, which are respectively described.
1) The applications of the android system comprise an email terminal, a calendar, a map, a browser, contacts, other settings and the like, and all the applications are realized by Java code and run in a host environment based on a Java virtual machine.
2) The application framework provides an open development platform, and supports multiple applications such as accessing position information, running background services, setting an alarm clock, adding notifications to the status bar and the like. The application framework structure aims to simplify the reuse of components, and any application can release the functions in the application framework for other applications (under the premise of complying with the execution security limitation of the application framework), and the application framework is realized based on Java code and runs in a host environment based on a Java virtual machine.
The application framework layer provides a range of services and systems, illustratively including:
a View (View) system, an extensible View set, that can be used to build an application, including lists, grids, text boxes and buttons, and even an embedded web browser;
content Providers (Content Providers) that enable applications to access data of other applications (e.g., contacts) or to share data of the applications themselves;
a Resource Manager (Resource Manager) providing access to non-code resources, such as localized strings, graphics and layout files;
a Notification Manager (Notification Manager), a support application, can display a custom Notification in the status bar;
the Activity Manager (Activity Manager) manages the application life cycle and provides a general navigation backspacing function;
and a Location Manager (Location Manager) for managing the authority of the application to use the real-time Location of the device.
3) Libraries, a collection of C/C + + libraries, can be provided to developers through an application framework, illustratively including:
the BSD derivation of the System C library, Standard C System library (Libc), tuned to an embedded Linux-based device;
a media library, a multimedia core (OpenCORE), which supports playing and recording of various audio and video formats and still image files, including MPEG4, h.264, MP3, AAC, AMR and JPG formats;
PNG interface management, managing access to the display subsystem and seamlessly combining two-dimensional and three-dimensional graphic layers of a plurality of application programs;
a WEB browser engine to drive a browser and an embedded network (WEB) view;
graphics engine (SGL), a basic 2D graphics engine 3D library, using hardware 3D acceleration or containing highly optimized 3D software raster;
a font engine (FreeType) responsible for bitmap and vector font rendering;
SQLite, a lightweight relational database engine that applications can use.
4) The Runtime library is a collection of core libraries, is realized based on C/C + + codes, and provides functions available in most Java language core class libraries. Each application is an instance in a Dalvik virtual machine (Java virtual machine) running in the process of the instance itself. The Dalvik virtual machine (Java virtual machine) supports running multiple virtual machines efficiently in one device (e.g., various terminals). The format of the executable file of the Dalvik virtual machine is 'dex', and the dex format is a compression format used in the Dalvik virtual machine, so that the Dalvik virtual machine is suitable for equipment with limited memory and processor speed, and resource occupation is saved.
The Dalvik virtual machine operates based on registers, and operation relies on basic functions provided by the kernel, such as thread and underlying memory management.
5) And the kernel is realized based on C/C + + codes and serves a core system. For example: security, memory management, process management, network, stack, and driver models. The kernel is used as an abstraction layer between hardware and application, and hides hardware details of different devices, so that differences of the hardware are shielded, and uniform service is provided for application and application frameworks of an upper layer. When the hardware of the bottom layer is changed, the application and the application framework of the upper layer are not affected.
6) Hardware, including the basic hardware of the code processing apparatus, such as: processors, input/output interfaces, storage media, and network interfaces, the components may communicate via a system bus connection.
The processor may be implemented by a Central Processing Unit (CPU), a Microprocessor (MCU), an Application Specific Integrated Circuit (ASIC), or a Field-Programmable Gate Array (FPGA).
The input/output interface may be implemented using input/output devices such as a display screen, touch screen, speakers, etc.
The storage medium may be implemented by a nonvolatile storage medium such as a flash memory, a hard disk, and an optical disc, or may also be implemented by a volatile storage medium such as a Double Data Rate (DDR) dynamic cache, in which an executable instruction for executing the code processing method is stored.
For example, the storage medium may be located at the same position (e.g., the user-side terminal) as other components of the code processing apparatus, or may be distributed with respect to other components of the code processing apparatus. The network interface provides the processor with Access capability of external data such as a storage medium set in a different location, and may exemplarily perform near field Communication based on a Near Field Communication (NFC) technology, a Bluetooth (Bluetooth) technology, or a ZigBee (ZigBee) technology, and may also implement cellular Communication based on a Communication scheme and an evolution scheme thereof, such as Code Division Multiple Access (CDMA) and Wideband Code Division Multiple Access (WCDMA), and also, for example, Communication based on a wireless compatibility authentication (WiFi) mode via an Access wireless Access Point (AP) to Access the network side.
Referring to fig. 2, fig. 2 is a schematic diagram of abstractly representing the terminal software architecture shown in fig. 1 according to an embodiment of the present invention, where an application implemented based on Java code runs in a host environment based on a Java virtual machine, and a Java local interface enables Native code to be integrated with Java code.
In practical applications, when the Java code is used to develop applications, the Java Native interface can not meet all development requirements, for example, when it is desired to improve the efficiency of some key modules, or when some high-performance multi-threaded codes must be used, such as a picture function, a database function, or an extension (program or function) of any third party in any android system, the Java Native interface can support invoking Native-based methods (such as Native methods implemented based on C-system code) in the Java code. The Java local interface can utilize the characteristics of cross-platform Java language, rich class libraries, convenience in development and the like, and can also utilize the high-efficiency advantage of the local code.
Referring to fig. 3, based on fig. 1 and fig. 2, fig. 3 is an optional implementation schematic diagram for implementing mutual access between Java code and Native code based on a Java local interface according to an embodiment of the present invention, where an android Java layer is implemented based on Java code, and includes any Java method, application, and application framework in an android system; the android local C-system implementation layer includes Native or third-party Native methods implemented based on C-system code in the android system, such as class libraries implemented based on C-system code shown in fig. 1, libraries, kernels, and the like.
It should be noted that, although fig. 3 is an exemplary description of mutual access between Java code and Native code for the example that the device runs the android system and the corresponding Native code is C-system code with respect to fig. 1 and fig. 2, the exemplary description of mutual access between Java code and Native code can also be applied to the development of Java applications in systems other than the android system, for example, it can be understood that, when the device runs the non-android system and the Native code is non-C-system code, the android C-system implementation and the like shown in fig. 3 are replaced by the Native code implementation layer of the system, and the android Java layer can be replaced by the Java layer of the corresponding system.
The Java Native interface is a part of implementation of a Java virtual machine, and as shown in fig. 3, taking Native code as C-system code as an example, the Java Native interface is a bidirectional interface: the Java local interface supports accessing (calling) a Native method in the android local C implementation layer in Java code, and can also embed a Java virtual machine in the Native code and access (call back) the Java method in the android Java layer through the Java local interface.
Therefore, the Java local interface plays a role of a bridge and connects the Java virtual machine with the Native method, so that mutual access of Java codes and Native codes is realized.
However, when the Native method for implementing the function is introduced into the Java layer, the Native method for implementing the function is called through the Java Native interface, and the logic operation related to the function is executed in the local C-system implementation layer, so that the logic of the android local C-system implementation layer code is complex, a large number of operations often need to be completed in the C interface of the android local C-system implementation layer, and an abnormal situation is easily caused by improper operations, which causes instability.
In view of the above problem, referring to fig. 4, fig. 4 is an optional structural schematic diagram of a software structure of a code processing apparatus provided in an embodiment of the present invention, and a software layer of the code processing apparatus shown in fig. 1 is divided into three layers, which are: java layer, Native layer and C system implementation layer, for each layer:
1) a Java layer, realized based on Java code, including any Java code-based method in a system run by a code processing apparatus; the Java layer is used for calling Native methods for realizing specific functions of the C system implementation layer through the Java local interface, declaring the Java local interface and calling the Java local interface.
2) A Native layer for connecting the Java layer and the C-system implementation layer; the Native layer holds the object reference of the C system realization layer and the object reference of the Java layer, so that the Native method of the C system realization layer can be called for the Java layer, and the Java method of the Java layer can be called reversely in Native codes by the C system realization layer.
3) A C-system implementation layer, including any Native method implemented based on C-system code as shown in fig. 1, run by a code processing apparatus, including a library function and a kernel based on C/C + + as shown in fig. 1; and the logic is responsible for responding to the call aiming at the Native module and executing the function of the called Native method.
The C system implementation layer is only used for responding to the call aiming at the Native method, and the logic for executing the function for realizing the Native method is caused by the call from the Java layer or the conventional C system call, so that the logic related to the call of the Native method is decoupled from the logic for executing the Native method, the framework structure of the software is simple and clear, the code reusability of the Native method is higher, and the later maintenance and management are facilitated.
Referring to fig. 5-1, fig. 5-1 is another optional structural schematic diagram of the code processing apparatus provided in the embodiment of the present invention, a software layer of the code processing apparatus is divided into three layers, which are: a first code layer, a second code layer, and an intermediate layer, for each level:
1) the first code layer is implemented based on first code running in a host environment of the virtual machine, and comprises applications installed in the code processing device.
Taking the information processing apparatus as an example of running the android system, the first code layer may be a Java layer shown in fig. 4, and may be constituted by the application and application framework shown in fig. 1 in terms of software structure, including any method formed by Java code. Of course, the first code may also be a code run by any other type of virtual machine-based host environment, and the first code is not particularly limited in the embodiment of the present invention.
2) And the second code layer is related to the processor adopted by the hardware layer of the information processing device and is realized based on the second code which can be directly compiled and run on a specific processor, and the second code can be compiled into machine code directly executed by the processor, so that the execution efficiency is higher than that of the first code executed by the host environment based on the virtual machine.
Continuing with the foregoing example of the first code layer, in fig. 1, the second code is native code written based on C/C + + computer language, and the second code layer structurally includes a kernel and library file implementation implemented by C/C + + code shown in fig. 1.
Of course, according to the difference of the information processing apparatus running the operating system, the second code may adopt other types of codes besides C/C + + codes, and the second code is not limited in the embodiment of the present invention.
3) The following describes examples of a method (formed by the second code, also referred to as a second code method) in which the first code layer calls the second code layer to the intermediate layer, a method in which the second code layer calls the intermediate layer to the second code layer, and a method (formed by the first code, also referred to as a first code method) in which the second code layer calls the intermediate layer to the second code layer.
Example 1) a first code layer requesting a second code method from an intermediate layer to invoke a second code layer
In order to realize the functions which the first code method does not have or to improve the execution efficiency of realizing certain functions, the first code layer calls a second code method which is formed by a second code and realizes the corresponding functions to the middle layer.
The middle layer establishes a connection relation between the first code layer and the second code layer based on the second code method based on the coupling with the first code layer and the coupling with the second code layer, and can respond to the calling aiming at the second code method based on the connection relation and call the second code method to the second code layer;
in one embodiment, a connection relation based on a second code method is established for connecting the first code layer, the intermediate layer and the second code layer, and specifically, objects of the first code class initialized by the intermediate layer and objects of the second code class initialized by the second code class are associated through initialization at the intermediate layer and the second code layer.
First, the first code layer encapsulates the first code class that is declared locally with the second code method at the first code layer in the following manner before invoking the second code method to the middle layer: and declaring the name of a library on which the called second code method depends in a source file of the first code, locally declaring the called second code method, and compiling the source file of the first code to form a first code class encapsulating the first code local interface. After encapsulating the first code class, an initialization method of the first code class is called to the middle layer.
Secondly, the intermediate layer responds to the calling of the first code layer aiming at the initialization method, and performs static initialization on the global data of the first code class corresponding to the intermediate layer, wherein the static initialization comprises the initialization of the identification of the associated domain of the first code class; initializing global data of the first code class in the middle layer; and initializing an identification of the first code class correlation method; in addition, a static initialization method of a second code class corresponding to the first code class in the second code layer is called.
And thirdly, the second code layer responds to the calling of the static initialization method of the intermediate layer aiming at the second code class, and performs static initialization on the second code class corresponding to the first code class in the second code layer, wherein the static initialization comprises the steps of initializing global variables of the second code class and initializing static member variables of the second code class, and forming an object of the second code class.
In another embodiment, after establishing a connection relationship based on the second code method connecting the first code layer, the intermediate layer and the second code layer, the intermediate layer completes holding an address (object reference) of an object of the second code class corresponding to an object of the first code class, and for the intermediate layer completing holding an address (object reference) of an object of the second code class corresponding to an object of the first code class, the following manner is provided:
firstly, a first code layer calls a creating method in a constructor of a first code class to a middle layer; secondly, the intermediate layer responds to the call aiming at the creating method, an object (intermediate object) corresponding to the first code class and a second code class corresponding to the first code class are created in the intermediate layer, the address of the intermediate object is returned to the object of the first code class for storage, and an initialization method for initializing the second code class is called; and thirdly, the second code layer responds to the initialization method aiming at the second code class, initializes the second code class, and initializes the member variable of the second code class and allocates the memory.
When the first code layer calls the second code method, the intermediate layer acquires the address of the object of the second code class pointed by the address through the address of the intermediate object stored by the held object of the first code class, and calls the second code method according to the acquired address of the object of the second code class.
In addition, the intermediate layer also returns a result to the first code layer based on a result returned by the second code method, such as a returned null value (void) or a returned result with information or a value.
Example 2) the second code layer requesting a call to the middle layer for the second code method of the second code layer
The second code layer calls the method of the first code layer to the middle layer, and it can be seen that the call of the second code method in the second code layer may be from the first code layer as described in example 1) or from the inside of the first code layer, and since the response to the call of the first code layer is uniformly taken over by the middle layer, the middle layer calls the first code method according to the call of the first code layer to the first code method, or calls the second code method inside the second code layer, calls the first code method and returns (returns null value, information or data) to the call initiator.
For the first code method, the internal logic for distinguishing different callers does not need to be realized in the first code method, the logic of the second code method is simplified, developers can concentrate on the realization of the functions of the first code method,
example 3) second code layer requesting a reverse call to the middle layer to the first code method of the first code layer
The second code layer calls the first code method realized by the first code to the middle layer in a reverse mode, the middle layer responds to the reverse call and calls the first code method to the first code layer, and the first code layer responds to the call of the middle layer aiming at the first code method and executes the first code method. The middle layer returns (returns null, information or data) to the second code layer according to the return operation included in the called first code method.
Referring to fig. 5-2, fig. 5-2 is an alternative diagram of a method call among the first code layer, the middle layer, and the second code layer according to an embodiment of the present invention. The first code layer comprises a live application and a browser which are realized by first codes (such as Java codes), and the second code layer comprises a positioning function and a compass function which are realized by C/C + + codes.
When the live application is started, a user needs to be positioned, a request for calling a positioning function is made to the middle layer, the middle layer calls the positioning function, the positioning function is positioned and returns a positioning result according to the calling of the middle layer, and the middle layer returns the positioning result to the live application for the live application to present contents related to the positioning result.
The compass needs to accurately determine the direction according to the positioning result, requests the middle layer to call the positioning function, calls the positioning function by the middle layer, and calculates the direction by the positioning function according to the calling positioning of the middle layer and returning the positioning result to the compass function.
For the positioning function, only the call from the middle layer is responded, and the wanted processing is performed without distinguishing whether the call is from the map application of the first code layer or the compass function of the second code layer, that is, for the positioning function, the complex calling logic is not required to be realized for different call initiators, so that the logic for responding to the call of the positioning function is simplified, and for developers, the subsequent code maintenance of the positioning function is facilitated.
In addition, for the positioning function, if a user needs to check a map of a positioned position in the using process, the positioning function initiates calling for the map application to the middle layer, and transmits a positioning result as a parameter to the map application for the map application to load the map of the corresponding position.
Corresponding to fig. 5-1, referring to fig. 6, fig. 6 is an optional flowchart of the code processing method provided by the embodiment of the present invention, and includes the following steps:
the intermediate layer is coupled to the first code layer and the second code layer, step 101.
The first code layer is realized based on a first code running in a host environment of the virtual machine and comprises an application installed in the code processing method; the second code layer is implemented based on a second code capable of being compiled running directly on the particular processor; the middle layer is implemented based on the second code.
At step 102, the first code layer calls a second code method formed by the second code to the middle layer.
And 103, connecting the first code layer and the second code layer by the intermediate layer, responding to the calling of the second code method, and calling the second code method to the second code layer.
104, the second code layer calls a second code method formed by the second code to the middle layer; and, in response to the invocation of the second code method by the intermediary layer, executing the second code method.
In addition, the middle layer may return results, such as return null values, information, numerical values, and the like, to the first code layer and the second code layer that called the first code method when the first code method includes a return operation.
In another embodiment, based on fig. 6, the method further comprises the following steps:
in step 105, the second code layer calls the first code method implemented by the first code back to the middle layer.
Step 106, the middle layer calls the first code method to the first code layer.
In one embodiment, the first code layer establishes a connection relationship between the first code layer, the middle layer and the second code layer based on the second code method before calling the second code method to the middle layer, as follows:
the first code layer encapsulates the first code class locally declared to have the second code method, calls the initialization method of the first code class:
the intermediate layer responds to the calling of the first code layer aiming at the initialization method, carries out static initialization on the global data of the first code class corresponding to the intermediate layer, and initializes the identification of the associated domain of the first code class; initializing global data of the first code class in the middle layer, initializing an identifier of a method related to the first code class so as to form an object of the first code class, and calling a static initialization method of a second code class corresponding to the first code class;
the second code layer responds to the calling of the intermediate layer aiming at the static initialization method, carries out static initialization on the second code class in the second code layer, and comprises the steps of initializing global variables of the second code class and static member variables of the second code class in the second code layer, initializing member variables of the second code class and allocating a memory, thereby forming an object of the second code class.
In another embodiment, after establishing a connection relationship based on the second code method connecting the first code layer, the intermediate layer and the second code layer, the intermediate layer completes holding an address (object reference) of an object of the second code class corresponding to an object of the first code class, and for the intermediate layer completing holding an address (object reference) of an object of the second code class corresponding to an object of the first code class, the following manner is provided: further comprising:
the first code layer calls a creating method in a constructor of the first code class;
the intermediate layer responds to the call aiming at the creating method, creates an intermediate object corresponding to the first code class and a second code class corresponding to the first code class, returns the address of the intermediate object to the object storage of the first code class, and calls an initializing method for initializing the second code class;
the second code layer initializes the second code class in response to an initialization method for the second code class.
In one embodiment, invoking the second code method to the second code layer comprises:
when the first code layer calls the second code method, the intermediate layer obtains the address of the object of the second code class pointed by the address through the address of the intermediate object stored in the object of the first code class, and calls the second code method according to the obtained address of the object of the second code class.
Step 107, the first code layer responds to the calling of the intermediate layer aiming at the first code method, and executes the first code method.
In another embodiment, based on fig. 6, the method further includes the following steps:
and step 108, when the first code method comprises a return operation, the middle layer returns results to the first code layer and the second code layer which call the first code method.
In the following, the description will be given with reference to a processing procedure of calling a C + + method of the C-based implementation layer in the Java layer, but it is understood that the Native layer is used to respond to both a call from the Java layer to the C-based implementation layer and a call from the C-based implementation layer to the Java method, and therefore, the processing of calling the C-based method of the C-based implementation layer by the Native layer described below can be easily performed by responding to the Java layer to call the C-based method of the C-based implementation layer.
Referring to fig. 7, fig. 7 is an optional flowchart illustrating that a Java layer calls a C + + method of a C system implementation layer according to an embodiment of the present invention, and in fig. 7, the C + + method for calling the C system implementation layer mainly involves the following aspects:
when the Java layer needs to use Java classes comprising Java local interfaces (when the Java classes have local declarations of C + + methods, a static initialization method is automatically called to initialize static variables of Native classes corresponding to the Java classes in the Native layer to form instantiated Native objects, and in this way, the Native objects (instances of the Native classes) are associated with the Java objects (instances of the Java classes);
when creating an object (namely a Java object and an instance of the Java class) of a Java class containing a Java local interface, the Java layer automatically calls a Native _ setup method, creates a Native class object (Native object) corresponding to the Java class on the Native layer, transmits the address of the Native object back to the Java object for storage, creates and holds a C + + class object on the Native layer, and associates the Java layer with a C system implementation layer in the manner on the Native layer to achieve the effect that the Java object, the Native object and the C + + object correspond to one another;
when the Java layer calls the JNI method in the Java class, the Native layer can actively acquire the corresponding address of the C + + object through the Native object address stored in the Java layer, and call the related C + + method.
The specific implementation of fig. 7 is explained below.
Firstly, when a Native method (such as a C + + method) of a C system implementation layer needs to be called, a Java layer creates a Java class including a JNI Native interface.
In a Java source file, the name of a library which is depended by a called Native method (such as a C + + method) is declared, the called Native method (such as the C + + method) is locally declared by taking Native as a key word, and the Java source file is compiled to form a Java class which is packaged with a Java local interface.
Secondly, the Java layer calls a static initialization method Native _ init of the Java class, and initializes the global data of the Java class in the Native layer to form a Java object.
For example, at the Native level, initialization operations are performed that include: initializing an Identification (ID) of an associated domain of the Java class; initializing global data of the Java class at a Native layer, wherein the global data comprises data related to the Java class, static data and Java layer data; initializing an Identification (ID) of a Java class related method; an initialization method for calling a C + + class associated with the Java class initializes the static variables.
The C-system implementation responds to the invocation of the initialization method of the Native layer for the C + + class related to the Java class, and the C-system implementation layer initializes the static variables related to the C + + class to form a C + + object, that is, static initialization, including: and initializing global variables of the C + + class related to the Java class, initializing C + + static member variables and other related initializations of the C + + class at a Native layer.
After the Native layer has finished executing the processing method and returned (returns a null value or a concrete result) to the Java layer, the Java class encapsulated by the Java layer is initialized at the Native layer and the C-system implementation layer, and the Native object (instance of the Native class) and the Java object (instance of the Java class) are associated with each other.
And thirdly, calling a creating method Native _ setup in the constructor of the Java class by the Java layer, and creating a packaging object corresponding to the Native class corresponding to the Java class by the Native layer.
The Native layer creates the corresponding encapsulated object, which comprises the following steps: creating a Java local interface class corresponding to the Java object; and creating a Native object corresponding to the Java object, creating a corresponding C + + class based on the related data of the initialized Native object, and calling a method for initializing the C + + class to initialize the C + + class.
The C system implementation layer responds to the call of the method for initializing the C + + class, and the method comprises the following steps: member variables of the C + + class are initialized, memory is allocated and other related initializations of the C + + class objects are made.
And returning (returning a null value or a specific result) the Java layer after the Native layer is established, so far, the Java layer can call the C + + method based on a Java local interface packaged in the Java class.
And finally, calling other functional do _ Native methods by the Java layer, acquiring the object address of the C + + class corresponding to the C + + method to be called at the Native layer, calling the C + + class method of the C system implementation layer by the Native layer according to the object address of the C + + class, executing the C + + method and returning (returning a null value or a specific result) to the Java layer.
The embodiment of the invention realizes the following technical effects:
1) the second code layer is only used for responding to the second code method call from the middle layer, and the logic for executing the function for realizing the second code method is caused by the call from the first code layer or caused by the internal call from the second code layer, so that the logic related to the call of the second code method is decoupled from the logic for executing the function for realizing the second code method, the framework structure of the code is simple and clear, the code reusability of the second code method is higher, and the later maintenance and management are convenient.
2) By arranging the intermediate layer, the first code layer and the second code layer can call the second code method in the second code layer, and the second code layer can also respond to the call of the first code method of the first code layer, so that the effect of intercommunication of different code layers is realized on the premise of simplifying call logic.
Those skilled in the art will understand that: all or part of the steps for implementing the method embodiments may be implemented by hardware related to program instructions, and the program may be stored in a computer readable storage medium, and when executed, the program performs the steps including the method embodiments; and the aforementioned storage medium includes: various media capable of storing program codes, such as a removable Memory device, a Random Access Memory (RAM), a Read-Only Memory (ROM), a magnetic disk, and an optical disk.
Alternatively, the integrated unit of the present invention may be stored in a computer-readable storage medium if it is implemented in the form of a software functional module and sold or used as a separate product. Based on such understanding, the technical solutions of the embodiments of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the methods according to the embodiments of the present invention. And the aforementioned storage medium includes: a removable storage device, a RAM, a ROM, a magnetic or optical disk, or other various media that can store program code.
The above description is only for the specific embodiments of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art can easily conceive of the changes or substitutions within the technical scope of the present invention, and all the changes or substitutions should be covered within the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the appended claims.
Claims (22)
1. A code processing apparatus, comprising:
a first code layer; a second code layer; an intermediate layer coupled with the first code layer and coupled with the second code layer; wherein,
the first code layer is realized based on first codes running in a host environment of the virtual machine, comprises installed applications and is used for calling a second code method formed by second codes to the middle layer;
the middle layer is realized based on the second code and is used for connecting the first code layer and the second code layer and responding to the calling of the second code method to call the second code method to the second code layer;
the second code layer is implemented based on the second code capable of being compiled and run directly on a specific processor and is used for calling the second code method formed by the second code to the middle layer; and, in response to the invocation of the second code method by the intermediary layer, executing the second code method.
2. The apparatus of claim 1,
the second code layer is also used for calling the first code method realized by the first code to the middle layer in a reverse direction;
the middle layer is also used for responding to the reverse calling of the second code layer for the first code method, and calling the first code method to the first code layer in a reverse direction;
the first code layer is further configured to execute the first code method in response to a reverse call by the middle layer to the first code method.
3. The apparatus of claim 1,
the middle layer is further configured to return a result to the first code layer and the second code layer that invoke the first code method when the first code method includes a return operation.
4. The apparatus of claim 1,
the first code layer is also used for encapsulating a first code class locally declared with the second code method and calling an initialization method of the first code class before calling the second code method to the middle layer;
the intermediate layer is further configured to perform static initialization on global data of the first code class corresponding to the intermediate layer to form an object of the first code class in response to the call of the first code layer to the initialization method, and call a static initialization method of a second code class corresponding to the first code class;
the second code layer is further configured to perform static initialization on the second code class in the second code layer in response to the call of the intermediate layer to the static initialization method, so as to form an object of the second code class.
5. The apparatus of claim 4,
the intermediate layer is further used for initializing the identification of the associated domain of the first code class; initializing global data of the first code class in the middle layer, and initializing the identification of the first code class correlation method.
6. The apparatus of claim 4,
the second code layer is further configured to initialize global variables of the second code class and to initialize static member variables of the second code class at the second code layer.
7. The apparatus of claim 4,
the first code layer is also used for calling a creating method in the constructor of the first code class;
the intermediate layer is also used for responding to the call of the creating method, creating an intermediate object corresponding to the first code class and a second code class corresponding to the first code class, returning the address of the intermediate object to the object storage of the first code class, and calling an initialization method for initializing the second code class;
the second code layer is further configured to initialize the second code class in response to a call to an initialization method for the second code class.
8. The apparatus of claim 7,
the second code layer is also used for initializing member variables of the second code class and distributing the memory.
9. The apparatus of claim 7,
the intermediate layer is further configured to, when the first code layer calls the second code method, obtain, through the address of the intermediate object stored in the object of the first code class, an address of the object of the second code class to which the address points, and call the second code method according to the obtained address of the object of the second code class.
10. The apparatus of claim 1,
the first code is Java code, and the second code is native code of an operating system.
11. A code processing method, comprising:
the middle layer is coupled with the first code layer and coupled with the second code layer;
wherein the first code layer is implemented based on first code running in a host environment of a virtual machine, including an installed application; the second code layer is implemented based on a second code capable of being compiled running directly on a particular processor; the middle layer is implemented based on the second code;
the first code layer calls a second code method formed by the second code to a middle layer;
said intermediate layer connecting said first code layer and said second code layer, calling said second code method to said second code layer in response to a call for said second code method;
the second code layer calling the second code method formed by the second code to the middle layer; and, in response to the invocation of the second code method by the intermediary layer, executing the second code method.
12. The method of claim 11, further comprising:
the second code layer calls a first code method realized by the first code to the middle layer in a reverse direction;
the intermediate layer calls the first code method back to the first code layer;
the first code layer executes the first code method in response to a reverse call by the middle layer to the first code method.
13. The method of claim 11, further comprising:
and when the first code method comprises a return operation, the intermediate layer returns results to the first code layer and the second code layer which call the first code method.
14. The method of claim 11, further comprising:
before the first code layer calls the second code method to the middle layer, a first code class which locally declares the second code method is packaged, and an initialization method of the first code class is called;
the intermediate layer responds to the calling of the first code layer aiming at the initialization method, carries out static initialization on the global data of the first code class corresponding to the intermediate layer to form an object of the first code class, and calls a static initialization method of a second code class corresponding to the first code class;
and the second code layer responds to the calling of the intermediate layer aiming at the static initialization method, and statically initializes the second code class in the second code layer to form an object of the second code class.
15. The method of claim 14, wherein the statically initializing global data corresponding to the first code class at the middle tier comprises:
the middle layer initializes an identification of an associated domain of the first code class; initializing global data of the first code class in the middle layer, and initializing the identification of the first code class correlation method.
16. The method of claim 14, wherein the statically initializing the second code class in the second code layer comprises:
the second code layer initializes global variables with the second code class and initializes static member variables of the second code class at the second code layer.
17. The method of claim 14, further comprising:
the first code layer calls a creating method in a constructor of the first code class;
the intermediate layer responds to the call aiming at the creating method, creates an intermediate object corresponding to the first code class and a second code class corresponding to the first code class, returns the address of the intermediate object to the object storage of the first code class, and calls an initialization method for initializing the second code class;
the second code layer initializes the second code class in response to an initialization method for the second code class.
18. The method of claim 17, wherein the initializing the second code class comprises:
and the second code layer initializes the member variables of the second code class and allocates the memory.
19. The method of claim 17, wherein said invoking said second code method to said second code layer comprises:
when the first code layer calls the second code method, the intermediate layer obtains the address of the object of the second code class pointed by the address through the address of the intermediate object stored in the object of the first code class, and calls the second code method according to the obtained address of the object of the second code class.
20. The method of claim 11,
the first code is Java code, and the second code is native code of an operating system.
21. A code processing apparatus, comprising:
a memory for storing executable instructions;
a processor for implementing the code processing method of any one of claims 11 to 20 when executing executable instructions stored in the memory.
22. A computer-readable storage medium having stored thereon executable instructions for causing a processor to perform the code processing method of any one of claims 11 to 20 when executed.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201611051637.9A CN108376066B (en) | 2016-11-24 | 2016-11-24 | Code processing device and code processing method |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201611051637.9A CN108376066B (en) | 2016-11-24 | 2016-11-24 | Code processing device and code processing method |
Publications (2)
Publication Number | Publication Date |
---|---|
CN108376066A CN108376066A (en) | 2018-08-07 |
CN108376066B true CN108376066B (en) | 2020-09-11 |
Family
ID=63016115
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201611051637.9A Active CN108376066B (en) | 2016-11-24 | 2016-11-24 | Code processing device and code processing method |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN108376066B (en) |
Families Citing this family (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109815205A (en) * | 2018-12-18 | 2019-05-28 | 创新科存储技术(深圳)有限公司 | Method, storage medium and the device of cloud computing platform access UFS |
CN110764779B (en) * | 2019-10-21 | 2023-09-15 | 百度在线网络技术(北京)有限公司 | Encapsulation method, registration method, device and rendering equipment |
CN110727504B (en) | 2019-10-21 | 2022-04-22 | 百度在线网络技术(北京)有限公司 | Code execution method and device and rendering equipment |
CN111324647A (en) * | 2020-01-21 | 2020-06-23 | 北京东方金信科技有限公司 | Method and device for generating ETL code |
CN111399844B (en) * | 2020-03-12 | 2023-11-24 | 同盾(广州)科技有限公司 | Secure compiling method, secure compiling device, electronic equipment and computer readable medium |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104236579A (en) * | 2014-10-09 | 2014-12-24 | 武汉大学 | High-precision satellite navigation implementing method based on Android kernel layer |
CN104469664A (en) * | 2014-12-26 | 2015-03-25 | 乐视致新电子科技(天津)有限公司 | Bluetooth device information processing method and device |
CN104778075A (en) * | 2015-04-03 | 2015-07-15 | 北京奇虎科技有限公司 | Method and device for calling Java layer API (Application Program Interface) by native layer in Android system |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US8756616B2 (en) * | 2006-12-29 | 2014-06-17 | Core Wireless Licensing S.A.R.L. | System and method for reducing the static footprint of mixed-language JAVA classes |
-
2016
- 2016-11-24 CN CN201611051637.9A patent/CN108376066B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104236579A (en) * | 2014-10-09 | 2014-12-24 | 武汉大学 | High-precision satellite navigation implementing method based on Android kernel layer |
CN104469664A (en) * | 2014-12-26 | 2015-03-25 | 乐视致新电子科技(天津)有限公司 | Bluetooth device information processing method and device |
CN104778075A (en) * | 2015-04-03 | 2015-07-15 | 北京奇虎科技有限公司 | Method and device for calling Java layer API (Application Program Interface) by native layer in Android system |
Also Published As
Publication number | Publication date |
---|---|
CN108376066A (en) | 2018-08-07 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN108376066B (en) | Code processing device and code processing method | |
CN109032691B (en) | Applet running method and device and storage medium | |
US10684868B2 (en) | Configuration for application using microservices | |
US11853820B2 (en) | Cross-process communication method, apparatus, and device | |
CN108563517B (en) | Calling method and device of system interface | |
CN110046023B (en) | Data processing method and system based on intelligent contract of block chain | |
US8966508B2 (en) | Method for executing hybrid web application and apparatus therefor | |
US8490117B1 (en) | Bridging script engines | |
CN110007980B (en) | Method and device for realizing multi-service server | |
US20210173665A1 (en) | Bootstrapping Profile-Guided Compilation and Verification | |
CN108108239A (en) | Method and device for providing service function and computer readable storage medium | |
CN104992081A (en) | Security enhancement method for third-party code of Android application program | |
CN110825499B (en) | Method and device for realizing security isolation based on unified container cloud platform | |
CN106598662B (en) | Android-based application loading method and device | |
EP2883133A1 (en) | Isa bridging including support for call to overidding virtual functions | |
CN115048642B (en) | Communication method between trusted applications in multi-trusted execution environment and electronic equipment | |
US20140040921A1 (en) | Isa bridging with callback | |
CN107678741B (en) | List view implementation method and device, readable storage medium and equipment | |
WO2022199111A1 (en) | Method and apparatus for implementing functions in application program, electronic device and storage medium | |
CN110069247A (en) | A kind of development approach and device, application method and device of application program | |
CN114547569A (en) | Account login processing method and device | |
CN115629976A (en) | Kernel testing method and device and storage medium | |
US10579373B2 (en) | Seamless extension porting | |
CN117992189A (en) | Window realization method under multiple systems and electronic equipment | |
CN113094123B (en) | Method and device for realizing functions in application program, electronic equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |