SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
Classbox/J: Controlling the
 Scope of Change in Java


Alexandre Bergel,
Stéphane Ducasse and
Oscar Nierstrasz

bergel@iam.unibe.ch
Outline

 1. AWT and Swing Anomalies

 2. Classbox/J

 3. Properties of Classboxes

 4. Swing as a Classbox

 5. Implementation

 6. Conclusion


Alexandre Bergel               2
Presentation of AWT
     java.awt

                                         Component


                                                           Button
                                         Container
                        Window
        Frame



     In the AWT framework:
 •
       – Widgets are components (i.e., inherit from Component)
       – A frame is a window (Frame is a subclass of Window)




Alexandre Bergel                     3
Problem: Broken Inheritance in Swing
    java.awt

                                 Component


                                              Button
                                 Container
                   Window
        Frame

   javax.swing
                                 JComponent
                   JWindow
        JFrame
                                              JButton




Alexandre Bergel             4
Problem: Code Duplication
                                                              Code Duplication
    java.awt

                                              Component


                                                              Button
                                               Container
                        Window
        Frame

   javax.swing
                                              JComponent
                        JWindow
        JFrame                               accessibleCont
                     accessibleContext
 accessibleContext                                            JButton
                                             ext
                                             update()
                     rootPane
 rootPane
                     update()
 update()
                     setLayout()
 setLayout()
                     ...
 ...

Alexandre Bergel                         5
Problem: Explicit Type Checks and Casts
    public class Container extends Component {
      Component components[] = new Component [0];
      public Component add (Component comp) {...}
    }

    public class JComponent extends Container {
      public void paintChildren (Graphics g) {
         for (; i>=0 ; i--) {
           Component comp = getComponent (i);
           isJComponent = (comp instanceof JComponent);
           ...
           ((JComponent) comp).getBounds();
         }
      }}


Alexandre Bergel           6
We need to Support Unanticipated Changes

     AWT couldn’t be enhanced without risk of breaking
 •
     existing code.

     Swing is, therefore, built on the top of AWT using
 •
     subclassing.

     As a result, Swing is a big mess internally!
 •


     We need a mechanism to support unanticipated changes.
 •




Alexandre Bergel                   7
Classbox/J

     Module system for Java allowing classes to be refined
 •
     without breaking former clients.

     A classbox is like a package where:
 •

       – a class defined or imported within a classbox p can be imported
         by another classbox (transitive import).

       – class members can be added or redefined on an imported class
         with the keyword refine.

       – a refined method can access its original behavior using the
         original keyword


Alexandre Bergel                      8
Refining Classes (1 / 2)

    A classbox widgetsCB

         package widgetsCB;

         public class Component {
         	

 public void update() {this.paint();}
           	

         	

 public void paint () {/*Old code*/}
           	

         }

         public class Button extends Component {
         	

 ...
           	

         }



Alexandre Bergel              9
Refining Classes (2 / 2)

    Widget enhancements defined in NewWidgetsCB:

         package NewWidgetsCB;
         import widgetsCB.Component;
         import widgetsCB.Button;

         refine Component {
                	

 Variable addition */
                  /*
         	

 private ComponentUI lookAndFeel;
           	


         	

 /* Redefinition of paint() */
           	

         	

 public void paint() {
           	

         	

 	

 /* Code that uses lookAndFeel*/ }
           	

         }

Alexandre Bergel               10
Multiple Versions of Classes
                                                                        Import
                                                                       class refinement
                                                                  C
    widgetsCB                                 NewWidgetsCB
                                                 Component
                   Component
                                                lookAndFeel
                   paint()
                   update()                     paint()


                      Button                       Button

             new Button(“Ok”).update()           new Button(“Ok”).update()




Alexandre Bergel                         11
Import over Inheritance
                                                                        Import
                                                                       class refinement
                                                                  C
    widgetsCB                                 NewWidgetsCB
                                                 Component
                   Component
                                                lookAndFeel
                   paint()
                   update()    4                              3
                                                paint()


                      Button                       Button
                               2                              1
             new Button(“Ok”).update()           new Button(“Ok”).update()




           Lookup of the update() method triggered within
       1   enhWidgetsCB.

Alexandre Bergel                         12
But update() calls paint()
                                                                        Import
                                                                       class refinement
                                                                  C
    widgetsCB                                 NewWidgetsCB
                                                 Component
                   Component
                                                lookAndFeel
                   paint()
                   update()                     paint()
                                                              3


                      Button                       Button
                               2                              1
             new Button(“Ok”).update()           new Button(“Ok”).update()




           Lookup of the paint() method triggered within
     1
           enhWidgetsCB

