SlideShare a Scribd company logo
Java 8 Type Annotations:
Tools and Opportunities
Todd Schiller | FinLingua
March 24, 2014
Copyright ©2014 FinLingua, Inc. 1
Java 7: annotations on declarations
@Override public boolean equals(Object obj)
@Entity class MyPojo implements Serializable
Java 8: annotations on any uses of types
@Encrypted String data
List<@NonNull String> strings
MyGraph = (@Immutable Graph) tmpGraph;
Copyright ©2014 FinLingua, Inc. 2
Annotations are just syntax,
tools give them their semantics (meaning)
Complementary Goals:
1. Error Checking: quality
2. Metaprogramming: productivity
Copyright ©2014 FinLingua, Inc. 3
Java 7: Overrides
@Override
protected boolean displaySensitiveInfo()
...
}
Problem: dynamic dispatch is tricky
Solution: have a tool (the compiler) check
inheritance automatically
Copyright ©2014 FinLingua, Inc. 4
Java 7: Persistence
@Entity
@Table(name="tbl_flight")
public class Flight implements Serializable {
@Id
public Long getId() { return id; }
...
}
Problem: DB mappings are redundant
Solution: have a tool (e.g., Hibernate) create
mappings automatically
Copyright ©2014 FinLingua, Inc. 5
Type Information Improves Quality
Mars Climate Orbiter
• Unit error in thruster
controller: lbf-s vs. N-s
• Crashed into Mars
• 3 years of work, > $125
million
Copyright ©2014 FinLingua, Inc. 6
Talk Outline
1. Type Annotation Syntax
2. Error Checking
3. Metaprogramming
Copyright ©2014 FinLingua, Inc. 7
Type Annotations on Any Uses of Types
(JSR 308)
@Encrypted String data
List<@NonNull String> strings
MyGraph = (@Immutable Graph) tmpGraph;
class UnmodifiableList<T>
implements @ReadOnly List<@ReadOnly T> {}
Copyright ©2014 FinLingua, Inc. 8
Type Annotations are Stored in the
Class File
• Can be accessed via reflection
• Local variable annotations are stored, too
• Backward-compatible with Java 7
Copyright ©2014 FinLingua, Inc. 9
Type Annotations Don’t Affect Execution
File file = ...;
@Encrypted File encryptedFile = ...;
// These lines call the same method
connection.Send(file);
connection.Send(encryptedFile);
Copyright ©2014 FinLingua, Inc. 10
class Connection{
// Impossible:
void send(@Encrypted File file) { ... }
void send( File file) { ... }
...
}
Copyright ©2014 FinLingua, Inc. 11
Type Annotations Don’t Affect Execution
Receiver Annotations
Receiver
@Open MyFile file = ...;
... = file.read();
class MyFile {
Byte[] read() { ... }
...
}
Where is the type
of this?
Copyright ©2014 FinLingua, Inc. 12
Receiver Annotations
Receiver
@Open MyFile file = ...;
... = file.read();
class MyFile {
Byte[] read(@Open MyFile this) { ... }
...
} New in Java 8
Copyright ©2014 FinLingua, Inc. 13
Type Annotations Don’t Affect
Execution
// This code will compile, run, (and crash)!
@Closed MyFile file = ...;
... = file.read();
Copyright ©2014 FinLingua, Inc. 14
Talk Outline
1. Type Annotation Syntax
2. Error Checking
3. Metaprogramming
Copyright ©2014 FinLingua, Inc. 15
Run-time Type Checking
Use Aspect Oriented Programming (AOP) to
insert run-time checks
Byte[] read(@Open MyFile this){
// Inserted by the AOP tool
if (!this.isOpen()){
throw new IllegalArgumentException(...);
}
...
}
Refined Claim: Type annotations don’t, on their own,
affect execution
Copyright ©2014 FinLingua, Inc. 16
Static Type Checking
Prevent run-time exceptions at compile-time:
List<String> xs = ...;
String x = xs.get(0); // Known to be valid
... = x.Trim(); // Method known to exist
int sum = xs.get(1) + 3; // Compiler error
... = x.Foo(); // Compiler error
NullPointerException!
Copyright ©2014 FinLingua, Inc. 17
Type Annotations Qualify Types
List<@NonNull String> xs = ...;
@NonNull String x = xs.get(0);
... = x.Trim(); // OK
Other ways to qualify the String type:
@Encrypted String
@Format({FLOAT, INT}) String
@Localized String
Copyright ©2014 FinLingua, Inc. 18
Type Checking Annotations
Annotations are just syntax,
tools give them their semantics (meaning)
Java 8 compiler does not check the annotations
Java provides a Pluggable Annotation Processing
API and Java Compiler API
Copyright ©2014 FinLingua, Inc. 19
Type Checking via Subtyping
@MaybeTainted
@Untainted
userInput = dbQuery; // Safe
dbQuery = "SELECT * FROM " + userInput; // Invalid!
@MaybeTainted String userInput;
@Untainted String dbQuery;
Copyright ©2014 FinLingua, Inc. 20
The Checker Framework: Pluggable
Type-Checking
• Many built-in checkers: null pointers, locking, security,
string syntax (regex, format strings),
internationalization, ...
• Quickly build your own checker
• Uses Java 8 type annotations (or comments)
– List<@Nullable String>
– List</*@Nullable*/ String>
• http://checkerframework.org
Copyright ©2014 FinLingua, Inc. 21
Copyright ©2014 FinLingua, Inc. 22
void nullSafe( MyObject nonNullByDefault,
@Nullable MyObject mightBeNull
){
// Smart defaults
nonNullByDefault.Foo(); // Safe
mightBeNull.Foo(); // Unsafe!
if (mightBeNull != null){
// Flow-sensitive
mightBeNull.Foo(); // Safe
}
}
Low Annotation Overhead
Our Experience
• Checkers reveal important latent bugs
–Ran on >3 million LOC of real-world code
–Found hundreds of user-visible bugs
• Mean 2.6 annotations per kLOC
• Quickly build your own checkers
Copyright ©2014 FinLingua, Inc. 23
Type System Brainstorming
1. Runtime Behavior to Prevent
2. Legal Operations
3. Types of Data
Copyright ©2014 FinLingua, Inc. 24
Type System Brainstorming
1. Runtime Behavior to Prevent
Don’t send unencrypted data over the network
2. Legal Operations
Only pass encrypted data to send(...)
3. Types of Data
Encrypted, Unencrypted
@MaybeEncrypted
@Encrypted
Copyright ©2014 FinLingua, Inc. 25
Error-Checking with Type Annotations
Support
Checker Framework Full support, including annotations in comments
Eclipse Null error analysis support
IntelliJ IDEA Can write custom inspectors, no null error
analysis support
No Support
PMD
Coverity
Find Bugs No Java 8 support
Check Style No Java 8 support
Copyright ©2014 FinLingua, Inc. 26
Talk Outline
1. Type Annotation Syntax
2. Error Checking
3. Metaprogramming
Copyright ©2014 FinLingua, Inc. 27
Metaprogramming
Aspect Oriented Programming
• AspectJ: @Aspect, @Pointcut
Dependency Injection
• Spring: @Autowired, @Required
• Guice: @Inject
Persistence
• Hibernate/JPA: @Entity
Copyright ©2014 FinLingua, Inc. 28
Fine-Grained Dependency Injection
@Autowired private Store<Product> s1;
@Autowired private Store<Service> s2;
Spring 4 considers generics a form of qualifier:
Type Annotations would allow further refinement:
@Autowired
private Store<@Prod(Type.Grocery) Product> s1;
Copyright ©2014 FinLingua, Inc. 29
Fine-Grained Aspect Oriented
Programming
Annotations on local variables:
Copyright ©2014 FinLingua, Inc. 30
// Trace all calls made to the ar object
@Trace AuthorizationRequest ar = ...
Refine Join-Points:
void showSecrets(@Authenticated User user);
EnerJ: Approximate Computing Model
Specify which data is non-critical:
@Approx int pixel
Run-time can approximate that data
(e.g., convergence criteria)
Checker ensures approximate data
doesn’t flow into precise
expressions
Copyright ©2014 FinLingua, Inc. 31
Java 8: Annotations on any Uses of
Types
Annotations are just syntax,
tools give them their semantics (meaning)
Complementary Goals:
1. Error Checking: quality
2. Metaprogramming: productivity
Copyright ©2014 FinLingua, Inc. 32
References
• The Checker Framework: http://checkerframework.org/
• MCO trajectory:
ftp://ftp.hq.nasa.gov/pub/pao/reports/1999/MCO_report.pdf
• Werner Dietl et al. Building and using pluggable type-
checkers. 2011.
• Marc Eaddy and Alfred Aho. Statement Annotations for Fine-
Grained Advising. 2006.
• Adrian Sampson et al. EnerJ: Approximate Data Types for Safe
and General Low-Power Computation. 2011.
Copyright ©2014 FinLingua, Inc. 33

