8014826: c.s.t.javac.tree.Pretty.visitNewArray() prints duplicate dimension markers

Wed, 31 Jul 2013 08:37:34 -0700

author
ksrini
date
Wed, 31 Jul 2013 08:37:34 -0700
changeset 1928
05370ef9dccb
parent 1925
7696282873f6
child 1929
99b60bcf3862

8014826: c.s.t.javac.tree.Pretty.visitNewArray() prints duplicate dimension markers
Reviewed-by: jjg, vromero

src/share/classes/com/sun/tools/javac/tree/Pretty.java file | annotate | diff | comparison | revisions
test/tools/javac/tree/NewArrayPretty.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java	Wed Jul 31 10:52:01 2013 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java	Wed Jul 31 08:37:34 2013 -0700
     1.3 @@ -973,7 +973,6 @@
     1.4                      printBrackets((JCArrayTypeTree) elem);
     1.5              }
     1.6              if (tree.elems != null) {
     1.7 -                if (tree.elemtype != null) print("[]");
     1.8                  print("{");
     1.9                  printExprs(tree.elems);
    1.10                  print("}");
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/tree/NewArrayPretty.java	Wed Jul 31 08:37:34 2013 -0700
     2.3 @@ -0,0 +1,83 @@
     2.4 +/*
     2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8014826
    2.30 + * @summary test Pretty print of NewArray
    2.31 + * @author ksrini
    2.32 + */
    2.33 +import com.sun.source.tree.ClassTree;
    2.34 +import com.sun.source.tree.CompilationUnitTree;
    2.35 +import com.sun.tools.javac.api.JavacTaskImpl;
    2.36 +import com.sun.tools.javac.tree.JCTree;
    2.37 +import java.io.IOException;
    2.38 +import java.net.URI;
    2.39 +import java.util.Arrays;
    2.40 +import javax.tools.JavaCompiler;
    2.41 +import javax.tools.JavaFileObject;
    2.42 +import javax.tools.SimpleJavaFileObject;
    2.43 +import javax.tools.ToolProvider;
    2.44 +
    2.45 +public class NewArrayPretty {
    2.46 +    private final JavaCompiler tool;
    2.47 +    NewArrayPretty() {
    2.48 +        tool = ToolProvider.getSystemJavaCompiler();
    2.49 +    }
    2.50 +    public static void main(String... args) throws Exception {
    2.51 +        NewArrayPretty nap = new NewArrayPretty();
    2.52 +        nap.run("Class[] cls = null");
    2.53 +        nap.run("Class[] cls = new Class[]{Object.class}");
    2.54 +    }
    2.55 +    void run(String code) throws IOException {
    2.56 +        String src = "public class Test {" + code + ";}";
    2.57 +
    2.58 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
    2.59 +                null, Arrays.asList(new MyFileObject(src)));
    2.60 +
    2.61 +        for (CompilationUnitTree cut : ct.parse()) {
    2.62 +            JCTree.JCVariableDecl var =
    2.63 +                    (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
    2.64 +
    2.65 +            if (!code.equals(var.toString())) {
    2.66 +                System.err.println("Expected: " + code);
    2.67 +                System.err.println("Obtained: " + var.toString());
    2.68 +                throw new RuntimeException("strings do not match!");
    2.69 +            }
    2.70 +        }
    2.71 +    }
    2.72 +}
    2.73 +class MyFileObject extends SimpleJavaFileObject {
    2.74 +
    2.75 +    private String text;
    2.76 +
    2.77 +    public MyFileObject(String text) {
    2.78 +        super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    2.79 +        this.text = text;
    2.80 +    }
    2.81 +
    2.82 +    @Override
    2.83 +    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    2.84 +        return text;
    2.85 +    }
    2.86 +}

mercurial