Java: Generics and Annotations
Java: 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
8.2
Roadmap
Generics
> Annotations
> Model-Driven Engineering
>
8.3
Roadmap
Generics
> Annotations
> Model-Driven Engineering
>
8.4
8.5
8.6
8.7
Old
way
...
Stone stone = (Stone) stones.get(0);
New
way
No check, unsafe
Runtime error
...
Stone stone = stones.get(0);
Runtime is safe
8.8
Stack Example
Old way
New way:
we define a
generic
interface that
takes a type
parameter
8.9
public E top() {
assert !this.isEmpty();
return top.item;
}
O. Nierstrasz, O. Greevy, A. Kuhn
8.10
8.11
Bar
G<Bar>
Foo
G<Foo>
8.12
List<Object> lo = ls;
Compile error as
it is not type safe!
8.13
In other words
Object
List<Object>
String
List<String>
8.14
Wildcards
void printCollection(Collection c) {
Iterator i = c.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
void printCollection(Collection<Object> c) {
for (Object e: c){
System.out.println(e);
}
}
printCollection(stones);
O. Nierstrasz, O. Greevy, A. Kuhn
We want a
method that
prints our all
the elements
of a collection
Here is a
nave attempt
at writing it
using generics
Wont compile!
8.15
Collection<?>
void printCollection(Collection<?> c) {
for (Object e: c){
System.out.println(e);
}
}
printCollection(stones);
stone(java.awt.Color[r=255,g=0,b=0])
stone(java.awt.Color[r=0,g=255,b=0])
stone(java.awt.Color[r=0,g=255,b=0])
O. Nierstrasz, O. Greevy, A. Kuhn
8.16
Pitfalls of wildcards
String myString;
Object myObject;
List<?> c = new ArrayList<String>();
// c.add("hello world");
// c.add(new Object());
((List<String>) c).add("hello world");
((List<Object>) c).add(new Object());
// compile error
// compile error
// compile error
// no compile error!
// run-time error!
8.17
Bounded Wildcards
Consider a simple drawing application to draw shapes
(circles, rectangles,)
Shape
Canvas
draw(Canvas)
draw(Shape)
drawAll(List<Shape>)
Circle
Rectangle
Limited to
List<Shape>
8.18
a bounded wildcard
8.19
import java.util.*;
8.20
Roadmap
Generics
> Annotations
> Model-Driven Engineering
>
8.21
Annotations
>
>
8.22
@Before
public void setup() {
@Test
public void someTest() {
@Test(expected=IOException.class)
public void anotherTest() {
8.23
Roadmap
Generics
> Annotations
> Model-Driven Engineering
>
8.24
Platform
Independent
Model
software
developer
automatic
translation
8.25
Example: a model-driven UI
>
>
8.26
Model-driven Engineering
:Book
title: String
author: String
model-driven
Model
Model-driven UI
8.27
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface GetProperty {
public String value();
}
8.28
@GetProperty(Titel)
public void getTitle() {
return title;
}
@GetProperty(Autor)
public void getAuthor() {
return author;
}
8.29
8.30
import java.reflect.Method;
public void processProperty(Object obj, Method m)
throws Exception {
GetProperty g = m.getAnnotation(GetProperty.class);
this.add(new Jlabel(g.value()));
String value = (String) m.invoke(obj);
this.add(new JTextField(value));
}
8.31
8.32
8.33
Safety Patterns
License
http://creativecommons.org/licenses/by-sa/2.5/
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
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