JEP 104: Annotations on Java Types

AuthorsMichael Ernst, Alex Buckley
OwnerMichael Ernst
Created2011/09/09 20:00
Updated2014/07/10 20:56
TypeFeature
StatusCompleted
ScopeSE
JSR308
Discussiontype dash annotations dash dev at openjdk dot java dot net
EffortL
DurationXL
Priority4
Reviewed byBrian Goetz
Endorsed byBrian Goetz, Mikael Vidstedt
Release8
Issue8046094
BlocksJEP 120: Repeating Annotations

Summary

Extend the set of annotatable locations in the syntax of the Java programming language to include names which indicate the use of a type as well as (per Java SE 5.0) the declaration of a type.

Goals

Allow development of useful pluggable type checkers that refine Java's built-in type system.

Non-Goals

Standardization of pluggable type checkers.

Success Metrics

Motivation

Java's annotation system has been an unquestioned success. Programmers can write annotations on type names which appear in the declaration of variables, methods, and classes, which are then read by enterprise frameworks for configuration purposes and by compilers/IDEs for software quality purposes. Annotations allow boilerplate to be removed from code, and enable basic errors to be detected at compile-time.

Annotations on type uses, not just type declarations, enable error detection by "pluggable type checkers" which strengthen and refine Java's built-in type system. The strengthened type system prevents, at compile-time, the kind of software quality errors which would otherwise manifest themselves at run time. Examples include null pointer errors, side effects on immutable data, race conditions, information leakage, and non-internationalized strings.

Description

JSR 308 makes targeted, low-level changes to the grammar of the Java language to allow annotations on the names of types in most places those names can be used. This includes the names of types occurring in Java SE 7 language constructs like try-with-resources and multi-catch.

JSR 308 defines new attributes for the JVM class file format to store these annotations on type names. Finally, it makes targeted changes to the java.lang.reflect and javax.lang.model API so that annotations on specific instances of type names can be retrieved.

Alternatives

Testing

Risks and Assumptions

Impact