SlideShare ist ein Scribd-Unternehmen logo
1 von 44
Downloaden Sie, um offline zu lesen
Dragan Zuvic 2016
@dzuvic
w11k / theCodeCampus
Besseren Java-Code
mit Type Annotations
Besseren Java-Code mit Type Annotations 1
About Me
JSR 308
Fazit
Motivation<>
(Typ) Annotationen
Checker Framework
Einbinden </>
Probleme<>
Vorteile
Fragen & Referenzen </>
Überblick
Dragan Zuvic:
Full-Stack Entwickler
+ weitere Rollen
mit Java / Scala / TypeScript
@dzuvic
Besseren Java-Code mit Type Annotations 2
<>
-
-
-
-
Dragan Zuvic
Besseren Java-Code mit Type Annotations 3
Gegründet 2000<>
Entwicklung / Consulting
Web / Java / Scala Projekte </>
Gegründet 2013<>
Schulungen (seit 2007)
Projekt-Kickoffs </>
Über uns
Motivation
Besseren Java-Code mit Type Annotations
Maßeinheiten
1983: Air Canada (Gewicht)
1999: Mars Climate Orbiter (Kraft)
2003: Hochrheinbrücke Laufenburg (0-Pegel)
Besseren Java-Code mit Type Annotations 5
<>
-
-
-
Wo Typ-Systeme geholfen hätte
Was ist ein Typsystem?
A type system is a tractable syntactic method for proving the absence of certain program behaviors by
classifying phrases according to the kinds of values they compute.
≈ System der Zusicherung
Vorteile in der Java Entwicklung
Verifikation
Dokumentation
Tool-Unterstützung
Besseren Java-Code mit Type Annotations 6
<>
-
-
<>
1
2
3
Typ System
Idee: Typ-System als Plug-In
Gilad Bracha: Pluggable Type Systems (2003)
Optionale Systeme: TypeScript, Groovy, Dart, Strongtalk
Plug-In Typ Systeme → Metadaten überall (AST)
Optionale Type Qualifier
Integer laenge=10 vs. @m Integer laenge=10 * m
Verifikation: Individuell & Zuschaltbar
Noch bessere Dokumentation →Besseres Verständnis
Besseren Java-Code mit Type Annotations 7
<>
-
-
-
<>
<> -
-
Ziel von JSR 308
Taxonomie Typ-Systeme?
Static, Dynamic, Typ-Sicher, Nominell, Strukturell, Plug-In Typen, Union, Intersection, Dependent,
Entscheidbar? ...
Wiederholung: Typen in Java
Primitives: IntegralType, FloatingPointType, boolean
Refrence Types: ClassType, InterfaceType, TypeVariable, ArrayType
Ohne Namen: null Type
void
Besseren Java-Code mit Type Annotations 8
<>
-
<>
-
-
- JLS 3.10.7
- JLS 14.8
Java Typ System
Expert
Annotations
Besseren Java-Code mit Type Annotations
Deklarative Annotationen
Java: v > 1.4 ∧ v < 1.8
Metadaten: Quelltext  Javadoc (xdoclet)
Retention: Source, Class, Runtime
Target: type, field, method, parameter, constructor, local_variable, annotation_type, package
Nur an Deklarationen
Verarbeitung: Introspection, JSR269 oder apt
Breite Akzeptanz: JUnit, TestNG, Hibernate, CDI, EJB, JAX-RS, JAX-B, Spring, Guice, Findbugs, Lombok ...
Besseren Java-Code mit Type Annotations 10
<>
-
-
-
-
-
-
Java Annotations
Type Annotations
Seit Java 8: An Typen
JSR 308: Syntax & Bytecode
In JDK 8: Keine Built-In Annotations
Neue Targets: type_parameter, type_use
Verarbeitung: JSR 269 & Compiler API (com.sun.*)
Reflection unsichtbar
Besseren Java-Code mit Type Annotations 11
<>
-
-
-
-
-
-
Type Annotations
Risk: That the broad Java development community is not interested in
developing or using pluggable type systems.
— JEP 104
Besseren Java-Code mit Type Annotations 12
Risiken Umsetzung JSR 308
Syntax
A type annotation appears before the type’s simple name
on a wildcard type argument [..] before the wildcard
[..] given array level prefixes the brackets [..]
[..] in front of a constructor declaration [..]
[..] explicitly declare the method receiver as the first formal parameter [..]
[..] write an annotation on a type parameter declaration. [..]
Besseren Java-Code mit Type Annotations 13
<>
-
-
-
-
-
-
Type Annotations Syntax
Eigentlich überall an Typen außer:
Besseren Java-Code mit Type Annotations 14
<>
// Annotationen1
@Annotation4TypeUse @TypeUse Object o;2
 3
//.class4
@TypeUse Integer.class5
 6
// Import7
import @TypeUse java.util.List8
 9
// static access (Clazz = scoping)10
@TypeUse Clazz.staticMember11
 12
