KEMBAR78
Type Annotations in Java 8 | PPTX
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

Type Annotations in Java 8

  • 1.
    Java 8 TypeAnnotations: Tools and Opportunities Todd Schiller | FinLingua March 24, 2014 Copyright ©2014 FinLingua, Inc. 1
  • 2.
    Java 7: annotationson 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 justsyntax, 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 protectedboolean 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") publicclass 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 ImprovesQuality 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. TypeAnnotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 7
  • 8.
    Type Annotations onAny 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 areStored 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’tAffect 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: voidsend(@Encrypted File file) { ... } void send( File file) { ... } ... } Copyright ©2014 FinLingua, Inc. 11 Type Annotations Don’t Affect Execution
  • 12.
    Receiver Annotations Receiver @Open MyFilefile = ...; ... = file.read(); class MyFile { Byte[] read() { ... } ... } Where is the type of this? Copyright ©2014 FinLingua, Inc. 12
  • 13.
    Receiver Annotations Receiver @Open MyFilefile = ...; ... = file.read(); class MyFile { Byte[] read(@Open MyFile this) { ... } ... } New in Java 8 Copyright ©2014 FinLingua, Inc. 13
  • 14.
    Type Annotations Don’tAffect Execution // This code will compile, run, (and crash)! @Closed MyFile file = ...; ... = file.read(); Copyright ©2014 FinLingua, Inc. 14
  • 15.
    Talk Outline 1. TypeAnnotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 15
  • 16.
    Run-time Type Checking UseAspect 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 Preventrun-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 QualifyTypes 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 Annotationsare 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 viaSubtyping @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 • Checkersreveal 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 TypeAnnotations 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. TypeAnnotation 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 @Autowiredprivate 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 Annotationson 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 ComputingModel 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: Annotationson 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 CheckerFramework: 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