Classboxes: A Minimal Module Model
  Supporting Local Class Extension

   Alexandre Bergel, Stéphane Ducasse,
             and Roel Wuyts
       Software Composition Group
      University of Bern (Switzerland)
           bergel@iam.unibe.ch
Outline
1.   Class extension and Package
2.   Supporting Unanticipated Changes
3.   The Classbox Model
4.   Implementation
5.   Conclusion
6.   Future Work


                                        1
Class Extension

• Languages such as CLOS or Smalltalk provide a
  mechanism of Class Extension

• A Class Extension is the ability to add or redefine
  methods on a class after its creation



                                                2
Class Extension: A powerful Mechanism
Visitor Example:
BlackPackage
              Opr



    Add             Mult



                                   3
Class Extension: A powerful Mechanism
Visitor Example:
BlackPackage                         BluePackage
                  Opr
           acceptVisitor()               Visitor
                                         doAdd()
     Add                Mult             doMult()
acceptVisitor()    acceptVisitor()


                                                    4
Subclassing does not Solve it

            Opr                        Parser
                                       parse()
   Add            Times
                                    …
                                    Add.new()
    Add’             Times’         …
acceptVisitor()   acceptVisitor()

Subclasses are not referenced by the tree constructor
                                                 5
In Smalltalk
• Class extensions are global
• Any application can modify any class in the system

Consequences
   – Conflicts may arise (e.g., two applications may add the
     same method)
   – Robustness aspect (e.g., an application may redefine a
     critical method)

                                                     6
Supporting Unanticipated Changes:
                Adaptation
• Java, Modula-3,…have package/modules but no Class
  Extension
• Smalltalk has Class Extension but no package/module

      fi Class Extension + Module ?

• How to control class extensions?
• How to apply a set of unanticipated changes without
  breaking existing clients?

                                                    7
Classbox Model Definition
• The Classbox Model is a minimal module model
  supporting local class extension
• A Classbox
   –   Is a unit of scoping (i.e., it acts as a namespace)
   –   Can define classes
   –   Can import class definitions from other classboxes
   –   Can define methods on classes in the scope: class extensions
• Classes and methods always belong to exactly one
  classbox
• Visibility of each element in a classbox is bound to this
  classbox

                                                             8
Classbox Model: Avoiding Conflict
  By having class           TextClassbox                   import
  extensions local to a
                            String
  classbox, conflicts are
  avoided                                  URLClassbox
                                                URL
WWWClassbox
                  WWWURL                          String
                                                  asUrl
                  String
WWWURL.new()      asUrl
…                                          URL.new()
                                           …

                                                       9
Classbox Model: Import and Visibility
 Extended classes can be          TextClassbox              import
 used by classboxes
                                   String
 through the import
 relationship                                    URLClassbox
                                                   URL
RedClassbox
                                                   String
…                                                  asUrl
url = “http://www.iam.unibe.ch/~scg”.asUrl()




                                                     10
Classbox Model: Classbox as a Sandbox
All the changes are local, so       CollectionClassbox           import
redefinition of a critical method
is limited to the Classbox          OrderedCollection
providing the extension              do()

                                                 GreenClassbox
                                              OrderedCollection
                                               do()
                      aCollection.do(…)
                                                        C
                  C.new().foo()                 foo()

                                                            11
Classbox Model: Flattening Property
Any expression executed in a       CollectionClassbox         import
classbox will be evaluated as if   OrderedCollection
the whole classbox graph were       do()
collapsed to a single classbox.     copy() 2

                                                GreenClassbox
      self.do(element.copy())                OrderedCollection
                                              do() 3
                     aCollection.copy(…)
                                                     C
                  C.new().bar()                bar() 1

                                                         12
From within the Green Classbox




                                       GreenClassbox
self.do(element.copy())             OrderedCollection
                                     do() 3
                                     copy() 2
              aCollection.copy(…)
                                           C
          C.new().bar()              bar() 1

                                             13
Implementation
• Implemented in Squeak, an open-source Smalltalk
• To avoid excessive memory costs:
  – modify the VM
• New method lookup taking some extra parameters
  into account such as:
  – Original classbox (referring the classbox where the
    initial expression is executed)
  – Collection of all the traversed classboxes
                                                   14
Lookup: Imports before Inheritance

CollectionClassbox      ImprovedClassbox     PinkClassbox
 OrderedCollection       OrderedCollection            Pink
 do()                    do()                 foo()
 copy()
                                             aSortedCollection.copy()

 SortedCollection        SortedCollection    SortedCollection



                                              Pink.new().foo()
Lookup of the copy() method
                                                        15
Lookup: Imports before Inheritance

CollectionClassbox      ImprovedClassbox     PinkClassbox
 OrderedCollection       OrderedCollection            Pink
 do()                    do()                 foo()
 copy() 6                         5                     4
                                             aSortedCollection.copy()

 SortedCollection        SortedCollection    SortedCollection
          3                       2                     1
                                              Pink.new().foo()
Lookup of the copy() method
                                                        16
Lookup: Imports before Inheritance
The copy() method invokes do() one
CollectionClassbox       ImprovedClassbox           PinkClassbox
 OrderedCollection         OrderedCollection        PinkClass
 do()                      do()                     foo()
 copy()                              5                         4
                                                    aSortedCollection.copy()

 SortedCollection          SortedCollection         SortedCollection
           3                         2                         1

  Without any drastic optimization, a system is 60% slower
                                                               17
Conclusion
• Definition of a module system for controlling class
  extensions:
   – Method addition and replacement are only visible in the
     module that defines them
   – Control the visibility
   – Support for unanticipated evolution
• New method lookup
   – This has a price: slow performance with the current
     implementation

                                                    18
Future Work
• Allow aliasing, when importing

