java annotations for types and expressions
Download
Skip this Video
Download Presentation
Java Annotations for Types and Expressions

Loading in 2 Seconds...

play fullscreen
1 / 18

Java Annotations for Types and Expressions - PowerPoint PPT Presentation


  • 182 Views
  • Uploaded on

Java Annotations for Types and Expressions. Mathias Ricken October 24, 2008 COMP 617 Seminar. Abstract Syntax Tree (AST). C. Object field. Object get(). C(Object p). =. return. field. p. field. Comments are Discarded. Parser. Source File. class C { Object field;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java Annotations for Types and Expressions' - aya


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
java annotations for types and expressions

Java Annotations for Types and Expressions

Mathias Ricken

October 24, 2008

COMP 617 Seminar

comments are discarded

Abstract Syntax

Tree (AST)

C

Object

field

Object

get()

C(Object p)

=

return

field

p

field

Comments are Discarded

Parser

Source File

class C {

Object field;

C(Object p) {

field = p;

}

Object get() {

return field;

}

}

// never null

// p never null

// never null

Program

Program

with

comments

Comments

annotations are retained

Abstract Syntax

Tree

C

Object

field

Object

get()

C(Object p)

=

return

field

p

field

Annotations are Retained
  • Annotations are part of the AST
    • Can be processed automatically

Parser

Source File

class C {

@NonNull

Object field;

C(@NonNull

Object p) {

field = p;

}

@NonNull

Object get() {

return field;

}

}

Program

with

annotations

@NonNull

Program

with

annotations

@NonNull

@NonNull

annotation definition
Annotation Definition
  • Structured Data
    • Comparable to records in OCaml

@interfaceMyAnnotation {

String value(); // member

int i() default 123; // member w. default value

}

@interfaceMarkerAnnotation {

// annotation without any members

}

annotation usage
Annotation Usage

@MyAnnotation(value="text", i=456)

void method() { … }

// default value for i: 123

@MyAnnotation(value="text")

void method2() { … }

// special case for members called "value"

@MyAnnotation("text")

void method3() { … }

// parenthesis can be omitted if no members

@MarkerAnnotation

void method4() { … }

annotation targets in java 5
Annotation Targets in Java 5

@Apackage some.package.name;

@BclassMyClass {

@CObject field;

@DMyClass(@EObject param) {

field = param;

}

@FObject method() {

@GObject localVar = field;

return localVar;

}

}

new annotation targets in jsr308
New Annotation Targets in JSR308
  • All Type Occurrences

HashMap<@AString, Object> m;

  • Arrays

String @B[][] a = new String @B[10][2];

  • Method Receivers

publicString toString() @C { … }

    • As opposed to return type

[email protected] String toString() { … }

  • Backward-Compatible to pre-JSR308
    • Annotations can be written as comments

publicString toString() /*@C*/ { … }

structure of java compiler

Parser

Type

Checker

Class File

Writer

Error

p

Structure of Java Compiler

Source File

Class

File

class C {

@NonNull

Object field;

C(@NonNull

Object p) {

field = p;

}

@NonNull

Object get() {

return field;

}

}

AST

structure of jsr308 compiler

Parser

Annotation

Checker

Type

Checker

Class File

Writer

p

Structure of JSR308 Compiler

Source File

Class

File

class C {

@NonNull

Object field;

C(@NonNull

Object p) {

field = p;

}

@NonNull

Object get() {

return field;

}

}

AST

Error

Error

Annotation

Checker

Plugins

annotation checker plugins
Annotation Checker Plugins
  • Nullness checker

@NonNull Object foo = null; // error: null!

  • Mutability checkers (Javari, IGJ)

@ReadOnly int i = 0; i = 1; // error: mutation!

  • Interning checker

@Interned String a = "x".intern();

String b = "x";

if (a==b) … // error: identity vs. equality

  • Defined using extensible framework
nullness checker

@Nullable Object

@NonNull Object

@Nullable Date

@NonNull Date

Nullness Checker
  • Define type hierarchy

@TypeQualifier

@SubtypeOf( {} )

@interface Nullable { }

@TypeQualifier

@SubtypeOf( { Nullable.class } )

@interface NonNull { }

nullness checker1
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 */ }

suggested extensions
Suggested Extensions
  • Annotations on Block Statements

@A { … }

  • Annotations on Parenthetical Expressions

@B ( … )

  • Problem: ambiguous

@interface @Sample { int value() default 0; }

return @Sample(1) +1;

// return @Sample(value=0) (1)+1; or

// return @Sample(value=1) +1; ???

resolving ambiguity
Resolving Ambiguity
  • Require parentheses for annotations with members

@interface @Sample { int value() default 0; }

@interface @MarkerAnnotation {}

return @Sample (1)+1;

// return @Sample(value=1) +1;

return @Sample() (1)+1;

// return @Sample(value=0) (1)+1;

return @MarkerAnnotation (1)+1;

// return @MarkerAnnotation (1)+1;

source code generation

Parser

Annotation

Checker

Code

Generator

Type

Checker

Class File

Writer

Source Code Generation

Source File

Class

File

class C {

@NonNull

Object field;

C(@NonNull

Object p) {

field = p;

}

@NonNull

Object get() {

return field;

}

}

Error

Error

Annotation

Checker

Plugins

Code

Generator

Plugins

multi stage java programs
Multi-Stage Java Programs

@Codedouble power(@Codedouble x, int n) {

if (n==0) return @Code (1.0);

else return @Code( @Escape(x) *

@Escape (power(x, n-1)) );

}

double square(doublex) {

return @Run(power(@Code (x), 2));

// generates x * x * 1.0;

}

Java MetaOCaml

@Code generate .<x>.

@Escape splice together .~x

@Run run generated code .!x

multi stage java programs1
Multi-Stage Java Programs

@Codedouble power(@Codedouble x, int n) {

if (n==0) return @Code (1.0);

else return @Code( @Escape(x) *

@Escape (power(x, n-1)) );

}

double square(doublex) {

return @Run(power(@Code (x), 2));

// generates x * x * 1.0;

}

let rec power(x, n) =

match n with

0 -> .<1>.

| n -> .<.~x * .~(power (n-1, x))>.;;

let square = .!.<fun x -> .~(power (.<x>., 2))>.;;

summary
Summary
  • Annotations can be processed automatically
    • Checker plugins for enhanced systems
  • Add annotations on
    • Block statements @A { … }
    • Parenthetical expressions @A ( … )
  • Provide code generator stage
    • Use for multi-stage programs
ad