Title: Signature Expansion in 'NET 2'0
1Signature Expansion in .NET 2.0
2Introduction
- Intern from Saxion Hogeschool
- August 28 2006 until Januari 29 2007
- Original assignment
- Visual Studio 2005 integration for Compose
3Outline
- Aspect Oriented Programming
- Compose and StarLight
- Assignment
- Problems
- Solution
4Aspect Oriented Programming
Scattering n Hard to keep overview n Hard to
maintain
Some concern e.g. tracing
5Aspect Oriented Programming
Multiple concerns e.g. tracing, logging,
parameter checking, memory allocation 30 of
total!
6Aspect Oriented Programming
Goal Separated concerns Specify a concern in
one place
These still influence the program globally!
7Composition Filters
- Mehmet Aksit, Lodewijk Bergmans et al.
- Key element message
- Essentially a method call
- Filtering and manipulation of messages
- Incoming inputfilter
- Method calls to a class
- Outgoing outputfilter
- Method calls from a class
8Compose
- Implementation of Composition Filters
- Several ports
- .NET 1.1 C and J
- Java
- C
- Uses an interpreter at runtime
9StarLight
- Light version of Compose for ASML
- Targets .NET 2.0
- Compiletime code generation
10Assignment
- Visual Studio 2005 integration for C.NET
- Implies .NET 2.0 support
- .NET 2.0 support problematic
11Assignment
- Expand StarLight with missing features
- Already has VS2005 integration
- Targets .NET 2.0
- Signature Expansion
- Adding additional methods to a class
- Example Inventory
12Use-case Inventory
13Making Inventory Observable
14Making Inventory Observable
products.add(p) notifyAll()
products.remove(p) notifyAll()
foreach (Observer o in observers)
o.update(this)
15The Compose Way
- filtermodule SubjectInventoryFilter
-
- internals
- subject InventoryTwo.Subject
- inputfilters
- ad Dispatch .attach subject.attach,
- .detach subject.detach
- notify After .addProduct
subject.notifyAll, - .removeProduct
subject.notifyAll
Specify behaviour
16The Compose Way
- filtermodule SubjectInventoryFilter
-
- internals
- subject InventoryTwo.Subject
- inputfilters
- ad Dispatch .attach subject.attach,
- .detach subject.detach
- notify After .addProduct
subject.notifyAll, - .removeProduct
subject.notifyAll
Implementation details in Subject
17The Compose Way
Add attach, detach Delegate to Subject
- filtermodule SubjectInventoryFilter
-
- internals
- subject InventoryTwo.Subject
- inputfilters
- ad Dispatch .attach subject.attach,
- .detach subject.detach
- notify After .addProduct
subject.notifyAll, - .removeProduct
subject.notifyAll
18The Compose Way
Add notifications void addProduct(Product p)
products.add(p) notifyAll()
- filtermodule SubjectInventoryFilter
-
- internals
- subject InventoryTwo.Subject
- inputfilters
- ad Dispatch .attach subject.attach,
- .detach subject.detach
- notify After .addProduct
subject.notifyAll, - .removeProduct
subject.notifyAll
void removeProduct(Product p)
products.remove(p) notifyAll()
19The Compose Way
Specify where to apply behaviour
- superimposition
-
- selectors
- subjects C
- isClassWithName(C, 'InventoryTwo.Inventory')
-
- filtermodules
- subjects lt- SubjectInventoryFilter
Apply behaviour specified in SubjectInventoryFilte
r to Inventory
20Using Inventory
- public static void main(String args)
-
- Inventory inventory new Inventory()
- inventory.attach(new InventoryCounter())
- inventory.attach(new InventoryDisplay())
- inventory.addProduct(new Product(Apple,
10.50)) - inventory.addProduct(new Product(Banana,
7.31))
21Compilation Process
- Simple process
- Compile sources using standard compiler
- Analyze resulting assembly
- Run Compose Core
- Weave assemblies
22Problem
- public static void main(String args)
-
- Inventory inventory new Inventory()
- inventory.attach(new InventoryCounter())
- inventory.attach(new InventoryDisplay())
- inventory.addProduct(new Product(Apple,
10.50)) - inventory.addProduct(new Product(Banana,
7.31))
Inventory.attach does not exist yet!
23Problem
- How do we compile?
- Add additional methods
- Which methods do we add?
- Type information needed
- How to get type information?
- Compile the code
- Analyze resulting assembly
24Solution in Compose
- Compile dummies instead
- Remove all method bodies from sources
- Including all method calls
- Result is called a dummy
- Contains the same type information
- This will compile!
- Example
25Dummy Generation
26Compilation Process
- Dummy process
- Create and compile dummies
- Analyze resulting assembly
- Run Compose Core
- Extend the dummies with additional methods
- Compile real sources referencing the dummies
- Weave assemblies
27Problem
- Dummies no longer work in .NET 2.0
- Type checking is more strict
- Considered several alternatives
- No signature expansion
- Special constructs
- Partial classes
- Source expansion
28Alternatives
- No signature expansion
- Easiest to implement
- Not acceptable important feature
29Alternatives
- Special constructs
- Make the compiler accept it
- User has to modify code
- Extra effort for special methods
- Not transparant
30Alternatives
- interface Observable
-
- void attach(Observer o)
- void detach(Observer o)
-
- public static void main(String args)
-
- Inventory inventory new Inventory()
- Observable observable (Observable)inventory
- observable.attach(new InventoryCounter())
- observable.attach(new InventoryDisplay())
31Alternatives
- Partial classes
- One class specified in multiple files
- Analyse sources instead of assembly
- Add additional methods in separate files
32Alternatives
- Only works for C and VB.NET (i.e. not J)
- In C original source needs to be modified
33Alternatives
- Source Expansion
- Analyse sources instead of compiled assembly
- Expand sources with additional methods
- Compile modified sources
- Chosen solution
34Compilation Process
- New process
- Analyze sources
- Run Compose Core
- Extend sources with additional methods
- Compile modified sources
- Weave assemblies
35Analysing the sources
- What type information needed?
- Assembly-level model
- All types in weavable assemblies
- Referenced types in non-weavable assemblies
36Assembly-level model
- Assembly
- Name
- Types (class, interface, enum, )
- Fully qualified name (namespace name)
- Base type as FQN (e.g. System.Object)
- Implemented interfaces as FQN
- Fields
- Methods
37Analysing the sources
- Parse sources
- Using ANTLR
- Language specific
- Currently only J support
- Generate a model
- Source-level model
38Source-level model
- Compilation Unit (CU)
- Namespace (package in J)
- Imports
- Whole namespace import java.util.
- Specific type import java.util.ArrayList
- Types
39Source Expansion
- Convert to assembly-level model
- Combine all CUs into one assembly element
- Resolve typenames to fully qualified names
- Object ? java.lang.Object
- List ? java.util.List or
- List ? java.awt.List ?
40Source Expansion
- Resolve typenames
- Try if its already a FQN
- Try specific imports
- Try current namespace
- Try imported namespaces
41Source Expansion
- Extend sources with additional methods
- Add the extra methods before the end of the class
- Store the character position of closing
- Example
42Source Expansion
43Source Expansion
- Drawbacks
- Parser needed for supported languages
- Error messages
- Reported by our parser
- Filenames and line numbers do not match
44Conclusion
- Summary
- Powerful feature
- Not easy to support
- Source expansion a good solution
- Future work
- J generic types, annotations
- C support
45Questions?
http//composestar.sourceforge.net
46Composition Filters
- Filtermodule contains
- Inputfilters
- Outputfilters
- Internals
- Externals
- Conditions
47Composition Filters
- Superimposition
- Placing a filters on a class
48Source-level model
- Type
- Local name
- Base type
- Implemented interfaces
- Fields
- Methods
- Referenced types (from method bodies)
49Assembly-level Model
- Field
- Name
- Type as FQN
- Method
- Name
- Return Type as FQN
- Parameter Types as FQN
50Source-level Model
- Field
- Name
- Type
- Method
- Name
- Return Types
- Parameter Types