• Refined visibility control (allowing an extension to be
  global: evolution of a classbox)

• Add multiple Classbox interfaces (clients may see a
  classbox under different views)

• Formalization to specify the flattening property

                                                       19
Contact Information


• Alexandre Bergel (bergel@iam.unibe.ch)

• Website:
  – http://www.iam.unibe.ch/~scg/Research/Classboxes/index.html




                                                        20

Classboxes

  • 1.
    Classboxes: A MinimalModule Model Supporting Local Class Extension Alexandre Bergel, Stéphane Ducasse, and Roel Wuyts Software Composition Group University of Bern (Switzerland) bergel@iam.unibe.ch
  • 2.
    Outline 1. Class extension and Package 2. Supporting Unanticipated Changes 3. The Classbox Model 4. Implementation 5. Conclusion 6. Future Work 1
  • 3.
    Class Extension • Languagessuch as CLOS or Smalltalk provide a mechanism of Class Extension • A Class Extension is the ability to add or redefine methods on a class after its creation 2
  • 4.
    Class Extension: Apowerful Mechanism Visitor Example: BlackPackage Opr Add Mult 3
  • 5.
    Class Extension: Apowerful Mechanism Visitor Example: BlackPackage BluePackage Opr acceptVisitor() Visitor doAdd() Add Mult doMult() acceptVisitor() acceptVisitor() 4
  • 6.
    Subclassing does notSolve it Opr Parser parse() Add Times … Add.new() Add’ Times’ … acceptVisitor() acceptVisitor() Subclasses are not referenced by the tree constructor 5
  • 7.
    In Smalltalk • Classextensions are global • Any application can modify any class in the system Consequences – Conflicts may arise (e.g., two applications may add the same method) – Robustness aspect (e.g., an application may redefine a critical method) 6
  • 8.
    Supporting Unanticipated Changes: Adaptation • Java, Modula-3,…have package/modules but no Class Extension • Smalltalk has Class Extension but no package/module fi Class Extension + Module ? • How to control class extensions? • How to apply a set of unanticipated changes without breaking existing clients? 7
  • 9.
    Classbox Model Definition •The Classbox Model is a minimal module model supporting local class extension • A Classbox – Is a unit of scoping (i.e., it acts as a namespace) – Can define classes – Can import class definitions from other classboxes – Can define methods on classes in the scope: class extensions • Classes and methods always belong to exactly one classbox • Visibility of each element in a classbox is bound to this classbox 8
  • 10.
    Classbox Model: AvoidingConflict By having class TextClassbox import extensions local to a String classbox, conflicts are avoided URLClassbox URL WWWClassbox WWWURL String asUrl String WWWURL.new() asUrl … URL.new() … 9
  • 11.
    Classbox Model: Importand Visibility Extended classes can be TextClassbox import used by classboxes String through the import relationship URLClassbox URL RedClassbox String … asUrl url = “http://www.iam.unibe.ch/~scg”.asUrl() 10
  • 12.
    Classbox Model: Classboxas a Sandbox All the changes are local, so CollectionClassbox import redefinition of a critical method is limited to the Classbox OrderedCollection providing the extension do() GreenClassbox OrderedCollection do() aCollection.do(…) C C.new().foo() foo() 11
  • 13.
    Classbox Model: FlatteningProperty Any expression executed in a CollectionClassbox import classbox will be evaluated as if OrderedCollection the whole classbox graph were do() collapsed to a single classbox. copy() 2 GreenClassbox self.do(element.copy()) OrderedCollection do() 3 aCollection.copy(…) C C.new().bar() bar() 1 12
  • 14.
    From within theGreen Classbox GreenClassbox self.do(element.copy()) OrderedCollection do() 3 copy() 2 aCollection.copy(…) C C.new().bar() bar() 1 13
  • 15.
    Implementation • Implemented inSqueak, an open-source Smalltalk • To avoid excessive memory costs: – modify the VM • New method lookup taking some extra parameters into account such as: – Original classbox (referring the classbox where the initial expression is executed) – Collection of all the traversed classboxes 14
  • 16.
    Lookup: Imports beforeInheritance CollectionClassbox ImprovedClassbox PinkClassbox OrderedCollection OrderedCollection Pink do() do() foo() copy() aSortedCollection.copy() SortedCollection SortedCollection SortedCollection Pink.new().foo() Lookup of the copy() method 15
  • 17.
    Lookup: Imports beforeInheritance CollectionClassbox ImprovedClassbox PinkClassbox OrderedCollection OrderedCollection Pink do() do() foo() copy() 6 5 4 aSortedCollection.copy() SortedCollection SortedCollection SortedCollection 3 2 1 Pink.new().foo() Lookup of the copy() method 16
  • 18.
    Lookup: Imports beforeInheritance The copy() method invokes do() one CollectionClassbox ImprovedClassbox PinkClassbox OrderedCollection OrderedCollection PinkClass do() do() foo() copy() 5 4 aSortedCollection.copy() SortedCollection SortedCollection SortedCollection 3 2 1 Without any drastic optimization, a system is 60% slower 17
  • 19.
    Conclusion • Definition ofa module system for controlling class extensions: – Method addition and replacement are only visible in the module that defines them – Control the visibility – Support for unanticipated evolution • New method lookup – This has a price: slow performance with the current implementation 18
  • 20.
    Future Work • Allowaliasing, when importing • Refined visibility control (allowing an extension to be global: evolution of a classbox) • Add multiple Classbox interfaces (clients may see a classbox under different views) • Formalization to specify the flattening property 19
  • 21.
    Contact Information • AlexandreBergel (bergel@iam.unibe.ch) • Website: – http://www.iam.unibe.ch/~scg/Research/Classboxes/index.html 20