Applications of enhanced dynamic code evolution for Java in GUI ...

2 downloads 3934 Views 521KB Size Report
Oct 13, 2010 - GUI Development and Dynamic Aspect-Oriented Programming. Thomas ... pects for dynamic analysis in sizable applications at runtime.
Applications of Enhanced Dynamic Code Evolution for Java in GUI Development and Dynamic Aspect-Oriented Programming Thomas W¨urthinger∗

Walter Binder† Danilo Ansaloni† Hanspeter M¨ossenb¨ock∗



Johannes Kepler University Linz, Austria {wuerthinger, moessenboeck}@ssw.jku.at

Abstract

General Terms

University of Lugano, Switzerland {firstname.lastname}@usi.ch

However, many people believe that it is an important feature. The high interest in advanced dynamic code evolution features is expressed in the official bug database for Oracle’s implementation of Java. In May 2010 the request for enhancement (RFE) “Enhance Hot Code Replacement” is with 412 votes number four in the list for RFEs for the Java platform. It is the topmost voted RFE concerning the Java Virtual Machine (JVM). We believe that enhanced dynamic code evolution for Java is especially beneficial in the following two areas:

While dynamic code evolution in object-oriented systems is an important feature supported by dynamic languages, there is currently only limited support for dynamic code evolution in highperformance, state-of-the-art runtime systems for statically typed languages, such as the Java Virtual Machine. In this tool demonstration, we present the Dynamic Code Evolution VM, which is based on a recent version of Oracle’s state-of-the-art Java HotSpotTM VM and allows unlimited changes to loaded classes at runtime. Based on the Dynamic Code Evolution VM, we developed an enhanced version of the Mantisse GUI builder (which is part of the NetBeans IDE) that allows adding GUI components without restarting the application under development. Furthermore, we redesigned the dynamic AOP framework HotWave to take advantage of the enhanced dynamic code evolution capabilities. The new version, HotWave2, now supports most AspectJ constructs, including around() advice and static cross-cutting. We will demonstrate both the enhanced Mantisse GUI builder as well as HotWave2, weaving several aspects for dynamic analysis in sizable applications at runtime. Categories and Subject Descriptors and Features]: Frameworks



Philippe Moret†

• Development Productivity. The need to shut down and restart

Java applications when a developer changes the code slows down the development process. The increased productivity because of quick code changes is considered one of the advantages of dynamic languages. Our enhanced virtual machine shows that this benefit can also be brought to a statically typed language like Java without losing performance. Section 4 describes how minor modifications to the Mantisse GUI builder (which is part of the NetBeans IDE) can help speed up Java GUI development when using the enhanced JVM.

