aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7080267 aoqi@0: * @summary Call to toString() from an ExpressionStatementTree doesn't take in aoqi@0: * consideration the ";" at the end aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.tree.BlockTree; aoqi@0: import com.sun.source.tree.CompilationUnitTree; aoqi@0: import com.sun.source.tree.StatementTree; aoqi@0: import com.sun.source.tree.Tree; aoqi@0: import java.io.IOException; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.Collections; aoqi@0: import java.util.List; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.source.util.TreeScanner; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: aoqi@0: public class TestToString { aoqi@0: String[] statements = { aoqi@0: "i = i + 1;", aoqi@0: "i++;", aoqi@0: "m();", aoqi@0: ";", aoqi@0: "if (i == 0) return;", aoqi@0: "while (i > 0) i--;", aoqi@0: "{ }", aoqi@0: "{ i++; }", aoqi@0: "class C { }" aoqi@0: }; aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new TestToString().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: for (String s: statements) { aoqi@0: test(s); aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors found"); aoqi@0: } aoqi@0: aoqi@0: void test(String stmt) throws IOException { aoqi@0: System.err.println("Test: " + stmt); aoqi@0: List options = Collections.emptyList(); aoqi@0: List files = Arrays.asList(new JavaSource(stmt)); aoqi@0: JavacTask t = tool.getTask(null, fm, null, options, null, files); aoqi@0: checkEqual(scan(t.parse()), stmt); aoqi@0: } aoqi@0: aoqi@0: String scan(Iterable trees) { aoqi@0: class Scanner extends TreeScanner { aoqi@0: String scan(Iterable trees) { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: scan(trees, sb); aoqi@0: return sb.toString(); aoqi@0: } aoqi@0: @Override aoqi@0: public Void scan(Tree tree, StringBuilder sb) { aoqi@0: if (print && tree instanceof StatementTree) { aoqi@0: sb.append(PREFIX); aoqi@0: sb.append(tree); aoqi@0: sb.append(SUFFIX); aoqi@0: return null; aoqi@0: } else { aoqi@0: return super.scan(tree, sb); aoqi@0: } aoqi@0: } aoqi@0: @Override aoqi@0: public Void visitBlock(BlockTree tree, StringBuilder sb) { aoqi@0: print = true; aoqi@0: try { aoqi@0: return super.visitBlock(tree, sb); aoqi@0: } finally { aoqi@0: print = false; aoqi@0: } aoqi@0: } aoqi@0: boolean print = false; aoqi@0: } aoqi@0: return new Scanner().scan(trees); aoqi@0: } aoqi@0: aoqi@0: void checkEqual(String found, String expect) { aoqi@0: boolean match = (found == null) ? (expect == null) : aoqi@0: expect.equals(found aoqi@0: .replaceAll("^\\Q" + PREFIX + "\\E\\s*", "") aoqi@0: .replaceAll("\\s*\\Q" + SUFFIX + "\\E$", "") aoqi@0: .replaceAll("\\s+", " ")); aoqi@0: aoqi@0: if (!match) aoqi@0: error("Mismatch: expected: " + expect + " found: " + found); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: static final String PREFIX = "#<"; aoqi@0: static final String SUFFIX = "#>"; aoqi@0: aoqi@0: JavacTool tool = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: int errors = 0; aoqi@0: aoqi@0: static class JavaSource extends SimpleJavaFileObject { aoqi@0: aoqi@0: String source = aoqi@0: "class Test {\n" + aoqi@0: " int i;\n" + aoqi@0: " void m() {\n" + aoqi@0: " #S\n" + aoqi@0: " }\n" + aoqi@0: "}"; aoqi@0: aoqi@0: public JavaSource(String stmt) { aoqi@0: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); aoqi@0: source = source.replace("#S", stmt); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: } aoqi@0: }