test/tools/javac/tree/TestToString.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

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

mercurial