Goto devoxx

download Goto devoxx

If you can't read please download the document

  • date post

    03-Aug-2015
  • Category

    Internet

  • view

    46
  • download

    2

Embed Size (px)

Transcript of Goto devoxx

1. @MSkarsaune#GotoJava goto java; - Java compilation under the hood Martin Skarsaune @kantega.no Kantega Norwegian software craftsmanship since 200 2. GOTO Statements in Java! 3. @MSkarsaune#GotoJava 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. 4. @MSkarsaune#GotoJava GOTO Statement Means OpenJDK Open source Java implementation Javac is implemented in plain Java Modify compiler to support GOTO 5. @MSkarsaune#GotoJava public class GotoSuccess { public static void main(String[] args) { one: System.out.print("goto "); two: System.out.print(DEV"); goto four; three: System.out.print(UK"); goto five; four: System.out.print(OXX "); goto three; five: System.out.print("!"); } } Success Case 6. @MSkarsaune#GotoJava GOTO AHEAD Dijkstra 1968: programmerquality =100 1- gotos LoC 7. @MSkarsaune#GotoJava What is a Compiler? Compiler 8. @MSkarsaune#GotoJava What is a Compiler? front end back end 9. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate processAnnotations(enterTrees( parseFiles())),) generate(desugar(flow(attribute()))) 10. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate processAnnotations(enterTrees( parseFiles())),) generate(desugar(flow(attribute()))) 11. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Syntax: goto identifier; First convert character stream to token stream (Scanner.java) GOTO IDENTIFIER SEMI [g][o][t][o][ ][f][o][u][r][;] 12. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate 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! 13. @MSkarsaune#GotoJava final Scanner scanner=factory.newScanner(goto identifier;, false); Parse Enter Process Attribute Flow Desugar Generate ... import com.sun.tools.javac.parser.Scanner; ... 14. @MSkarsaune#GotoJava final Scanner scanner=factory.newScanner(goto identifier;, false); scanner.nextToken(); Parse Enter Process Attribute Flow Desugar Generate 15. @MSkarsaune#GotoJava final Scanner scanner=factory.newScanner(goto identifier;, false); scanner.nextToken(); assertThat(First token is GOTO, scanner.token().kind, is(GOTO)); Parse Enter Process Attribute Flow Desugar Generate 16. @MSkarsaune#GotoJava final Scanner scanner=factory.newScanner(goto identifier;, false); scanner.nextToken(); assertThat(First token is GOTO, scanner.token().kind, is(GOTO)); scanner.nextToken(); Parse Enter Process Attribute Flow Desugar Generate 17. @MSkarsaune#GotoJava 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)); Parse Enter Process Attribute Flow Desugar Generate 18. @MSkarsaune#GotoJava 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(); Parse Enter Process Attribute Flow Desugar Generate 19. @MSkarsaune#GotoJava 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)); Parse Enter Process Attribute Flow Desugar Generate 20. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate 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 21. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Class Interface 22. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Interface based visitors 23. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate 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 } 24. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate public static class JCLabeledStatement extends JCStatement implements LabeledStatementTree { public GotoResolver handler; } public class GotoResolver { Map gotos; Map targets; List statementsInSequence; ... } Helper object 25. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate 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; } 26. @YourTwitterHandle@MSkarsaune#GotoJava 27. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate processAnnotations(enterTrees( parseFiles())),) generate(desugar(flow(attribute()))) 28. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate processAnnotations(enterTrees( parseFiles())),) generate(desugar(flow(attribute()))) 29. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Basic sanity testing of compilation unit File name and folder location Duplicate class names Corrections Add default constructor if no constructors are declared 30. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Default Constructor public class SimpleClass { } 31. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Default Constructor public class SimpleClass { public SimpleClass() { super(); } } 32. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate processAnnotations(enterTrees( parseFiles())),) generate(desugar(flow(attribute()))) 33. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate processAnnotations(enterTrees( parseFiles())),) generate(desugar(flow(attribute()))) 34. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Process Annitations Annotation processing API Part of ordinary javac process since Java 1.6 Plugin API (see javac documentation) 35. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Output controlled by command line switches @ - proc:only only process annotations, do not compile - proc:none do not process annotations 36. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate Annotation processor Example ensure that all fields with @Config annotation are primitive or strings @Config private String sender;//ok @Config private int retries;//ok @Config private String[] receivers;//not ok 37. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate META- INF/services/javax.annotation.processing.Proce ssor: org.kantega.reststop.apt.ConfigFieldProcessor 38. @MSkarsaune#GotoJava Parse Enter Process Attribute Flow Desugar Generate ConfigFieldProcessor.java : @SupportedAnnotationTypes("org.kantega.reststop.api.Config") @SupportedSourceVersion(SourceVersion.RELEASE_7) public class ConfigFieldProcessor extends AbstractProcessor { //... public boolean process(Set