More Related Content

What's hot (20)

PDF
Introduction to JAVA
Professional Guru
 
PPT
Java Basics
shivamgarg_nitj
 
PPT
Java basic
Arati Gadgil
 
PDF
Bt0074 oops with java
Techglyphs
 
PPT
Java platform
BG Java EE Course
 
ODP
Java Code Generation for Productivity
David Noble
 
DOCX
The Seven Pillars Of Asp.Net
Anand Kumar Rajana
 
PPTX
Java features
Prashant Gajendra
 
PPTX
Core java
Shivaraj R
 
DOCX
Java se 8 fundamentals
megharajk
 
PDF
Java Presentation For Syntax
PravinYalameli
 
PPT
Java tutorial PPT
Intelligo Technologies
 
PDF
Qb it1301
ArthyR3
 
PPT
Unit 5 Java
arnold 7490
 
ZIP
Introduction to the Java(TM) Advanced Imaging API
white paper
 
PPTX
Java annotations
Sujit Kumar
 
PDF
Java Interview Questions
Kuntal Bhowmick
 
PDF
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Raffi Khatchadourian
 
PPTX
Annotations
swapna reniguntla
 
PDF
Understanding And Using Reflection
Ganesh Samarthyam
 
Introduction to JAVA
Professional Guru
 