// super reference13
@TypeUse TASyntax.super.memberName14
Invalide Type Annotations Syntax
Besseren Java-Code mit Type Annotations 15
public class TASyntax<@TypeParam T extends @TypeUse Number>1
implements @TypeParam Virtual {2
Map<@TypeUse String, @TypeUse List<@TypeUse Document>> xmlDocument;3
Collection<@TypeParam ? super @TypeUse Document> contractCollection;4
String @TypeUse [] typedArray;5
public void whereToPut () throws @TypeUse IOException {6
@TypeParam Car c = new @TypeParam DirtyCar();7
DirtyCar d = (@TypeUse DirtyCar & Virtual) c;8
boolean b = d instanceof @TypeUse Car;9
}10
public static final TASyntax<Integer> newone() {11
return new <Integer>@TypeUse TASyntax();12
}13
public <@TypeParam X extends T> void specialBound ( X x ) {} ;14
}15
Type Annotations Syntax Beispiele
Dem
o
Checker Framework
Besseren Java-Code mit Type Annotations
Umfang
Reihe von Typ-Annotationen: NonNull ...
Einzeln nutzbare Typ Checker
diese bauen auf: Checker Framework
Wrapper für javac
Annotierte Bibliotheken: guava, rt.jar,..
Handbuch, Beispiele, Eclipse-Plugin,...
Unterstützung: Java 8 & Java 7
Aus dem universitären Umfeld (seit 2007)
Michael Ernst - University of Washington
Werner M. Dietl - University of Waterloo
Besseren Java-Code mit Type Annotations 17
<>
-
-
-
-
-
-
<>
/* @Nonull */ Integer laenge;1
<>
-
-
Checker Framework
Besseren Java-Code mit Type Annotations 18
static final @m int m = new @m Integer(1);1
static final @s int s = new @s Integer(1);2
 3
void demo() {4
@m int laenge = 5 * m;5
@s double zeit = 1 * s;6
 7
@mPERs double mProS = laenge / zeit;8
// Fails9
// @kmPERh double kmPERhdouble = laenge / zeit;10
}11
Der UnitChecker
Dem
o
Besseren Java-Code mit Type Annotations 19
String str = "str";1
@Nullable String strN = null;2
 3
String format="%s";4
String twoformat="%s %f";5
 6
String s1 = String.format("%s", str);7
String s_checkedBy = formatMe(twoformat, str, 1.0f);8
 9
// String s_errorNull = String.format("%s", strN);10
// String s_errorWrongType = String.format("%d", "zero.one");11
Der String Format Checker
Dem
o
Besseren Java-Code mit Type Annotations 20
public final static String parenOpen ="(";1
 2
public void doSometing() {3
String thePattern = "[a-z]+";4
 5
// @RegEx is inferred, so the following would not compile6
// String thePattern = "invalidPattern" + parenOpen;7
Pattern compiledPattern = Pattern.compile( thePattern );8
}9
Der Regular Expression Checker
Dem
o
@UnkownRegex
@RegEx(0)
@RegEx(x)
@RegExBottom
@PartialRegEx
Besseren Java-Code mit Type Annotations 21
Regex Typ Hierarchie
@NonNull @Nullable @MonotonicNonNull
De-Referenzierung von Nicht @NonNull Typen
Belegen von null bei @NonNull Typen
Nullness Checker "aktiviert" noch:
Initilization Checker
Prüft Zugriff nicht initialisierten Code (NPE)
Rawness Initialization Checker
Alternative Prüfung der Initialisierung
Map Key Checker
Prüft: Map.containsKey(key)==true
Besseren Java-Code mit Type Annotations 22
<>
-
-
<>
-
-
-
Nullness Checker
@NoNull Obj
@Nullable Obj
@Nonull X
@Nullable X
@Initialized Obj
@UnknownInitialization Obj
@UnkownInitialization X
@UnderInitialization Obj
@Initialized X @UnderInitialization X
Besseren Java-Code mit Type Annotations 23
Nullness Checker Hierarchie
Folgendes compiliert nicht:
Besseren Java-Code mit Type Annotations 24
public class NonNullInstance {1
final @NonNull Object nn;2
final List<@NonNull String> list = new ArrayList<String>();3
 4
public NonNullInstance(@NonNull Object nn, String ... entries) {5
this.nn = nn;6
this.list.addAll(Arrays.asList(entries));7
}8
}9
NonNullInstance x = new NonNullInstance(null);1
NonNullInstance y = new NonNullInstance("some", null, null);2
3
Nullness Checker Einfaches Beispiel
Dem
o
@NonNull / @Nullable abhängig vom Aufruf
Besseren Java-Code mit Type Annotations 25
<>
public class QueueDelegate {1
private final @NonNull Queue<@NonNull Integer> queue = new LinkedList<>();2
 3
@EnsuresNonNullIf(expression="pop()", result=false)4
public boolean isEmpty() {return this.queue.isEmpty();}5
 6
public Integer pop() {7
return this.queue.isEmpty() ? null : this.queue.poll();8
}9
}10
/* ... */11
QueueDelegate dlg = new QueueDelegate();12
@NonNull Integer doesCompile = dlg.isEmpty() ? 0 : dlg.pop();13
//@NonNull Integer doesNotCompile = dlg.pop();14
Nullness Checker - @EnsuresNonNullIf
Dem
o
@MonotonicNonNull
@PolyNull
Inferenz (x != null)
Externe Tools für Inferenz Annotation
System.getProperty("java.version") ist @NonNull !
Arrays
@NonNull Integer @Nullable []
@NonNull String[] x = new @NonNull String[10]; // FEHLER
Besseren Java-Code mit Type Annotations 26
<>
<>
<>
<>
<>
<>
-
-
Nullness Checker Advanced
Interning → x.equals(y) ≡ x==y
Andere Beispiele: Integer; Read-Only Objekte
Interning bspw. mit WeakHashMap → Guava Cache
Besseren Java-Code mit Type Annotations 27
<>
-
-
@Interned String s1 = "10";1
@Interned String s1_same = intern(new String("10"));2
// @Interned String s1_same = new String("10");3
 4
