goto java; (Jfokus)

MartinSkarsaune 877 views 100 slides Feb 08, 2017
Slide 1
Slide 1 of 139
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139

About This Presentation

Please download original Powerpoint for animations to work :-)


Slide Content

Martin Skarsaune Java Developer and Co- Owner A peek into the OpenJDK compiler : goto java ; 高馬丁 We’re Hiring!

GOTO Statements in Java!

GOTO Statement – Objective Syntax goto identifier; Runtime Program control moves to target statement Other Semantics Target (statement label) must be defined in same scope, compilation error if not Potential circular gotos should give compilation warning.

GOTO Statement – Means OpenJDK Open source Java implementation Javac is implemented in plain Java  Modify compiler to support GOTO

public class GotoSuccess { public static void main(String[] args ) { one: System.out.print ( " goto " ); two: System.out.print ( ” J" ); goto four; three: System.out.print ( ” 2017" ); goto five; four: System.out.print ( ” Fokus " ); goto three; five: System.out.print ( "!" ); } } Success Case

GOTO AHEAD Dijkstra 1968 :

What i s a C ompiler? Compiler

What is a Compiler? front end back end

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute(…))))

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute(…))))

Syntax : goto identifier; First c onvert character stream to token stream ( Scanner.java ) [ g ][ o ][ t ][ o ][ ][f][o][u][r][;]

goto is already a reserved word in Java! Lucky for us , goto is therefore defined as a TokenKind . Tokens.java:141: GOTO (“ goto ”) The scanner therefore works out of the box !

... import com.sun.tools.javac.parser.Scanner ; ... final Scanner scanner= factory. newScanner ( ” goto identifier ; ” , false );

final Scanner scanner= factory. newScanner ( ” goto identifier ; ” , false ); scanner .nextToken ();

final Scanner scanner= factory. newScanner ( ” goto identifier ; ” , false ); scanner .nextToken (); assertThat ( ”First token is GOTO”, scanner .token (). kind , is( GOTO ));

final Scanner scanner= factory. newScanner ( ” goto identifier ; ” , false ); scanner .nextToken (); assertThat ( ”First token is GOTO”, scanner .token (). kind , is( GOTO )); scanner .nextToken ();

final Scanner scanner= factory .newScanner ( ” goto identifier ; ” , false); scanner .nextToken (); assertThat ( ”First token is GOTO”, scanner .token (). kind , is( GOTO )); scanner .nextToken (); assertThat ( "Second token is IDENTIFIER" , scanner .token (). kind , is( IDENTIFIER ));

final Scanner scanner= factory .newScanner ( ” goto identifier ; ” , false); scanner . nextToken (); assertThat ( ”First token is GOTO”, scanner .token (). kind , is( GOTO )); scanner .nextToken (); assertThat ( "Second token is IDENTIFIER" , scanner .token (). kind , is( IDENTIFIER )); scanner .nextToken ();

final Scanner scanner= factory .newScanner ( ” goto identifier ; ” , false); scanner .nextToken (); assertThat ( ”First token is GOTO”, scanner .token (). kind , is( GOTO )); scanner .nextToken (); assertThat ( "Second token is IDENTIFIER" , scanner .token (). kind , is( IDENTIFIER )); scanner .nextToken (); assertThat ( "Third token is SEMI" , scanner .token (). kind , is( SEMI ));

visitClassDef (..) visitMethodDef (..) visitIf (..) Abstract Syntax Tree [g][o][t][o] [ ][f][o][u][r][;] Wikipedia: “ the visitor design pattern is a way of separating an algorithm from an object structure on which it operates ”

Class Interface

Interface based visitors

Class based visitors public void visitGoto ( JCGoto tree ) { try { print( "goto " + tree . label + ";" ); } catch ( IOException e ) { throw new UncheckedIOException ( e ); } } public void visitGoto ( JCGoto tree ) { //TODO  implement }

public static class JCLabeledStatement extends JCStatement implements LabeledStatementTree { … public GotoResolver handler ; … } public class GotoResolver { Map< GotoTree , Name> gotos ; Map<Name, LabeledStatementTree > targets; List< StatementTree > statementsInSequence ; ... } Helper object

JavacParser.parseStatement () case GOTO : { nextToken (); Name label = ident(); JCGoto t = to ( F .at ( pos ). Goto( label , getGotoResolver ()); accept ( SEMI ); return t ; } TreeMaker.java: public JCGoto Goto(Name label , GotoResolver resolver ) { JCGoto tree = new JCGoto( label , resolver ); tree . pos = pos ; return tree ; }

Demo

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute(…)))) ✔

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute(…))))