Alexandre Bergel                         13
Old and New Clients at the Same Time
                                                                     Import
                                                                    class refinement
                                                                C
    widgetsCB                                   NewWidgetsCB
                                                   Component
                   Component
                                                  lookAndFeel
                   paint()
                   update()                       paint()


                      Button                         Button



                                                NewGUIAppCB
    OldGUIAppCB

                                                    Button      NewGUIApp
            Button             OldGUIApp




Alexandre Bergel                           14
Properties of Classboxes

     Minimal extension of the Java syntax (transitive import,
 •
     refine and original keywords).

     Refinements are confined to the classbox that define them
 •
     and to classboxes that import refined classes.

     Method redefinitions have precedence over previous
 •
     definitions.

     Classes can be refined without risk of breaking former
 •
     clients.


Alexandre Bergel                 15
Swing Refactored as a Classbox
    AwtCB

                                               Component


                                      Container                  Button
                     Window
           Frame

    SwingCB
                                               Component
                      Window                 accessibleContext
           Frame                             component
                   rootPane
                                                                 Button
                                             update()
                   setLayout()               add(Component)
                   setRootPane()             remove(Component)
                   setContentPane()
                   ...



Alexandre Bergel                        16
Swing Refactoring

     6500 lines of code refactored over 4 classes.
 •


     Inheritance defined in AwtCB is fully preserved in
 •
     SwingCB:
       – In SwingCB, every widget is a component (i.e., inherits from the
         extended AWT Component).
       – The property “a frame is a window” is true in SwingCB.

     Removed duplicated code: the refined Frame is 29 %
 •
     smaller than the original JFrame.

 •   Explicit type checks like obj instanceof JComponent and
     (JComponent)obj are avoided.

Alexandre Bergel                      17
Naive Implementation

     Based on source code manipulation.
 •


     The method call stack is introspected to determine the
 •
     right version of a method to be triggered.

     No cost for method additions, however slowdown of 1000
 •
     times when calling a redefined method.

     However, much better results were obtained in Smalltalk.
 •
     5 byte-codes are added to redefined methods (see our
     previous work).


Alexandre Bergel                18
Conclusion

     Classboxes delimit visibility of a change and avoid impacting
 •
     clients that should not be affected.

     Java is extended with two new keywords and transitive
 •
     import.

     Large case study showing how classboxes can be more
 •
     powerful than inheritance to support unanticipated
     changes.

     Performance could be improved by modifying the VM.
 •




Alexandre Bergel                 19
We need an alternative to
    inheritance to support
    unanticipated changes!




    Alexandre Bergel:
    bergel@iam.unibe.ch

    google “classboxes”



Alexandre Bergel                20
END




Alexandre Bergel   21
A JWidget is not necessary a JComponent
    java.awt

                                     Component


                                                  Button
                                     Container
                     Window
        Frame

   javax.swing
                                     JComponent
                     JWindow
        JFrame
                                                  JButton


                   Are not subclasses of JComponent
Alexandre Bergel                22
A JFrame is not a JWindow
    java.awt

                                   Component


                                                 Button
                                   Container
                   Window
        Frame

   javax.swing
                                   JComponent
                   JWindow
        JFrame
                                                 JButton

   Missing inheritance link between JFrame and JWindow

Alexandre Bergel              23
AWT and Swing Anomalies

     Features defined in JWindow are duplicated in JFrame (half
 •
     of JWindow code is in JFrame).
     The Swing design breaks the AWT inheritance relation:
 •
       – AWT: a Window is a Component
       – Swing: a JWindow is not a JComponent
     Need of explicit type checks and casts in Swing:
 •
       – For instance a JWindow needs to check if its elements are issued
         from Swing or not before rendering them
       – 82 type checks (instanceof) and 151 cast to (JComponent)




Alexandre Bergel                     24
Method Call Stack Introspected

 NewWidgetsCB and WidgetsCB define the paint method:
      package WidgetsCB;
      public class Component {
       public void paint() {

      	

        	

   if (ClassboxInfo.methodVisible (
      	

        	

        “NewWidgetsCB”, “Component”, “paint”)){
      	

        	

   	

    //Enhanced paint
      	

        	

   }
      	

        	

      	

        	

   if (ClassboxInfo.methodVisible (
      	

        	

        “WidgetsCB”, “Component”, “paint”)){
      	

        	

   	

    //Original paint
      	

        	

   }}}