Java Basics
shivamgarg_nitj
 
Java basic
Arati Gadgil
 
Bt0074 oops with java
Techglyphs
 
Java platform
BG Java EE Course
 
Java Code Generation for Productivity
David Noble
 
The Seven Pillars Of Asp.Net
Anand Kumar Rajana
 
Java features
Prashant Gajendra
 
Core java
Shivaraj R
 
Java se 8 fundamentals
megharajk
 
Java Presentation For Syntax
PravinYalameli
 
Java tutorial PPT
Intelligo Technologies
 
Qb it1301
ArthyR3
 
Unit 5 Java
arnold 7490
 
Introduction to the Java(TM) Advanced Imaging API
white paper
 
Java annotations
Sujit Kumar
 
Java Interview Questions
Kuntal Bhowmick
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Raffi Khatchadourian
 
Annotations
swapna reniguntla
 
Understanding And Using Reflection
Ganesh Samarthyam
 

Viewers also liked (11)

PPTX
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Anna Shymchenko
 
PPTX
Trends and future of java
Csaba Toth
 
PPTX
Java World, Java Trends, Java 8 and Beyond (iForum - 2014)
Olena Syrota
 
PPTX
Java annotation
Natanael Fonseca
 
PDF
Hacking Java - Enhancing Java Code at Build or Runtime
Sean P. Floyd
 
PDF
A Java Implementer's Guide to Boosting Apache Spark Performance by Tim Ellison.
J On The Beach
 
PDF
Anatomy of Spark SQL Catalyst - Part 2
datamantra
 
PDF
Anatomy of spark catalyst
datamantra
 
PDF
Ingesting Drone Data into Big Data Platforms
Timothy Spann
 
PPTX
Annotations in Java
Kirill Kulakov
 
PDF
Conférence sur les annotations Java par Olivier Croisier (Zenika) au Paris JUG
Zenika
 
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Anna Shymchenko
 
