Signature Expansion in 'NET 2'0 - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Signature Expansion in 'NET 2'0

Description:

public int tick(float delta) if (evilTime 0) evilTime -= delta; if (evilTime = 0) ... Using ANTLR. Language specific. Currently only J# support. Generate a ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 51
Provided by: Doorn
Category:

less

Transcript and Presenter's Notes

Title: Signature Expansion in 'NET 2'0


1
Signature Expansion in .NET 2.0
  • Marcus Klimstra

2
Introduction
  • Intern from Saxion Hogeschool
  • August 28 2006 until Januari 29 2007
  • Original assignment
  • Visual Studio 2005 integration for Compose

3
Outline
  • Aspect Oriented Programming
  • Compose and StarLight
  • Assignment
  • Problems
  • Solution

4
Aspect Oriented Programming
Scattering n Hard to keep overview n Hard to
maintain
Some concern e.g. tracing
5
Aspect Oriented Programming
Multiple concerns e.g. tracing, logging,
parameter checking, memory allocation 30 of
total!
6
Aspect Oriented Programming
Goal Separated concerns Specify a concern in
one place
These still influence the program globally!
7
Composition 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

8
Compose
  • Implementation of Composition Filters
  • Several ports
  • .NET 1.1 C and J
  • Java
  • C
  • Uses an interpreter at runtime

9
StarLight
  • Light version of Compose for ASML
  • Targets .NET 2.0
  • Compiletime code generation

10
Assignment
  • Visual Studio 2005 integration for C.NET
  • Implies .NET 2.0 support
  • .NET 2.0 support problematic

11
Assignment
  • Expand StarLight with missing features
  • Already has VS2005 integration
  • Targets .NET 2.0
  • Signature Expansion
  • Adding additional methods to a class
  • Example Inventory

12
Use-case Inventory
13
Making Inventory Observable
14
Making Inventory Observable
products.add(p) notifyAll()
products.remove(p) notifyAll()
foreach (Observer o in observers)
o.update(this)
15
The 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
16
The 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
17
The 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

18
The 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()
19
The 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
20
Using 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))

21
Compilation Process
  • Simple process
  • Compile sources using standard compiler
  • Analyze resulting assembly
  • Run Compose Core
  • Weave assemblies

22
Problem
  • 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!
23
Problem
  • 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

24
Solution 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

25
Dummy Generation
26
Compilation 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

27
Problem
  • 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

28
Alternatives
  • No signature expansion
  • Easiest to implement
  • Not acceptable important feature

29
Alternatives
  • Special constructs
  • Make the compiler accept it
  • User has to modify code
  • Extra effort for special methods
  • Not transparant

30
Alternatives
  • 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())

31
Alternatives
  • Partial classes
  • One class specified in multiple files
  • Analyse sources instead of assembly
  • Add additional methods in separate files

32
Alternatives
  • Only works for C and VB.NET (i.e. not J)
  • In C original source needs to be modified

33
Alternatives
  • Source Expansion
  • Analyse sources instead of compiled assembly
  • Expand sources with additional methods
  • Compile modified sources
  • Chosen solution

34
Compilation Process
  • New process
  • Analyze sources
  • Run Compose Core
  • Extend sources with additional methods
  • Compile modified sources
  • Weave assemblies

35
Analysing the sources
  • What type information needed?
  • Assembly-level model
  • All types in weavable assemblies
  • Referenced types in non-weavable assemblies

36
Assembly-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

37
Analysing the sources
  • Parse sources
  • Using ANTLR
  • Language specific
  • Currently only J support
  • Generate a model
  • Source-level model

38
Source-level model
  • Compilation Unit (CU)
  • Namespace (package in J)
  • Imports
  • Whole namespace import java.util.
  • Specific type import java.util.ArrayList
  • Types

39
Source 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 ?

40
Source Expansion
  • Resolve typenames
  • Try if its already a FQN
  • Try specific imports
  • Try current namespace
  • Try imported namespaces

41
Source Expansion
  • Extend sources with additional methods
  • Add the extra methods before the end of the class
  • Store the character position of closing
  • Example

42
Source Expansion
43
Source Expansion
  • Drawbacks
  • Parser needed for supported languages
  • Error messages
  • Reported by our parser
  • Filenames and line numbers do not match

44
Conclusion
  • Summary
  • Powerful feature
  • Not easy to support
  • Source expansion a good solution
  • Future work
  • J generic types, annotations
  • C support

45
Questions?
  • ?

http//composestar.sourceforge.net
46
Composition Filters
  • Filtermodule contains
  • Inputfilters
  • Outputfilters
  • Internals
  • Externals
  • Conditions

47
Composition Filters
  • Superimposition
  • Placing a filters on a class

48
Source-level model
  • Type
  • Local name
  • Base type
  • Implemented interfaces
  • Fields
  • Methods
  • Referenced types (from method bodies)

49
Assembly-level Model
  • Field
  • Name
  • Type as FQN
  • Method
  • Name
  • Return Type as FQN
  • Parameter Types as FQN

50
Source-level Model
  • Field
  • Name
  • Type
  • Method
  • Name
  • Return Types
  • Parameter Types
Write a Comment
User Comments (0)
About PowerShow.com