Basic sanity testing of compilation unit: File name and folder location Duplicate class names Corrections Add default constructor if no constructors are declared

Default Constructor public class SimpleClass { }

Default Constructor public class SimpleClass { public SimpleClass() { super (); } }

✔ processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute(…))))

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute(…))))

Process Annitations Annotation processing API Part of ordinary javac process since Java 1.6 Plugin API (see javac documentation)

Output controlled by command line switches @ - proc:only – only process annotations, do not compile - proc:none – do not process annotations

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow(attribute( …)))) ✔

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow( attribute ( …))))

Attribution Semantic checks Types References Corrections Add required calls to super constructor

Ensure target label exists in current scope public class GotoMissingLabel { public static void main(String[] args ) { one: System.out.print ( " goto " ); two: System.out.print ( ”Java" ); goto six ; three: System.out.print ( ”2016" ); goto five; four: System.out.print ( ”One " ); goto three; five: System.out.print ( "!" ); } }

Attr.java : @Override public void visitGoto ( JCGoto that ) { that .findTarget (); if ( that . target == null ) log .error ( that .pos (), " undef.label " , that . label ); result = null ; } class JCGoto : … public void findTarget () { this . target = ( JCLabeledStatement ) this . handler .findTarget ( this ); }

Demo

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow( attribute ( …)))) ✔

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar ( flow ( attribute ( …))))

Flow analysis Detect unreachable code Verify assignments Ensure proper method return Verify (effectively) final Check exception flow

Detect circular gotos public class GotoCircularWarning { public static void main(String[] args ) { one: System.out.print ( " goto " ); two: System.out.print ( ”Java" ); goto four; three: System.out.print ( ”2016" ); goto five; four: System.out.print ( ”One " ); goto three; five: System.out.println ( "!" ); g oto one ; //forms infinite loop } }

Flow.FlowAnalyzer class: compiler.properties : @Override public void visitGoto ( JCGoto tree ) { if ( tree . handler . detectCircularGotoPosition ( tree )) log .warning ( tree . pos , " circular.goto " ); } ... compiler.warn.circular.goto =circular goto ...

Demo

✔ processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar ( flow ( attribute ( …))))

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow( attribute ( …))))

