test/langtools/tools/javac/resolve/BrokenAnonymous.java
author jlahoda
Wed, 21 Feb 2018 17:07:12 +0100
changeset 48931 b25eb74ec283
permissions -rw-r--r--
8197439: Crash with -XDfind=lambda for anonymous class in anonymous class. Summary: Ensuring unresolvable anonymous classes are attributed. Reviewed-by: mcimadamore, sadayapalam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48931
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     1
/*
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     4
 *
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     8
 *
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    14
 *
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    18
 *
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    21
 * questions.
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    22
 */
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    23
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    24
/*
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    25
 * @test
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    26
 * @bug 8197439
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    27
 * @summary Ensuring that unresolvable anonymous class is still attributed.
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    28
 * @modules jdk.compiler
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    29
 */
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    30
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    31
import java.io.*;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    32
import java.net.*;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    33
import java.util.*;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    34
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    35
import javax.tools.*;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    36
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    37
import com.sun.source.tree.CompilationUnitTree;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    38
import com.sun.source.tree.IdentifierTree;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    39
import com.sun.source.tree.MethodTree;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    40
import com.sun.source.tree.VariableTree;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    41
import com.sun.source.util.*;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    42
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    43
public class BrokenAnonymous {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    44
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    45
    static class JavaSource extends SimpleJavaFileObject {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    46
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    47
        final static String source =
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    48
                        "class C {\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    49
                        "    Object o1 = new Undef1() {" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    50
                        "        int i = 0;\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    51
                        "        int m(int j) { return i + j; }\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    52
                        "    }\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    53
                        "    Object o2 = new Undef2<>() {" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    54
                        "        int i = 0;\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    55
                        "        int m(int j) { return i + j; }\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    56
                        "    }\n" +
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    57
                        "}";
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    58
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    59
        JavaSource() {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    60
            super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    61
        }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    62
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    63
        @Override
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    64
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    65
            return source;
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    66
        }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    67
    }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    68
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    69
    public static void main(String... args) throws IOException {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    70
        new BrokenAnonymous().run();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    71
    }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    72
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    73
    void run() throws IOException {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    74
        File destDir = new File("classes"); destDir.mkdir();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    75
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    76
        JavaSource source = new JavaSource();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    77
        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    78
                Arrays.asList("-d", destDir.getPath()),
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    79
                null,
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    80
                Arrays.asList(source));
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    81
        CompilationUnitTree cut = ct.parse().iterator().next();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    82
        Trees trees = Trees.instance(ct);
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    83
        ct.analyze();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    84
        new TreePathScanner<Void, Void>() {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    85
            @Override
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    86
            public Void visitVariable(VariableTree node, Void p) {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    87
                verifyElementType();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    88
                return super.visitVariable(node, p);
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    89
            }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    90
            @Override
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    91
            public Void visitMethod(MethodTree node, Void p) {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    92
                verifyElementType();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    93
                return super.visitMethod(node, p);
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    94
            }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    95
            @Override
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    96
            public Void visitIdentifier(IdentifierTree node, Void p) {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    97
                verifyElementType();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    98
                return super.visitIdentifier(node, p);
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
    99
            }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   100
            private void verifyElementType() {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   101
                TreePath tp = getCurrentPath();
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   102
                assertNotNull(trees.getElement(tp));
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   103
                assertNotNull(trees.getTypeMirror(tp));
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   104
            }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   105
        }.scan(cut, null);
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   106
    }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   107
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   108
    private void assertNotNull(Object obj) {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   109
        if (obj == null) {
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   110
            throw new AssertionError("Unexpected null value.");
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   111
        }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   112
    }
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents:
diff changeset
   113
}