Alexandre Bergel                   25

Mais conteúdo relacionado

Mais procurados

E catt tutorial
E catt tutorialE catt tutorial
E catt tutorial
Naveen Raj
 
13 gui development
13 gui development13 gui development
13 gui development
APU
 

Mais procurados (19)

Swings
SwingsSwings
Swings
 
GWT Training - Session 2/3
GWT Training - Session 2/3GWT Training - Session 2/3
GWT Training - Session 2/3
 
CDI: How do I ?
CDI: How do I ?CDI: How do I ?
CDI: How do I ?
 
Building a web application with ontinuation monads
Building a web application with ontinuation monadsBuilding a web application with ontinuation monads
Building a web application with ontinuation monads
 
GWT Training - Session 1/3
GWT Training - Session 1/3GWT Training - Session 1/3
GWT Training - Session 1/3
 
On Processors, Compilers and @Configurations
On Processors, Compilers and @ConfigurationsOn Processors, Compilers and @Configurations
On Processors, Compilers and @Configurations
 
Are app servers still fascinating
Are app servers still fascinatingAre app servers still fascinating
Are app servers still fascinating
 
Android native gl
Android native glAndroid native gl
Android native gl
 
React 16: new features and beyond
React 16: new features and beyondReact 16: new features and beyond
React 16: new features and beyond
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the question
 
Java Swing
Java SwingJava Swing
Java Swing
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics Programming
 
Spring 3 to 4
Spring 3 to 4Spring 3 to 4
Spring 3 to 4
 
E catt tutorial
E catt tutorialE catt tutorial
E catt tutorial
 
Generic UXD Legos - Selenium Conference 2015
Generic UXD Legos - Selenium Conference 2015Generic UXD Legos - Selenium Conference 2015
Generic UXD Legos - Selenium Conference 2015
 
XMPPart5
XMPPart5XMPPart5
XMPPart5
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
 
GR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul KingGR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul King
 
13 gui development
13 gui development13 gui development
13 gui development
 

Destaque

Multi dimensional profiling
Multi dimensional profilingMulti dimensional profiling
Multi dimensional profiling
bergel
 

Destaque (7)

2008 Sccc Inheritance
2008 Sccc Inheritance2008 Sccc Inheritance
2008 Sccc Inheritance
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
 
tres fotos
tres fotostres fotos
tres fotos
 
2006 Small Scheme
2006 Small Scheme2006 Small Scheme
2006 Small Scheme
 
2006 Esug Omnibrowser
2006 Esug Omnibrowser2006 Esug Omnibrowser
2006 Esug Omnibrowser
 
Multi dimensional profiling
Multi dimensional profilingMulti dimensional profiling
Multi dimensional profiling
 
2008 Sccc Smalltalk
2008 Sccc Smalltalk2008 Sccc Smalltalk
2008 Sccc Smalltalk
 

Semelhante a 2005 Oopsla Classboxj

MEF Deep Dive by Piotr Wlodek
MEF Deep Dive by Piotr WlodekMEF Deep Dive by Piotr Wlodek
MEF Deep Dive by Piotr Wlodek
infusiondev
 
Java session10
Java session10Java session10
Java session10
Niit Care
 

Semelhante a 2005 Oopsla Classboxj (20)

Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Console to GUI
Console to GUIConsole to GUI
Console to GUI
 
Image filters
Image filtersImage filters
Image filters
 
MEF Deep Dive by Piotr Wlodek
MEF Deep Dive by Piotr WlodekMEF Deep Dive by Piotr Wlodek
MEF Deep Dive by Piotr Wlodek
 
Lab 2: Importing requirements artifacts from a CSV file
Lab 2: Importing requirements artifacts from a CSV file Lab 2: Importing requirements artifacts from a CSV file
Lab 2: Importing requirements artifacts from a CSV file
 
GWT training session 2
GWT training session 2GWT training session 2
GWT training session 2
 
Chapter 02: Eclipse Vert.x - Java First Verticle
Chapter 02: Eclipse Vert.x - Java First VerticleChapter 02: Eclipse Vert.x - Java First Verticle
Chapter 02: Eclipse Vert.x - Java First Verticle
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Java session10
Java session10Java session10
Java session10
 
Making React Native UI Components with Swift
Making React Native UI Components with SwiftMaking React Native UI Components with Swift
Making React Native UI Components with Swift
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Patches_Presentation.pptx
Patches_Presentation.pptxPatches_Presentation.pptx
Patches_Presentation.pptx
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Building kubectl plugins with Quarkus | DevNation Tech Talk
Building kubectl plugins with Quarkus | DevNation Tech TalkBuilding kubectl plugins with Quarkus | DevNation Tech Talk
Building kubectl plugins with Quarkus | DevNation Tech Talk
 
