Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar
1 // never null // p never null // never null class C { Object field; C(Object p) { field = p; } Object get() { return field; } Comments are Discarded Parser Source File Program with comments Program Comments Abstract Syntax Tree (AST) C Object field C(Object p) = fieldp Object get() return field
2 class C Object field; Object p) { field = p; Object get() { return field; } Annotations are Retained Parser Source File Abstract Syntax Tree C Object field C(Object p) = fieldp Object get() return field Program with annotations Program with Annotations are part of the AST Can be processed automatically
3 Annotation Definition Structured Data Comparable to records in MyAnnotation { String value(); // member int i() default 123; // member w. default value MarkerAnnotation { // annotation without any members }
4 Annotation i=456) void method() { … } // default value for i: void method2() { … } // special case for members called void method3() { … } // parenthesis can be omitted if no void method4() { … }
5 Annotation MyAnnotation { int intMember(); // primitives String stringMember(); // strings Class classMember(); // class literals SomeEnum enumMember(); // enums // annotions OnlyThreadWithName annotMember(); // arrays of the above OnlyThreadWithName[] arrayMember(); }
6 Annotation Targets in Java package class MyClass Object Object param) { field = param; Object method() Object localVar = field; return localVar; }
7 New Annotation Targets in JSR308 Types Type parameters HashMap m; Inheritance, bounds class MyClass SuperClass { … } Object creation Integer(5); Class literals Class c String.class; Static MyClass.field Type casts String s = String) myObject;
8 New Annotation Targets in JSR308 All Type Occurrences HashMap m; Arrays a = new Method Receivers public String { … } As opposed to return type String toString() { … } Backward-Compatible to pre-JSR308 Annotations can be written as comments public String toString() { … }
9 class C Object field; Object p) { field = p; Object get() { return field; } Structure of Java Compiler Parser Source File Class File Writer Class File Error p Type Checker AST
10 class C Object field; Object p) { field = p; Object get() { return field; } Structure of JSR308 Compiler Parser Annotation Checker Plugins AST p Annotation Checker Class File Writer Class File Type Checker Error Source File
11 Annotation Checker Plugins Nullness Object foo = null; // error: null! Mutability checkers (Javari, int i = 0; i = 1; // error: mutation! Interning String a = "x".intern(); String b = "x"; if (a==b) … // error: identity vs. equality Defined using extensible framework
12 Nullness Checker Define {} Nullable { Nullable.class } NonNull { Date
13 Nullness Checker Override processing for certain AST nodes Flow analysis Recognizes assignment of constants Integer i = null; // i always null from here on Integer j = 1; // j never null from here on i = 2; // i never null from here on Recognizes simple conditionals if (i==null){ /* i must be null */ } else { /* i can't be null */ }
14 Suggested Extensions Annotations on Block { … } Annotations on Parenthetical ( … ) { int value() default 0; } +1; // (1)+1; or // +1; ???
15 Resolving Ambiguity Require parentheses for annotations { int value() default {} (1)+1; // +1; (1)+1; // (1)+1; (1)+1; // (1)+1;
16 class C Object field; Object p) { field = p; Object get() { return field; } Source Code Generation Parser Annotation Checker Class File Writer Class File Type Checker Error Source File Code Generator Annotation Checker Plugins Code Generator Plugins
17 class C Object field; Object p) { field = p; Object get() { return field; } AST Generation Parser Annotation Checker Plugins Annotation Checker Class File Writer Class File Type Checker Error Source File Code Generator Code Generator Plugins
18 Multi-Stage Java double double x, int n) { if (n==0) (1.0); else (x) (power(x, n-1)) ); } double square(double x) { (x), 2)); // generates x * x * 1.0; } Java splice run generated code.!x
19 Multi-Stage Java double double x, int n) { if (n==0) (1.0); else (x) (power(x, n-1)) ); } double square(double x) { (x), 2)); // generates x * x * 1.0; } let rec power(x, n) = match n with 0 ->.. | n ->..;; let square =.!..~(power (.., 2))>.;;
20 Summary Annotations can be processed automatically Checker plugins for enhanced systems Add annotations on Block { … } Parenthetical ( … ) Provide code generator stage Use for multi-stage programs