Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 7080267 |
aoqi@0 | 27 | * @summary Call to toString() from an ExpressionStatementTree doesn't take in |
aoqi@0 | 28 | * consideration the ";" at the end |
aoqi@0 | 29 | */ |
aoqi@0 | 30 | |
aoqi@0 | 31 | import com.sun.source.tree.BlockTree; |
aoqi@0 | 32 | import com.sun.source.tree.CompilationUnitTree; |
aoqi@0 | 33 | import com.sun.source.tree.StatementTree; |
aoqi@0 | 34 | import com.sun.source.tree.Tree; |
aoqi@0 | 35 | import java.io.IOException; |
aoqi@0 | 36 | import java.net.URI; |
aoqi@0 | 37 | import java.util.Arrays; |
aoqi@0 | 38 | import java.util.Collections; |
aoqi@0 | 39 | import java.util.List; |
aoqi@0 | 40 | import javax.tools.JavaFileObject; |
aoqi@0 | 41 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 42 | import javax.tools.StandardJavaFileManager; |
aoqi@0 | 43 | |
aoqi@0 | 44 | import com.sun.source.util.JavacTask; |
aoqi@0 | 45 | import com.sun.source.util.TreeScanner; |
aoqi@0 | 46 | import com.sun.tools.javac.api.JavacTool; |
aoqi@0 | 47 | |
aoqi@0 | 48 | public class TestToString { |
aoqi@0 | 49 | String[] statements = { |
aoqi@0 | 50 | "i = i + 1;", |
aoqi@0 | 51 | "i++;", |
aoqi@0 | 52 | "m();", |
aoqi@0 | 53 | ";", |
aoqi@0 | 54 | "if (i == 0) return;", |
aoqi@0 | 55 | "while (i > 0) i--;", |
aoqi@0 | 56 | "{ }", |
aoqi@0 | 57 | "{ i++; }", |
aoqi@0 | 58 | "class C { }" |
aoqi@0 | 59 | }; |
aoqi@0 | 60 | |
aoqi@0 | 61 | public static void main(String... args) throws Exception { |
aoqi@0 | 62 | new TestToString().run(); |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | void run() throws Exception { |
aoqi@0 | 66 | for (String s: statements) { |
aoqi@0 | 67 | test(s); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | if (errors > 0) |
aoqi@0 | 71 | throw new Exception(errors + " errors found"); |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | void test(String stmt) throws IOException { |
aoqi@0 | 75 | System.err.println("Test: " + stmt); |
aoqi@0 | 76 | List<String> options = Collections.<String>emptyList(); |
aoqi@0 | 77 | List<? extends JavaFileObject> files = Arrays.asList(new JavaSource(stmt)); |
aoqi@0 | 78 | JavacTask t = tool.getTask(null, fm, null, options, null, files); |
aoqi@0 | 79 | checkEqual(scan(t.parse()), stmt); |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | String scan(Iterable<? extends CompilationUnitTree> trees) { |
aoqi@0 | 83 | class Scanner extends TreeScanner<Void,StringBuilder> { |
aoqi@0 | 84 | String scan(Iterable<? extends Tree> trees) { |
aoqi@0 | 85 | StringBuilder sb = new StringBuilder(); |
aoqi@0 | 86 | scan(trees, sb); |
aoqi@0 | 87 | return sb.toString(); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | @Override |
aoqi@0 | 90 | public Void scan(Tree tree, StringBuilder sb) { |
aoqi@0 | 91 | if (print && tree instanceof StatementTree) { |
aoqi@0 | 92 | sb.append(PREFIX); |
aoqi@0 | 93 | sb.append(tree); |
aoqi@0 | 94 | sb.append(SUFFIX); |
aoqi@0 | 95 | return null; |
aoqi@0 | 96 | } else { |
aoqi@0 | 97 | return super.scan(tree, sb); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | } |
aoqi@0 | 100 | @Override |
aoqi@0 | 101 | public Void visitBlock(BlockTree tree, StringBuilder sb) { |
aoqi@0 | 102 | print = true; |
aoqi@0 | 103 | try { |
aoqi@0 | 104 | return super.visitBlock(tree, sb); |
aoqi@0 | 105 | } finally { |
aoqi@0 | 106 | print = false; |
aoqi@0 | 107 | } |
aoqi@0 | 108 | } |
aoqi@0 | 109 | boolean print = false; |
aoqi@0 | 110 | } |
aoqi@0 | 111 | return new Scanner().scan(trees); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | void checkEqual(String found, String expect) { |
aoqi@0 | 115 | boolean match = (found == null) ? (expect == null) : |
aoqi@0 | 116 | expect.equals(found |
aoqi@0 | 117 | .replaceAll("^\\Q" + PREFIX + "\\E\\s*", "") |
aoqi@0 | 118 | .replaceAll("\\s*\\Q" + SUFFIX + "\\E$", "") |
aoqi@0 | 119 | .replaceAll("\\s+", " ")); |
aoqi@0 | 120 | |
aoqi@0 | 121 | if (!match) |
aoqi@0 | 122 | error("Mismatch: expected: " + expect + " found: " + found); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | |
aoqi@0 | 126 | |
aoqi@0 | 127 | void error(String msg) { |
aoqi@0 | 128 | System.err.println("Error: " + msg); |
aoqi@0 | 129 | errors++; |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | static final String PREFIX = "#<"; |
aoqi@0 | 133 | static final String SUFFIX = "#>"; |
aoqi@0 | 134 | |
aoqi@0 | 135 | JavacTool tool = JavacTool.create(); |
aoqi@0 | 136 | StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); |
aoqi@0 | 137 | int errors = 0; |
aoqi@0 | 138 | |
aoqi@0 | 139 | static class JavaSource extends SimpleJavaFileObject { |
aoqi@0 | 140 | |
aoqi@0 | 141 | String source = |
aoqi@0 | 142 | "class Test {\n" + |
aoqi@0 | 143 | " int i;\n" + |
aoqi@0 | 144 | " void m() {\n" + |
aoqi@0 | 145 | " #S\n" + |
aoqi@0 | 146 | " }\n" + |
aoqi@0 | 147 | "}"; |
aoqi@0 | 148 | |
aoqi@0 | 149 | public JavaSource(String stmt) { |
aoqi@0 | 150 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
aoqi@0 | 151 | source = source.replace("#S", stmt); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | @Override |
aoqi@0 | 155 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
aoqi@0 | 156 | return source; |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |
aoqi@0 | 159 | } |