intro_gui
intro_guiintro_gui
intro_gui
 
Acceleo Code Generation
Acceleo Code GenerationAcceleo Code Generation
Acceleo Code Generation
 
TestExec SL 7.1
TestExec SL 7.1TestExec SL 7.1
TestExec SL 7.1
 
What is new with JavaScript in Gnome: The 2021 edition
What is new with JavaScript in Gnome: The 2021 editionWhat is new with JavaScript in Gnome: The 2021 edition
What is new with JavaScript in Gnome: The 2021 edition
 

Mais de bergel

Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
bergel
 

Mais de bergel (9)

Building Neural Network Through Neuroevolution
Building Neural Network Through NeuroevolutionBuilding Neural Network Through Neuroevolution
Building Neural Network Through Neuroevolution
 
Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
 
2011 famoosr
2011 famoosr2011 famoosr
2011 famoosr
 
2011 ecoop
2011 ecoop2011 ecoop
2011 ecoop
 
Profiling blueprints
Profiling blueprintsProfiling blueprints
Profiling blueprints
 
The Pharo Programming Language
The Pharo Programming LanguageThe Pharo Programming Language
The Pharo Programming Language
 
Presentation of Traits
Presentation of TraitsPresentation of Traits
Presentation of Traits
 
2006 Seaside
2006 Seaside2006 Seaside
2006 Seaside
 
2004 Esug Prototalk
2004 Esug Prototalk2004 Esug Prototalk
2004 Esug Prototalk
 

Último

Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
lizamodels9
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
Matteo Carbone
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
lizamodels9
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
amitlee9823
 

Último (20)

Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Falcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in indiaFalcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in india
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 
Uneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration PresentationUneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration Presentation
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
 

