Eclipse Architecture and Patterns

Eclipse Architecture and Patterns
Mirko Stocker
Advanced Patterns and Frameworks
April, 2014
IFS
INSTITUTE FOR
SOFTWARE
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
2 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
3 / 74
Eclipse History
Origins
1999 Work begins on Eclipse inside IBM
2001 Eclipse 1.0 released as open source
2002 Eclipse 2.0 released
2004 Eclipse 3.0 released based on OSGi
2012 Eclipse 4.2 released with completely new fundament
Mirko Stocker
Eclipse Architecture and Patterns
2014
4 / 74
Eclipse History
Eclipse 1.0
Mirko Stocker
Eclipse Architecture and Patterns
2014
5 / 74
Eclipse History
Eclipse 2.0
Mirko Stocker
Eclipse Architecture and Patterns
2014
6 / 74
Eclipse Goals
Open platform for application development to make building tools easier
and to provide common tools
Started as an IDE (-framework) but then became a general platform
Integration and interaction of various tools and services based on plug-ins
Platform independent but native widgets (SWT)
The Eclipse platform itself is a sort of universal tool platform – it is an
IDE for anything and nothing in particular.
Mirko Stocker
Eclipse Architecture and Patterns
2014
7 / 74
Eclipse Components
Figure : Source [GammaBeck03]
Mirko Stocker
Eclipse Architecture and Patterns
2014
8 / 74
Eclipse Components
Figure : Source Eclipse.org
Mirko Stocker
Eclipse Architecture and Patterns
2014
9 / 74
Eclipse Components
Figure : Source Eclipse.org
Mirko Stocker
Eclipse Architecture and Patterns
2014
10 / 74
Project Stats
Over 250 projects under development at eclipse.org
Yearly releases in June for projects that want to participate
Figure : Source [Aniszczyk]
Mirko Stocker
Eclipse Architecture and Patterns
2014
11 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
12 / 74
SWT
Standard Widget Toolkit
Native Widgets and mostly written
in Java
No performance impact from UI
Native library and thin JNI layer for
each toolkit
SWT implementation per platform
Why not AWT or Swing?
Mirko Stocker
Eclipse Architecture and Patterns
2014
13 / 74
JFace
UI-Toolkit
SWT provides basic components
JFace builds on top of SWT: Actions, Menus, Dialogs, Wizards, Fonts
Figure : Source Eclipse.org
Mirko Stocker
Eclipse Architecture and Patterns
2014
14 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
15 / 74
OSGi
Open Services Gateway initiative
Specifies a component and service model for Java
Components (bundles) can be dynamically installed, started, stopped,
and uninstalled
Bundles define dependencies and export some of their packages (unlike
a JAR)
Similar to Java 9 (former 8) Modularization proposal
Several OSGi implementations, Eclipse’s is Equinox
GlassFish, JBoss, WebSphere, IntelliJ, Netbeans, and many others are
all based on OSGi
Mirko Stocker
Eclipse Architecture and Patterns
2014
16 / 74
OSGi Bundles
Eclipse plug-ins are OSGi bundles! (since Eclipse 3)
Packaged as JARs
MANIFEST.MF file contains the bundle metadata
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Popup Plug-in
Bundle-SymbolicName: com.example.myosgi;
singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: com.example.myosgi.Activator
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime;bundle-version="4.2.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: com.example.myosgi
Mirko Stocker
Eclipse Architecture and Patterns
2014
17 / 74
OSGi Services
OSGi also offers services to connect bundles
Service is a POJO and can be registered at a Service Registry at bundle
start
For historical reasons, Eclipse does not use OSGi services but
Extensions and Extension Points
Mirko Stocker
Eclipse Architecture and Patterns
2014
18 / 74
Extension Points
Extension Point collects contributions to the plug-in offering the Extension
Point
Extension contributes to an Extension Point
Each plug-in contributes to at least one Extension Point
Optionally declares a new EP
Extension Points and Extensions are not part of the Manifest but
separate XML files
Figure : Source Eclipse.org
Mirko Stocker
Eclipse Architecture and Patterns
2014
19 / 74
Eclipse Plug-ins
Plug-ins can add new Actions, Editors, Views, Menus, Perspectives,
Preferences
Each plug-in gets its own class loader
Can only access specified dependencies
Mirko Stocker
Eclipse Architecture and Patterns
2014
20 / 74
Plug-in Example
A plug-in has three interesting files:
Manifest
plugin.xml to wire up the Extension
Class that implements the Extension
Mirko Stocker
Eclipse Architecture and Patterns
2014
21 / 74
Plug-in Example
Mirko Stocker
Eclipse Architecture and Patterns
2014
22 / 74
Plug-in Example
Figure : Source [EclipseZone]
Mirko Stocker
Eclipse Architecture and Patterns
2014
23 / 74
Eclipse Plug-ins
Eclipse platform manages plug-ins:
discover installed plug-ins from various disk locations
builds the extension registry
connects extensions and extension points, only activating plug-ins when
needed
caches the extension registry to disc (-clean option)
Mirko Stocker
Eclipse Architecture and Patterns
2014
24 / 74
Plug-in Example
Mirko Stocker
Eclipse Architecture and Patterns
2014
25 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
26 / 74
Classical GoF Design Patterns
Creational Patterns
Factory
Builder
Singleton
...
Structural Patterns
Adapter
Bridge
Composite
Proxy
Façade
...
Behavioral Patterns
Observer
Command
Memento
Strategy
Visitor
...
Mirko Stocker
Eclipse Architecture and Patterns
2014
27 / 74
Classical GoF Design Patterns
Creational Patterns
Factory
Builder
Singleton
...
Structural Patterns
Adapter
Bridge
Composite
Proxy
Façade
...
Behavioral Patterns
Observer
Command
Memento
Strategy
Visitor
...
Mirko Stocker
Platform
Singleton: getting Workbench, Plug-in
Workspace Resources
Proxy and Bridge: Accessing File System
Composite: the workspace
Observer: tracking resource changes
Core Runtime
IAdaptable and Adapter Factories: Property View SWT
SWT
Composite: composing widgets
Strategy: defining layouts
JFace
Observer: responding to events
Pluggable adapter: Connecting widget to model
Strategy: customize a viewer without subclassing
Command: Actions
UI Workbench
Memento: persisting UI state
Virtual Proxy: lazy loading with E.P.
LTK
Template Method, Composite, Memento
CDT
Visitor: to traverse the AST
Eclipse Architecture and Patterns
2014
27 / 74
Singleton
Ensure that a class only has one instance, and provide a global point of
access to it:
PlatformUI.getWorkbench()
Platform.getAdapterManager()
ResourcesPlugin.getWorkspace()
Mirko Stocker
Eclipse Architecture and Patterns
2014
28 / 74
Singleton
Ensure that a class only has one instance, and provide a global point of
access to it:
PlatformUI.getWorkbench()
Platform.getAdapterManager()
ResourcesPlugin.getWorkspace()
Problem: coupling, code hard to test
Mirko Stocker
Eclipse Architecture and Patterns
2014
28 / 74
Proxy
Provide a surrogate or place holder for another object to control access to it:
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
29 / 74
Proxy
Files and Resources
How can we represent an (external) resource in a progam?
1
Provide a handle for the resource
2
Handle acts as a proxy for the
resource
Mirko Stocker
Eclipse Architecture and Patterns
2014
30 / 74
Proxy
Files and Resources
Advantages of the Proxy pattern approach:
Small value objects
Define behaviour but don’t contain any state information
Can refer to non-existing resource (file not yet created)
Mirko Stocker
Eclipse Architecture and Patterns
2014
31 / 74
Proxy
Lazy Loading
The Proxy pattern can also be used to implement lazy-loading:
Eclipse only loads a plug-in when it’s needed.
UI actions are proxies for the real actions that are loaded when used the
first time.
Mirko Stocker
Eclipse Architecture and Patterns
2014
32 / 74
Bridge
Decouple an abstraction from its implementation so that the two can vary
independently:
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
33 / 74
Bridge
API interface is implemented by an internal class that delegates the calls to
another implementation.
Figure : Source [GammaBeck03]
Both Proxy and Bridge patterns are used here.
Mirko Stocker
Eclipse Architecture and Patterns
2014
34 / 74
Adapter
Adapt an interface for a class to make it compatible with another class:
Figure : Source [DoFactory]
Mirko Stocker
Eclipse Architecture and Patterns
2014
35 / 74
Adapter
Pluggable Adapters
JFace uses the Adapter pattern prominently:
IContentProvider to get at the contents of a domain object
ILabelProvider for the text or image to display
Figure : Source [IlyaShinkarenko]
Mirko Stocker
Eclipse Architecture and Patterns
2014
36 / 74
Observer
Define a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically.
Figure : Source [IlyaShinkarenko]
IWorkspace ws = ResourcesPlugin.getWorkspace();
ws.addResourceChangeListener(new IResourceChangeListener() {
@Override
public void resourceChanged(IResourceChangeEvent event){
// handle change
}
});
Mirko Stocker
Eclipse Architecture and Patterns
2014
37 / 74
Strategy
Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients
that use it.
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
38 / 74
Strategy
Strategy in SWT
Mirko Stocker
Eclipse Architecture and Patterns
2014
39 / 74
Strategy
Strategy in JFace
Filtering and sorting of elements in views are als handled by Strategies:
Figure : Source [IlyaShinkarenko]
Mirko Stocker
Eclipse Architecture and Patterns
2014
40 / 74
Command
Encapsulate a request as an object, thereby letting you parameterize clients
with different requests, queue or log requests, and support for undoable
operations.
Mirko Stocker
Figure : Source Wikipedia
Eclipse Architecture and Patterns
2014
41 / 74
Command
JFace IAction
IAction has a run() method encaspulating the code to be executed when
it is called by the Invoker.
More than just a function: stores additional data (label, icon, state).
Can be used (attached) to multiple menus, tool bars, buttons.
IAction action = new Action("Exit") {
@Override
public void run() {
System.exit(0);
}
};
action.setDescription("Exits the VM");
menu.add(action);
Mirko Stocker
Eclipse Architecture and Patterns
2014
42 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
43 / 74
LTK
What is the LTK?
Refactoring Language Toolkit (LTK) - a language neutral API for
refactorings:
Classes for constructing refactoring user interfaces (common L&F for
wizards and dialogs)
Refactoring wizard with diff preview
Classes for modelling resource changes (e. g., "insert method ’foo’ into
test.java at offset XY")
Refactoring participant functionality
Scriptable refactorings and refactoring history
Used by Java Development Tools (JDT), C/C++ Development Platform
(CDT), Eclipse Scala IDE and others
Consists of two plug-ins:
org.eclipse.ltk.core.refactoring
org.eclipse.ltk.ui.refactoring
Mirko Stocker
Eclipse Architecture and Patterns
2014
44 / 74
LTK
Elements of LTK
Figure : Source http://people.clarkson.edu/~dhou/courses/EE564-s07/
Eclipse-Refactoring.pdf
Mirko Stocker
Eclipse Architecture and Patterns
2014
45 / 74
LTK
Refactoring UI Example: Specifying refactoring parameters
Figure : LTK wizard for specifying refactoring parameters of "extract interface"
Mirko Stocker
Eclipse Architecture and Patterns
2014
46 / 74
LTK
Diff browser: change 1/2
Figure : LTK diff view with change 1/2 of "extract interface"
Mirko Stocker
Eclipse Architecture and Patterns
2014
47 / 74
LTK
Diff browser: change 2/2
Figure : LTK diff view with change 2/2 of "extract interface"
Mirko Stocker
Eclipse Architecture and Patterns
2014
48 / 74
LTK
Refactoring Lifecycle Overview
Figure : Source [Widmer06]
Problem: How does the LTK provide a skeleton of this algorithm, while
deferring some steps to subclasses?
Mirko Stocker
Eclipse Architecture and Patterns
2014
49 / 74
LTK
Template Method Pattern
Template method defines the skeleton of an algorithm as an abstract class,
allowing its subclasses to provide concrete behavior.
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
50 / 74
LTK
GoF Patterns applied: Template Method
Example of LTKs precondition checking before a refactoring is applied:
public a b s t r a c t class R e f a c t o r i n g extends P l a t f o r m O b j e c t {
public R e f a c t o r i n g S t a t u s c h e c k A l l C o n d i t i o n s ( I P r o g r e s s M o n i t o r pm) {
RefactoringTickProvider provider = getRefactoringTickProvider ( ) ;
pm. beginTask ( " " , p r o v i d e r . g e t C h e c k A l l C o n d i t i o n s T i c k s ( ) ) ;
R e f a c t o r i n g S t a t u s r e s u l t = new R e f a c t o r i n g S t a t u s ( ) ;
r e s u l t . merge ( c h e c k I n i t i a l C o n d i t i o n s ( ) ) ;
i f ( ! r e s u l t . hasFatalError ( ) ) {
r e s u l t . merge ( c h e c k F i n a l C o n d i t i o n s ( ) ) ;
}
pm. done ( ) ;
return r e s u l t ;
}
public a b s t r a c t R e f a c t o r i n g S t a t u s c h e c k I n i t i a l C o n d i t i o n s ( ) ;
public a b s t r a c t R e f a c t o r i n g S t a t u s c h e c k F i n a l C o n d i t i o n s ( ) ;
}
Mirko Stocker
Eclipse Architecture and Patterns
2014
51 / 74
LTK
Applied code transformations with change objects
After all preconditions have been checked AND no errors are detected,
LTK calls createChange(IProgressMonitor) on the refactoring
createChange yields a Change object
Change is used by LTK to genereate a change preview (diff) and to apply
the recorded change to the workspace
There are different kinds of change objects, e. g.:
TextChange: applies a TextEdit to a document
MoveResourceChange: used to move a resource
UndoTextFileChange: to perform the reverse of a TextFileChange
NullChange: refactoring change that does nothing
Most refactorings require several changes ⇒ Changes can be composed
to trees of changes
Problem: How can we treat these compositions and the individual
changes uniformly?
Mirko Stocker
Eclipse Architecture and Patterns
2014
52 / 74
LTK
Composite Pattern
Compose object into tree structures to represent part/whole hierarchies.
Composite lets clients treat individual objects and compositions of objects
uniformly.
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
53 / 74
LTK
GoF Patterns applied: Composite pattern
Change
perform(IProgressMonitor): Change
dispose()
0..*
Child
CompositeChange
TextFileChange
fEdit: TextEdit
changes: List
perform(IProgressMonitor): Change
dispose()
add(Change)
addAll(Change[])
perform(IProgressMonitor)
dispose()
1
Parent
Figure : Composite pattern with LTK’s change objects
Mirko Stocker
Eclipse Architecture and Patterns
2014
54 / 74
LTK
Participants
A refactoring participant can participate in the condition checking and
change creation of a refactoring processor
Refactorings that change several source files may have impact on some
of the other integrated tools
Examples:
Renaming classes in the Plug-in Development Environment (PDE)
Setting breakpoints in a debugger
Consistency of C function declarations and their JNI bindings
Mirko Stocker
Eclipse Architecture and Patterns
2014
55 / 74
LTK
Refactoring History
Refactorings that are intended to
participate in the refactoring
history and scripting service must
implement a descriptor and a
contribution object
Two scenarios for scriptable
refactorings:
Reapplying refactorings on a
previous version of a code base
Composing large and complex
refactorings from smaller
refactorings
Mirko Stocker
Eclipse Architecture and Patterns
2014
56 / 74
LTK
Scriptable Refactorings
<?xml version= " 1 . 0 " encoding= "UTF−8" ?>
< s e s s i o n version= " 1 . 0 " >
< r e f a c t o r i n g comment= " E x t r a c t i n t e r f a c e
f o r c l a s s &apos ; Foo&apos ; " d e s c r i p t i o n =
" Extract Interface Refactoring "
fileName = " f i l e : / tmp / TestLTK / t e s t . cpp "
f l a g s = " 4 " name= " IFoo " p r o j e c t = " TestLTK "
i d = " ch . h s r . E x t r a c t I n t e r f a c e R e f a c t o r i n g "
r e p l a c e = " t r u e " s e l e c t i o n = " 8 ,3 " / >
< / session>
Problem: How can be capture refactoring
states externally so that they can be applied to
code without user interaction?
Mirko Stocker
Eclipse Architecture and Patterns
2014
57 / 74
LTK
Memento Pattern
Capture and externalize an objects internal state without violating its
encapsulation, so that the object can be returned to this state later.
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
58 / 74
LTK
GoF Patterns applied: Memento
LTK’s RefactoringDescriptor: Memento that captures the
properties of a specific refactoring instance
Contains unique refactoring ID, timestamp, description and further
refactoring-specific data like selection, file path, chosen user arguments:
protected R e f a c t o r i n g D e s c r i p t o r g e t R e f a c t o r i n g D e s c r i p t o r ( ) {
Map< S t r i n g , S t r i n g > args = new HashMap< S t r i n g , S t r i n g > ( ) ;
args . p u t ( C R e f a c t o r i n g D e s c r i p t o r . FILE_NAME , fileName ) ;
args . p u t ( C R e f a c t o r i n g D e s c r i p t o r . SELECTION ,
s e l e c t e d R e g i o n . g e t O f f s e t ( ) + " , " + s e l e c t e d R e g i o n . getLength ( ) ) ;
args . p u t ( E x t r a c t C o n s t a n t R e f a c t o r i n g D e s c r i p t o r .NAME, i n f o . getName ( ) ) ;
/ / more r e f a c t o r i n g −s p e c i f i c i n f o r m a t i o n
r e t u r n new E x t r a c t C o n s t a n t R e f a c t o r i n g D e s c r i p t o r (
p r o j e c t . g e t P r o j e c t ( ) . getName ( ) , " E x t r a c t Constant R e f a c t o r i n g " ,
" Create c o n s t a n t f o r " + t a r g e t . getRawSignature ( ) , args ) ;
}
Mirko Stocker
Eclipse Architecture and Patterns
2014
59 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
60 / 74
CDT
Overview
First release with Eclipse 2.0 in April 2003
Fully functional C and C++ IDE based on the Eclipse platform
Features:
Support for project creation and managed build for various toolchains (e. g.,
GCC, Clang)
Standard make build
Source navigation
Type hierarchy, call graph, include browser, macro definition browser
Code editor with syntax highlighting, folding and hyperlink navigation
Source code refactoring and code generation
Visual debugging tools, including memory, registers, and disassembly
viewers
Mirko Stocker
Eclipse Architecture and Patterns
2014
61 / 74
CDT
CDT’s AST nodes
In C/C++, a translation unit (TU) is a source file with all included headers
The root node of CDT’s abstract syntax tree (AST) has the type
org.eclipse.cdt.core.dom.ast.IASTTranslationUnit
In CDT, C and C++ AST nodes are separated (e. g.,
IASTUnaryExpression and ICPPASTUnaryExpression); C has
~60, C++ ~90 nodes
org.eclipse.cdt.core.dom.ast.IASTNode is the parent interface
of all nodes in the AST
The AST can be traversed by calling IASTNode’s getParent() and
getChildren() methods
Calling child/parent methods can get cumbersome ⇒ We want to
decouple the data from the operations that process the data
Solution: visitor pattern
Mirko Stocker
Eclipse Architecture and Patterns
2014
62 / 74
CDT
Example of an AST
Figure : Example of CDT’s abstract syntax tree (AST)
Mirko Stocker
Eclipse Architecture and Patterns
2014
63 / 74
CDT
Visitor Pattern
Represent an operation to be performed on the elements of an object
structure. Visitor lets you define a new operation without changing the classes
of the elements on which it operates.
Figure : Source Wikipedia
Mirko Stocker
Eclipse Architecture and Patterns
2014
64 / 74
CDT
GoF Patterns applied: Visitor
To create a visitor, subclass from ASTVisitor
ASTVisitor has overloaded visit methods for each node type
Each node class has an accept(ASTVisitor) method (defined in
IASTNode) ⇒ calls visit(this)
Example visitor to collect all names:
class ASTNameVisitor extends ASTVisitor {
List<IASTName> names = new ArrayList<IASTName>();
{
this.shouldVisitNames = true;
}
@Override
public int visit(IASTName name) {
names.add(name);
return PROCESS_CONTINUE;
}
}
Mirko Stocker
Eclipse Architecture and Patterns
2014
65 / 74
Outline
1
Eclipse Overview
2
SWT and JFace
3
OSGi Bundles and Eclipse Plug-ins
4
Eclipse Core Patterns
5
Language Toolkit LTK Patterns
6
C/C++ Development Tooling Patterns
7
Eclipse 4
Mirko Stocker
Eclipse Architecture and Patterns
2014
66 / 74
Eclipse 4
Why do we need a new version?
Questions every architecture has to be examined against:
Is it still able to incorporate new technology?
Is it easy enough to attract new contributors?
Does it encourage community growth?
In late 2007, the Eclipse project committers decided that the answer is
NO
The project e4 was initiated as a playground for new ideas to improve
Eclipse
e4 is an incubator project
Eclipse 4.2 SDK is the new IDE that implements the ideas taken from e4
Mirko Stocker
Eclipse Architecture and Patterns
2014
67 / 74
Eclipse 4
What’s new?
The application model (based on the Eclipse Modeling Framework EMF)
is decoupled from its presentation
Different user interface toolkits (e. g., SWT, JavaFX) can be used to
render the model
Ability to use stylesheets to easily change the L&F of an Eclipse
application
Uniform use of dependency injection (DI)
IEclipseContext: hierarchically organized context (Map<String,
Object>) used as a registry and to lookup services
Core services (aka "20 Things"): standalone APIs that clients can use
without needing a deep understanding of them (logging, event handling,
access to perspectives, etc.)
Mirko Stocker
Eclipse Architecture and Patterns
2014
68 / 74
Eclipse 4
The decline of the Singleton Pattern
Eclipse code frequently uses global singleton accessors
Problem: tight coupling of consumers to their provider, inhibits reuse and
harms testability
DI separates the configuration of an object from its behaviour
DI provides a number of advantages:
Clients are able to write POJOs and list the services they need
Useful for testing: the assumptions are placed in the DI container rather than
in the client code
DI has some disadvantages too (no service discovery through code
completion, debugging issues)
E4’s injector is based on the standard JSR 330 annotations (e. g.,
javax.inject.Inject)
Mirko Stocker
Eclipse Architecture and Patterns
2014
69 / 74
Eclipse 4
Singleton vs. Dependency Injection
Usage of singletons with typical Eclipse 3.x view:
class MyView extends ViewPart {
public void c r e a t e P a r t C o n t r o l ( Composite p a r e n t ) {
Button button = . . . ;
P l a t f o r m U I . getWorkbench ( ) . getHelpSystem ( ) . s e t H e l p ( b u t t o n , " . . . " ) ;
}
}
With E4, parts are POJO’s and have their application services directly
injected:
class MyView {
@I nj ect
public void c r e a t e ( Composite parent , IWorkbenchHelpSystem h e l p ) {
Button button = . . . ;
help . setHelp ( button , " . . . " ) ;
}
}
Mirko Stocker
Eclipse Architecture and Patterns
2014
70 / 74
Eclipse 4
Programming Model
Typical usage of interfaces and cumbersome unpacking of generic event
to the object of interest in Eclipse 3.x:
@Override
public O b j e c t execute ( ExecutionEvent event ) throws E x e c u t i o n E x c e p t i o n
I S e l e c t i o n s e l e c t i o n = H a n d l e r U t i l . g e t C u r r e n t S e l e c t i o n ( event ) ;
i f ( s e l e c t i o n instanceof I S t r u c t u r e d S e l e c t i o n ) {
Object f i r s t = ( ( I S t r u c t u r e d S e l e l c t i o n ) s e l e c t i o n ) . getFirstElement ( ) ;
MyObject myObject = ( MyObject ) f i r s t E l e m e n t ;
}
}
With E4, it is enough to specify the object of interest as a parameter in
the annotated handler (precondition: MyObject is part of the Eclipse
context):
@Execute
public void execute ( MyObject myObject ) {
/ / h a n d l e r code
}
Mirko Stocker
Eclipse Architecture and Patterns
2014
71 / 74
Eclipse 4
Backwards Compatibility
Compatibility layer is an adapter of the workbench and delegates calls from
3.x based APIs to the corresponding e4 bundles. Precondition: existing
plug-in has to be API-clean!
Figure : Source http://www.eclipse.org/e4/resources/slides/Boris_
Bokowski-e4-EclipseRT-day-toronto.ppt
Mirko Stocker
Eclipse Architecture and Patterns
2014
72 / 74
Bibliography I
Erich Gamma and Kent Beck.
Contributing to Eclipse: Principles, Patterns, and Plug-Ins.
Addison Wesley, 2003.
Marc Teufel and Dr. Jonas Helming.
Eclipse 4: Rich Clients mit dem Eclipse SDK 4.2.
entwickler.press, 2012.
Ilya Shinkarenko.
Design Patterns Used in Eclipse
Bangalore, Eclipse Summit India 2009.
Neil Bartlett.
A Comparison of Eclipse Extensions and OSGi Services
http:
//www.eclipsezone.com/articles/extensions-vs-services
Mirko Stocker
Eclipse Architecture and Patterns
2014
73 / 74
Bibliography II
Adapter Pattern
http://www.dofactory.com/Patterns/PatternAdapter.aspx
Chris Aniszczyk.
Ecipse Juno Released for “Friends”
http://aniszczyk.org/2012/06/26/
ecipse-juno-released-for-friends
Tobias Widmer.
Unleashing the Power of Refactoring.
Eclipse Corner Articles, 2006.
Michael Petito.
Eclipse Refactoring.
EE564, 2007.
Various authors.
The Architecture of Open Source Applications.
http://www.aosabook.org/en/index.html
Mirko Stocker
Eclipse Architecture and Patterns
2014
74 / 74