Summary of the invention
The object of this part is some aspects of general introduction embodiments of the invention and briefly introduces some preferred embodiments.May do in the specification digest and denomination of invention of this part and the application a little simplify or omit with avoid making this part, specification digest and denomination of invention object fuzzy, and this simplification or omit and can not be used for limiting the scope of the invention.
The object of the present invention is to provide the method for field in a kind of dynamic replacement class, it dynamically can change field in class when not restarting JVM, significantly improves development efficiency.
For achieving the above object, the invention provides the method for field in a kind of dynamic replacement class, it comprises:
Start JVM, the class that loading one is new after Class File Load Hook event is triggered, judge whether such is be written into first time;
If described class is loaded into for the first time, then in global information table, register all field informations in such; All fields in described class are all extracted, replace by a field information class field, add a static initialization block, utilize the field information extracted to carry out initialization to described field information class field, described field information class field comprises a Hash table, obtains field method and amendment field method; Utilize the method information in such to generate an abstract class, and define the field of this abstract class type in such; A specific implementation class of described abstract class is generated according to the method information in such and described abstract class, needs any in such are read or revise and is enhanced calling of class field, all change the acquisition field method or amendment field method that call the class of field information described in such field into, needs any in class are called and is enhanced calling of class methods, all change into and call method corresponding to the type field of abstract class described in such; Detect such whether to be dynamically updated, if such is dynamically updated, use the specific implementation class of last abstract class before dynamically updating by its initialization;
Otherwise, in global information table, detect the information upgrading its field; The method information in such and such abstract class generated when first is loaded into is utilized to generate a specific implementation class of described abstract class, needs any in such are read or revise and is enhanced calling of class field, all change the acquisition field method or amendment field method that call the class of field information described in such field into, needs any in class are called and is enhanced calling of class methods, all change into and call method corresponding to the type field of abstract class described in such;
Wherein, described acquisition field method comprises:
From global information table, to inquire about under ClassLoader such all field informations, to judge whether comprise this field in described field information, if do not comprise, produce an exception without this field;
If desired this field obtained is static fields information, then from global information table, inquire about the value of such this enhancing class field under this ClassLoader, and return with an Object;
If desired this field obtained is non-static field information, then obtain the information of this enhancing class field in the Hash table from this info class, and return with an Object;
Wherein, described amendment field method comprises:
The up-to-date field information that such loads such is inquired about from global information table, to judge whether comprise this field in up-to-date field information, if do not comprise, produce an exception without this field, if this field is unchangeable, produce a unchangeable exception;
If desired the field revised is static fields, then from global information table, arrange the value that such loads such this field down;
If desired the field revised is non-static field, then revise the value of this field in the Hash table from this info class.
Further, also comprise loading one dynamic link library when starting JVM, described dynamic link library comprises interface and intrinsic function, and utilizes the function of interface interchange inside to process described class, abstract class and specific implementation class.
Further, judge that whether such be written into first time, comprising: the bytecode of the class be loaded described in analysis, according to such ClassLoader of global information table look-up to judge whether such is loaded into for the first time.
Further, all field informations in registration class in global information table, comprising: according to all fields in such, and with such ClassLoader and such signature, all field informations in such are registered, to show that such is enhanced in global information table.
Further, in described abstract class, comprise methods all in described class, and the described method in abstract class is abstract method.
Compared with prior art, the present invention uses a particular event Class File Load Hook in JVMTI (JVM Tool Interface), this event can after JVM obtains a class data file, but be put into before in internal memory and call being built into a class, in the function that this event triggers, can change arbitrarily the bytecode of this class file.The present invention gets involved after being mainly triggered from this event exactly, class file is modified enhancing, thus realize when application system need not be restarted, field in class will be had to the class of any change, and in class, existing method body there is the class of any change dynamically to replace.
Embodiment
Detailed description of the present invention describes the running of technical solution of the present invention directly or indirectly mainly through logical block or other recapitulative descriptions.For thorough understanding the present invention, in ensuing description, set forth a lot of specific detail.And when not having these specific detail, the present invention then may still can realize.Herein these describe and state mainly in order to effectively introduce the essence of technical solution of the present invention to the others skilled in the art in affiliated field.In other words, be object of the present invention of avoiding confusion, due to the method known and program easy understand, therefore they are not described in detail.
Alleged herein " embodiment " or " embodiment " refers to special characteristic, structure or the characteristic that can be contained at least one implementation of the present invention.Different local in this manual " in one embodiment " occurred not all refers to same embodiment, neither be independent or optionally mutually exclusive with other embodiments embodiment.In addition, represent sequence of modules in the functional block diagram of one or more embodiment and revocablely refer to any particular order, not also being construed as limiting the invention.
Usually, use a particular event Class File LoadHook in JVMTI (JVM Tool Interface), this event can after JVM obtains a class data file, but be put into before in internal memory and call being built into a class, in the function that this event triggers, can change arbitrarily the bytecode of this class file.The present invention gets involved after being mainly triggered from this event exactly, class file is modified enhancing, thus realize when application system need not be restarted, field in class will be had to the class of any change, and in class, existing method body there is the class of any change dynamically to replace.
The method of field in a kind of dynamic replacement class provided by the invention, the method for a class and field information split and safeguard process by it, and complete the amendment renewal of method body in class with the realization of an abstract class.Its detailed process can be shown in Figure 1.
Fig. 1 is the method flow diagram dynamically replacing field in class in the present invention, and its method comprises:
Step 110, starts JVM, loads the process that a specific agentlib realizes JVMTI event.
Agentlib described here is equivalent to a dynamic link library, in one embodiment, described agentlib can comprise interface and event etc., after different events is triggered, it by function corresponding in interface interchange inside, all can carry out the amendment (comprising the production etc. of the generation of abstract class and the specific implementation class of abstract class) of class in following in described agentlib.Whether whether usually, when specific implementation, its inside can also comprise a global information table, marked each class and be loaded and by information such as first time loadings in described global information table.
Step 120, after Class File Load Hook event is triggered, has a new class to be loaded.
General, a new class can be loaded in master routine by described Class File Load Hook event after receiving the message.
Step 130, analyzes such bytecode (ByteCode), and according to global information table, whether be this ClassLoader first time be loaded into such, if so, then perform step 140 if detecting; Otherwise, perform step 150.
General, when running java applet, first run JVM, and then Java class is loaded in JVM runs, this part of load loading Java class is just called ClassLoader.
Described global information table is included in the specific agentlib of described loading, and it is equivalent to a database, inside marked each class whether be loaded and class whether by information such as first time loadings.Can detect whether the class for ClassLoader loading is be loaded into first time according to described global information table like this.
Step 140, if described class belongs to the class that needs are dynamically replaced, then realize dynamically replacing field in described class, flow process terminates.Wherein, the detailed process of carrying out dynamically replacing to field in described class can be shown in Figure 2.
In Fig. 2 the present invention, class is loaded into its field rear by the method flow diagram dynamically replaced by first time, and its method comprises:
Step 1401, with such ClassLoader and such signature, and after analyzing all fields in such, registers the information of its field to show that such is enhanced in global information table.
All fields in such are all extracted by step 1402, replace with a self-defining field information class field (_ field_).
Step 1403, adds a static initialization block (Static Initialization Blocks), utilizes the field information extracted to do initialization to described self-defining field information class field.
When specific implementation, described field category information field _ field_ comprises a Hash (hash) table, and obtains the information such as field method (getfield) and amendment field method (setfield).
Step 1404, utilizes the method information in such, generates an abstract class (abstract class), and adds the field (_ method_) of this abstract class type in such.Wherein, in the described abstract class of generation, comprise methods all in former class, but the described method in abstract class is abstract method.
Due in the process of process, each example of described original class may need to create different objects, can very loaded down with trivial details and trouble if repeatedly create class and method, so utilize the method information creating abstract class in original class here.Different specific implementation classes is generated to realize different examples like this by abstract class.Like this, from the characteristic of abstract class, when carrying out specific implementation to original class, without the need to changing any data in original class, and only need to utilize abstract class.
Step 1405, utilize the method information in such, described generation abstract class to generate the specific implementation class of a described abstract class, and needs reading any in class or amendment are enhanced calling of class field, all change the acquisition field method (getField) or amendment field method (setField) that call field information class in such into, and to need the signature of the field read for parameter, then do necessary type conversion according to actual conditions; Needs any in class are called and is enhanced calling of class methods, all change into and call method corresponding in type field _ method_ of abstract class described in such.
As from the foregoing, reading or revising to needing in class the amendment being enhanced class field and the method for calling to enhancing class methods here, being all carry out in the concrete implementation of class of abstract class, but not changing original class.
In one embodiment, need to read or revise to be enhanced calling of class field when finding in the process scanned original class A (namely strengthening class) to exist, then directly utilize self-defining getField method to call and strengthen class field.That is, when exist in original class A read or amendment is enhanced the calling of class field time do not recycle method of calling in original class A, but change a kind of self-defining getField method into and realize strengthening calling of class field.
In one embodiment, described acquisition field method getField flow process is as follows: the up-to-date field information first inquiring about under this ClassLoader such from described global information table, to determine that whether comprising this in described up-to-date field information is enhanced class field, if be enhanced class field described in not comprising, produce an exception without this field; If comprise this to be enhanced class field, then judge further to need whether the class field that is enhanced obtained is static fields information (Static Fields), if static fields information from described global information table, then to inquire about under described ClassLoader such this be enhanced the value of class field, and to return with an Object; If desired the class field that is enhanced obtained is non-static field information, then obtain the information of this field in the hash table from this info class, and return with an Object.
In another embodiment, described amendment field method setfield comprises: the up-to-date field information inquiring about under this ClassLoader such from described global information table, to determine that whether comprising this in described up-to-date field information is enhanced class field, if do not comprise this to be enhanced class field, produce an exception without this field, if this is enhanced class field is unchangeable field, then produce a unchangeable exception; Otherwise judge that this is enhanced class field whether as static fields, the class field that is enhanced if desired revised is static fields, then this arranging under this ClassLoader such in described global information table is enhanced the value of class field; If desired the class field that is enhanced revised is non-static field, then revise the value that this is enhanced class field in the hash table from this info class.
Wherein hash table is applicable to preserve the object that in former class, field is corresponding, and the field information in the value of its inside and class maps one by one.
If that is detect in original class that needs call to read or amendment when being enhanced class field, do not recycle original method of calling, but call self-defining getField or setField mode and realize, thus dynamic replacement can be realized; If scan in original class containing needs call read or amendment is enhanced class methods time, do not recycle original method of calling yet, but call method corresponding in self-defining _ method_ to realize dynamic replacement.
Step 1406, adds a static original block, goes to check whether such has dynamically updated according to the information of some necessity, and use the up-to-date specific implementation class of abstract class by its initialization, flow process terminates.
The information of described necessity can realize class mark etc. for up-to-date in global information table.In one embodiment, a static code can be added in such, when then starting the ClassLoader loading such after class instantiation, now go the change checking static code described in the rear class of concrete enforcementization, if change, showing that such is dynamically updated.
The above-mentioned a kind of method only enumerated simple detection type and dynamically update, usually checking whether class has by the technology dynamically updated is that those of ordinary skill in the field can both realize, so just no longer specifically describe in detail one by one here.
And up-to-date specific implementation class can be understood as: when loading classes again, its class realize the class that class obtains instantiation after initialization, its described abstract class obtained by original class also by instantiation, thus can produce new specific implementation class.Therefore, when checking that such is dynamically updated and needing to reinitialize, then before up-to-date being also being dynamically updated the specific implementation class of last abstract class come initialization such.Here the feature of the specific implementation class of abstract class and abstract class is existing background knowledge, and not as technical essential of the present invention, so just no longer describe in detail.
Step 150, if described class belongs to the class that needs are dynamically replaced, then realize respectively dynamically replacing field in described class and method body, flow process terminates; Wherein, realize respectively carrying out dynamically replacing being specially to field in described class and method body:
Utilize such ClassLoader and such signature, and after analyzing all fields in such, in a global information table, check the information upgrading its field; Utilize the method information in such and such abstract class generated when first time is loaded into generate the specific implementation class of a described abstract class, and needs reading any in described primitive class or amendment are enhanced calling of class field, all change getField or the setField method (specifically see foregoing description) calling field information class in such into, and to need the signature of the field read for parameter, then do necessary type conversion according to actual conditions; Needs any in class are called and are enhanced calling of class methods, all change into call in such _ method_ in corresponding method.
Itself and described class is loaded into for the first time time the difference that processes be: not regeneration abstract class, but the abstract class generated when utilizing upper first time to be loaded into is to generate a specific implementation class.
In sum, the present invention safeguards process by the method for a class and field information being split to come, and the amendment renewal of method in class is completed with the realization of an abstract class, process and remould is carried out from the aspect of the ByteCode of Class file, make user can without the need to additionally developing coding, both can under the prerequisite without the need to restarting application, the field of java class is added arbitrarily, deletes and revised, realize dynamically replacing, significantly improve development efficiency, and can be easy to use on general JVM.
Above-mentioned explanation fully discloses the specific embodiment of the present invention.It is pointed out that the scope be familiar with person skilled in art and any change that the specific embodiment of the present invention is done all do not departed to claims of the present invention.Correspondingly, the scope of claim of the present invention is also not limited only to previous embodiment.