Trends and future of java
Csaba Toth
 
Java World, Java Trends, Java 8 and Beyond (iForum - 2014)
Olena Syrota
 
Java annotation
Natanael Fonseca
 
Hacking Java - Enhancing Java Code at Build or Runtime
Sean P. Floyd
 
A Java Implementer's Guide to Boosting Apache Spark Performance by Tim Ellison.
J On The Beach
 
Anatomy of Spark SQL Catalyst - Part 2
datamantra
 
Anatomy of spark catalyst
datamantra
 
Ingesting Drone Data into Big Data Platforms
Timothy Spann
 
Annotations in Java
Kirill Kulakov
 
Conférence sur les annotations Java par Olivier Croisier (Zenika) au Paris JUG
Zenika
 
Ad

Similar to Type Annotations in Java 8 (20)

PDF
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
Vladimir Ivanov
 
KEY
2 the essentials of effective java
Honnix Liang
 
PDF
On Processors, Compilers and @Configurations
Netcetera
 
PPTX
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
Jorge Hidalgo
 
PPTX
Beginning Java for .NET developers
Andrei Rinea
 
PPTX
Java 2
Michael Shrove
 
ODP
Static Analysis in IDEA
HamletDRC
 
PDF
Annotation based null analysis in Eclipse JDT
Eclipse Day India
 
PPTX
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
PPT
core java
Vinodh Kumar
 
PPTX
What is new in Java 8
Sandeep Kr. Singh
 
PDF
Core Java Volume I Fundamentals 12th Horstmann Cay
luellarensdw
 
PDF
Refactoring annotations
Carlos Noguera
 
PDF
Google Dart
Eberhard Wolff
 
PDF
Google Dart
adesso AG
 
PDF
[FREE PDF sample] Object Oriented Programming and Java Second Edition Danny P...
ackeylocusr5
 
PDF
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
 
PPTX
Java 8 - New Updates and Why It Matters?
CTE Solutions Inc.
 
PPT
java training faridabad
Woxa Technologies
 
PPT
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
Vladimir Ivanov
 
2 the essentials of effective java
Honnix Liang
 
On Processors, Compilers and @Configurations
Netcetera
 
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
Jorge Hidalgo
 
Beginning Java for .NET developers
Andrei Rinea
 
Static Analysis in IDEA
HamletDRC
 
Annotation based null analysis in Eclipse JDT
Eclipse Day India
 
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
core java
Vinodh Kumar
 
What is new in Java 8
Sandeep Kr. Singh
 
Core Java Volume I Fundamentals 12th Horstmann Cay
luellarensdw
 
Refactoring annotations
Carlos Noguera
 
Google Dart
Eberhard Wolff
 
Google Dart
adesso AG
 
[FREE PDF sample] Object Oriented Programming and Java Second Edition Danny P...
ackeylocusr5
 
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
 
Java 8 - New Updates and Why It Matters?
CTE Solutions Inc.
 
java training faridabad
Woxa Technologies
 
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
Ad

Recently uploaded (20)

PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Français Patch Tuesday - Juillet
Ivanti
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
July Patch Tuesday
Ivanti
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 