Erase generic types public class Bridge implements Comparator { } public interface Comparator< T > { int compare( T o1 , T o1 ); } or<T> {

Erase generic types public class Bridge implements Comparator< Integer > { public int compare( Integer first , Integer second ) { return first - second ; } } public interface Comparator< T > { int compare( T o1 , T o1 ); } or<T > {

Erasure - Runtime public class Bridge implements Comparator { public int compare( Integer first , Integer second ) { return first - second ; } }

Erasure - Bridge public class Bridge implements Comparator { public int compare( Integer first , Integer second ) { return first - second ; } /*synthetic*/ public int compare(Object first , Object second ) { return this .compare ((Integer) first , (Integer) second ); } }

Extract inner class public class Outer { private void foo() { } public Runnable fooRunner() { return new Runnable() { public void run() { foo(); } }; } }

Extract inner class public class Outer { private void foo() { } public Runnable fooRunner() { return new Runnable() { public void run() { foo(); } }; } }

Extract inner class public class Outer { private void foo() { } public Runnable fooRunner () { return new Outer$1( this ); } } class Outer$1 implements Runnable { final Outer this$0 ; Outer$1( final Outer this$0 ) { this . this$0 = this$0 ; super (); } public void run() { this$0 .foo(); } }

Extract inner class public class Outer { private void foo() { } public Runnable fooRunner () { return new Outer$1( this ); } } class Outer$1 implements Runnable { final Outer this$0 ; Outer$1( final Outer this$0 ) { this . this$0 = this$0 ; super (); } public void run() { this$0 . foo() ; } }

Extract inner class public class Outer { private void foo() { } public Runnable fooRunner () { return new Outer$1( this ); } /*synthetic*/ static void access$000( Outer x0 ) { x0 .foo(); } } class Outer$1 implements Runnable { final Outer this$0 ; Outer$1( final Outer this$0 ) { this . this$0 = this$0 ; super (); } public void run() { Outer.access$000( this$0) ; } }

Boxing List<Integer> list = Arrays. asList (1 , 2); for (Integer i : list ) { System . out .println ( ”Double: “ + i * 2 ); }

Boxing List<Integer> list = Arrays. asList (1 , 2); for (Integer i : list ) { System . out .println ( ”Double: “ + i * 2); } public static < T > List< T > asList ( T ... a ) { return new ArrayList <>( a ); }

Boxing List<Integer> list = Arrays. asList ( Integer.valueOf (1), Integer.valueOf (2) ); for (Integer i : list ) { System . out .println ( ”Double: “ + i * 2); }

Unboxing List<Integer> list = Arrays. asList ( Integer.valueOf (1), Integer.valueOf (2)); for (Integer i : list ) { System . out .println ( ”Double: “ + i * 2); }

Unboxing List<Integer> list = Arrays. asList ( Integer.valueOf (1), Integer.valueOf (2)); for (Integer i : list ) { System . out .println ( ”Double: “ + i.intValue () * 2); }

Varargs List<Integer> list = Arrays. asList ( Integer.valueOf (1), Integer.valueOf (2)); for (Integer i : list ) { System . out .println ( ”Double: “ + i .intValue () * 2); } public static < T > List< T > asList ( T ... a ) { return new ArrayList <>( a ); }

Varargs List<Integer> list = Arrays. asList ( Integer.valueOf (1), Integer.valueOf (2) ); for (Integer i : list ) { System . out .println ( ”Double: “ + i .intValue () * 2); } public static < T > List< T > asList ( T... a ) { return new ArrayList <>( a ); }

Varargs - runtime List<Integer> list = Arrays. asList ( new Integer[]{ Integer.valueOf (1 ), Integer.valueOf (2 ) } ); for (Integer i : list ) { System . out .println ( ”Double: “ + i .intValue () * 2); }

For each loop List<Integer> list = Arrays.asList ( new Integer[]{ Integer.valueOf (1), Integer.valueOf (2) } ); for (Integer i : list) { System . out .println ( ”Double: “ + i .intValue () * 2); }

For each loop List<Integer> list = Arrays.asList ( new Integer[]{ Integer.valueOf (1), Integer.valueOf (2) } ); for (Integer i : list ) { System . out .println ( ”Double: “ + i .intValue () * 2); } public interface Iterable <T> { Iterator<T> iterator (); }

For each loop List<Integer> list = Arrays.asList ( new Integer[]{ Integer.valueOf (1), Integer.valueOf (2) } ); for (;; ) { System . out .println ( ”Double: “ + i .intValue () * 2); }

For each loop List<Integer> list = Arrays.asList ( new Integer[]{ Integer.valueOf (1), Integer.valueOf (2) } ); for ( Iterator i $ = list.iterator ();;) { Integer i System . out .println ( ”Double: “ + i .intValue () * 2); }

For each loop List<Integer> list = Arrays.asList ( new Integer[]{ Integer.valueOf (1), Integer.valueOf (2) } ); for ( Iterator i $ = list.iterator (); i $ . hasNext ();) { Integer i System . out .println ( ”Double: “ + i .intValue () * 2); }

For each loop List<Integer> list = Arrays.asList ( new Integer[]{ Integer.valueOf (1), Integer.valueOf (2) } ); for ( Iterator i $ = list.iterator (); i $ . hasNext ();) { Integer i = (Integer) i $ .next(); System . out .println ( ”Double: “ + i .intValue () * 2); }

Enums public enum Status { YES , NO , MAYBE }

Enums - constructor public enum Status { YES , NO , MAYBE private Status(String $ enum$name , int $ enum$ordinal ) { super ($ enum$name , $ enum$ordinal ); } } public static final Status TRUE = new Status( "TRUE" , 0); public static final Status FALSE = new Status( "FALSE" , 1); public static final Status MAYBE = new Status( "MAYBE" , 2);

Enums - valueOf public enum Status { YES , NO , MAYBE private Status(String $enum$name, int $enum$ordinal) { super ($enum$name, $enum$ordinal); } public static Status valueOf(String name) { return (Status)Enum.valueOf(Status. class , name); } }

Enums - values public enum Status { YES , NO , MAYBE private Status(String $ enum$name , int $ enum$ordinal ) { super ($ enum$name , $ enum$ordinal ); } public static Status valueOf (String name) { return (Status) Enum.valueOf ( Status. class , name); } private static final Status[] $VALUES = new Status[]{ Status.YES , Status.NO , Status.MAYBE }; public static Status[] values() { return (Status[])$ VALUES.clone (); } }

Enum switch statement public class SwitchStatus { void switchStatus (Status status ) { switch ( status ) { case MAYBE : return ; default : break ; } } }

Enum switch statement public class SwitchStatus { void switchStatus (Status status ) { switch ( status ) { case MAYBE : return ; default : break ; } } }

Enum switch statement public class SwitchStatus { void switchStatus (Status status ) { switch ( SwitchStatus$1.$SwitchMap$Status[ (status).ordinal() ] ) { case 1 : return ; default : break ; } } } class SwitchStatus$1 { }

Enum switch statement public class SwitchStatus { void switchStatus (Status status ) { switch ( SwitchStatus$1.$SwitchMap$Status[ (status).ordinal() ] ) { case 1 : return ; default : break ; } } } class SwitchStatus$1 { static final int [] $ SwitchMap$Status = new int [ Status.values (). length ]; }

Enum switch statement public class SwitchStatus { void switchStatus (Status status ) { switch ( SwitchStatus$1.$SwitchMap$Status[ (status).ordinal() ] ) { case 1 : return ; default : break ; } } } class SwitchStatus$1 { static final int [] $ SwitchMap$Status = new int [ Status.values (). length ]; [ 0][0][0] }

Enum switch statement public class SwitchStatus { void switchStatus (Status status ) { switch ( SwitchStatus$1.$SwitchMap$Status[ (status).ordinal() ] ) { case 1 : return ; default : break ; } } } class SwitchStatus$1 { static final int [] $ SwitchMap$Status = new int [ Status.values (). length ]; [ 0][0] [1] static { try { SwitchStatus$1. $SwitchMap$Status [ Status.MAYBE.ordinal () ] = 1 ; } catch ( NoSuchFieldError ex ) { } } }

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate( desugar (flow( attribute ( …)))) ✔

processAnnotations ( enterTrees (… parseFiles (…))),…) … generate ( desugar (flow( attribute ( …))))

Organize initializers String concatenation Generate bytecodes

Organize < init > (Constructor ) public class InstanceInitialization { String key = "key" ; String value ; public InstanceInitialization (String value ) { this . value = value ; } }

Organize < init > (Constructor ) public class InstanceInitialization { String key = "key" ; String value ; public InstanceInitialization (String value ) { super(); this . value = value ; } }

Organize < init > (Constructor ) public class InstanceInitialization { String key ; String value ; public InstanceInitialization (String value ) { super (); key = ”key”; this . value = value ; } }

Organize < init > (Constructor ) public class InstanceInitialization { String key ; String value ; public void < init > () { super (); key = ”key”; this . value = value ; } }

Organize < clinit > (static initialization) public class StaticInitialization { static String key = "key" ; static { init (); } }

Organize < clinit > (static initialization) public class StaticInitialization { static String key; static { key = "key" ; init (); } }

Organize < clinit > (static initialization) public class StaticInitialization { static String key; static void < clinit >() { key = "key" ; init (); } }

String concatenation Source code “Generated code” ” string ” + value

String concatenation Source code “Generated code” ” string ” + value new StringBuilder ()

String concatenation Source code “Generated code” ” string ” + value new StringBuilder () .append( ”string” )

String concatenation Source code “Generated code” ” string ” + value new StringBuilder () .append( ”string” ) .append( value )

String concatenation Source code “Generated code” ” string ” + value new StringBuilder () .append( ”string” ) .append( value ) . toString ()

Goto generation Luckily for us there is a GOTO byte code goto < addr >

if (< test> ) { < ifblock > } else { < elseblock > } < codeafter > Source code Byte code C I 9 ifeq < elseblock > goto < stackmap > 22 < ifblock > 22 < stackmap > 29 < codeafter > 29 Java >= 1.6: Stack map frames must be embedded at target of jump instruction Code generator ( Code.java ) marked as not alive. Goto instruction added to list of pending jumps ( Chain.java ) Pending jumps processed Normal GOTO usage

Source code Byte code C I … label: < somecode > … g oto label ; … 2 … goto 20 < stackmap > < somecode > Used by goto ? Must emit stackmap Emit goto to label and turn code generation on again GOTO scenario 1 : jump back

Source code Byte code C I GOTO scenario 2 : jump forward … g oto label ; … label: < somecode > … … 29 < somecode > goto < stackmap > Label position not yet known? emit goto add to list of pending gotos t urn generation on again 29 Label used? emit stack frame patch pending gotos

Goto generation Gen.java , visitor for code generation Modify for LabelledStatement Add implementation for Goto

Gen.java – Labelled Statement public void visitLabelled ( JCLabeledStatement tree ) { // if the label is used from gotos , have to emit stack map if ( tree . handler .isUsed ( tree )) code .emitStackMap (); … }

Gen.java – Goto public void visitGoto ( JCGoto tree ) { tree . handler .addBranch ( new Chain( code .emitJump ( goto _ ), null , code . state .dup ()), tree . target ); //normally goto marks code as not alive, turn generation on code .entryPoint (); } Target position known? Yes – patch immediately No – add to list of pending gotos

Demo

ambda λ

Lambda implementation in Java 8 Language change Compilation Runtime support Many interesting design considerations

Simple Example public Comparator<String> lambdaExample () { return (String a , String b ) -> a .compareTo ( b ); }

LambdaToMethod.java public Comparator<String> lambdaExample () { return (String a , String b ) -> a .compareTo ( b ); } /*synthetic*/ private static int lambda$lambdaExample$0( , ) { return ; } final String a final String b a.compareTo (b)

Runtime public Comparator<String> lambdaExample () { return < invokedynamic > LambdaMetafactory.metafactory ( } /*synthetic*/ private static int lambda$lambdaExample$0 ( final String a , final String b ) { return a .compareTo ( b ); } Lookup ( LambdaExample ) , / *caller* / "compare" , ()Comparator, /* MethodType */ ( Object,Object ) int , / * MethodType * / lambda$lambdaexample$0, /* MethodHandle * / ( String,String ) int ); /* MethodType */ final class LambdaExample $$Lambda$1/1834188994 implements Comparator { private LambdaExample $$Lambda$1/1834188994() public int compare( Object,Object ) } public interface Comparator { / *erased* / int compare(Object o1 , Object o2 ); }

Runtime implementation Lambda Class

Serialization Lambda Instance Serialize Serialized Lambda Deserialize Lambda Meta Factory

Possible to back port ? Capture generated class ? Compile time generation of inner class!

Step 1: Source.java public boolean allowLambda () { return compareTo ( ) >= 0; } JDK1_8 JDK1_5

Step 2: Special handling boolean mustBackportLambda () { retur n thi s.target.compareTo (Target.JDK1_8) < 0 ; }

Step 3: Call backport if (! this . attr .mustBackportLambda () ) { result = makeMetafactoryIndyCall ( ... ); } else { result = new LambdaBackPorter ( ... ). implementLambdaClass ( ... ); }

Example implementation private static final class Lambda$$2 implements Comparator<String> { Lambda$$2 { super (); } public int compare(String arg0 , String arg1 ) { return LambdaExample.lambda$lambdaExample$0( arg0 , arg1 ); } public int compare(Object o1 , Object o2 ) { return this .compare ((String) o1 , (String) o2 ); } }

Example invoking public Comparator<String> lambdaExample () { } return LambdaMetafactory. metafactory (...); return new Lambda$$ 2();

Demo

ambda λ

Experiment

< invokedynamic > “playground” @ InvokeIndy annotation on method with reference to boostrap method Annotation processor that validates reference Compiler plugin that replaces method invocation with invokedynamic

@ IndyMethod public class SomeService { //must refer to a valid public static method //with certain characteristics @ IndyMethod ( implementation = " no.kantega.jvm.indy.example.SomeProvider " , method = " dummyMethod " ) public static void doStuff () { throw new UnsupportedOperationException ( "..." ); } }

no.kantega.jvm.indy.compiler.plugin.IndyAnnotationChecker Hooking in the processor: META-INF/services/ javax.annotation.processing.Processor :

Annotation processor : setup, compliance + mapping @ SupportedAnnotationTypes ( " no.... IndyMethod " ) @ SupportedSourceVersion (SourceVersion. RELEASE_8 ) //Java version public class IndyAnnotationChecker extends AbstractProcessor { //... public boolean process(Set <...> annotations , RoundEnvironment roundEnv ) { for ( TypeElement annotation : annotations ) { for (Element element : roundEnv .getElementsAnnotatedWith ( annotation )) { //...raise error if earlier than Java 7 or missing plugin IndyMethod indyMethodRef = element .getAnnotation ( IndyMethod . class ); if ( indyMethodRef != null ) { //...check existance of type and compliance of method ... processingEnv .getMessager (). printMessage ( Kind. ERROR , "..." , element ); } } //...

... < build > < plugins > < plugin > < artifactId > maven-compiler-plugin </ artifactId > < configuration > <!-- Disable annotation processing for ourselves. --> < compilerArgument > - proc:none </ compilerArgument > </ configuration > </ plugin > </ plugins > </ build > ... Disable annotation processing in the project that implements the plugin (!): pom.xml :

Devoxx BE 2014 : “Plugging into the Java Compiler” , https ://www.parleys.com/tutorial/plugging-java-compiler -1 Google the session title for additional resources.

Compiler plugin Hook straight into the compilation process Respond to events from compilation process Make changes to AST

no.kantega.jvm.indy.compiler.plugin.IndyPlugin Hooking in the plugin: META-INF/services/ com.sun.source.util.Plugin :

public class IndyPlugin implements Plugin { public String getName () { return " IndyPlugin " ; } public void init ( JavacTask paramJavacTask , String ... paramArrayOfString ) { paramJavacTask .addTaskListener ( new GenerateInvokeDynamicHandler ()); } } Typical plugin definition: Unique name Delegate to task listener(s)

public class GenerateInvokeDynamicHandler implements TaskListener { public void started( TaskEvent start ) { if ( start .getKind () == Kind. GENERATE ) { for (Tree tree : start .getCompilationUnit (). getTypeDecls ()) { tree .accept ( new IndyMethodInvocationReplacer (), tree ); } } } Task listener: Receive callback, check stage Insert visitor to process ASTs

public class IndyMethodInvocationReplacer extends TreeScanner { public Object visitMethodInvocation ( MethodInvocationTree node , Tree p ) { { //...various checks on the method call //...see if annotation processor has created mapping for it MethodSymbol replacementMethod = IndyMethodMappings. getInstance (). mappingFor (( MethodSymbol ) identifier . sym ); if ( replacementMethod != null ) { //insert reference to bootstrap identifier . sym = new Symbol.DynamicMethodSymbol (...); } } } } return super .visitMethodInvocation ( node , p ); } } Visitor: Make modifications

< build > < plugins > < plugin > < artifactId > maven-compiler-plugin </ artifactId > < configuration > < compilerArgs > < arg > - Xplugin:IndyPlugin </ arg > </ compilerArgs > < source > 1.8 </ source > < target > 1.8 </ target > </ configuration > </ plugin > </plugins> </build> Users: Enable plugin in compilation

Demo

Wrap Up The Java compiler is written in pure Java Compilation is done in phases Programming language advances (syntactic sugar) require good compiler support Lambdas are compiled in a forward compatible manner Annotation processors and compiler plugins may be used to tailor the compilation process to specific needs

Resources OpenJDK Source : http://hg.openjdk.java.net/ Compiler hacking tutorial http ://www.ahristov.com/tutorial/java-compiler.html Sample code: https :// github.com/skarsaune/goto https://github.com/skarsaune/indy-plugin Slide pack: http :// www.slideshare.net/MartinSkarsaune 60 minute video ( Devoxx ) : https:// youtu.be/gGTDQq6ZjIk

Questions or Comments ? Martin Skarsaune Java Developer and Co- Owner

Thank You for Your Time! Martin Skarsaune Java Developer and Co- Owner