test/tools/javac/tree/NewArrayPretty.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2013, 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 8014826
    27  * @summary test Pretty print of NewArray
    28  * @author ksrini
    29  */
    30 import com.sun.source.tree.ClassTree;
    31 import com.sun.source.tree.CompilationUnitTree;
    32 import com.sun.tools.javac.api.JavacTaskImpl;
    33 import com.sun.tools.javac.tree.JCTree;
    34 import java.io.IOException;
    35 import java.net.URI;
    36 import java.util.Arrays;
    37 import javax.tools.JavaCompiler;
    38 import javax.tools.JavaFileObject;
    39 import javax.tools.SimpleJavaFileObject;
    40 import javax.tools.ToolProvider;
    42 public class NewArrayPretty {
    43     private final JavaCompiler tool;
    44     NewArrayPretty() {
    45         tool = ToolProvider.getSystemJavaCompiler();
    46     }
    47     public static void main(String... args) throws Exception {
    48         NewArrayPretty nap = new NewArrayPretty();
    49         nap.run("Class[] cls = null");
    50         nap.run("Class[] cls = new Class[]{Object.class}");
    51     }
    52     void run(String code) throws IOException {
    53         String src = "public class Test {" + code + ";}";
    55         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
    56                 null, Arrays.asList(new MyFileObject(src)));
    58         for (CompilationUnitTree cut : ct.parse()) {
    59             JCTree.JCVariableDecl var =
    60                     (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
    62             if (!code.equals(var.toString())) {
    63                 System.err.println("Expected: " + code);
    64                 System.err.println("Obtained: " + var.toString());
    65                 throw new RuntimeException("strings do not match!");
    66             }
    67         }
    68     }
    69 }
    70 class MyFileObject extends SimpleJavaFileObject {
    72     private String text;
    74     public MyFileObject(String text) {
    75         super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    76         this.text = text;
    77     }
    79     @Override
    80     public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    81         return text;
    82     }
    83 }

mercurial