if (s1 == s1_same) System.out.println("same string");5
Interning Checker
Expert
Tainting: Typ-Übergang @Tainted → @Untainted
SQL-Injection, Passwort Hashing, Verschlüsselung Kreditkarten
Besseren Java-Code mit Type Annotations 28
<>
-
@Tainted @Nullable String pass = console.readLine("Password:");1
@Untainted String unTaintedPassword = hashPassword(pass);2
boolean passwordCorrect = checkPassword(unTaintedPassword);3
//...4
@NonNull boolean checkPassword(@Untainted @Nullable String pass) { .. }5
//...6
@Untainted7
@NonNull8
public String hashPassword(@Nullable String pass) { ...9
@SuppressWarnings({"tainting"})10
@Untainted String result = Base64.getEncoder().encodeToString(res);11
return result;12
}13
Tainting Checker
Expert
Eigene Typ-Hierarchien definieren:
Typ-Hierarchie als Parameter:
-Aquals=Encrypted,PossiblyUnencrypted,PolyEncrypted
Besseren Java-Code mit Type Annotations 29
<>
- @SubtypeOf(PossiblyUnencrypted.class)1
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})2
public @interface Encrypted {}3
- @SubtypeOf({})1
@DefaultQualifierInHierarchy2
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})3
public @interface PossiblyUnencrypted {}4
<>
<> // does not compile1
@Encrypted String enrypted="look it's unencrypted";2
Subytpe Checker
Expert
Definition einer Typ-Hierarchie
Typ-Regeln: BaseTypeVisitor → Compiler Tree Api
Typ-Inferenz: @ImplicitFor Annotation. Beispiel RegexBottom
@ImplicitFor(literals = {LiteralKind.NULL}..)
ggf. DataFlow-Analyse: Subklassen von
CFAbstractAnalysis & CFAbstractTransfer
Implementierung der Compiler Schnittstelle:
SourceChecker meist ← BaseTypeChecker oder AggregateChecker
Besseren Java-Code mit Type Annotations 30
<1>
<2>
<3>
<4>
<5>
Der Weg zum eigenen Checker (Skizze)
Expert
Besseren Java-Code mit Type Annotations 31
Aliasing Checker
Constant Value Checker
Fake Enum Checker
Format String Checker *
GUI Effect Checker
I18n Format String Checker
Interning Checker *
Linear Checker
Lock Checker
Map Key Checker
Nullness Checker *
Property File Checker
Reflection resolution Checker
Regex Checker *
Signature String Checker
Subtyping Checker *
Tainting Checker *
Units Checker *
Liste der mitgelieferten Checker
Integration
IDEs & Buildsysteme
Besseren Java-Code mit Type Annotations
Wrapper oder -processor
javacheck: Wrapper für javac
javac -processor
javacheck: Für Kommandozeilentools
-processor: mvn, gradle, ...
-Xbootclasspath/p:${annotatedJdk}
-processingpath checker.jar
Java 7: -J-Xbootclasspath/p:${typeAnnotationsJavac}
Weitere Argumente, bspw. Warn modus: -Awarns
Automatisches Processing:
META-INF/services/javax.annotation.processing.Processor
Besseren Java-Code mit Type Annotations 33
<>
-
-
<>
<>
-
-
-
-
<>
Checker Framework Integration
Klassenpfad für
jdk{7,8}.jar: Boot Class Path
checker-qual.jar: compile / compileOnly CP
checker.jar: +processing path
Annotierte Fremdbibliotheken vs. -Astubs=
JDK 9?
The Checkers Framework must continue to work.
— JEP 217: Annotations Pipeline 2.0
Besseren Java-Code mit Type Annotations 34
<>
-
-
-
<>
<>
Checker Framework Class Path
Processor in Settings → Build... → Compiler
Processor in Settings → Build... → Processing
Besseren Java-Code mit Type Annotations 35
IDE Einrichten Beispiel Intellij
Expert
Fazit
Besseren Java-Code mit Type Annotations
Eigene Typ-Qualifier → komplex
Compiler API
Handbuch: Typ-Designer
Test=Compilieren
Compilierung etwas langsamer
Nicht eigängige Fehler
Integration: Processing Classpath?
Besseren Java-Code mit Type Annotations 37
<>
-
-
-
<>
<>
<>
Nachteile & Probleme
Plug-In Verfikiation
Weiniger Tests
Weniger x!=null && x.doSomething()
Programmfluss bestimmen: SQL check
Große Auswahl an Checker
Flexibel (Eigene Typprüfung)
Keine Laufzeit Änderung
Typen können feiner Dokumentiert werden
Besseren Java-Code mit Type Annotations 38
<>
-
-
-
-
<>
<>
<>
Vorteile
Sinnvolles, Mächtiges und Komplexes Werkzeug
Nicht vertieft:
Typ-Polymorphie (Generics)
Invariante Array Types
Kontextsensitive Typ-Inferenz
Type Refinement (flow-sensitive)
Java Expressions:@EnsuresNonNullIf, @EnsuresQualifierIf, @GuaredBy ...
Dokumentation Typen → Besseren Java Code
Besseren Java-Code mit Type Annotations 39
<>
<>
-
-
-
-
-
<>
IMHO
Tipps zum Einsteigen
Nicht mit NullChecker beginnen
Sub-Typing als Type-Annotations
infer-and-annotate.sh
-Awarns
Sinnvoll ab Java 8
Online Demo Checker Framework
Besseren Java-Code mit Type Annotations 40
<>
1
2
3
4
5
6
Tipps zum Einsteigen
Expert
Checker Framework
http://checkerframework.org/
Online Demo Checker Framework
http://eisop.uwaterloo.ca/live (Source)
Data Flow Handbuch
http://types.cs.washington.edu/checker-framework/current/checker-framework-dataflow-manual.pdf
Pluggable Type Systems
http://bracha.org/pluggable-types.pdf
The Hitchhiker's Guide to javac
http://openjdk.java.net/groups/compiler/doc/hhgtjavac/index.html
JEP 217: Annotations Pipeline 2.0
http://openjdk.java.net/jeps/217
Besseren Java-Code mit Type Annotations 41
Referenzen
Fragen und Antworten
Danke...
Besseren Java-Code mit Type Annotations
Fragen und
Antworten
Vortrag & Quellen
github.com/dzuvic/jsr308-
examples
→
Dragan Zuvic
@dzuvic
www.w11k.de
www.thecodecampus.de