2005 Oopsla Classboxj

  • 1. Classbox/J: Controlling the Scope of Change in Java Alexandre Bergel, Stéphane Ducasse and Oscar Nierstrasz bergel@iam.unibe.ch
  • 2. Outline 1. AWT and Swing Anomalies 2. Classbox/J 3. Properties of Classboxes 4. Swing as a Classbox 5. Implementation 6. Conclusion Alexandre Bergel 2
  • 3. Presentation of AWT java.awt Component Button Container Window Frame In the AWT framework: • – Widgets are components (i.e., inherit from Component) – A frame is a window (Frame is a subclass of Window) Alexandre Bergel 3
  • 4. Problem: Broken Inheritance in Swing java.awt Component Button Container Window Frame javax.swing JComponent JWindow JFrame JButton Alexandre Bergel 4
  • 5. Problem: Code Duplication Code Duplication java.awt Component Button Container Window Frame javax.swing JComponent JWindow JFrame accessibleCont accessibleContext accessibleContext JButton ext update() rootPane rootPane update() update() setLayout() setLayout() ... ... Alexandre Bergel 5
  • 6. Problem: Explicit Type Checks and Casts public class Container extends Component { Component components[] = new Component [0]; public Component add (Component comp) {...} } public class JComponent extends Container { public void paintChildren (Graphics g) { for (; i>=0 ; i--) { Component comp = getComponent (i); isJComponent = (comp instanceof JComponent); ... ((JComponent) comp).getBounds(); } }} Alexandre Bergel 6
  • 7. We need to Support Unanticipated Changes AWT couldn’t be enhanced without risk of breaking • existing code. Swing is, therefore, built on the top of AWT using • subclassing. As a result, Swing is a big mess internally! • We need a mechanism to support unanticipated changes. • Alexandre Bergel 7
  • 8. Classbox/J Module system for Java allowing classes to be refined • without breaking former clients. A classbox is like a package where: • – a class defined or imported within a classbox p can be imported by another classbox (transitive import). – class members can be added or redefined on an imported class with the keyword refine. – a refined method can access its original behavior using the original keyword Alexandre Bergel 8
  • 9. Refining Classes (1 / 2) A classbox widgetsCB package widgetsCB; public class Component { public void update() {this.paint();} public void paint () {/*Old code*/} } public class Button extends Component { ... } Alexandre Bergel 9
  • 10. Refining Classes (2 / 2) Widget enhancements defined in NewWidgetsCB: package NewWidgetsCB; import widgetsCB.Component; import widgetsCB.Button; refine Component { Variable addition */ /* private ComponentUI lookAndFeel; /* Redefinition of paint() */ public void paint() { /* Code that uses lookAndFeel*/ } } Alexandre Bergel 10
  • 11. Multiple Versions of Classes Import class refinement C widgetsCB NewWidgetsCB Component Component lookAndFeel paint() update() paint() Button Button new Button(“Ok”).update() new Button(“Ok”).update() Alexandre Bergel 11
  • 12. Import over Inheritance Import class refinement C widgetsCB NewWidgetsCB Component Component lookAndFeel paint() update() 4 3 paint() Button Button 2 1 new Button(“Ok”).update() new Button(“Ok”).update() Lookup of the update() method triggered within 1 enhWidgetsCB. Alexandre Bergel 12
  • 13. But update() calls paint() Import class refinement C widgetsCB NewWidgetsCB Component Component lookAndFeel paint() update() paint() 3 Button Button 2 1 new Button(“Ok”).update() new Button(“Ok”).update() Lookup of the paint() method triggered within 1 enhWidgetsCB Alexandre Bergel 13
  • 14. Old and New Clients at the Same Time Import class refinement C widgetsCB NewWidgetsCB Component Component lookAndFeel paint() update() paint() Button Button NewGUIAppCB OldGUIAppCB Button NewGUIApp Button OldGUIApp Alexandre Bergel 14
  • 15. Properties of Classboxes Minimal extension of the Java syntax (transitive import, • refine and original keywords). Refinements are confined to the classbox that define them • and to classboxes that import refined classes. Method redefinitions have precedence over previous • definitions. Classes can be refined without risk of breaking former • clients. Alexandre Bergel 15
  • 16. Swing Refactored as a Classbox AwtCB Component Container Button Window Frame SwingCB Component Window accessibleContext Frame component rootPane Button update() setLayout() add(Component) setRootPane() remove(Component) setContentPane() ... Alexandre Bergel 16
  • 17. Swing Refactoring 6500 lines of code refactored over 4 classes. • Inheritance defined in AwtCB is fully preserved in • SwingCB: – In SwingCB, every widget is a component (i.e., inherits from the extended AWT Component). – The property “a frame is a window” is true in SwingCB. Removed duplicated code: the refined Frame is 29 % • smaller than the original JFrame. • Explicit type checks like obj instanceof JComponent and (JComponent)obj are avoided. Alexandre Bergel 17
  • 18. Naive Implementation Based on source code manipulation. • The method call stack is introspected to determine the • right version of a method to be triggered. No cost for method additions, however slowdown of 1000 • times when calling a redefined method. However, much better results were obtained in Smalltalk. • 5 byte-codes are added to redefined methods (see our previous work). Alexandre Bergel 18
  • 19. Conclusion Classboxes delimit visibility of a change and avoid impacting • clients that should not be affected. Java is extended with two new keywords and transitive • import. Large case study showing how classboxes can be more • powerful than inheritance to support unanticipated changes. Performance could be improved by modifying the VM. • Alexandre Bergel 19
  • 20. We need an alternative to inheritance to support unanticipated changes! Alexandre Bergel: bergel@iam.unibe.ch google “classboxes” Alexandre Bergel 20
  • 22. A JWidget is not necessary a JComponent java.awt Component Button Container Window Frame javax.swing JComponent JWindow JFrame JButton Are not subclasses of JComponent Alexandre Bergel 22
  • 23. A JFrame is not a JWindow java.awt Component Button Container Window Frame javax.swing JComponent JWindow JFrame JButton Missing inheritance link between JFrame and JWindow Alexandre Bergel 23
  • 24. AWT and Swing Anomalies Features defined in JWindow are duplicated in JFrame (half • of JWindow code is in JFrame). The Swing design breaks the AWT inheritance relation: • – AWT: a Window is a Component – Swing: a JWindow is not a JComponent Need of explicit type checks and casts in Swing: • – For instance a JWindow needs to check if its elements are issued from Swing or not before rendering them – 82 type checks (instanceof) and 151 cast to (JComponent) Alexandre Bergel 24
  • 25. Method Call Stack Introspected NewWidgetsCB and WidgetsCB define the paint method: package WidgetsCB; public class Component { public void paint() { if (ClassboxInfo.methodVisible ( “NewWidgetsCB”, “Component”, “paint”)){ //Enhanced paint } if (ClassboxInfo.methodVisible ( “WidgetsCB”, “Component”, “paint”)){ //Original paint }}} Alexandre Bergel 25