langtools/test/tools/javac/generics/6413682/TestPos.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 5520 86e4b9a9da40
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 5520
diff changeset
     2
 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug     6413682
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary Compiler confused about implicit type args and arrays
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @author  Peter von der Ah\u00e9
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 5520
diff changeset
    29
 * @modules jdk.compiler
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.source.tree.CompilationUnitTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.source.tree.ErroneousTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.source.tree.Tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.source.util.JavacTask;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.source.util.TreeScanner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.source.util.Trees;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.net.URI;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import javax.tools.Diagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import javax.tools.DiagnosticListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import javax.tools.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import javax.tools.SimpleJavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import static javax.tools.JavaFileObject.Kind.SOURCE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import javax.tools.ToolProvider;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public class TestPos {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    static final String errCode = "compiler.err.cannot.create.array.with.type.arguments";
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    static final String expected =
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        String.format("%s%n%s%n",
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
                      "compiler.err.cannot.create.array.with.type.arguments @ 33",
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
                      "begin=28, end=50 : new Object[0],T,e,s,t");
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public static void main(String... args) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        final boolean[] sawError = { false };
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        final StringBuilder log = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        class MyFileObject extends SimpleJavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            MyFileObject() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                super(URI.create("myfo:///Test.java"), SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            public String getCharContent(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                //      0         1         2         3         4         5
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                //      0123456789012345678901234567890123456789012345678901234
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                return "class Test { { Object[] o = new <T,e,s,t>Object[0]; } }";
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        class Scanner extends TreeScanner<Void,Trees> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            CompilationUnitTree toplevel = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            public Void visitCompilationUnit(CompilationUnitTree node, Trees trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                toplevel = node;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                return super.visitCompilationUnit(node, trees);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            public Void visitErroneous(ErroneousTree node, Trees trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                sawError[0] = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                long startPos = trees.getSourcePositions().getStartPosition(toplevel, node);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                long endPos = trees.getSourcePositions().getEndPosition(toplevel, node);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                log.append(String.format("begin=%s, end=%s : %s%n",
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                                         startPos,
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                                         endPos,
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                                         node.getErrorTrees()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                if (startPos != 28)
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                    error("Start pos for %s is incorrect (%s)!", node, startPos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                if (endPos != 50)
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                    error("End pos for %s is incorrect (%s)!", node, endPos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        List<JavaFileObject> compilationUnits =
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                Collections.<JavaFileObject>singletonList(new MyFileObject());
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            public void report(Diagnostic<? extends JavaFileObject> diag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                log.append(String.format("%s @ %s%n", diag.getCode(), diag.getPosition()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                if (!diag.getCode().equals(errCode))
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                    error("unexpected error");
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                if (diag.getPosition() != 33)
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                    error("Error pos for %s is incorrect (%s)!",
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                          diag.getCode(), diag.getPosition());
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                sawError[0] = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        JavacTask task = (JavacTask)javac.getTask(null, null, dl, null, null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                                                  compilationUnits);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        Trees trees = Trees.instance(task);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        Iterable<? extends Tree> toplevels = task.parse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        if (!sawError[0])
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            error("No parse error detected");
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        sawError[0] = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        new Scanner().scan(toplevels, trees);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        if (!sawError[0])
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            error("No error tree detected");
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        if (!log.toString().equals(expected))
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            error("Unexpected log message: %n%s%n", log);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        System.out.print(log);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        System.out.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    static void error(String format, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        throw new AssertionError(String.format(format, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
}