Type Annotations in Java 8

3,609 views

Published on

Java 8 introduces new type annotation syntax (JSR 308) permitting annotations to appear on any use of a type. Type annotations provide exciting new opportunities for tooling such as detecting additional classes of errors at compile-time. This presentation provides an overview of the new type annotation syntax, tools for leveraging type annotations, and type annotation design patterns.

These slides are from Todd Schiller's talk at the March 24th New York City Java Meetup.

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
No Downloads
Views
Total views
3,609
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
67
Comments
1
Likes
6
Embeds 0
No embeds

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

    1. 1. Java 8 Type Annotations: Tools and Opportunities Todd Schiller | FinLingua March 24, 2014 Copyright ©2014 FinLingua, Inc. 1
    2. 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. 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. 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. 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. 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. 7. Talk Outline 1. Type Annotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 7
    8. 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. 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. 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. 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. 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. 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. 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. 15. Talk Outline 1. Type Annotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 15
    16. 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. 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. 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. 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. 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. 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. 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. 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. 24. Type System Brainstorming 1. Runtime Behavior to Prevent 2. Legal Operations 3. Types of Data Copyright ©2014 FinLingua, Inc. 24
    25. 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. 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. 27. Talk Outline 1. Type Annotation Syntax 2. Error Checking 3. Metaprogramming Copyright ©2014 FinLingua, Inc. 27
    28. 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. 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. 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. 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. 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. 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

    ×