test/tools/javac/tree/TestToString.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/tree/TestToString.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,159 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7080267
    1.30 + * @summary Call to toString() from an ExpressionStatementTree doesn't take in
    1.31 + *      consideration the ";" at the end
    1.32 + */
    1.33 +
    1.34 +import com.sun.source.tree.BlockTree;
    1.35 +import com.sun.source.tree.CompilationUnitTree;
    1.36 +import com.sun.source.tree.StatementTree;
    1.37 +import com.sun.source.tree.Tree;
    1.38 +import java.io.IOException;
    1.39 +import java.net.URI;
    1.40 +import java.util.Arrays;
    1.41 +import java.util.Collections;
    1.42 +import java.util.List;
    1.43 +import javax.tools.JavaFileObject;
    1.44 +import javax.tools.SimpleJavaFileObject;
    1.45 +import javax.tools.StandardJavaFileManager;
    1.46 +
    1.47 +import com.sun.source.util.JavacTask;
    1.48 +import com.sun.source.util.TreeScanner;
    1.49 +import com.sun.tools.javac.api.JavacTool;
    1.50 +
    1.51 +public class TestToString {
    1.52 +    String[] statements = {
    1.53 +        "i = i + 1;",
    1.54 +        "i++;",
    1.55 +        "m();",
    1.56 +        ";",
    1.57 +        "if (i == 0) return;",
    1.58 +        "while (i > 0) i--;",
    1.59 +        "{ }",
    1.60 +        "{ i++; }",
    1.61 +        "class C { }"
    1.62 +    };
    1.63 +
    1.64 +    public static void main(String... args) throws Exception {
    1.65 +        new TestToString().run();
    1.66 +    }
    1.67 +
    1.68 +    void run() throws Exception {
    1.69 +        for (String s: statements) {
    1.70 +            test(s);
    1.71 +        }
    1.72 +
    1.73 +        if (errors > 0)
    1.74 +            throw new Exception(errors + " errors found");
    1.75 +    }
    1.76 +
    1.77 +    void test(String stmt) throws IOException {
    1.78 +        System.err.println("Test: " + stmt);
    1.79 +        List<String> options = Collections.<String>emptyList();
    1.80 +        List<? extends JavaFileObject> files = Arrays.asList(new JavaSource(stmt));
    1.81 +        JavacTask t = tool.getTask(null, fm, null, options, null, files);
    1.82 +        checkEqual(scan(t.parse()), stmt);
    1.83 +    }
    1.84 +
    1.85 +    String scan(Iterable<? extends CompilationUnitTree> trees) {
    1.86 +        class Scanner extends TreeScanner<Void,StringBuilder> {
    1.87 +            String scan(Iterable<? extends Tree> trees) {
    1.88 +                StringBuilder sb = new StringBuilder();
    1.89 +                scan(trees, sb);
    1.90 +                return sb.toString();
    1.91 +            }
    1.92 +            @Override
    1.93 +            public Void scan(Tree tree, StringBuilder sb) {
    1.94 +                if (print && tree instanceof StatementTree) {
    1.95 +                    sb.append(PREFIX);
    1.96 +                    sb.append(tree);
    1.97 +                    sb.append(SUFFIX);
    1.98 +                    return null;
    1.99 +                } else {
   1.100 +                    return super.scan(tree, sb);
   1.101 +                }
   1.102 +            }
   1.103 +            @Override
   1.104 +            public Void visitBlock(BlockTree tree, StringBuilder sb) {
   1.105 +                print = true;
   1.106 +                try {
   1.107 +                    return super.visitBlock(tree, sb);
   1.108 +                } finally {
   1.109 +                    print = false;
   1.110 +                }
   1.111 +            }
   1.112 +            boolean print = false;
   1.113 +        }
   1.114 +        return new Scanner().scan(trees);
   1.115 +    }
   1.116 +
   1.117 +    void checkEqual(String found, String expect) {
   1.118 +        boolean match = (found == null) ? (expect == null) :
   1.119 +            expect.equals(found
   1.120 +                .replaceAll("^\\Q" + PREFIX + "\\E\\s*", "")
   1.121 +                .replaceAll("\\s*\\Q" + SUFFIX + "\\E$", "")
   1.122 +                .replaceAll("\\s+", " "));
   1.123 +
   1.124 +        if (!match)
   1.125 +            error("Mismatch: expected: " + expect + " found: " + found);
   1.126 +    }
   1.127 +
   1.128 +
   1.129 +
   1.130 +    void error(String msg) {
   1.131 +        System.err.println("Error: " + msg);
   1.132 +        errors++;
   1.133 +    }
   1.134 +
   1.135 +    static final String PREFIX = "#<";
   1.136 +    static final String SUFFIX = "#>";
   1.137 +
   1.138 +    JavacTool tool = JavacTool.create();
   1.139 +    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
   1.140 +    int errors = 0;
   1.141 +
   1.142 +    static class JavaSource extends SimpleJavaFileObject {
   1.143 +
   1.144 +        String source =
   1.145 +                "class Test {\n" +
   1.146 +                "    int i;\n" +
   1.147 +                "    void m() {\n" +
   1.148 +                "        #S\n" +
   1.149 +                "    }\n" +
   1.150 +                "}";
   1.151 +
   1.152 +        public JavaSource(String stmt) {
   1.153 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.154 +            source = source.replace("#S", stmt);
   1.155 +        }
   1.156 +
   1.157 +        @Override
   1.158 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.159 +            return source;
   1.160 +        }
   1.161 +    }
   1.162 +}

mercurial