Weitere ähnliche Inhalte

Ähnlich wie Besseren Java Code mit Type Annotations

Prototype 1.7
Prototype 1.7Prototype 1.7
Prototype 1.7msebel
 
JavaScript für Java-Entwickler W-JAX 2013
JavaScript für Java-Entwickler W-JAX 2013JavaScript für Java-Entwickler W-JAX 2013
JavaScript für Java-Entwickler W-JAX 2013Oliver Zeigermann
 
AdvancedTdd
AdvancedTddAdvancedTdd
AdvancedTddjlink
 
Testgetriebene Softwareentwicklung
Testgetriebene SoftwareentwicklungTestgetriebene Softwareentwicklung
Testgetriebene Softwareentwicklungjlink
 
Funktionale Reaktive Programmierung mit Sodium
Funktionale Reaktive Programmierung mit SodiumFunktionale Reaktive Programmierung mit Sodium
Funktionale Reaktive Programmierung mit SodiumTorsten Fink
 
Referat T13 Javadoc
Referat T13 JavadocReferat T13 Javadoc
Referat T13 Javadocminiroquer
 
Was ist neu in Java 6, 7, 8, ...
Was ist neu in Java 6, 7, 8, ...Was ist neu in Java 6, 7, 8, ...
Was ist neu in Java 6, 7, 8, ...Andreas Schreiber
 
Nigh Session Scala
Nigh Session ScalaNigh Session Scala
Nigh Session Scalaadesso AG
 
Legacy Code refaktorisieren
Legacy Code refaktorisierenLegacy Code refaktorisieren
Legacy Code refaktorisierenHendrik Lösch
 
iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...
iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...
iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...Lukas Eder
 
The Lotus Code Cookbook
The Lotus Code CookbookThe Lotus Code Cookbook
The Lotus Code CookbookUlrich Krause
 
Einführung in die funktionale Programmierung
Einführung in die funktionale ProgrammierungEinführung in die funktionale Programmierung
Einführung in die funktionale ProgrammierungDigicomp Academy AG
 
Was jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollteWas jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollteberndmueller
 
JSF Testing - Tools und Technics
JSF Testing - Tools und TechnicsJSF Testing - Tools und Technics
JSF Testing - Tools und Technicsadesso AG
 
Einführung in die funktionale Programmierung mit Clojure
Einführung in die funktionale Programmierung mit ClojureEinführung in die funktionale Programmierung mit Clojure
Einführung in die funktionale Programmierung mit ClojureSascha Koch
 
