1/*
2 * Copyright (c) 2006, 2015, 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 */
23
24/*
25 * @test
26 * @bug     6413682
27 * @summary Compiler confused about implicit type args and arrays
28 * @author  Peter von der Ah\u00e9
29 * @modules jdk.compiler
30 */
31
32import com.sun.source.tree.CompilationUnitTree;
33import com.sun.source.tree.ErroneousTree;
34import com.sun.source.tree.Tree;
35import com.sun.source.util.JavacTask;
36import com.sun.source.util.TreeScanner;
37import com.sun.source.util.Trees;
38import java.io.IOException;
39import java.net.URI;
40import java.util.Collections;
41import java.util.List;
42import javax.tools.Diagnostic;
43import javax.tools.DiagnosticListener;
44import javax.tools.JavaCompiler;
45import javax.tools.JavaFileObject;
46import javax.tools.SimpleJavaFileObject;
47import static javax.tools.JavaFileObject.Kind.SOURCE;
48import javax.tools.ToolProvider;
49
50public class TestPos {
51
52    static final String errCode = "compiler.err.cannot.create.array.with.type.arguments";
53    static final String expected =
54        String.format("%s%n%s%n",
55                      "compiler.err.cannot.create.array.with.type.arguments @ 33",
56                      "begin=28, end=50 : new Object[0],T,e,s,t");
57
58    public static void main(String... args) throws IOException {
59        final boolean[] sawError = { false };
60        final StringBuilder log = new StringBuilder();
61        class MyFileObject extends SimpleJavaFileObject {
62            MyFileObject() {
63                super(URI.create("myfo:///Test.java"), SOURCE);
64            }
65            @Override
66            public String getCharContent(boolean ignoreEncodingErrors) {
67                //      0         1         2         3         4         5
68                //      0123456789012345678901234567890123456789012345678901234
69                return "class Test { { Object[] o = new <T,e,s,t>Object[0]; } }";
70            }
71        }
72        class Scanner extends TreeScanner<Void,Trees> {
73            CompilationUnitTree toplevel = null;
74            @Override
75            public Void visitCompilationUnit(CompilationUnitTree node, Trees trees) {
76                toplevel = node;
77                return super.visitCompilationUnit(node, trees);
78            }
79            @Override
80            public Void visitErroneous(ErroneousTree node, Trees trees) {
81                sawError[0] = true;
82                long startPos = trees.getSourcePositions().getStartPosition(toplevel, node);
83                long endPos = trees.getSourcePositions().getEndPosition(toplevel, node);
84                log.append(String.format("begin=%s, end=%s : %s%n",
85                                         startPos,
86                                         endPos,
87                                         node.getErrorTrees()));
88                if (startPos != 28)
89                    error("Start pos for %s is incorrect (%s)!", node, startPos);
90                if (endPos != 50)
91                    error("End pos for %s is incorrect (%s)!", node, endPos);
92                return null;
93            }
94        }
95        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
96        List<JavaFileObject> compilationUnits =
97                Collections.<JavaFileObject>singletonList(new MyFileObject());
98        DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
99            public void report(Diagnostic<? extends JavaFileObject> diag) {
100                log.append(String.format("%s @ %s%n", diag.getCode(), diag.getPosition()));
101                if (!diag.getCode().equals(errCode))
102                    error("unexpected error");
103                if (diag.getPosition() != 33)
104                    error("Error pos for %s is incorrect (%s)!",
105                          diag.getCode(), diag.getPosition());
106                sawError[0] = true;
107            }
108        };
109        JavacTask task = (JavacTask)javac.getTask(null, null, dl, null, null,
110                                                  compilationUnits);
111        Trees trees = Trees.instance(task);
112        Iterable<? extends Tree> toplevels = task.parse();
113        if (!sawError[0])
114            error("No parse error detected");
115        sawError[0] = false;
116        new Scanner().scan(toplevels, trees);
117        if (!sawError[0])
118            error("No error tree detected");
119        if (!log.toString().equals(expected))
120            error("Unexpected log message: %n%s%n", log);
121        System.out.print(log);
122        System.out.flush();
123    }
124
125    static void error(String format, Object... args) {
126        throw new AssertionError(String.format(format, args));
127    }
128
129}
130