8.
Java: Generics and Annotations
P2 Clients and Servers
Generics and Annotations
Sources
> David Flanagan, Java in a Nutshell, 5th Edition, OReilly.
> GoF, Design Patterns. Elements of Reusable ObjectOriented Software, Addison Wesley,1997.
> Gilad Bracha, Generics in the Java Programming Language, 2004
O. Nierstrasz, O. Greevy, A. Kuhn
8.2
P2 Clients and Servers
Roadmap
> Generics
> Annotations
> Model-Driven Engineering
O. Nierstrasz, O. Greevy, A. Kuhn
8.3
P2 Clients and Servers
Roadmap
> Generics
> Annotations
> Model-Driven Engineering
O. Nierstrasz, O. Greevy, A. Kuhn
8.4
P2 Clients and Servers
Why do we need generics?
Generics allow you to abstract over types.
The most common examples are container types,
the collection hierarchy.
O. Nierstrasz, O. Greevy, A. Kuhn
8.5
P2 Clients and Servers
Motivating Example Old Style
List stones = new LinkedList();
[Link](new Stone(RED));
[Link](new Stone(GREEN));
[Link](new Stone(RED));
Stone first = (Stone) [Link](0);
public int countStones(Color color) {
int tally = 0;
Iterator it = [Link]();
while ([Link]()) {
Stone stone = (Stone) [Link]();
if ([Link]() == color) {
tally++;
}
}
return tally;
}
O. Nierstrasz, O. Greevy, A. Kuhn
The cast is annoying
but essential!
8.6
P2 Clients and Servers
Motivating example new style using generics
List is a generic
interface that takes a type
as a parameter.
List<Stone> stones = new LinkedList<Stone>();
[Link](new Stone(RED));
[Link](new Stone(GREEN));
[Link](new Stone(RED));
Stone first = /*no cast*/ [Link](0);
public int countStones(Color color) {
int tally = 0;
/*no temporary*/
for (Stone stone : stones) {
/*no temporary, no cast*/
if ([Link]() == color) {
tally++;
}
}
return tally;
}
O. Nierstrasz, O. Greevy, A. Kuhn
8.7
P2 Clients and Servers
Compile Time vs. Runtime Safety
List stones = new LinkedList();
[Link](ceci nest pas un stone);
...
Stone stone = (Stone) [Link](0);
Old
way
No check, unsafe
Runtime error
New
way
List<Stone> stones = new LinkedList<Stone>();
[Link](ceci nest pas un stone);
...
Stone stone = [Link](0);
Compile time check
Runtime is safe
O. Nierstrasz, O. Greevy, A. Kuhn
8.8
P2 Clients and Servers
Stack Example
public interface StackInterface {
public boolean isEmpty();
public int size();
public void push(Object item);
public Object top();
public void pop();
}
public interface StackInterface<E> {
public boolean isEmpty();
public int size();
public void push(E item);
public E top();
public void pop();
}
O. Nierstrasz, O. Greevy, A. Kuhn
Old way
New way:
we dene a generic interface that
takes a type parameter
8.9
P2 Clients and Servers
Linked Stack Example
public class LinkStack<E> implements StackInterface<E> {
public class Cell {
public E item;
public Cell next;
public Cell(E item, Cell next) {
[Link] = item;
[Link] = next;
}
}
public E top() {
assert ![Link]();
return [Link];
}
O. Nierstrasz, O. Greevy, A. Kuhn
8.10
P2 Clients and Servers
Creating a Stack of Integers
Stack<Integer> myStack = new LinkedStack<Integer>();
[Link](42); // autoboxing
When a generic is instantiated, the actual type parameters are substituted for the formal type parameters.
O. Nierstrasz, O. Greevy, A. Kuhn
8.11
P2 Clients and Servers
Generics and Subtyping
Bar
G<Bar>
Foo
G<Foo>
In Java, Foo is a subtype of Bar only if Foos interface strictly includes Bars interface. Instantiated generics normally have different interfaces.
(I.e., if the type parameters are used in the public interface.)
O. Nierstrasz, O. Greevy, A. Kuhn
8.12
P2 Clients and Servers
Generics and Subtyping (II)
List<String> ls = new ArrayList<String>();
List<Object> lo = ls;
[Link](0, new Object()); // legal?!
[Link](0); // Not a string?!
Compile error as it is not type safe!
O. Nierstrasz, O. Greevy, A. Kuhn
8.13
P2 Clients and Servers
In other words
Object
List<Object>
String
List<String>
O. Nierstrasz, O. Greevy, A. Kuhn
8.14
P2 Clients and Servers
Wildcards
We want a method that prints our all the elements of a collection
void printCollection(Collection c) {
Iterator i = [Link]();
while ([Link]()) {
[Link]([Link]());
}
}
void printCollection(Collection<Object> c) {
for (Object e: c){
[Link](e);
}
}
Here is a nave attempt at writing it using generics
printCollection(stones);
O. Nierstrasz, O. Greevy, A. Kuhn
Wont compile!
8.15
P2 Clients and Servers
What type matches all kinds of collections?
Collection<?>
collection of unknown is a collection whose element type matches anything a wildcard type
void printCollection(Collection<?> c) {
for (Object e: c){
[Link](e);
}
}
printCollection(stones);
stone([Link][r=255,g=0,b=0])
stone([Link][r=0,g=255,b=0])
stone([Link][r=0,g=255,b=0])
O. Nierstrasz, O. Greevy, A. Kuhn
8.16
P2 Clients and Servers
Pitfalls of wildcards
String myString;
Object myObject;
List<?> c = new ArrayList<String>();
// [Link]("hello world");
// compile error
// [Link](new Object());
// compile error
((List<String>) c).add("hello world");
((List<Object>) c).add(new Object());
// no compile error!
// String myString = [Link](0); myString = (String) [Link](0);
myObject = [Link](0);
myString = (String) [Link](1);
// compile error
// run-time error!
O. Nierstrasz, O. Greevy, A. Kuhn
8.17
P2 Clients and Servers
Bounded Wildcards
Consider a simple drawing application to draw shapes (circles, rectangles,)
Shape
draw(Canvas)
Canvas
draw(Shape)
drawAll(List<Shape>)
Circle
Rectangle
Limited to
List<Shape>
8.18
O. Nierstrasz, O. Greevy, A. Kuhn
P2 Clients and Servers
A Method that accepts a List of any kind of Shape
public void drawAll(List<? extends Shape>) {}
a bounded wildcard
Shape is the upper bound of the wildcard
O. Nierstrasz, O. Greevy, A. Kuhn
8.19
P2 Clients and Servers
More fun with generics
import [Link].*;
public void pushAll(Collection<? extends E> collection) {
for (E element : collection) {
All elements must
[Link](element);
be at least an E
}
}
public List<E> sort(Comparator<? super E> comp) {
List<E> list = [Link]();
[Link](list, comp);
return list;
}
The comparison method must require at most an E
O. Nierstrasz, O. Greevy, A. Kuhn
8.20
P2 Clients and Servers
Roadmap
> Generics
> Annotations
> Model-Driven Engineering
O. Nierstrasz, O. Greevy, A. Kuhn
8.21
P2 Clients and Servers
Annotations
> Annotations are a special kind of comment
As with comments, annotations do not change or affect the semantics of the program, i.e. the runtime behavior.
> Annotations are meta-descriptions
Unlike comments, annotations can be accessed and used by third-party tools (e.g. JUnit) or even your program itself.
O. Nierstrasz, O. Greevy, A. Kuhn
8.22
P2 Clients and Servers
JUnit uses annotations
@Before
public void setup() {
@Test
public void someTest() {
@Test(expected=[Link])
public void anotherTest() {
JUnit uses annotations to nd out which methods are test methods, and which are part of the setup. You may even pass parameters to the annotations.
O. Nierstrasz, O. Greevy, A. Kuhn
8.23
P2 Clients and Servers
Roadmap
> Generics
> Annotations
> Model-Driven Engineering
O. Nierstrasz, O. Greevy, A. Kuhn
8.24
P2 Clients and Servers
The Vision of MDE
Platform
Independent
Model
software
developer
automatic
translation
O. Nierstrasz, O. Greevy, A. Kuhn
8.25
P2 Clients and Servers
Example: a model-driven UI
> We want a UI to edit any kind of object with any kind of
properties (i.e. Model-driven Engineering)
> The example requires these steps
Dene custom annotations for getters and setters.
Annotate our classes with these annotations
Write a UI class that access these annotations at runtime to create a custom UI
O. Nierstrasz, O. Greevy, A. Kuhn
8.26
P2 Clients and Servers
Model-driven Engineering
:Book
title: String
author: String
model-driven
Model
can be any kind of object
with any kind of properties
Model-driven UI
labels and eld are automatically
created based on the model
O. Nierstrasz, O. Greevy, A. Kuhn
8.27
P2 Clients and Servers
Dening our custom annotations
import [Link].*;
@Retention([Link])
@Target([Link])
public @interface GetProperty {
public String value();
}
This denes a @GetProperty annotation for methods. The annotation is accessible at runtime.
O. Nierstrasz, O. Greevy, A. Kuhn
8.28
P2 Clients and Servers
Annotating our domain classes
@GetProperty(Titel)
public void getTitle() {
return title;
}
@GetProperty(Autor)
public void getAuthor() {
return author;
}
O. Nierstrasz, O. Greevy, A. Kuhn
8.29
P2 Clients and Servers
Use reection to access the annotations of any object
import [Link];
public void printAnnotatedMethods(Object obj) {
for (Method m : [Link]().getMethods()) {
if ([Link]([Link])) {
[Link](obj, m);
}
}
}
The for loop iterates over all methods of objs Class.
The if block is only entered for annotated methods.
8.30
O. Nierstrasz, O. Greevy, A. Kuhn
P2 Clients and Servers
Use reection to call any method of any object
import [Link];
public void processProperty(Object obj, Method m)
throws Exception {
GetProperty g = [Link]([Link]);
[Link](new Jlabel([Link]()));
String value = (String) [Link](obj);
[Link](new JTextField(value));
}
We use reection to invoke the method m on the
object obj.
O. Nierstrasz, O. Greevy, A. Kuhn
8.31
P2 Clients and Servers
What you should know!
Why do I need generics?
Why is casting dangerous?
How do I use generics?
Can I subtype a generic type?
When is the Abstract Factory pattern useful?
Some uses of Annotations?
A Model-Driven Engineering Example
O. Nierstrasz, O. Greevy, A. Kuhn
8.32
P2 Clients and Servers
Can you answer these questions?
Why is List<Object> not the supertype of List<String>?
Which pattern could we use to implement a Windowing Toolkit that
supports multiple look-and-feel user interfaces?
What are the advantages and disadvantages of using the Abstract Factory Pattern?
O. Nierstrasz, O. Greevy, A. Kuhn
8.33
Safety Patterns
License
[Link]
Attribution-ShareAlike 2.5
You are free:
to copy, distribute, display, and perform the work
to make derivative works
to make commercial use of the work
Under the following conditions:
Attribution. You must attribute the work in the manner specied by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.
For any reuse or distribution, you must make clear to others the license terms of this work.
Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.
Oscar Nierstrasz
34