The content of the invention
In view of this, the application provides the replacement method and device of a kind of method in java class, to solve phase
Method needs to restart the problem of related application during java class is replaced in the technology of pass.
Specifically, the application is achieved by the following technical solution:
A kind of replacement method of method in java class, methods described includes:
Java Virtual Machine loads the first java class and the second java class;
The first structure information and described of method to be replaced in first java class is determined by JNI
Second structural information of replacement method in two java class;
The first structure information is replaced according to second structural information.
Optionally, it is described to determine that the first structure of method to be replaced in first java class is believed by JNI
Second structural information of replacement method in breath and second java class, including:
Determine the Java Virtual Machine type and version;
According to the Java Virtual Machine type and version, determine to be treated in first java class by JNI
Second structural information of replacement method in the first structure information of replacement method and second java class.
Optionally, the first property parameters of the first structure information including the method to be replaced and its
One address information;
Second structural information includes second property parameters and its second address information of the replacement method.
Optionally, it is described to determine that the first structure of method to be replaced in first java class is believed by JNI
Second structural information of replacement method in breath and second java class, including:
The first mark according to the method to be replaced, in obtaining the first structure information by JNI
First address information, and the first category in the first structure information is searched according to first address information
Property parameter;
The second mark according to the replacement method, the in second structural information is obtained by JNI
Double-address information, and the second attribute in second structural information is searched according to second address information
Parameter.
Optionally, it is described that the first structure information is replaced according to second structural information, including:
First property parameters correspondence is replaced with into second property parameters.
A kind of alternative of method in java class, described device includes:
Class loading unit, Java Virtual Machine loading the first java class and the second java class;
Structure determination unit, the first structure of method to be replaced in first java class is determined by JNI
Second structural information of replacement method in information and second java class;;
Structure replacement unit, the first structure information is replaced according to second structural information.
Optionally, the structure determination unit, specifically determines the Java Virtual Machine type and version;
According to the Java Virtual Machine type and version, determine to be treated in first java class by JNI
Second structural information of replacement method in the first structure information of replacement method and second java class.
Optionally, the first property parameters of the first structure information including the method to be replaced and its
One address information;
Second structural information includes second property parameters and its second address information of the replacement method.
Optionally, the structure determination unit, with specific reference to the first mark of the method to be replaced, leads to
The first address information crossed during JNI obtains the first structure information, and according to first address information
Search the first property parameters in the first structure information;
The second mark according to the replacement method, the in second structural information is obtained by JNI
Double-address information, and the second attribute in second structural information is searched according to second address information
Parameter.
Optionally, the structure replacement unit, specifically replaces with first property parameters correspondence described
Second property parameters.
By above description as can be seen that the application loads the first java class and second in Java Virtual Machine
After java class, the first structure information of method to be replaced in first java class can be determined by JNI
And in second java class replacement method the second structural information, and according to second structural information
The first structure information is replaced, to cause that the follow-up method to be replaced in first java class is adjusted
During with performing, can call on the replacement method, to realize to method in each type java class
Dynamic replacement, without restarting related application, without java class is reloaded, improves Java
The efficiency that method is replaced in class, while improving Consumer's Experience.
Specific embodiment
Here exemplary embodiment will be illustrated in detail, its example is illustrated in the accompanying drawings.Following
When description is related to accompanying drawing, unless otherwise indicated, the same numbers in different accompanying drawings represent same or analogous
Key element.Implementation method described in following exemplary embodiment does not represent the institute consistent with the application
There is implementation method.Conversely, they are only with described in detail in such as appended claims, the application
The example of the consistent apparatus and method of a little aspects.
It is the purpose only merely for description specific embodiment in term used in this application, and is not intended to be limiting
The application." one kind ", " institute of the singulative used in the application and appended claims
State " and " being somebody's turn to do " be also intended to include most forms, unless context clearly shows that other implications.Should also
Work as understanding, term "and/or" used herein refers to and associated lists item comprising one or more
Purpose any or all may combine.
It will be appreciated that though may describe various using term first, second, third, etc. in the application
Information, but these information should not necessarily be limited by these terms.These terms only be used for by same type of information that
This is distinguished.For example, in the case where the application scope is not departed from, the first information can also be referred to as
Two information, similarly, the second information can also be referred to as the first information.Depending on linguistic context, as in this institute
Use word " if " can be construed to " and ... when " or " when ... when " or " response
In it is determined that ".
In the related art, by taking Android application and developments as an example, needed to solve method in replacing java class
Restart the problem of related application, it is possible to use Classloader (Classloader) loads Java
Class, when method in needing to replace java class, can create new Classloader to reload
New java class, to reach the purpose of method in java class of replacing.However, in such implementation
In, it is necessary to loaded to java class again, replace the less efficient of method in java class.
Additionally, for the java class of interface type, Java can also be realized by the proxy class of Java
The replacement of method in class, however, the proxy class of java class can only realize method in the java class of interface type
Replacement, for other kinds of java class, then cannot realize.
Regarding to the issue above, the application provides the alternative of a kind of method in java class, is replacing Java
In class during method, without restarting related application, the efficiency of method replacement in lifting java class, while
Lifting Consumer's Experience.
Fig. 1 is the stream of the replacement method of method in a kind of java class shown in the exemplary embodiment of the application one
Journey schematic diagram.
Fig. 1 is refer to, the replacement method of method can be included using in the terminal in the java class
Following steps:
Step 101, Java Virtual Machine loading the first java class and the second java class.
In the present embodiment, the java class that will include method to be replaced is referred to as the first java class, will wrap
The java class for including replacement method is referred to as the second java class.In the replacement of method in carrying out java class,
Method to be replaced in first java class is replaced with needs the replacement side in second java class
Method.
In this step, Java Virtual Machine can load first java class and the 2nd Java
Class.
Step 102, the first structure information of method to be replaced in first java class is determined by JNI
And in second java class replacement method the second structural information.
Based on abovementioned steps 101, Java Virtual Machine is in loading first java class and the 2nd Java
After class, described first can be determined by JNI (Java Native Interface, Java local interface)
The first structure information of method to be replaced described in java class, and determine described in second java class
Second structural information of replacement method.
Wherein, the first structure information includes first property parameters and its first of the method to be replaced
Address information, second structural information includes second property parameters and its second ground of the replacement method
Location information.In this step, institute can be obtained by JNI by the first of the method to be replaced the mark
The first address information in first structure information is stated, and described first is searched according to first address information
The first property parameters in structural information.Agree to, can be led to according to the second of the replacement method the mark
The second address information crossed during JNI obtains second structural information, and according to second address information
Search the second property parameters in second structural information.
Step 103, the first structure information is replaced according to second structural information.
Based on abovementioned steps 102, it is determined that the first structure information and second structural information it
Afterwards, the first structure information can be replaced according to second structural information, such as:By described first
First property parameters correspondence in structural information replaces with described second in second structural information
Property parameters, to realize replacing with the method to be replaced the purpose of the replacement method.
By above description as can be seen that the application loads the first java class and second in Java Virtual Machine
After java class, the first structure information of method to be replaced in first java class can be determined by JNI
And in second java class replacement method the second structural information, and according to second structural information
The first structure information is replaced, to cause that the follow-up method to be replaced in first java class is adjusted
During with performing, can call on the replacement method, to realize to method in each type java class
Dynamic replacement, without restarting related application, without java class is reloaded, improves Java
The efficiency that method is replaced in class, while improving Consumer's Experience.
The implementation process of the application is described with reference to specific embodiment.
Fig. 2 is the replacement method of method in another java class shown in the exemplary embodiment of the application one
Schematic flow sheet.
Fig. 2 is refer to, the replacement method of method can be included using in the terminal in the java class
Following steps:
Step 201, Java Virtual Machine loading the first java class and the second java class.
For ease of description, the java class that can will include method to be replaced is referred to as the first java class, will
The java class for including replacement method is referred to as the second java class, i.e. included in first java class
Method to be replaced, replacement method is included in second java class.In the present embodiment, it is necessary to by institute
State method to be replaced and replace with the replacement method.
In this step, Java Virtual Machine can respectively load first java class and described second
Java class.
Step 202, according to the first mark of method to be replaced in first java class, is obtained by JNI
Take the first address information in the first structure information of the method to be replaced.
In the present embodiment, described first identify for identifying the method to be replaced, first mark
Can be the information such as title, the ID of the method to be replaced.The first structure information is the Java
After first java class is loaded, the method to be replaced is in the Java Virtual Machine for virtual machine
Structural information, the first structure information can include:The property parameters of the method to be replaced and institute
The address information of property parameters is stated, in the present embodiment, can be by the property parameters of the method to be replaced
Referred to as the first property parameters, the first address information is referred to as by the positional information of first property parameters.JNI
It is Java local interfaces, Java Virtual Machine can be operated by JNI.
In the present embodiment, the first structure can be obtained by JNI according to the first mark specified
The first address information in information, first address information can be memory address, the memory address
In be stored with first property parameters.
Step 203, the first attribute in the first structure information is searched according to first address information
Parameter.
Based on abovementioned steps 202, after first address information is got, can be according to described
One address information searches the first property parameters of storage.First property parameters can include:It is described to treat
The information such as the title of replacement method, access information and outlet information.
Step 204, according to the second mark of replacement method in second java class, is obtained by JNI
The second address information in second structural information of the replacement method.
In the present embodiment, similar with the described first mark, described second identifies for identifying the replacement
Method, second mark can be the information such as title, the ID of the replacement method.Second structure
Information be the Java Virtual Machine after second java class is loaded, the replacement method is in the Java
Structural information in virtual machine.Second structural information can include:The attribute ginseng of the replacement method
The address information of the several and property parameters, in the present embodiment, can be by the category of the replacement method
Property parameter be referred to as the second property parameters, by the positional information of second property parameters be referred to as the second address letter
Breath.
In the present embodiment, second structure can be obtained by JNI according to the second mark specified
The second address information in information, second address information can also be memory address, the memory address
In be stored with second property parameters.
Step 205, the second attribute in second structural information is searched according to second address information
Parameter.
Based on abovementioned steps 204, after second address information is got, can be according to described
Second property parameters of double-address information searching storage.Second property parameters can include:It is described to replace
Change the information such as title, access information and the outlet information of method.
Step 206, second property parameters are replaced with by first property parameters correspondence.
Based on abovementioned steps 203 and step 205, first property parameters and described are being found
After second property parameters, in this step, first property parameters correspondence can be replaced with described
Second property parameters.Such as:Title in first property parameters is replaced with into the second attribute ginseng
Title in number, during the access information in first property parameters replaced with into second property parameters
Access information etc..Because the attribute information of the method to be replaced has been replaced by the replacement method
Attribute information, so follow-up when execution is called during the method to be replaced, it will call execution described
Replacement method, realizes the dynamic replacement of method in java class.Using in the java class that the application is provided
The alternative of method, after will be using user is distributed to, can still replace in real time, to user's unaware
The method changed in java class, improves the experience of user.
By above description as can be seen that the application loads the first java class and second in Java Virtual Machine
After java class, the first structure information of method to be replaced in first java class can be determined by JNI
And in second java class replacement method the second structural information, and according to second structural information
The first structure information is replaced, to cause that the follow-up method to be replaced in first java class is adjusted
During with performing, can call on the replacement method, to realize to method in each type java class
Dynamic replacement, without restarting related application, without java class is reloaded, improves Java
The efficiency that method is replaced in class, while improving Consumer's Experience.
Optionally, in another embodiment of the application, Java Virtual Machine can first be determined in initialization
Type and version, then it is determined that when the first structure information and second structural information, can
Determine the first structure information and described second with according to the type and version of the Java Virtual Machine
Structural information.Because, different types of Java Virtual Machine, after loading java class, java class
The structural information of middle method may be different, and the Java Virtual Machine of same type, load java class
Afterwards, in java class the structural information of method be likely to it is different.Therefore, it is virtual according to the Java
The type and version of machine determine accurate first structure information and the second structural information.
Embodiment with the replacement method of method in foregoing java class is corresponding, present invention also provides Java
The embodiment of the alternative of method in class.
The embodiment of the alternative of method can be applied in terminal in the application java class.Device is implemented
Example can be realized by software, it is also possible to be realized by way of hardware or software and hardware combining.With software
As a example by realization, used as the device on a logical meaning, being will be non-easy by the processor of terminal where it
Corresponding computer program instructions run what is formed in reading internal memory in the property lost memory.From hardware view
For, as shown in figure 3, being a kind of hardware of the alternative place terminal of method in the application java class
Structure chart, except the processor shown in Fig. 3, internal memory, network interface and nonvolatile memory it
Outward, the terminal in embodiment where device can also include other generally according to the actual functional capability of the terminal
Hardware, repeats no more to this.
Fig. 4 is the knot of the alternative of method in a kind of java class shown in the exemplary embodiment of the application one
Structure schematic diagram.
Fig. 4 is refer to, the alternative 300 of method can be applied shown in Fig. 3 in the java class
In terminal, include:Class loading unit 301, structure determination unit 302 and structure replacement unit 303.
Wherein, the class loading unit 301, Java Virtual Machine loading the first java class and the 2nd Java
Class;
The structure determination unit 302, method to be replaced in first java class is determined by JNI
Second structural information of replacement method in first structure information and second java class;;
The structure replacement unit 303, the first structure information is replaced according to second structural information.
Optionally, the structure determination unit 302, specifically determines the Java Virtual Machine type and version;
According to the Java Virtual Machine type and version, determine to be treated in first java class by JNI
Second structural information of replacement method in the first structure information of replacement method and second java class.
Optionally, the first property parameters of the first structure information including the method to be replaced and its
One address information;
Second structural information includes second property parameters and its second address information of the replacement method.
Optionally, the structure determination unit 302, with specific reference to the first mark of the method to be replaced,
The first address information in the first structure information is obtained by JNI, and is believed according to first address
Breath searches the first property parameters in the first structure information;
The second mark according to the replacement method, the in second structural information is obtained by JNI
Double-address information, and the second attribute in second structural information is searched according to second address information
Parameter.
Optionally, the structure replacement unit 303, specifically replaces with first property parameters correspondence
Second property parameters.
The function of unit and the implementation process of effect specifically refer to correspondence in the above method in said apparatus
The implementation process of step, will not be repeated here.
For device embodiment, because it corresponds essentially to embodiment of the method, so related part ginseng
See the part explanation of embodiment of the method.Device embodiment described above be only it is schematical,
It is wherein described as separating component illustrate unit can be or may not be it is physically separate, make
For the part that unit shows can be or may not be physical location, you can with positioned at a place,
Or can also be distributed on multiple NEs.Can select according to the actual needs part therein or
Person whole modules realize the purpose of application scheme.Those of ordinary skill in the art are not paying creativeness
In the case of work, you can to understand and implement.
The preferred embodiment of the application is the foregoing is only, is not used to limit the application, it is all at this
Within the spirit and principle of application, any modification, equivalent substitution and improvements done etc. should be included in
Within the scope of the application protection.