Praesentation TYPO3Camp Berlin Speed mit Extbase
Praesentation TYPO3Camp Berlin Speed mit ExtbasePraesentation TYPO3Camp Berlin Speed mit Extbase
Praesentation TYPO3Camp Berlin Speed mit ExtbaseStefan Frömken
 
Test-driven Development mit TYPO3
Test-driven Development mit TYPO3Test-driven Development mit TYPO3
Test-driven Development mit TYPO3Oliver Klee
 
JPA the Versant Way
JPA the Versant WayJPA the Versant Way
JPA the Versant Wayjubecker
 

Ähnlich wie Besseren Java Code mit Type Annotations (20)

Prototype 1.7
Prototype 1.7Prototype 1.7
Prototype 1.7
 
JavaScript für Java-Entwickler W-JAX 2013
JavaScript für Java-Entwickler W-JAX 2013JavaScript für Java-Entwickler W-JAX 2013
JavaScript für Java-Entwickler W-JAX 2013
 
AdvancedTdd
AdvancedTddAdvancedTdd
AdvancedTdd
 
Testgetriebene Softwareentwicklung
Testgetriebene SoftwareentwicklungTestgetriebene Softwareentwicklung
Testgetriebene Softwareentwicklung
 
Funktionale Reaktive Programmierung mit Sodium
Funktionale Reaktive Programmierung mit SodiumFunktionale Reaktive Programmierung mit Sodium
Funktionale Reaktive Programmierung mit Sodium
 
Referat T13 Javadoc
Referat T13 JavadocReferat T13 Javadoc
Referat T13 Javadoc
 
Was ist neu in Java 6, 7, 8, ...
Was ist neu in Java 6, 7, 8, ...Was ist neu in Java 6, 7, 8, ...
Was ist neu in Java 6, 7, 8, ...
 
Nigh Session Scala
Nigh Session ScalaNigh Session Scala
Nigh Session Scala
 
Legacy Code refaktorisieren
Legacy Code refaktorisierenLegacy Code refaktorisieren
Legacy Code refaktorisieren
 
iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...
iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...
iJUG Java Aktuell [Februar 2015] Lukas Eder - jOOQ - ein alternativer Weg mit...
 
The Lotus Code Cookbook
The Lotus Code CookbookThe Lotus Code Cookbook
The Lotus Code Cookbook
 
Einführung in die funktionale Programmierung
Einführung in die funktionale ProgrammierungEinführung in die funktionale Programmierung
Einführung in die funktionale Programmierung
 
Was jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollteWas jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollte
 
JSF Testing - Tools und Technics
JSF Testing - Tools und TechnicsJSF Testing - Tools und Technics
JSF Testing - Tools und Technics
 
A/B Testing mit Node.js
A/B Testing mit Node.jsA/B Testing mit Node.js
A/B Testing mit Node.js
 
Einführung in die funktionale Programmierung mit Clojure
Einführung in die funktionale Programmierung mit ClojureEinführung in die funktionale Programmierung mit Clojure
Einführung in die funktionale Programmierung mit Clojure
 
Praesentation TYPO3Camp Berlin Speed mit Extbase
Praesentation TYPO3Camp Berlin Speed mit ExtbasePraesentation TYPO3Camp Berlin Speed mit Extbase
Praesentation TYPO3Camp Berlin Speed mit Extbase
 
Test-driven Development mit TYPO3
Test-driven Development mit TYPO3Test-driven Development mit TYPO3
Test-driven Development mit TYPO3
 
JPA the Versant Way
JPA the Versant WayJPA the Versant Way
JPA the Versant Way
 
TypeScript
TypeScriptTypeScript
TypeScript
 

