User devices is a term that applies to computer systems such as desktop computers, smart televisions (TVs) and mobile computing devices such as laptop computers, mobile phones, tablets, “smart” watches, and eye-glasses, to list a few examples. More specific examples of user devices include smartphones, tablet computing devices, and laptop computers running operating systems such as Windows, Android, Linux, or IOS, in examples.
Software developers are increasingly utilizing modern software development platforms to enable the creation of machine-independent applications for installation and execution on different target user devices, or simply user devices. Unlike machine-dependent applications, which are software applications that can run only on a particular type of computer/user device, machine-independent applications can run on a variety of user devices. The machine-independent applications that the developers create for execution on the user devices are also known as user apps.
Software development platforms are applications running on host systems that enable software developers to build and test the user apps on the host system before installing and executing the user apps on the user devices.
Software development platforms typically use a combination of software libraries and other executables that present well-defined Application Programming Interfaces (“API”) to software developers for creating and testing the user apps. Modern software development platforms typically include a programming language, the compiled output of which executes within the context of a runtime environment of the user devices. Modern user devices typically utilize machine-dependent programs known as virtual machines (“VM”) to implement the runtime environment on the user devices.
VMs permit an isolated processing environment to exist on a computer system. VMs run on top of an operating system of the computer system. VMs hide or abstract the details of the underlying computer system from software applications that execute within the context of the VMs, also referred to as “running on top of the VMs.” To create platform-independent applications, such as user apps, software developers use the software development platforms to compile the programming language source code of the user apps into a machine independent output format for execution on the VMs of the user devices. The machine independent output format is also known as bytecode.
Bytecode is typically a set of binary files that include platform-neutral instructions for implementing application behavior. The VMs interpret the bytecode, and execute corresponding native (e.g. machine-dependent) instructions on the target computer system/user device associated with the bytecode.
Examples of software development platforms include the Android, Java, and .NET platforms. Android is a registered trademark of Google, Inc. Google associates the Java trademark with its eponymous computer programming language, operating system, and related infrastructure and tools. In examples, runtime environments for Android include the Dalvik and ART VMs. Java is a registered trademark of Oracle Corporation. Oracle associates the Java trademark with its eponymous computer programming language, Java Virtual Machine (“JVM”) runtime environment, and related infrastructure and tools. .NET is a registered trademark of Microsoft, Inc.
Developers use the software development platforms to author the source code of the user apps. In the case of the Java programming language, the source code is included within class files. A Java compiler converts the source code for each Java class definition into its associated bytecode. For example, the Java compiler accepts a source file named “MyClass.java” with source code that includes the class definition for named class “MyClass,” converts the source code to bytecode, and stores the bytecode in class file “MyClass.class.”
Android is a mobile operating system for Android user devices. Android is a registered trademark of Google, Inc. and is based on the Linux kernel. User apps that extend the functionality of Android devices are developed primarily in the Java programming language.
In class-based object-oriented programming, a constructor in a class is a special type of subroutine or method that is called to create an object for the class. An object is a specific instance of a class. Constructors of a class prepare the new object for use, often accepting arguments to set and/or initialize required member variables of the class. A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers than instance methods. Constructors often have the same name as the declaring class. Constructors have the task of initializing an object's data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting object created for a class in a valid and deterministic state.
Redefinition of classes at runtime is a well-known practice. In Java, the HotSpot VM has provided the ability to redefine classes at runtime since JDK 1.4. This functionality is based on the work of Mikhail Dmitriev, from “Safe Class and Data Evolution in Large and Long-Lived Java Applications,” PhD thesis, University of Glasgow, 2001. This functionality is better known as HotSwap. In addition, a publication by Allan Raundahl Gregersen, “Extending NetBeans with Dynamic Update of Active Modules,” PhD thesis, University of Southern Denmark, 2010, discusses dynamic update of code modules using the NetBeans development platform. NetBeans is a registered trademark of Oracle, Inc.
Class loading refers to loading of class files for an application such as a user app on a target user device The class files are included within a file system of either the user app or of a desktop system, in examples. A class loader loads the class files for a user app when an instance of the user app is first created. Class reloading also involves loading of classes, but is associated with loading changes to the classes initially loaded by the class loader.
The classes loaded when the original instance of the user app is first created are also known as original classes. Typically, the original classes of a user app are maintained within a file system. The constructors within an original class are also known as original constructors.
Classes that include changes to the original classes are also known as changed classes. Changed classes can include new constructors, original constructors, and modified versions of the original constructors, also known as changed constructors. The changed classes are also maintained on a file system.
Class transformation is the process of modifying the bytecode of original classes and changed classes of an application. Class transformation is typically executed offline.
Class transformation of original classes is typically executed via a service running on the server system. Class transformation of changed classes is performed while an application instance has completed initializing and is currently executing.
Current software development platforms like Java support limited types of runtime class reloading in their VMs, such as that provided by HotSwap for the JVM runtime environment. Using HotSwap, a developer can create a new definition for a class file of a user app currently loaded in a currently running instance of the user app, and apply this new definition of the class file without having to stop and restart the instance of the user app on the user device to incorporate the new class definition. The new class definition is also known as a class redefinition.
The runtime class redefinition capability of HotSwap is limited. HotSwap supports the ability to perform runtime modification of the fields and methods of classes of a running user app. However, HotSwap does not support the ability to modify constructors of, nor add new constructors to, the classes of a running user app.
Current HotSwap implementations are built into stock versions of major JVMs, and only support changes to method bodies. However, an extended capability set has been proposed first by Mikhail Dmitriev, in the aforementioned reference, and later by Thomas Würthinger in “Dynamic code evolution for Java, PPPJ '10 Proceedings of the 8th International Conference on the Principles and Practice of Programming in Java.” The Dynamic Code Evolution VM (DCEVM) allows arbitrary changes to class definitions. Currently, the most widely used class reloading system is the JRebel system, an application-level system that enables runtime reloading of classes by utilizing bytecode re-writing at class load time. JRebel is a registered trademark of ZeroTurnaround USA, Inc. The JRebel system does support reloading of constructors in general for the Java Platform.
Spring Loaded is a class reloading system capable of reloading complex class changes including changes to constructors.
However, the successful reloading of changed constructors with JRebel or Spring Loaded requires that either HotSwap is available on the target platform, or that the bytecode verifier is turned off. On the Java Platform, HotSwap was added with the release of Java 5.0. For Java versions prior to Java 5.0, such as Java 4.0, developers running with JRebel have to specify a JVM command-line argument to turn off bytecode verification. In other words, constructor reloading for Java versions prior to Java 5.0 with current technologies such as JRebel or Spring Loaded is only possible by producing illegal bytecode.
On the Android platform, HotSwap is not implemented at all. Neither the Dalvik nor the ART VMs support runtime class redefinition. Moreover, turning off the bytecode verifier while developing applications such as user apps is not always possible, is cumbersome, and can lead to unforeseen issues when the user app later goes into production.
While the class reloading systems mentioned herein above target the Java platform, none of them works in an off-the-shelf manner on Android user devices. There is currently one approach that does target the Android Platform, namely InstaReloader, which allows runtime class reloading of Android applications. It supports a broad spectrum of changes at runtime, but does not support changes to constructors. InstaReloader is an application level approach to runtime class reloading, thus it is not a virtual machine. InstaReloader injects bytecode into application classes to support runtime class reloading.
The present invention relates to the ability to dynamically redefine classes in a running Java application. More particularly, the present invention enables correct runtime behavior when constructors of original classes of a currently running instance of a user app on a user device are added or changed on a host system, and the classes including the additional constructors and the changed constructors are then sent to the user device and reloaded by a dynamic update. In response to the dynamic update, the running instance of the user app executes the functionality associated with the additional constructors and the changed constructors. In examples, changes to the constructors include when the arguments that are passed to the mandatory “super/this” constructor call in an original constructor have been changed. The method not only does not require runtime class redefinition capabilities like Java HotSwap, but also does not require disabling the standard Java bytecode verification feature.
In a preferred embodiment, the invention supports runtime class redefinition of classes of user apps running on Android user devices, where the redefined versions of the classes include additional constructors and/or changed constructors of the classes of the currently running user apps.
In general, according to one aspect, the invention features a method for updating a user app running within an Android virtual machine on a user device. The method comprises creating helper classes for changed classes of the user app, where the changed classes includes changed and/or new constructors, and the user app reloading the helper classes on the user device. Preferably, the helper classes are created on a host system, and the host system sends the helper classes to the user device.
The method further comprises creating transformed classes for original classes of the user app, wherein the original classes include original constructors, and the user app reloading the transformed classes on the user device along with the helper classes.
In one implementation, creating the transformed classes comprises providing identifiers for the original constructors; and transforming bytecode of the original classes into the transformed classes based on the identifiers. Preferably, creating the transformed classes comprises transforming bytecode of the original constructors of the original classes into transformed constructors based on the identifiers, and generating bytecode for a selector constructor within each of the transformed classes. The selector constructor enables runtime selection of most recent versions of the transformed constructors for each of the transformed classes, and enables runtime selection of most recent versions of the changed and/or new constructors for each of the changed classes.
In addition, the selector constructor enables runtime invocation of most recent mandatory constructor calls and runtime invocation of most recent constructor bodies of the transformed classes based on the identifiers.
In another implementation, creating the helper classes of the user app comprises providing identifiers for the changed and/or new constructors, and transforming bytecode of the changed classes into the helper classes based on the identifiers. Preferably, transforming bytecode of the changed classes into the helper classes comprises transforming bytecode of each changed and/or new constructor of the changed classes into a set of functionally equivalent static methods for each changed and/or new constructor based on the identifiers.
In examples, the user app can run within a Dalvik Android virtual machine of the user device or within an ART Android virtual machine of the user device.
In general, according to another aspect, the invention features a system for updating a user app. The system includes a user device running the user app within an Android virtual machine of the user device, and a host system. The host system creates helper classes for changed classes of the user app, where the changed classes include changed and/or new constructors. The host system then sends the helper classes to the user app, which reloads the helper classes on the user device. Typically, the user device includes a class reload system that enables the user app to reload the helper classes.
In general, according to yet another aspect, the invention features a method for updating a user app running within a virtual machine on a user device, wherein the virtual machine lacks runtime class redefinition support. The method comprises creating helper classes for changed classes of the user app, where the changed classes includes changed and/or new constructors, and the user app reloading the helper classes on the user device. In examples, virtual machines lacking runtime class redefinition support include the Dalvik and ART VMs, and Java Virtual Machine releases prior to Java 5.0, such as Java 4.0.
The above and other features of the invention including various novel details of construction and combinations of parts, and other advantages, will now be more particularly described with reference to the accompanying drawings and pointed out in any claims. It will be understood that the particular method and device embodying the invention are shown by way of illustration and not as a limitation of the invention. The principles and features of this invention may be employed in various and numerous embodiments without departing from the scope of the invention.
In the accompanying drawings, reference characters refer to the same parts throughout the different views. The drawings are not necessarily to scale; emphasis has instead been placed upon illustrating the principles of the invention. Of the drawings:
The host system 150 includes an operating system 170-1, a file system 122, and a virtual machine 50-1. The host system 150 also includes a service module desktop application (“service module”) 110. The service module 110 enables reloading of classes including constructors for a user app 108 running on a user device 151.
The user device 151 includes an operating system 170-2 and a virtual machine 50-2. User apps 108 and other applications on the user device 151 execute within the context of virtual machine 50-2, also referred to as running “on top of” the virtual machine 50-2. Applications other than the user apps 108 include a class reload system 134 In one example, the operating system 170-2 is Android.
The class reload system 134 includes a runtime constructor cache 132. The runtime constructor cache 132 includes constructor entries 138. The class reloading system 134 also defines utility classes 123 that implement useful common functions.
The host system 150 includes a service module 110, a virtual machine 50-1, and an operating system 170-1. The service module 110 runs on top of the virtual machine 50-1 and the virtual machine 50-1 runs on top of the operating system 170-1. The host system 150 also includes a file system 122 that includes classes of the user app 108.
The file system 122 of the host system 150 includes original classes 102 and changed classes 104 of the user app 108. The classes include bytecode, and it is the bytecode of the original classes 102 and changed classes 104 that the service module 110 modifies (e.g. transforms) to enable the runtime reloading of changes to constructors/addition of new constructors for classes of a running user app 108. The original classes 102 include one or more original constructors 112. The changed classes 104 may include the original constructors 112, one or more modified versions of the original instructors, also known as changed constructors 113, and one or more new constructors 114. The changed constructors 113 typically retain the function signature of the original constructors 112 but include changes to the code bodies of the original constructors 112. The new constructors 114 correspond to constructors having different function signatures than the original constructors 112. Developers create the changed classes 104 on the host system 150 to change the behavior of the user app 108.
In one example, the original and changed classes 102/104 are Java classes, and the virtual machine 50-2 of the user device 151 is a Java Virtual Machine (JVM) 50-2. However, the bytecode format of the classes can also be a non-standard or proprietary format, as long as the VM 50-2 is also instrumented such that it capable of understanding and executing the associated bytecode format of the classes.
If the user apps 108 are Android-based, meaning that the user apps 108 execute on an Android operating system 170-2 of the user devices 151, the source code of the user apps 108 is typically Java-based and typically compiled to standard Java bytecode classes. In other examples, the source code of the user apps 108 can be Kotlin, Groovy, Scala or any other language having a compiler that produces Java bytecode classes. Using Android-specific tools, developers then convert the java bytecode to an Android-proprietary bytecode/class format called DEX. These classes then execute on an Android-specific virtual machine 50-2 such as “Dalvik” or “ART” on the user devices 151. It is these Android-specific classes that developers preferably create on the host system 150 and then send to the Android user device 151 to be loaded/reloaded by the user apps 108. For Android user apps 108 that execute on a Dalvik VM 170-2 on an Android user device 151, in one example, developers convert Java class files into DEX files on the host system 150.
The host system 150 and the user devices 151 communicate via a network connection 86. The user devices 151 receive the classes and other data sent by the host system 150 over the network connection 86. In examples, the network connection 86 is a wired USB connection, or wireless Bluetooth/WiFi connection.
The service module 110 includes a class processing tool 120, one or more class listener threads 131 and a constructor cache 130. The constructor cache 130 includes constructor entries 138. The class processing tool 120 includes a parser 106.
The class processing tool 120 preferably operates with standard Java classes and Java bytecode format. However, the class processing tool 120 can also process classes that were compiled in an Android-proprietary bytecode format, in another example.
On the Android platform, there is no Java Virtual Machine. Instead, Java classes are compiled into an Android-proprietary bytecode format and run on an Android-specific VM 50-2 on the user devices 151. Examples of Android-specific VMs 50-2 include Dalvik and Android Runtime (ART).
The file system 122 also includes transformed classes 184 and an update package 136. The service module 110 typically creates a transformed class 184 for each original class 102. Each of the transformed classes 184 includes one or more transformed constructors 116 and a selector constructor 118. The update package 136 includes one or more versioned helper classes 124 and includes constructor entries 138 obtained from the constructor cache 130.
The service module 110 processes (arrow 205) original classes 102 of/for a user app 108 by reading the bytecode of the original classes 102 from the file system 122 into the memory of the service module 110. The bytecode is represented in memory on the service module 110 as a byte array. Note that the service module 110 can process the original classes 102 before the user app 108 is running. The parser 106 of the service module 110 parses the bytecode of the original classes 102 and saves information (arrow 204) associated with each of the original constructors 112 within a constructor entry 138 in the constructor cache 130. The information saved to each constructor entry 138 includes a unique index associated with each constructor for each of the original classes 102.
The service module 110 then transforms the original classes 102 by modifying the bytecode of the original classes 102. The complete set of modified bytecode for each original class 102 is also known as a transformed class 184. The transformed classes 184 are the produced result (arrow 206) from processing of the original classes 112. To create the transformed classes 184, the service module 110 uses the parsed bytecode of the original classes 102 in conjunction with the constructor entries 138 of the constructor cache 130. The service module 110 saves the transformed classes 184 onto the file system 122. In another example, the service module 110 can maintain an in-memory representation of the processed classes instead of saving them to the file system 122.
The service module 110 then reads in the transformed classes 184 (arrow 207a), and sends them to the user device 151 (arrow 207b). In one example, the service module 110 starts the instance of the user app 108 with the set of transformed classes 184 and the set of constructor cache entries 138 that were produced from processing of the original classes 102.
In another example, the constructor cache entries 138 are sent with the set of transformed classes 184 during the initial startup of the user app 108. When the system 134 receives constructor cache entries 138 from the service module 110, the class reload system 134 copies the received constructor cache entries 138 into the runtime constructor cache 132. In yet another example, the constructor entries 138 are not sent to the class reload system with the transformed classes 184. Instead, the constructor entries 138 are sent with the first update package 136 that the service module 110 sends to the class reload system 134. The service module 110 then creates versioned helper classes 124 for each of the changed classes 104, and includes the versioned helper classes 124 along with relevant constructor cache entries 138 in an archive. This archive is also known as an update package 136. In one example, the archive is an Android Package File (APK).
To process the changed classes 104 into the versioned helper classes 124, in a preferred implementation, the class listener thread 131 of the service module 110 determine which classes have changed (and therefore which classes to process once the user app 108 is already running) by detecting changes to the classes on the file system 122. The class listener thread 131 automatically detects changes to the classes by identifying changes to time stamps of the classes. The class listener thread 131 then provide the names of the changed classes 104 to the class processing tool 120. In another implementation, the names of the changed classes 104 can be sent manually to the service module 110 via a command-line interface or Graphical User Interface (GUI) tool.
In response to detecting changes to any of the original class files 102 residing on the file system 122, the service module 110 reads in (arrow 210) the bytecode of the changed classes 104. The parser 106 of the service module 110 parses the bytecode of the changed classes 104 and saves information (arrow 204) associated with each of the changed constructors 113 and new constructors within a constructor entry 138 in the constructor cache 130.
One example of a changed constructor 113 is when a mandatory constructor call statement (MCC) of an original constructor 112 is modified. An MCC of a changed constructor 113 specifies a different super constructor to invoke than the super constructor specified by the MCC of the original constructor 112.
Via the class processing tool 120, the service module 110 uses the parsed bytecode of the changed classes 104 in conjunction with the constructor entries 138 of the constructor cache 130 to transform the bytecode of the changed classes 104 into a set of versioned helper classes 124. This is indicated by arrow 212. The service module 110 includes the versioned helper classes 124, and the set of constructor cache entries 138 not already synched from the constructor cache 130 to the runtime constructor cache 132, within an update package 136 and saves the update package to the file system 122.
The constructor cache 130 includes a master copy of all constructor entries 138 for the constructors of the classes for the user app 108. The service module 110 copies the constructor entries 138 from the constructor cache 130 into the runtime constructor cache 132 on the user device 151. This provides the utility classes 123 with the ability to lookup information associated with the constructors, which the utility classes then use to select the proper methods to invoke within the versioned helper classes 124.
The service module 110 preferably creates one versioned helper class 124 for each changed class 104. Each versioned helper class 124 includes bytecode for implementing the behavior of its associated changed class 104, including bytecode for each new constructor 114 and changed constructor 113 of each changed class 104. The service module 110 also creates the update packages 136. Each update package 136 includes one versioned helper class 124 for each changed class 104 and the constructor entries 138. When the classes use Java bytecode format, the update packages 136 include bytecode in Java bytecode format.
If the user apps 108 are Android-based, meaning that the user apps 108 execute on an Android VM 50-2 and operating system 170-2, the service module 110 utilizes Android-specific conversion tools to convert the java bytecode of the processed classes to Android-proprietary bytecode/class format. The service module 110 then includes classes having Android-specific bytecode format within the update packages 136.
The service module 110 then controls the class reloading by loading (208a) the update package 136 from the file system 122, and sending the update package (arrow 208b) to the class reload system 134 of the user app 108. The class reload system 134 then loads the bytecode of the versioned helper classes 124 in the update packages 136 into the user app 108 and effectuates the reload operation.
The class reloading system 134 also provides public interfaces that indicate if an original class 102 has been reloaded. The class reload system 134 determines that an original class 102 has been reloaded when the class reload system 134 receives an update package 136 for the original class.
On the user device 151, the class reloading system 134 uses the utility classes 123 at runtime when the code of selector constructors 118 are executed. The utility classes perform lookups of the constructor entries 138 in the runtime constructor cache 132 to obtain up-to-date information for the constructors of the classes. The utility classes 123 then use the updated constructor information to enable the execution of methods in versioned helper classes 124.
The class reload system 134 is able to communicate freely with the user app 108. During the bytecode transformation process, the service module 110 can inject bytecode, the code statements of which link the transformed classes 184 with classes declared by the class reload system 134 when the transformed classes 184 are loaded into the virtual machine 50-2.
The service module 110 is included within the user app and is preferably implemented as a Java agent. This enables direct communication between the user app 108 and the service module 110 on the user device 151.
Unlike in
As in the system of
The file system 122 also includes versioned helper classes 124 and transformed classes 184. Each of the transformed classes 184 includes one or more transformed constructors 116 and a selector constructor 118. The service module 110 includes a class reload system 134.
The service module 110 determines the class path for the user app 108. This enables the processing of original classes of the user app 108. Whenever a class loading event occurs within the virtual machine 50-2 on the user device 151, the service module 110 determines if the class loading event is for loading an original class 102.
If the service module 110 determines that a user app 108 class loading event is associated with an original class 102, the service module 110 processes (arrow 205) the bytecode of the original classes 102 of a user app 108. In the event that the service module 110 is implemented as a Java agent to intercept loading of the classes, arrow 205 also represents the byte array passed to the Java agent class loading hook (e.g. “-javaagent . . . ”). when the contents of the classes are passed in a byte array format to the java agent. The parser 106 of the service module 110 parses the bytecode of the original classes 102 and saves information (arrow 204) associated with each of the original constructors 112 within a constructor entry 138 in the runtime constructor cache 132. The information saved to each constructor entry 138 includes a unique index associated with each constructor for each of the original classes 102.
The service module 110 then further transforms the original classes 102 by modifying or transforming the bytecode of each original classes 102 into bytecode of an associated transformed class 184. To create the transformed classes 184, the service module 110 uses the parsed bytecode of the original classes 102 in conjunction with the constructor entries 138 of the runtime constructor cache 132. The service module 110 saves the transformed classes 184 as a new byte array.
The service module 110 then passes the byte array for the transformed class 184 to the virtual machine 50-2, which in turn defines the transformed class 184 into the native code of the virtual machine 50-2.
In response to detecting changes to any of the original class files 102 residing on the file system 122, the changes of which are included in associated changed classes 104, the service module 110 reads (arrow 210) the bytecode of the changed classes 104. The parser 106 of the service module 110 parses the bytecode of the changed classes 104 and saves information (arrow 204) associated with each of the changed constructors 113 and new constructors 114 within a constructor entry 138 in the runtime constructor cache 132. The service module 110 updates existing constructor entries 138 in the constructor cache 130 with the information from the changed constructors 133, and adds a new constructor entry 138 for each new constructor 114. The information saved to the updated or new constructor entries 138 includes a unique index associated with each changed or new constructor for each of the changed classes 104.
Using the class processing tool 120, the service module 110 transforms (arrow 212) the bytecode of changed classes 104 into a set of versioned helper classes 124. To create the versioned helper classes 124, the service module 110 uses the parsed bytecode of the changed classes 104 in conjunction with the constructor entries 138 of the runtime constructor cache 132.
In general, when processing any classes of the user app 108, the service module 110 identifies each constructor of each class, and creates a constructor entry 138 with a unique constructor index 201 for each constructor within the current class. The service module 110 stores information for each constructor within its associated constructor entry 138 in the constructor cache 130. The service module 110 uses the constructor entries 138 to keep track of the versions of all constructors of all classes ever loaded (or reloaded) for each instance of a user app 108 on a user device 151.
The constructor data 202 includes the original class name 190 that declares the constructor, the constructor signature 191, a Boolean value, isOriginalConstructor 192, and one or more mandatory constructor call (MCC) indices 203.
Typically, the uniqueness of the constructor indices 201 for each constructor entry 138 is ensured by combining multiple different values to create the indices 201. In one implementation, the constructor indices 201 are calculated by combining the identifier (id) of the class loader that loads the class declaring the constructor, the original class name 190 that declares the constructor, and the signature of the constructor 191.
Exemplary values “1,” “2,” and “3” for constructor indices 201-1, 201-2, and 201-3, respectively, are shown. This allows each of the associated constructor entries 138-1, 138-2, 138-3 to be uniquely searched or “looked up” within the constructor cache 130.
Each MCC index 203 refers to a constructor entry 138 in the constructor cache 130. This is indicated by reference 139. Specifically, the value of each MCC index 203 corresponds to the value of the constructor index 201 of an associated constructor entry 138. For example, with respect to reference 139, MCC index 203-1 value “1” indicates that the constructor for constructor entry 138-1 includes an MCC. The constructor that the MCC statement invokes is represented by constructor entry 138-3.
The value of isOriginalConstructor 192 indicates whether the constructor is an original constructor 112 or a new constructor 114.
The constructor cache 130 includes one constructor cache entry 138 for each original constructor 112 and new constructor 114. When an original constructor 112 has been changed (e.g. the MCC within the constructor now references a different super constructor to invoke), the service module 110 updates the value of the MCC index 203 of the original constructor 112 to “point” to the constructor index 201 for the different super constructor. Note that the service module 110 does not create unique constructor cache 130 entries for changed constructors 113. Instead, the service module 110 updates the contents of an existing constructor cache 130 entry in response to detecting changes to the constructor associated with the constructor cache 130 entry.
Storing unique constructor cache entries 138 for each constructor, and maintaining up-to-date information about MCCs within each constructor, is a preferred implementation within the service module 110 to uniquely identify all constructors of all classes loaded by a user app 108 and to identify how the constructors are chained together by the MCCs. Using the constructor cache entries 138, the service module 110 can reconstruct the constructor call hierarchy of all loaded and reloaded constructors of a running user app 108. In this way, the service module 110 can provide deterministic runtime behavior of the user apps 108 in the presence of runtime class reloading of the user app's classes, when the reloaded classes include changes to the original versions of the constructors and new constructors, in examples. It can be appreciated, however, that there can be other implementations.
When processing the classes, the method executes different code paths. The method executes code path 111-1 when processing original classes 102 and executes code path 111-2 when processing changed classes 104. Note that code paths 111-1 and 111-2 both initially traverse steps 402 through 414, and then diverge thereafter.
To illustrate the bytecode transformation that the service module 110 executes on the original and changed classes 102/104, the method of
Processing of Original Classes: Applying the Method of
Returning to
In step 404, the parser 106 parses the compiled bytecode of the current class, and identifies each constructor within the current class. With respect to the example original classes 102-A and 102-B, the parser 106 identifies original constructor 112-1 of original class 102-A and original constructor 112-2 of original class 102-B.
In step 406, the service module 110 creates a unique constructor entry 138 in the constructor cache 130 for each identified constructor in the current class, and for all constructors referenced in the inheritance hierarchy of each identified constructor, unless the identified constructor already has a constructor entry 138 in the constructor cache 130.
When processing example original class 102-A, the parser 106 identifies one constructor, 112-1. Then, the parser 106 identifies one constructor within the call hierarchy of constructor 112-1, an implied constructor that invokes the super class of the original class 102-A. The super class is implicitly “java.lang.Object.” This is because no super class is explicitly stated in the class definition of class 102-A (e.g. the Java “extends” keyword does not specify the name of another class which class A “extends.”) For the implicit super( ) constructor, the parser 106 creates the constructor entry 138-1 and writes a unique value “1” for constructor index 201-1. Then, the parser 106 creates constructor entry 138-2 for the actual identified constructor 112-1, and writes value “2” for its constructor index 201-2.
For constructor entry 138-1, the parser 106 creates constructor data 202-1 and initializes its data fields. Within constructor data 202-1, the parser 106 writes value “java.lang.Object” for the original class name field 190-1, and a no-argument value for the constructor signature 191-1.
When processing example original class 102-B, the parser 106 identifies one constructor, 112-2. Then, the parser 106 identifies one constructor within the call hierarchy of constructor 112-2, the constructor 112-1 of class 102-A. This is because class B (102-B) “extends” class A (102-A) in the class definition of class 102-A.
Because a constructor entry for constructor 112-1 already exists in the constructor cache 130, however, the parser 106 only creates constructor entry 138-3 for the actual identified constructor 112-2 for original class 102-B, and writes value “3” for its constructor index 201-3.
Returning to
In step 410, the parser 106 parses the bytecode instructions within each constructor of the current class to identify the bytecode of any mandatory constructor calls/invocations (MCCs) within each constructor. In examples, mandatory constructor calls (e.g. invocations) are associated with Java “super( )” and “this( )” code statements. In
In step 412, for each identified MCC of any changed constructors 113, the service module 110 stores a unique identifier for each mandatory constructor call statement. The identifier for the MCC is stored within the constructor's associated constructor entry 138 in the constructor cache 130. Applying step 412 to the example original classes 102-A and 102-B, in
Then, because constructor entry 138-1 is associated with a non-reloadable class, java.lang.Object( ), the service module 110 writes value “0” to the MCC index 203-1 field of the constructor data 202-1 of constructor entry 138-1. Value 0 is a special “don't care” value for all constructor entries associated with non-reloadable classes. For this purpose, the service module 110 first processes MCC 302 for original class 102-B. To represent MCC 302, the service module 110 writes value “2” to the MCC index 203-3 field of the constructor data 202-3 of constructor entry 138-3.
It is important to note that value “1,” for MCC index 203-2, is the same as the value of the constructor index 201-1 for constructor entry 138-1. This is indicated by reference 139-1. In a similar fashion, value “2,” for MCC index 203-3, is the same as the value of the constructor index 201-2 for constructor entry 138-2. This is indicated by reference 139-3. This mapping between the constructors associated with constructor entries 138 and MCCs referenced within constructor entries provides the critical ability for the service module 110 to track all constructors of all versions of all classes ever loaded (or reloaded) on user apps 108. This is especially the case for user apps 108 running on top of the Android operating system 170-2 on a user device 151.
Returning to
With respect to the constructor entries 138 created for 102-A and 102-B, in
Returning to
When generating the bytecode of the transformed classes 184-A and 184-B, the service module 110 utilizes the utility classes 123 of
In
Utility class 123-2 for class ConstructorHelper includes five exemplary helper methods. The first helper method is getMCCIndex(int constructorIndex) 1102. This helper method returns the index representing the mandatory constructor call for the constructor with the input “constructorIndex.” Method getTrueMCCIndex( ) 1103 operates on changed constructors 113 and new constructors 114.
For the example versioned helper class A_1124-A as shown in
Returning to
In step 418, within the “if” block of the conditional statement created in step 416, insert bytecode that invokes a selector constructor 118 of the current class. The arguments passed to the selector constructor 118 include the unique index for the currently parsed constructor 112. In
In step 420, within the “else” block of the conditional statement of step 416, which is reached at runtime on the user app 108 when there is no versioned class 124 for the currently executing class, the service module 110 inserts bytecode that jumps to the beginning of the currently parsed constructor. In
In step 500, upon reaching the end of the bytecode of the current class, the service module 110 generates bytecode for the body of a selector constructor 118. The service module then appends the bytecode for the selector constructor 118 to the current transformed class 184. In
In step 502, the service module 110 creates a function signature for the selector constructor 118. The formal parameters of the selector constructor 118 include an object array type indicated by ‘originalArguments,’ and a special placeholder of type ConstructorPlaceHolder that internally stores a specific unique constructor id, indicated by “index.” In
In step 504, the service module 110 inserts bytecode for a method invocation (“getMCCIndex”) indicated by reference 1102 in
In
In step 604, a lookup of the MCC index 203 within the “callerEntry” constructor data 202 is performed. The constructor entry 138 pointed to by the MCC index 203 returned from the lookup is saved to local variable “MCCIndex”. According to step 606, the method looks up the constructor entry 138 for the saved “MCCIndex” within the runtime constructor cache 132. The constructor cache entry 138 returned from the lookup of the runtime constructor cache 132 is saved to local variable “calleeEntry”.
In step 608, which is a conditional block where the method checks whether the constructor data 202 of the “calleeEntry” is an original constructor 112. In that case the execution flow transitions to step 610, in which the already found “MCCIndex” is returned from the method. Returning to step 608, in case of a new constructor 114, a further conditional check is carried out by step 612, where the original class name data 190 within the constructor data 202 of the “callerEntry” and “calleeEntry” constructor entries 138 are checked for equality. In the “yes” branch from step 612, the method returns the special signal value (−1) in step 614, indicating that the MCC should currently be invoked to a new constructor 114 within the same class as the selector constructor 118 that called the “getMCCIndex”. In the “no” branch of step 612, the method carries on to step 616 where the special signal value (−2) is returned, indicating that the MCC should currently be invoked to a new constructor 114 within the super class of the class declaring the selector constructor 118 that called the “getMCCIndex”.
If the constructor index 201 initially found within the body of the “getMCCIndex” method 1102 corresponds to an entry within the constructor cache 130 that represents a new constructor 114, where the new constructor 114 was added by a previous class reload operation, method 1102 will return one of the two special signal values, (−1) or (−2). These signal values are used to specify to the selector constructor 118 that a direct call to the MCC, as referenced by 125-2 case “1” in
Hence, the service module 110 inserts bytecode for invoking either the same selector constructor 118 in the class or to the selector constructor 118 within the superclass. At runtime, when the special signal values (−1) or (−2) occur when the constructor selector 118 executes, the selector constructor 118 creates a new ConstructorPlaceHolder object with the “truelndex” as returned from the utility method getTrueMCCIndex( ). This is indicated by reference 1103 in
In step 620, the getTrueMCCIndex method 1103 initiates execution by looking up the constructor cache entry 138, within the runtime constructor cache 132, for the input argument “callerIndex.” The constructor entry 138 object returned from the lookup is saved to local variable “callerEntry”.
In step 622, the MCC index 203 of the “callerEntry” is looked up from the constructor data 202 and saved to a local variable “trueMCCIndex”, which is then returned in step 624.
Returning to
The service module 110 also inserts bytecode for preparing associated arguments, if any are required, for the chosen MCCs indicated by reference 301 and 302 in
In step 508, the service module 110 generates an opening brace for the switch/if-else code block. In
In step 510, the service module 110 checks if the super class of the class being processed is also a reloadable class. In one example, non-reloadable classes are system classes such as java.lang. Object or any other class within the Java JDK library. In other examples, the set of non-reloadable classes besides the JDK core classes also contains classes within referenced third party libraries of the user app 108.
In the event the superclass is a reloadable class, the control transitions to step 511 in which the method generates a separate case or conditional block within the “switch” statement that (at runtime) can handle invocation to the mandatory constructor call for constructors that might be added by class reloads within the superclass as the class currently being processed. In
In step 510, when the superclass is not reloadable, the control immediately transitions to step 512, leaving out the construction of the separate case or conditional block that can handle new constructors in superclasses. Applying
In step 512, for each constructor identified within the currently parsed class and the direct super class, create a new “case” block. The operand of the “case” block is the value of the constructor index 203 for the constructor's associated constructor entry 138 in the constructor cache 130.
These case blocks are added for all original constructors 112. The code statements within each case block will handle, at runtime, the MCC to the original constructors 112 in the class itself (i.e. all the this( ) calls with the class itself) as well as any original constructor in the super class (i.e. all the super( ) calls) regardless of the superclass being reloadable or non-reloadable. In
In step 514, within those case blocks, insert bytecode for one or more method invocations (“getCurrentConstructorArgs” referred to by
This enables the utility class 123 to locate the specific methods within versioned helper classes 124-A and 124-B, for which the arguments to the current MCC can be extracted by invocation of the most recent version of the synthetically generated method getCurrentConstructorArgs( ). This is indicated in
In step 630, the getCurrentConstructorArgs( ) method 1104 initiates execution by looking up the constructor entry 138, within the runtime constructor cache 132, for the input argument “callerIndex.” The constructor entry 138 object returned from the lookup is saved to local variable “callerEntry.”
Then, in step 632, the method looks up the constructor signature 191 from the constructor data 202 in the “callerEntry” and saves the result in a local variable “signature.”
In step 634, the method looks up the original class name 190, from the constructor data 202, within the “callerEntry” and saves the result in a local variable “originalClassName.”
In step 636, the method utilizes the class reload system 134 to lookup the most recent versioned helper class 124 for the “originalClassName” and stores the result of the lookup to local variable “versionedHelperClass.”
In step 638, the method constructs the method name and signature of the specific “getCurrentConstructorArgs” method, which is located in the versioned helper class 124, using the “originalClassName” and the “signature.”
Based on the constructed name and signature in the previous step, step 640 looks up the specific getCurrentConstructorArgs( ) method within the versioned helper class 124 and saves the result in a local variable “getMCCArgsMethod.” In one example, the lookup of the specific method is carried out by using the Reflection API of the Java platform.
In step 642, the method finally executes the “getMCCArgsMethod” using the input “thisObject” and the “originalArguments” array and returns the result of the invocation. The “thisObject” and “originalArguments” array is indicated in
Returning to
In step 650, the getArg method 1105, initiates execution by performing a conditional check if the input “arglndex” is zero or “0”. The “arglndex” is indicated in
Returning to
In step 654, which is reached directly through both the “yes” branch of step 650 as well as from 652, then retrieves the object/value stores by the thread local “args” value at the index given by the “argslndex” input value.
Returning to
In step 516, within each specific case block in which handling the MCCs for every existing/original constructor, the arguments, that were obtained from the subsequent invocations of the getArg method 1105, are now unpacked to the current stack, so that they match the formal parameter types of the constructor represented by the constructor entry 138 associated with the value of the current case block. In
In step 518, within those case blocks, insert bytecode that at runtime executes the mandatory constructor call represented by the value of the current case block passing the unpacked arguments from step 516 as arguments. In
In step 520, the service module 110 inserts a default case that throws a NoSuchMethodError at runtime. In
In step 522, the service module 110 generates a closing brace to end bytecode generation of the switch/if-else code block. In
Returning to
In step 541, the method inserts bytecode to invoke the method invokeBody( ) as referenced by 1106 in
In step 660, the invokeBody( ) method 1106 initiates execution by looking up the constructor entry 138, within the runtime constructor cache 132, for the input argument “callerlndex.” The constructor entry 138 object returned from the lookup is saved to local variable “callerEntry.”
Then in step 662, the method looks up the constructor signature 191 from the constructor data 202 in the “callerEntry” and saves the result in a local variable “signature.”
In step 664, the method looks up the original class name 190, from the constructor data 202, within the “callerEntry” and save the result in a local variable “originalClassName.”
In step 666, the method utilizes the class reload system 134 to lookup the most recent versioned helper class 124 for the “originalClassName” and store the result in a local variable “versionedHelperClass.”
In step 668, the method constructs the method name and signature of the specific “runConstructorBody” method, which is located in the versioned helper class 124, using the “originalClassName” and the “signature.”
Based on the constructed name and signature in the previous step, step 670 looks up the specific runConstructorBody( ) method within the versioned helper class 124 and saves the result in a local variable “runBodyMethod.” In one example, the lookup of the specific method is carried out by using the Reflection API of the Java platform.
In step 672, the method finally executes the “runBodyMethod” using the input “thisObject” and the “originalArguments” array. The “thisObject” and “originalArguments” array is indicated in
Returning to
In
In step 490, the service module 110 looks for more classes to process. If there are more classes, the method transitions to step 492 to go to the next class file, and then to step 404 to parse the current class for constructors. If there are no more classes to process in step 490, the method transitions to step 494 and ends processing.
Processing of Changed Classes: Applying the Method of
In
In
In
The parser 106 writes a value of “4” in constructor index 201-4 and value “A(int i, int j)” for constructor signature 191-4. The parser writes value “false” for the isOriginalConstructor( ) field 192-4 because the constructor was not present in original class 102-A, and writes value “1” for MCC index 203-4. Note that MCC index 203-4 references the java.lang. Object default constructor, given by constructor cache entry 138-1 with constructor index 201-1 value “1.” This is indicated by reference 139-1.
Constructor entry 138-5 is created in response to the parser 106 identifying new constructor 114-2, “public B(String message),” of changed class 104-B in
The parser 106 writes a value of “5” in constructor index 201-5 and value “B(String str)” for constructor signature 191-5. The parser writes value “false” for the isOriginalConstructor( ) field 192-5 because the constructor was not present in original class 102-B, and writes value “4” for MCC index 203-5. Note that MCC index 203-5 references the “A(int i, intj)” constructor, given by constructor cache entry 138-4 with constructor index 201-4 value “4.” This is indicated by reference 139-5.
For changed class 104-A, the parser 106 identifies one constructor within the call hierarchy of changed constructor 113-1, MCC 304 “super( ).” Because the value of MCC 304 of changed constructor 113-1 has not changed as compared to the value of MCC 301 of original constructor 112-1 (e.g. they both invoke “super( ),” the service module 110 does not create a new constructor entry 138 for MCC 304.
In a similar fashion, the parser 106 identifies one constructor within the call hierarchy of changed constructor 113-1, MCC 305 “super( ).” Because constructor entry 138-1 has already been created for “super( ),” the service module 110 does not create a new constructor entry 138 for MCC 305.
For changed class 104-B, the parser 106 identifies one constructor within the call hierarchy of changed constructor 113-2, MCC 306 “super(0, 200).” Changed class 104-B is a child class of changed class 104-A. Because constructor entry 138-4 has already been created for constructor 114-1 with signature “A(int i, int j),” the service module 110 does not create a new constructor entry 138 for MCC 306.
In a similar fashion, the parser 106 identifies one constructor within the call hierarchy of new constructor 114-2, MCC 307 “super(0, message.length).” Because constructor entry 138-4 has already been created for constructor 114-1 with signature “A(int i, intj),” the service module 110 does not create a new constructor entry 138 for MCC 307.
Returning to
In step 412, for each constructor 118, the method stores and/or updates unique identifiers for each mandatory constructor invocation MCC of any changed constructors 113.
In
However, for changed class 104-B, MCC 306 of changed constructor 113-2 does cause a change in the value of its associated MCC index 203-3, and the parser 106 updates its value from “2” to “4” accordingly. This is indicated by reference 139-3. As a result, MCC index 203-3 “points” to constructor entry 138-4.
Returning to
In step 460, the service module 110 generates bytecode for versioned helper classes 124 for each changed class 104. Each versioned helper class 124 includes new method definitions for the methods of its associated changed class 104. The content of the new method definitions in each versioned helper class 124 are based on and include the bytecode instructions of each currently changed class 104.
The method of
In step 462, the method begins creation of a versioned helper class 124 for the current changed class by generating bytecode of an opening brace of the versioned helper class 124.
Returning to
In
Returning to
In step 466, the service module 110 parses the bytecode of the changed classes 104 to identify the mandatory constructor invocations of the current changed classes 104. Then, the service module 110 parses the current constructor collecting all bytecode instructions that are present in the changed class 104 before the currently identified mandatory constructor invocation, and places the instructions in a buffer.
In
Returning to
In step 470, the service module 110 generates bytecode for storing the contents of the runtime stack into an array of objects that represent the arguments that will be passed to the current constructor's MCC, and append to buffer. The runtime stack refers to the actual values loaded onto the stack by method “getCurrentConstructorArgs” at runtime when executing the instructions that are present before the MCC, as collected in step 466.
In step 472, the service module 110 copies the buffer contents to the versioned helper class 124, generating a closing brace for the “getCurrentConstructorArgs” method and resets the buffer.
In
Returning to
In step 476, the service module 110 collects all bytecode instructions that are present after the MCC, and place the instructions in a buffer. In step 478, the service module 110 copies the contents of the buffer to versioned helper class 124, generating a closing brace for “runConstructorBody” and resets the buffer.
In
Returning to
Returning to step 480, when there are no more new or changed constructors in the current class, the method transitions to step 482. In step 482, the service module 110 generates bytecode for a closing brace of the versioned helper class 124 and resets the buffer, ending the flow.
As a result of processing changed class 104-A in
In a similar fashion, as a result of processing changed class 104-B in
Runtime Execution Flow of Example Client Class, Utilizing Selector Constructors to Correctly Invoke Changed and New Constructors in Reloaded Classes
The service module 110 processes all of the changed classes before they are loaded by the user app 108. In example, the service module 110 will produce the changed classes referenced by
The service module 110 however, performs specific transformations to produce changed class 104-C′. In particular, the service module 110 performs bytecode modifications of constructor invocations in classes that attempt to invoke new constructors 114. Such invocations are converted into calls to the selector constructor 118-A and 118-B respectively.
When the selector constructors 118 are invoked at runtime, two arguments are passed to match the formal parameters of the selector constructor 118. The first argument is an object array that is packed from the original arguments. The second argument is the index value associated with the constructor cache entry 138. This index value is passed on by constructing a new ConstructorPlaceHolder object that internally stores the unique index value within the runtime constructor cache 132. This is indicated by references 1201 and 1202.
Reference 1201 points to an example where a client program (here, changed class 104-C′) invokes new constructor 114-1 of changed class 104-A. In a similar fashion, reference 1202 points to where the client program including changed class 104-C′ invokes new constructor 114-2 of changed class 104-B.
In response to a runtime execution path that leads to execution of the code statements indicated by reference 1201, the selector constructor 118-A in
Within selector constructor 118-A in
Returning execution to the selector constructor 118-A within
Then, the execution continues outside the switch block 1122-1, with a call to the invokeBody method 1106. Execution then follows the detailed actions of method 1106 as indicated by steps 660-672 in
Returning to
Returning to
In step 670, the constructed method 1157-2 is looked up and invoked, passing as arguments the “this” object, which is the object currently under construction, and the original arguments “(100, 300).” Upon finishing the constructor body initialization of constructed method 1157-2, execution flow then returns to changed class 104-C′ of
In
Within selector constructor 118-B in
Returning execution to the selector constructor 118-B within
Returning to
Then the execution flow returns to
Returning to
Then, the execution continues outside the switch block 1122-2, with a call to invokeBody method 1106. Execution then follows the detailed actions of method 1106 as indicated by steps 660-672 in
Returning to
In step 668, the method constructs the method name and signature “runConstructorBody(B originalB, String str)” based on the found original class name and signature. The associated method that is generated is in
In step 670-672, the located method is looked up and invoked passing the “this” object, which is the object currently under construction, and the original argument “(some message).” Upon finishing the constructor body initialization of method 1157-4, the execution then returns back to changed class 104-C′, as indicated in
While this invention has been particularly shown and described with references to preferred embodiments thereof, it will be understood by those skilled in the art that various changes in form and details may be made therein without departing from the scope of the invention.
This application claims the benefit under 35 U.S.C. §119(e) of U.S. Provisional Application No. 62/114,223, filed on Feb. 10, 2015, which is incorporated herein by reference in its entirety.
Number | Date | Country | |
---|---|---|---|
62114223 | Feb 2015 | US |