Type Annotations in Java 8

  • 1. Java 8 Type Annotations: Tools and Opportunities Todd Schiller | FinLingua March 24, 2014 Copyright ©2014 FinLingua, Inc. 1
  • 2. Java 7: annotations on declarations @Override public boolean equals(Object obj) @Entity class MyPojo implements Serializable Java 8: annotations on any uses of types @Encrypted String data List<@NonNull String> strings MyGraph = (@Immutable Graph) tmpGraph; Copyright ©2014 FinLingua, Inc. 2
  • 3. Annotations are just syntax, tools give them their semantics (meaning) Complementary Goals: 1. Error Checking: quality 2. Metaprogramming: productivity Copyright ©2014 FinLingua, Inc. 3
  • 4. Java 7: Overrides @Override protected boolean displaySensitiveInfo() ... } Problem: dynamic dispatch is tricky Solution: have a tool (the compiler) check inheritance automatically Copyright ©2014 FinLingua, Inc. 4
  • 5. Java 7: Persistence @Entity @Table(name="tbl_flight") public class Flight implements Serializable { @Id public Long getId() { return id; } ... } Problem: DB mappings are redundant Solution: have a tool (e.g., Hibernate) create mappings automatically Copyright ©2014 FinLingua, Inc. 5
  • 6. Type Information Improves Quality Mars Climate Orbiter • Unit error in thruster controller: lbf-s vs. N-s • Crashed into Mars • 3 years of work, > $125 million Copyright ©2014 FinLingua, Inc. 6
  • 7. Talk Outline 1. Type Annotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 7
  • 8. Type Annotations on Any Uses of Types (JSR 308) @Encrypted String data List<@NonNull String> strings MyGraph = (@Immutable Graph) tmpGraph; class UnmodifiableList<T> implements @ReadOnly List<@ReadOnly T> {} Copyright ©2014 FinLingua, Inc. 8
  • 9. Type Annotations are Stored in the Class File • Can be accessed via reflection • Local variable annotations are stored, too • Backward-compatible with Java 7 Copyright ©2014 FinLingua, Inc. 9
  • 10. Type Annotations Don’t Affect Execution File file = ...; @Encrypted File encryptedFile = ...; // These lines call the same method connection.Send(file); connection.Send(encryptedFile); Copyright ©2014 FinLingua, Inc. 10
  • 11. class Connection{ // Impossible: void send(@Encrypted File file) { ... } void send( File file) { ... } ... } Copyright ©2014 FinLingua, Inc. 11 Type Annotations Don’t Affect Execution
  • 12. Receiver Annotations Receiver @Open MyFile file = ...; ... = file.read(); class MyFile { Byte[] read() { ... } ... } Where is the type of this? Copyright ©2014 FinLingua, Inc. 12
  • 13. Receiver Annotations Receiver @Open MyFile file = ...; ... = file.read(); class MyFile { Byte[] read(@Open MyFile this) { ... } ... } New in Java 8 Copyright ©2014 FinLingua, Inc. 13
  • 14. Type Annotations Don’t Affect Execution // This code will compile, run, (and crash)! @Closed MyFile file = ...; ... = file.read(); Copyright ©2014 FinLingua, Inc. 14
  • 15. Talk Outline 1. Type Annotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 15
  • 16. Run-time Type Checking Use Aspect Oriented Programming (AOP) to insert run-time checks Byte[] read(@Open MyFile this){ // Inserted by the AOP tool if (!this.isOpen()){ throw new IllegalArgumentException(...); } ... } Refined Claim: Type annotations don’t, on their own, affect execution Copyright ©2014 FinLingua, Inc. 16
  • 17. Static Type Checking Prevent run-time exceptions at compile-time: List<String> xs = ...; String x = xs.get(0); // Known to be valid ... = x.Trim(); // Method known to exist int sum = xs.get(1) + 3; // Compiler error ... = x.Foo(); // Compiler error NullPointerException! Copyright ©2014 FinLingua, Inc. 17
  • 18. Type Annotations Qualify Types List<@NonNull String> xs = ...; @NonNull String x = xs.get(0); ... = x.Trim(); // OK Other ways to qualify the String type: @Encrypted String @Format({FLOAT, INT}) String @Localized String Copyright ©2014 FinLingua, Inc. 18
  • 19. Type Checking Annotations Annotations are just syntax, tools give them their semantics (meaning) Java 8 compiler does not check the annotations Java provides a Pluggable Annotation Processing API and Java Compiler API Copyright ©2014 FinLingua, Inc. 19
  • 20. Type Checking via Subtyping @MaybeTainted @Untainted userInput = dbQuery; // Safe dbQuery = "SELECT * FROM " + userInput; // Invalid! @MaybeTainted String userInput; @Untainted String dbQuery; Copyright ©2014 FinLingua, Inc. 20
  • 21. The Checker Framework: Pluggable Type-Checking • Many built-in checkers: null pointers, locking, security, string syntax (regex, format strings), internationalization, ... • Quickly build your own checker • Uses Java 8 type annotations (or comments) – List<@Nullable String> – List</*@Nullable*/ String> • http://checkerframework.org Copyright ©2014 FinLingua, Inc. 21
  • 22. Copyright ©2014 FinLingua, Inc. 22 void nullSafe( MyObject nonNullByDefault, @Nullable MyObject mightBeNull ){ // Smart defaults nonNullByDefault.Foo(); // Safe mightBeNull.Foo(); // Unsafe! if (mightBeNull != null){ // Flow-sensitive mightBeNull.Foo(); // Safe } } Low Annotation Overhead
  • 23. Our Experience • Checkers reveal important latent bugs –Ran on >3 million LOC of real-world code –Found hundreds of user-visible bugs • Mean 2.6 annotations per kLOC • Quickly build your own checkers Copyright ©2014 FinLingua, Inc. 23
  • 24. Type System Brainstorming 1. Runtime Behavior to Prevent 2. Legal Operations 3. Types of Data Copyright ©2014 FinLingua, Inc. 24
  • 25. Type System Brainstorming 1. Runtime Behavior to Prevent Don’t send unencrypted data over the network 2. Legal Operations Only pass encrypted data to send(...) 3. Types of Data Encrypted, Unencrypted @MaybeEncrypted @Encrypted Copyright ©2014 FinLingua, Inc. 25
  • 26. Error-Checking with Type Annotations Support Checker Framework Full support, including annotations in comments Eclipse Null error analysis support IntelliJ IDEA Can write custom inspectors, no null error analysis support No Support PMD Coverity Find Bugs No Java 8 support Check Style No Java 8 support Copyright ©2014 FinLingua, Inc. 26
  • 27. Talk Outline 1. Type Annotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 27
  • 28. Metaprogramming Aspect Oriented Programming • AspectJ: @Aspect, @Pointcut Dependency Injection • Spring: @Autowired, @Required • Guice: @Inject Persistence • Hibernate/JPA: @Entity Copyright ©2014 FinLingua, Inc. 28
  • 29. Fine-Grained Dependency Injection @Autowired private Store<Product> s1; @Autowired private Store<Service> s2; Spring 4 considers generics a form of qualifier: Type Annotations would allow further refinement: @Autowired private Store<@Prod(Type.Grocery) Product> s1; Copyright ©2014 FinLingua, Inc. 29
  • 30. Fine-Grained Aspect Oriented Programming Annotations on local variables: Copyright ©2014 FinLingua, Inc. 30 // Trace all calls made to the ar object @Trace AuthorizationRequest ar = ... Refine Join-Points: void showSecrets(@Authenticated User user);
  • 31. EnerJ: Approximate Computing Model Specify which data is non-critical: @Approx int pixel Run-time can approximate that data (e.g., convergence criteria) Checker ensures approximate data doesn’t flow into precise expressions Copyright ©2014 FinLingua, Inc. 31
  • 32. Java 8: Annotations on any Uses of Types Annotations are just syntax, tools give them their semantics (meaning) Complementary Goals: 1. Error Checking: quality 2. Metaprogramming: productivity Copyright ©2014 FinLingua, Inc. 32
  • 33. References • The Checker Framework: http://checkerframework.org/ • MCO trajectory: ftp://ftp.hq.nasa.gov/pub/pao/reports/1999/MCO_report.pdf • Werner Dietl et al. Building and using pluggable type- checkers. 2011. • Marc Eaddy and Alfred Aho. Statement Annotations for Fine- Grained Advising. 2006. • Adrian Sampson et al. EnerJ: Approximate Data Types for Safe and General Low-Power Computation. 2011. Copyright ©2014 FinLingua, Inc. 33

Editor's Notes

  • #14: The receiver syntax is optional. It does not affect semantics as is useful only for writing type annotations.
  • #19: Type annotations can be independent from types. For example, @NonNull and @Nullable can be applied to any Object.
  • #21: The intuition is that the supertype is a superset of the values represented by the subtype.
  • #22: Comment are for backwards compatibility. You can use the Checker Framework even if you have not adopted Java 8.http://checkerframework.org