Besseren Java Code mit Type Annotations

  • 1. Dragan Zuvic 2016 @dzuvic w11k / theCodeCampus Besseren Java-Code mit Type Annotations
  • 2. Besseren Java-Code mit Type Annotations 1 About Me JSR 308 Fazit Motivation<> (Typ) Annotationen Checker Framework Einbinden </> Probleme<> Vorteile Fragen & Referenzen </> Überblick
  • 3. Dragan Zuvic: Full-Stack Entwickler + weitere Rollen mit Java / Scala / TypeScript @dzuvic Besseren Java-Code mit Type Annotations 2 <> - - - - Dragan Zuvic
  • 4. Besseren Java-Code mit Type Annotations 3 Gegründet 2000<> Entwicklung / Consulting Web / Java / Scala Projekte </> Gegründet 2013<> Schulungen (seit 2007) Projekt-Kickoffs </> Über uns
  • 6. Maßeinheiten 1983: Air Canada (Gewicht) 1999: Mars Climate Orbiter (Kraft) 2003: Hochrheinbrücke Laufenburg (0-Pegel) Besseren Java-Code mit Type Annotations 5 <> - - - Wo Typ-Systeme geholfen hätte
  • 7. Was ist ein Typsystem? A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute. ≈ System der Zusicherung Vorteile in der Java Entwicklung Verifikation Dokumentation Tool-Unterstützung Besseren Java-Code mit Type Annotations 6 <> - - <> 1 2 3 Typ System
  • 8. Idee: Typ-System als Plug-In Gilad Bracha: Pluggable Type Systems (2003) Optionale Systeme: TypeScript, Groovy, Dart, Strongtalk Plug-In Typ Systeme → Metadaten überall (AST) Optionale Type Qualifier Integer laenge=10 vs. @m Integer laenge=10 * m Verifikation: Individuell & Zuschaltbar Noch bessere Dokumentation →Besseres Verständnis Besseren Java-Code mit Type Annotations 7 <> - - - <> <> - - Ziel von JSR 308
  • 9. Taxonomie Typ-Systeme? Static, Dynamic, Typ-Sicher, Nominell, Strukturell, Plug-In Typen, Union, Intersection, Dependent, Entscheidbar? ... Wiederholung: Typen in Java Primitives: IntegralType, FloatingPointType, boolean Refrence Types: ClassType, InterfaceType, TypeVariable, ArrayType Ohne Namen: null Type void Besseren Java-Code mit Type Annotations 8 <> - <> - - - JLS 3.10.7 - JLS 14.8 Java Typ System Expert
  • 11. Deklarative Annotationen Java: v > 1.4 ∧ v < 1.8 Metadaten: Quelltext  Javadoc (xdoclet) Retention: Source, Class, Runtime Target: type, field, method, parameter, constructor, local_variable, annotation_type, package Nur an Deklarationen Verarbeitung: Introspection, JSR269 oder apt Breite Akzeptanz: JUnit, TestNG, Hibernate, CDI, EJB, JAX-RS, JAX-B, Spring, Guice, Findbugs, Lombok ... Besseren Java-Code mit Type Annotations 10 <> - - - - - - Java Annotations
  • 12. Type Annotations Seit Java 8: An Typen JSR 308: Syntax & Bytecode In JDK 8: Keine Built-In Annotations Neue Targets: type_parameter, type_use Verarbeitung: JSR 269 & Compiler API (com.sun.*) Reflection unsichtbar Besseren Java-Code mit Type Annotations 11 <> - - - - - - Type Annotations
  • 13. Risk: That the broad Java development community is not interested in developing or using pluggable type systems. — JEP 104 Besseren Java-Code mit Type Annotations 12 Risiken Umsetzung JSR 308
  • 14. Syntax A type annotation appears before the type’s simple name on a wildcard type argument [..] before the wildcard [..] given array level prefixes the brackets [..] [..] in front of a constructor declaration [..] [..] explicitly declare the method receiver as the first formal parameter [..] [..] write an annotation on a type parameter declaration. [..] Besseren Java-Code mit Type Annotations 13 <> - - - - - - Type Annotations Syntax
  • 15. Eigentlich überall an Typen außer: Besseren Java-Code mit Type Annotations 14 <> // Annotationen1 @Annotation4TypeUse @TypeUse Object o;2  3 //.class4 @TypeUse Integer.class5  6 // Import7 import @TypeUse java.util.List8  9 // static access (Clazz = scoping)10 @TypeUse Clazz.staticMember11  12 // super reference13 @TypeUse TASyntax.super.memberName14 Invalide Type Annotations Syntax
  • 16. Besseren Java-Code mit Type Annotations 15 public class TASyntax<@TypeParam T extends @TypeUse Number>1 implements @TypeParam Virtual {2 Map<@TypeUse String, @TypeUse List<@TypeUse Document>> xmlDocument;3 Collection<@TypeParam ? super @TypeUse Document> contractCollection;4 String @TypeUse [] typedArray;5 public void whereToPut () throws @TypeUse IOException {6 @TypeParam Car c = new @TypeParam DirtyCar();7 DirtyCar d = (@TypeUse DirtyCar & Virtual) c;8 boolean b = d instanceof @TypeUse Car;9 }10 public static final TASyntax<Integer> newone() {11 return new <Integer>@TypeUse TASyntax();12 }13 public <@TypeParam X extends T> void specialBound ( X x ) {} ;14 }15 Type Annotations Syntax Beispiele Dem o
  • 17. Checker Framework Besseren Java-Code mit Type Annotations
  • 18. Umfang Reihe von Typ-Annotationen: NonNull ... Einzeln nutzbare Typ Checker diese bauen auf: Checker Framework Wrapper für javac Annotierte Bibliotheken: guava, rt.jar,.. Handbuch, Beispiele, Eclipse-Plugin,... Unterstützung: Java 8 & Java 7 Aus dem universitären Umfeld (seit 2007) Michael Ernst - University of Washington Werner M. Dietl - University of Waterloo Besseren Java-Code mit Type Annotations 17 <> - - - - - - <> /* @Nonull */ Integer laenge;1 <> - - Checker Framework
  • 19. Besseren Java-Code mit Type Annotations 18 static final @m int m = new @m Integer(1);1 static final @s int s = new @s Integer(1);2  3 void demo() {4 @m int laenge = 5 * m;5 @s double zeit = 1 * s;6  7 @mPERs double mProS = laenge / zeit;8 // Fails9 // @kmPERh double kmPERhdouble = laenge / zeit;10 }11 Der UnitChecker Dem o
  • 20. Besseren Java-Code mit Type Annotations 19 String str = "str";1 @Nullable String strN = null;2  3 String format="%s";4 String twoformat="%s %f";5  6 String s1 = String.format("%s", str);7 String s_checkedBy = formatMe(twoformat, str, 1.0f);8  9 // String s_errorNull = String.format("%s", strN);10 // String s_errorWrongType = String.format("%d", "zero.one");11 Der String Format Checker Dem o
  • 21. Besseren Java-Code mit Type Annotations 20 public final static String parenOpen ="(";1  2 public void doSometing() {3 String thePattern = "[a-z]+";4  5 // @RegEx is inferred, so the following would not compile6 // String thePattern = "invalidPattern" + parenOpen;7 Pattern compiledPattern = Pattern.compile( thePattern );8 }9 Der Regular Expression Checker Dem o
  • 23. @NonNull @Nullable @MonotonicNonNull De-Referenzierung von Nicht @NonNull Typen Belegen von null bei @NonNull Typen Nullness Checker "aktiviert" noch: Initilization Checker Prüft Zugriff nicht initialisierten Code (NPE) Rawness Initialization Checker Alternative Prüfung der Initialisierung Map Key Checker Prüft: Map.containsKey(key)==true Besseren Java-Code mit Type Annotations 22 <> - - <> - - - Nullness Checker
  • 24. @NoNull Obj @Nullable Obj @Nonull X @Nullable X @Initialized Obj @UnknownInitialization Obj @UnkownInitialization X @UnderInitialization Obj @Initialized X @UnderInitialization X Besseren Java-Code mit Type Annotations 23 Nullness Checker Hierarchie
  • 25. Folgendes compiliert nicht: Besseren Java-Code mit Type Annotations 24 public class NonNullInstance {1 final @NonNull Object nn;2 final List<@NonNull String> list = new ArrayList<String>();3  4 public NonNullInstance(@NonNull Object nn, String ... entries) {5 this.nn = nn;6 this.list.addAll(Arrays.asList(entries));7 }8 }9 NonNullInstance x = new NonNullInstance(null);1 NonNullInstance y = new NonNullInstance("some", null, null);2 3 Nullness Checker Einfaches Beispiel Dem o
  • 26. @NonNull / @Nullable abhängig vom Aufruf Besseren Java-Code mit Type Annotations 25 <> public class QueueDelegate {1 private final @NonNull Queue<@NonNull Integer> queue = new LinkedList<>();2  3 @EnsuresNonNullIf(expression="pop()", result=false)4 public boolean isEmpty() {return this.queue.isEmpty();}5  6 public Integer pop() {7 return this.queue.isEmpty() ? null : this.queue.poll();8 }9 }10 /* ... */11 QueueDelegate dlg = new QueueDelegate();12 @NonNull Integer doesCompile = dlg.isEmpty() ? 0 : dlg.pop();13 //@NonNull Integer doesNotCompile = dlg.pop();14 Nullness Checker - @EnsuresNonNullIf Dem o
  • 27. @MonotonicNonNull @PolyNull Inferenz (x != null) Externe Tools für Inferenz Annotation System.getProperty("java.version") ist @NonNull ! Arrays @NonNull Integer @Nullable [] @NonNull String[] x = new @NonNull String[10]; // FEHLER Besseren Java-Code mit Type Annotations 26 <> <> <> <> <> <> - - Nullness Checker Advanced
  • 28. Interning → x.equals(y) ≡ x==y Andere Beispiele: Integer; Read-Only Objekte Interning bspw. mit WeakHashMap → Guava Cache Besseren Java-Code mit Type Annotations 27 <> - - @Interned String s1 = "10";1 @Interned String s1_same = intern(new String("10"));2 // @Interned String s1_same = new String("10");3  4 if (s1 == s1_same) System.out.println("same string");5 Interning Checker Expert
  • 29. Tainting: Typ-Übergang @Tainted → @Untainted SQL-Injection, Passwort Hashing, Verschlüsselung Kreditkarten Besseren Java-Code mit Type Annotations 28 <> - @Tainted @Nullable String pass = console.readLine("Password:");1 @Untainted String unTaintedPassword = hashPassword(pass);2 boolean passwordCorrect = checkPassword(unTaintedPassword);3 //...4 @NonNull boolean checkPassword(@Untainted @Nullable String pass) { .. }5 //...6 @Untainted7 @NonNull8 public String hashPassword(@Nullable String pass) { ...9 @SuppressWarnings({"tainting"})10 @Untainted String result = Base64.getEncoder().encodeToString(res);11 return result;12 }13 Tainting Checker Expert
  • 30. Eigene Typ-Hierarchien definieren: Typ-Hierarchie als Parameter: -Aquals=Encrypted,PossiblyUnencrypted,PolyEncrypted Besseren Java-Code mit Type Annotations 29 <> - @SubtypeOf(PossiblyUnencrypted.class)1 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})2 public @interface Encrypted {}3 - @SubtypeOf({})1 @DefaultQualifierInHierarchy2 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})3 public @interface PossiblyUnencrypted {}4 <> <> // does not compile1 @Encrypted String enrypted="look it's unencrypted";2 Subytpe Checker Expert
  • 31. Definition einer Typ-Hierarchie Typ-Regeln: BaseTypeVisitor → Compiler Tree Api Typ-Inferenz: @ImplicitFor Annotation. Beispiel RegexBottom @ImplicitFor(literals = {LiteralKind.NULL}..) ggf. DataFlow-Analyse: Subklassen von CFAbstractAnalysis & CFAbstractTransfer Implementierung der Compiler Schnittstelle: SourceChecker meist ← BaseTypeChecker oder AggregateChecker Besseren Java-Code mit Type Annotations 30 <1> <2> <3> <4> <5> Der Weg zum eigenen Checker (Skizze) Expert
  • 32. Besseren Java-Code mit Type Annotations 31 Aliasing Checker Constant Value Checker Fake Enum Checker Format String Checker * GUI Effect Checker I18n Format String Checker Interning Checker * Linear Checker Lock Checker Map Key Checker Nullness Checker * Property File Checker Reflection resolution Checker Regex Checker * Signature String Checker Subtyping Checker * Tainting Checker * Units Checker * Liste der mitgelieferten Checker
  • 33. Integration IDEs & Buildsysteme Besseren Java-Code mit Type Annotations
  • 34. Wrapper oder -processor javacheck: Wrapper für javac javac -processor javacheck: Für Kommandozeilentools -processor: mvn, gradle, ... -Xbootclasspath/p:${annotatedJdk} -processingpath checker.jar Java 7: -J-Xbootclasspath/p:${typeAnnotationsJavac} Weitere Argumente, bspw. Warn modus: -Awarns Automatisches Processing: META-INF/services/javax.annotation.processing.Processor Besseren Java-Code mit Type Annotations 33 <> - - <> <> - - - - <> Checker Framework Integration
  • 35. Klassenpfad für jdk{7,8}.jar: Boot Class Path checker-qual.jar: compile / compileOnly CP checker.jar: +processing path Annotierte Fremdbibliotheken vs. -Astubs= JDK 9? The Checkers Framework must continue to work. — JEP 217: Annotations Pipeline 2.0 Besseren Java-Code mit Type Annotations 34 <> - - - <> <> Checker Framework Class Path
  • 36. Processor in Settings → Build... → Compiler Processor in Settings → Build... → Processing Besseren Java-Code mit Type Annotations 35 IDE Einrichten Beispiel Intellij Expert
  • 37. Fazit Besseren Java-Code mit Type Annotations
  • 38. Eigene Typ-Qualifier → komplex Compiler API Handbuch: Typ-Designer Test=Compilieren Compilierung etwas langsamer Nicht eigängige Fehler Integration: Processing Classpath? Besseren Java-Code mit Type Annotations 37 <> - - - <> <> <> Nachteile & Probleme
  • 39. Plug-In Verfikiation Weiniger Tests Weniger x!=null && x.doSomething() Programmfluss bestimmen: SQL check Große Auswahl an Checker Flexibel (Eigene Typprüfung) Keine Laufzeit Änderung Typen können feiner Dokumentiert werden Besseren Java-Code mit Type Annotations 38 <> - - - - <> <> <> Vorteile
  • 40. Sinnvolles, Mächtiges und Komplexes Werkzeug Nicht vertieft: Typ-Polymorphie (Generics) Invariante Array Types Kontextsensitive Typ-Inferenz Type Refinement (flow-sensitive) Java Expressions:@EnsuresNonNullIf, @EnsuresQualifierIf, @GuaredBy ... Dokumentation Typen → Besseren Java Code Besseren Java-Code mit Type Annotations 39 <> <> - - - - - <> IMHO
  • 41. Tipps zum Einsteigen Nicht mit NullChecker beginnen Sub-Typing als Type-Annotations infer-and-annotate.sh -Awarns Sinnvoll ab Java 8 Online Demo Checker Framework Besseren Java-Code mit Type Annotations 40 <> 1 2 3 4 5 6 Tipps zum Einsteigen Expert
  • 42. Checker Framework http://checkerframework.org/ Online Demo Checker Framework http://eisop.uwaterloo.ca/live (Source) Data Flow Handbuch http://types.cs.washington.edu/checker-framework/current/checker-framework-dataflow-manual.pdf Pluggable Type Systems http://bracha.org/pluggable-types.pdf The Hitchhiker's Guide to javac http://openjdk.java.net/groups/compiler/doc/hhgtjavac/index.html JEP 217: Annotations Pipeline 2.0 http://openjdk.java.net/jeps/217 Besseren Java-Code mit Type Annotations 41 Referenzen
  • 43. Fragen und Antworten Danke... Besseren Java-Code mit Type Annotations
  • 44. Fragen und Antworten Vortrag & Quellen github.com/dzuvic/jsr308- examples → Dragan Zuvic @dzuvic www.w11k.de www.thecodecampus.de