JavaZone 2014 - goto java;

download JavaZone 2014 - goto java;

If you can't read please download the document

  • date post

    16-Dec-2014
  • Category

    Software

  • view

    207
  • download

    1

Embed Size (px)

description

Slide pack for goto java; presentation at JavaZone 2014. The talk looks into the Java compiler and adds support for goto statements.

Transcript of JavaZone 2014 - goto java;

  • 1. A peek into the OpenJDKcompiler : goto java;Martin SkarsauneJava Developer and Co-Owner

2. GOTO statementsin Java! 3. GOTO Statement Objective Syntax:goto identifier; Runtime Program control moves to target statement Other Semantics Target (statement label) must be defined in samescope, compilation error if not Potential circular gotos should give compilationwarning. 4. GOTO Statement Means1. OpenJDK Open source Java implementation Compiler implemented in Java2. Modify compiler source to support gotostatements 5. Success casepublic class GotoSuccess {public static void main(String[] args) {one: System.out.print("goto ");two: System.out.print("Java");goto four;three: System.out.print("2014");goto five;four: System.out.print("Zone ");goto three;five: System.out.print("!");}} 6. GOTOAHEADDijkstra 1968:programmerquality =100 1-gotosLoC 7. What is aCompiler?Compiler 8. What is aCompiler?front back 9. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 10. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 11. Parse Enter Process Attribute Flow Desugar Generate Syntax: goto identifier; First convert character stream to tokenstream (Scanner.java)[g][o][t][o][ ][f][o][u][r][;]GOTO IDENTIFIER SEMI 12. Parse Enter Process Attribute Flow Desugar Generate goto is already a reserved word in Java! Lucky for us, goto is therefore defined as aTokenKind. Tokens.java:141: GOTO("goto") The scanner therefore works out of the box! 13. Parse Enter Process Attribute Flow Desugar Generatefinal Scanner scanner=factory.newScanner(goto identifier;, false);...import com.sun.tools.javac.parser.Scanner;... 14. Parse Enter Process Attribute Flow Desugar Generatefinal Scanner scanner=factory.newScanner(goto identifier;, false);scanner.nextToken(); 15. Parse Enter Process Attribute Flow Desugar Generatefinal Scanner scanner=factory.newScanner(goto identifier;, false);scanner.nextToken();assertThat(First token is GOTO, scanner.token().kind, is(GOTO)); 16. Parse Enter Process Attribute Flow Desugar Generatefinal Scanner scanner=factory.newScanner(goto identifier;, false);scanner.nextToken();assertThat(First token is GOTO, scanner.token().kind, is(GOTO));scanner.nextToken(); 17. Parse Enter Process Attribute Flow Desugar Generatefinal 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)); 18. Parse Enter Process Attribute Flow Desugar Generatefinal 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(); 19. Parse Enter Process Attribute Flow Desugar Generatefinal 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)); 20. Parse Enter Process Attribute Flow Desugar Generate Abstract SyntaxCompilationUnitClassDefinitionFieldDefintionMethodDefinitionSignatureBodyIfStatementGotoWikipedia: the visitor design patternis a way of separating an algorithmfrom an object structure on which itoperatesvisitClassDef(..)visitMethodDef(..)visitIf(..)TreeGOTO IDENTIFIER SEMI[g][o][t][o][ ][f][o][u][r][;] 21. Parse Enter Process Attribute Flow Desugar GenerateClass Interface 22. Parse Enter Process Attribute Flow Desugar Generate Interface based visitors 23. Parse Enter Process Attribute Flow Desugar Generate Class based visitorspublic void visitGoto(JCGoto tree) {try {print("goto " + tree.label + ";");} catch (IOException e) {throw new UncheckedIOException(e);}}public void visitGoto(JCGoto tree) {//TODO implement} 24. Parse Enter Process Attribute Flow Desugar Generatepublic class GotoResolver {Map gotos;Map targets;List statementsInSequence;...}public static class JCLabeledStatement extends JCStatementimplements LabeledStatementTree {public GotoResolver handler;} 25. Parse Enter Process Attribute Flow Desugar GenerateTreeMaker.java:public JCGoto Goto(Name label, GotoResolver resolver) {JCGoto tree = new JCGoto(label, resolver);tree.pos = pos;return tree;} JavacParser.parseStatement()case GOTO: {nextToken();Name label = ident();JCGoto t = to(F.at(pos).Goto(label, getGotoResolver()));accept(SEMI);return t;} 26. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 27. 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 aredeclared 28. Parse Enter Process Attribute Flow Desugar Generate Default constructorpublic class SimpleClass {} 29. Parse Enter Process Attribute Flow Desugar Generate Default constructorpublic class SimpleClass {public SimpleClass() {super();}} 30. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 31. Parse Enter Process Attribute Flow Desugar Generate Annotation processing API Part of ordinary javac process since Java 1.6 Plugin API (see javac documentation) 32. Parse Enter Process Attribute Flow Desugar Generate Output controlled by command line [email protected] proc:none do not process annotations- proc:only only process annotations, do not compile 33. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 34. Parse Enter Process Attribute Flow Desugar Generate Semantic checks Types References Corrections Add required calls to super constructor 35. Parse Enter Process Attribute Flow Desugar Generate We want to verify that goto target exists incurrent scopepublic class GotoMissingLabel {public static void main(String[] args) {one: System.out.print("goto ");two: System.out.print("Java");goto six;three: System.out.print("2014");goto five;four: System.out.print("Zone ");goto three;five: System.out.print("!");}} 36. Parse Enter Process Attribute Flow Desugar Generate Attr.java:@Overridepublic 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);} 37. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 38. Parse Enter Process Attribute Flow Desugar GenerateFlow analysis Detect unreachable code Verify assignments Ensure proper method return Verify (effectively) final Check exception flow 39. Parse Enter Process Attribute Flow Desugar GenerateWe want to 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("2014");goto five;four: System.out.print("Zone ");goto three;five: System.out.println("!");goto one;//forms infinite loop}} 40. Parse Enter Process Attribute Flow Desugar GenerateFlow.FlowAnalyzer class:@Overridepublic void visitGoto(JCGoto tree) {if (tree.handler.detectCircularGotoPosition(tree))log.warning(tree.pos, "circular.goto");}compiler.properties:...compiler.warn.circular.goto=circular goto... 41. Parse Enter Process Attribute Flow Desugar GenerateprocessAnnotations(enterTrees(parseFiles())),)generate(desugar(flow(attribute()))) 42. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Erase generic typespublic class Bridge implements Comparator {} 43. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Erase generic typespublic class Bridge implements Comparator {}public interface Comparator {tint compare(T o1, T o2);}or { 44. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Erase generic typespublic class Bridge implements Comparator {}public interface Comparator {tint compare(T o1, T o2);}or { 45. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Erase generic typespublic class Bridge implements Comparator {public int compare(Integer first, Integer second) {return first - second;}}public interface Comparator {tint compare(T o1, T o2);}or { 46. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Erase generic typespublic class Bridge implements Comparator {public int compare(Integer first, Integer second) {return first - second;}} 47. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Erase generic typespublic 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);}} 48. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Extract Inner Classpublic class Outer {private void foo() { }public Runnable fooRunner() {return new Runnable() {public void run() {foo();}};}} 49. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Extract Inner Classpublic class Outer {private void foo() { }public Runnable fooRunner() {return new Runnable() {public void run() {foo();}};}} 50. Parse Enter Process Attribute Flow Desugar GenerateTransTypes Unlambda Lower Extract Inner Cla