D.3.3 [Language Constructs

• Dynamic Aspect-Oriented Programming (AOP). Several dy-

namic AOP frameworks use the hotswapping feature of the Java HotSpotTM VM to (re)weave classes at runtime. Examples include one version of PROSE [14], JBossAOP [10], Wool [3], JAsCo [21], AspectWerkz [22], and HotWave [23, 24]. Their feature set is currently restricted, because they can only swap method bodies. Dynamic AOP frameworks can immediately benefit from our enhanced JVM: Their implementation becomes simpler and they can allow more advanced dynamic AOP features. In Section 5, we describe how the new dynamic AOP framework HotWave2 benefits from the enhanced JVM.

Algorithms, Languages

Keywords Dynamic aspect-oriented programming, runtime weaving, dynamic code evolution, dynamic software updates, code hotswapping, Java Virtual Machine

1. Introduction Changing programs dynamically at runtime has been investigated for a long time in programming history. With the advent of virtual machines, the possibilities for implementing dynamic code evolution increased. Nevertheless, the features for dynamically changing a program remain very restricted for high-performance statically typed virtual machines. Neither the main implementations of the Java platform nor Microsoft’s implementation of the Common Language Runtime provide full support for dynamic code evolution.

While not the main focus of our tool demonstration, we identify two further applications of dynamic code evolution: It can be used to update long-running server applications. In addition, implementations of dynamic languages on top of the Java platform can benefit from it. This paper contributes the following: • A description of the features of our enhanced JVM (Section 3). • We show how slight modifications of the Mantisse GUI builder

Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. GPCE’10, October 10–13, 2010, Eindhoven, The Netherlands. c 2010 ACM 978-1-4503-0154-1/10/10. . . $10.00 Copyright

can speed up Java GUI development when using our JVM (Section 4). • A case study explaining how the dynamic AOP framework Hot-

Wave2 takes advantage from advanced dynamic code evolution (Section 5).

123

2. State of the Art

The transformation is implemented as a stop-the-world action and includes a full garbage collection run. The standard transformation strategy for object instances is to copy fields that have the same name and signature as in the old version of the program. All other fields are initialized to the default Java value (0, false, or null). The same strategy is applied to static fields. To allow more customized ways for the transformation step, the JVM executes a static method $staticTransformer on modified classes and a method $transformer on modified instances. When a currently active method is modified, then the old code continues to run, but new invocations of the method immediately target the latest version of a method. Old code can reference methods or fields that were deleted when shifting to the new version of the program. In case of deleted methods or static fields, the enhanced JVM will still access them correctly as if the program was never modified. When a deleted instance field is accessed, we throw a NoSuchFieldError exception. The implementation is part of a larger effort to extend the Java HotSpotTM VM with first-class support for languages other than Java (especially dynamic languages), called the Da Vinci Machine Project or Multi-Language Virtual Machine Project (MLVM) [15]. A patch against a recent development version of OpenJDK 7 that contains our modifications as well as a binary version of the Dynamic Code Evolution VM for 32-bit Windows is freely available for download [9].

Dynamic code evolution for object-oriented systems is a feature implemented first for dynamic languages, such as Smalltalk [7] and CLOS [19]. Developers switching from those languages to Java are limited in the style of programming. The first implementation of the Java HotSpotTM VM did not support any kind of dynamic code evolution. Only later, in JDK 1.4, a limited form of evolution that only allows swapping method bodies was added to the JVM. This modification was originally developed by Dimitriev [4, 5]. Our implementation is based on this work and significantly enhances it to allow more advanced changes like adding and removing fields, methods, and also changing the super types of a class. There exist more advanced implementations for older versions of Java [1, 11]. They are however still limited in the kind of possible changes and also imply a performance penalty compared to the unmodified JVM. Subramanian et al. [20] implemented code evolution for the Jikes RVM. They support adding and removing methods and fields, but do not support changes to the class hierarchy. An update can only be performed, if it does not affect an active method. Our virtual machine is the first to allow unlimited code evolution at any point a Java program can be suspended. Additionally, there is no performance penalty during normal execution. The implementation of our approach in a recent version of a productionquality virtual machine ensures that it is immediately usable when developing Java programs using standard tools. Concerning dynamic AOP in Java, several recent systems use the hotswapping mechanism of the Java Platform Debugger Architecture (JPDA); examples include one version of PROSE [14], Wool [3], and AspectWerkz [22]. Other systems rely on the java.lang.instrument API that can be accessed in pure Java; examples include JBossAOP [10], JAsCo [21], and HotWave [23, 24]. All these systems are limited by the constraints on class redefinition in current JVMs. None of them supports all features of the AspectJ language, the de-facto standard for (static) AOP in Java. Steamloom [2] is one of the few frameworks to provide dynamic AOP support at the JVM level, enabling more efficient runtime weaving. Steamloom supports the dynamic modification and reinstallation of method bytecodes and provides dedicated support for managing aspects. While Steamloom is based on a modified Jikes RVM, the HotWave2 framework presented in this paper relies on an extension of Oracle’s state-of-the-art Java HotSpotTM VM. In contrast to Steamloom, which severely restricts the aspect language (e.g., around() advice are not supported and access to join point information is limited), HotWave2 aims at complete support of the AspectJ language features, including around() advice and also static cross-cutting.

4. Case Study: GUI Builder A main benefit of advanced dynamic code evolution for Java programs is the increased productivity during development. With a standard JVM, a programmer will always have to restart an application even when he makes a small change such as adding a method to a class. For stateful applications with a long startup time, this significantly slows down the development process. As an example of the improved productivity with our modified JVM, we created a slightly modified version of the NetBeans IDE that allows the incremental creation of Java GUI programs. The user is able to immediately apply changes in the Mantisse GUI builder while the application is running. We only had to do minor changes to the automatic code generator of the GUI builder.

Listing 1. Instance transformer method added to reinitialize a JFrame object after class redefinition. c l a s s NewJFrame e x t e n d s JFrame { p r i v a t e JButton jButton1 ; //

...

p u b l i c NewJFrame ( ) { i n i t C o m p o n e n t s ( ) ; }

3. Dynamic Code Evolution VM

void $ tr an s f o r m er ( ) { t h i s . g e t C o n t e n t P a n e ( ) . r em o v eA ll ( ) ; initComponents ( ) ; }

We developed an enhanced version of the production-quality Java HotSpotTM VM that allows arbitrary changes to loaded classes while the program is running [26]. Valid changes include adding and removing methods and fields as well as changes to the class hierarchy (e.g., changing the super class). There is no performance penalty before or after the change and the change can be carried out at any Java bytecode. Instead of introducing new APIs, we fully implement the existing command for redefining the classes as specified in the Java Platform Debugging Architecture. Therefore, any Java debugger using either the Java Debug Wire Protocol (JDWP) [16] or the Java Virtual Machine Tool Interface (JVMTI) [17] is immediately capable of using our enhanced JVM. This includes in particular standard Java development environments such as Eclipse [6] or NetBeans [18].

/ / Method a u t o m a t i c a l l y g e n e r a t e d by t h e / / M a n t i s s e GUI b u i l d e r p r i v a t e void initComponents ( ) { i f ( j B u t t o n 1 == n u l l ) { j B u t t o n 1 = new J B u t t o n ( ) ; } // } }

124

. . . a d d i n g b u t t o n t o f r am e

...

Figure 1. Using the modified NetBeans to interactively create a Java GUI program. Since HotWave1 is compatible with any standard JVM, it is limited by the serious restrictions on class redefinition. Current JVMs only support modifications of method bodies and the constant pool. New members (i.e., fields, methods, or constructors) cannot be added to classes and method signatures cannot be changed. The class hierarchy cannot be modified either. These restrictions collide with several code transformations performed by the AspectJ weaver [8]. HotWave1 relies on several workarounds in order to support at least part of the features of AspectJ, such as static join point instances [23, 24]. However, HotWave1 supports neither around() advice nor static cross-cutting (i.e., aspects that perform structural modifications of classes, such as inserting extra fields). HotWave2 is based on the Dynamic Code Evolution VM presented in Section Section 3, which significantly relaxes the restrictions on class redefinition. Thanks to the novel features of the Dynamic Code Evolution VM, HotWave2 avoids the aforementioned limitations. Below we discuss the limitations that are removed by the new design of HotWave2.

For every new frame, Mantisse creates a new class that extends JFrame. The constructor calls the automatically generated method initComponents() that creates the frame contents. We modified the generated code to include an instance transformer method as shown in Listing 1. This transformer method is necessary to recompute the layout and to reinitialize the GUI components after modifying the form. We also needed slight adjustments to the generated code, because the initialization method can now be called more than once. A new instance of a component is only created the first time it was added to the form. This makes sure that the components preserve their state (e.g., the text in a JTextField component). Figure 1 demonstrates a productive screen layout when using our enhanced JVM to build a Java GUI application. The Java application is running all the time and need not be suspended by the debugger. The programmer has three different views on the form: The running form where he can interact with the components on the left side, the Java source code of the frame class in the middle, and the edit mode representation of the frame in the Mantisse GUI builder on the right side. When the form is changed in either the source code window or the GUI builder window, a toolbar button is enabled to apply the code changes directly to the running application. The modified version of the program is loaded and the instance transformer method ensures that the layout of the components is recomputed and the components are reinitialized. In contrast to our Dynamic Code Evolution VM, the current version of the standard Java HotSpotTM VM would not allow us to implement the described extension of the Mantisse GUI builder, because there is neither support for adding fields or methods at runtime nor for transformer methods. However, these features are essential, since the Mantisse GUI builder often creates new fields (e.g., for new components) or new methods (e.g., an action handler for a button) and the transformer method is necessary to relayout the frame.

Support for around() advice. In AspectJ, weaving of around() advice creates extra methods [8]. Because the introduction of extra methods would violate constraints on class redefinition in standard JVMs, HotWave1 does not support around() advice. HotWave2 fully supports around() advice, because it can use the enhanced class redefinition facilities of the JVM in order to add new methods to existing classes. Static cross-cutting. Static cross-cutting enables explicit structural transformations on classes, such as the insertion of fields or methods. Static cross-cutting is typically supported in static AOP frameworks, such as in AspectJ, but not in dynamic AOP systems. For example, AspectJ supports static cross-cutting with inter-type member declarations, whereas HotWave1 does not support static cross-cutting at all. HotWave2 supports inter-type member declarations and also allows for an evolution of aspects, where a new aspect version may drop members that were introduced by inter-type member declarations in a previous aspect version.

5. Case Study: HotWave2

In this tool demonstration, we present the new features of HotWave2 with several aspects that perform dynamic program analysis. The aspects can be (re)woven at any moment during program execution. In particular, we demonstrate various profiling aspects that make use of around() advice and static cross-cutting. The demonstration of HotWave2 also features an improved front-end for visualizing large calling context profiles which helps explore the inter-procedural control-flow of large object-oriented systems [12, 13]. The front-end is optimized for rapid navigation in profiles comprising up to millions of different calling contexts,

HotWave2 is the new version of a dynamic AOP framework for Java. HotWave2 supports (re)weaving of aspects at runtime, and aspects may be woven into any modifiable class linked in a JVM, including classes in the standard Java class library. Similar to the initial version of HotWave [23, 24] (henceforth called HotWave1), HotWave2 is based on AspectJ and uses the existing, unmodified AspectJ compiler and weaver. HotWave2 relies on pre- and postweaving bytecode transformations and on the class redefinition functionality provided by the package java.lang.instrument. An overview of the HotWave2 design is given in [25].

125

offers regularly updated views of running applications, and highlights recently active parts of a program. This demonstration shows how highly customizable and efficient program analysis tools can be rapidly developed with dynamic AOP in HotWave2 and integrated with a user-friendly interface capable of manipulating very large structures.

[8] E. Hilsdale and J. Hugunin. Advice weaving in AspectJ. In AOSD ’04: Proceedings of the 3rd International Conference on Aspect-Oriented Software Development, pages 26–35, New York, NY, USA, 2004. ACM. [9] Institute for System Software, Johannes Kepler University Linz. Dynamic Code Evolution VM, 2010. http://ssw.jku.at/dcevm/. [10] JBoss. Open source middleware software. http://labs.jboss.com/jbossaop/, 2010.

6. Conclusion Dynamic code evolution is an important feature that is useful in object-oriented systems. Whereas many dynamic languages offer full support for dynamic code evolution, state-of-the-art runtime systems for statically typed languages, such as the JVM, currently provide only very limited support. Thus, the software developer has the choice between more flexible—and potentially more productive—development in a dynamic language, or higher application performance in a statically typed language where the runtime system offers advanced compiler technology including aggressive optimizations. The Dynamic Code Evolution VM introduces unrestricted dynamic code evolution in Oracle’s state-of-the-art Java HotSpotTM VM. It allows a wide range of code modifications at runtime, such as adding and removing fields, methods, and also changing the super types of a class. The Dynamic Code Evolution VM enables similar flexibility in Java software development tools as commonly offered by tools based on dynamic languages. In this tool demonstration, we back this claim by showing two software development tools that take advantage of enhanced dynamic code evolution capabilities. First, we demonstrate an extended version of the Mantisse GUI builder that allows adding GUI components at runtime without interrupting the application under development. This feature is very useful in the development of long-running, stateful applications, where it would take long time to restart the application and bring it back into the desired state after a modification. Second, we present HotWave2, the new version of a dynamic AOP framework for Java, which is fully compatible with AspectJ, supports runtime weaving, as well as weaving in the standard Java class library. In contrast to a prior version, HotWave2 also supports around() advice and static cross-cutting. We demonstrate HotWave2 with several aspects for dynamic program analysis, which are woven into sizable Java applications at runtime.

[11] S. Malabarba, R. Pandey, J. Gragg, E. Barr, and J. F. Barnes. Runtime support for type-safe dynamic Java classes. In Proceedings of the European Conference on Object-Oriented Programming, pages 337– 361. Springer-Verlag, 2000. [12] P. Moret, W. Binder, D. Ansaloni, and A. Villaz´on. Exploring Large Profiles with Calling Context Ring Charts. In WOSP/SIPEW 2010: Proceedings of the First Joint International Conference on Performance Engineering, pages 63–68. ACM Press, Jan. 2010. [13] P. Moret, W. Binder, A. Villaz´on, D. Ansaloni, and A. Heydarnoori. Visualizing and Exploring Profiles with Calling Context Ring Charts. Software: Practice and Experience, 2010. [14] A. Nicoara, G. Alonso, and T. Roscoe. Controlled, Systematic, and Efficient Code Replacement for Running Java Programs. In Eurosys ’08: Proceedings of the 3rd ACM SIGOPS/EuroSys European Conference on Computer Systems 2008, pages 233–246, New York, NY, USA, 2008. ACM. [15] Oracle Corporation. Da Vinci Machine Project, 2010. http://openjdk.java.net/projects/mlvm/. [16] Oracle Corporation. Java Debug Wire Protocol (JDWP), 2010. http://java.sun.com/javase/6/docs/technotes/guides/ jpda/jdwp-spec.html. [17] Oracle Corporation. Java Virtual Machine Tool Interface (JVM TI), 2010. http://java.sun.com/javase/6/docs/technotes/guides/ jvmti/index.html. [18] Oracle Corporation. NetBeans, 2010. http://netbeans.org/. [19] G. L. Steele, Jr. Common LISP: the Language. Digital Press, second edition, 1990. [20] S. Subramanian, M. Hicks, and K. S. McKinley. Dynamic software updates: a VM-centric approach. In Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation, pages 1–12. ACM Press, 2009. [21] W. Vanderperren, D. Suv´ee, B. Verheecke, M. A. Cibr´an, and V. Jonckers. Adaptive Programming in JAsCo. In AOSD ’05: Proceedings of the 4th international conference on Aspect-oriented software development, pages 75–86, New York, NY, USA, 2005. ACM.

Acknowledgments The work presented in this paper has been supported by the Swiss National Science Foundation and Oracle Corporation.

[22] A. Vasseur. Dynamic AOP and Runtime Weaving for Java – How does AspectWerkz address it? In Dynamic Aspects Workshop (DAW04), Lancaster, England, Mar. 2004.

References

[23] A. Villaz´on, D. Ansaloni, W. Binder, and P. Moret. HotWave: Creating Adaptive Tools with Dynamic Aspect-Oriented Programming in Java. In GPCE ’09: Proceedings of the Eighth International Conference on Generative Programming and Component Engineering. ACM, Oct. 2009.

[1] J. Andersson and T. Ritzau. Dynamic code update in JDrums. In Workshop on Software Engineering for Wearable and Pervasive Computing, 2000. [2] C. Bockisch, M. Haupt, M. Mezini, and K. Ostermann. Virtual machine support for dynamic join points. In AOSD ’04: Proceedings of the 3rd international conference on Aspect-oriented software development, pages 83–92, New York, NY, USA, 2004. ACM. [3] S. Chiba, Y. Sato, and M. Tatsubori. Using Hotswap for Implementing Dynamic AOP Systems. In Proceedings of the 1st Workshop on Advancing the State-of-the-Art in Run-time Inspection, 2003. [4] M. Dmitriev. Safe Class and Data Evolution in Large and Long-Lived Java Applications. PhD thesis, University of Glasgow, 2001. [5] M. Dmitriev. Towards flexible and safe technology for runtime evolution of Java language applications. In Proceedings of the Workshop on Engineering Complex Object-Oriented Systems for Evolution, 2001. [6] Eclipse Foundation. Eclipse, 2010. http://www.eclipse.org/. [7] A. Goldberg and D. Robson. Smalltalk-80: the language and its implementation. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1983.

[24] A. Villaz´on, W. Binder, D. Ansaloni, and P. Moret. Advanced Runtime Adaptation for Java. In GPCE ’09: Proceedings of the Eighth International Conference on Generative Programming and Component Engineering, pages 85–94. ACM, Oct. 2009. [25] T. W¨urthinger, W. Binder, D. Ansaloni, P. Moret, and H. M¨ossenb¨ock. Improving aspect-oriented programming with dynamic code evolution in an enhanced Java virtual machine. In RAM-SE ’10: Proceedings of the 7th ECOOP Workshop on Reflection, AOP and Meta-Data for Software Evolution. ACM, June 2010. [26] T. W¨urthinger, L. Stadler, and C. Wimmer. Dynamic code evolution for java. In Proceedings of the 8th International Conference on the Principles and Practice of Programming in Java (PPPJ’10), 2010.

126