langtools/test/tools/javac/AnonymousSubclassTest.java
author shade
Tue, 02 Feb 2016 18:15:15 +0300
changeset 35639 f34e7e8b4eac
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
permissions -rw-r--r--
8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested Reviewed-by: vlivanov, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     4
 *
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     8
 *
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    13
 * accompanied this code).
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    14
 *
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    18
 *
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    21
 * questions.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    22
 */
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    23
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    24
/*
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    25
 * @test
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    26
 * @bug 8023945
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    27
 * @summary javac wrongly allows a subclass of an anonymous class
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    28
 * @library /tools/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.main
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    32
 * @build ToolBox
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    33
 * @run main AnonymousSubclassTest
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    34
 */
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    35
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    36
public class AnonymousSubclassTest {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    37
    public static void main(String... args) throws Exception {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    38
        new AnonymousSubclassTest().run();
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    39
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    40
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    41
    ToolBox tb = new ToolBox();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    42
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    43
    // To trigger the error we want, first we need to compile
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    44
    // a class with an anonymous inner class: Foo$1.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    45
    final String foo =
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    46
        "public class Foo {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    47
        "  void m() { Foo f = new Foo() {}; }" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    48
        "}";
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    49
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    50
    // Then, we try to subclass the anonymous class
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    51
    // Note: we must do this in two classes because a different
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    52
    // error will be generated if we don't load Foo$1 through the
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    53
    // class reader.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    54
    final String test1 =
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    55
        "public class Test1 {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    56
        "  void m() {"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    57
        "    Foo f1 = new Foo();"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    58
        "    Foo f2 = new Foo$1(f1) {};"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    59
        "  }" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    60
        "}";
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    61
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    62
    final String test2 =
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    63
        "public class Test2 {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    64
        "  class T extends Foo$1 {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    65
        "    public T(Foo f) { super(f); }" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    66
        "  }"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    67
        "}";
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    68
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    69
    void compOk(String code) throws Exception {
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    70
        tb.new JavacTask()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    71
                .sources(code)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    72
                .run();
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    73
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    74
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    75
    void compFail(String code) throws Exception {
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    76
        String errs = tb.new JavacTask()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    77
                .sources(code)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    78
                .classpath(".")
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    79
                .options("-XDrawDiagnostics")
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    80
                .run(ToolBox.Expect.FAIL)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    81
                .writeAll()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    82
                .getOutput(ToolBox.OutputKind.DIRECT);
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    83
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23809
diff changeset
    84
        if (!errs.contains("cant.inherit.from.anon")) {
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    85
            throw new Exception("test failed");
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    86
        }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    87
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    88
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    89
    void run() throws Exception {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    90
        compOk(foo);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    91
        compFail(test1);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    92
        compFail(test2);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    93
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    94
}