langtools/test/tools/javac/AnonymousSubclassTest.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 23809 9405883da95f
child 26100 bb7dd001d190
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
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
/*
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    28
 * @library /tools/javac/lib
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    29
 * @build ToolBox
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    30
 * @run main AnonymousSubclassTest
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    31
 */
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    32
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    33
import java.util.ArrayList;
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    34
import java.io.IOException;
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
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    41
    // 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
    42
    // a class with an anonymous inner class: Foo$1.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    43
    final String foo =
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    44
        "public class Foo {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    45
        "  void m() { Foo f = new Foo() {}; }" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    46
        "}";
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    47
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    48
    // Then, we try to subclass the anonymous class
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    49
    // 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
    50
    // 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
    51
    // class reader.
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    52
    final String test1 =
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    53
        "public class Test1 {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    54
        "  void m() {"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    55
        "    Foo f1 = new Foo();"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    56
        "    Foo f2 = new Foo$1(f1) {};"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    57
        "  }" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    58
        "}";
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
    final String test2 =
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    61
        "public class Test2 {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    62
        "  class T extends Foo$1 {" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    63
        "    public T(Foo f) { super(f); }" +
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    64
        "  }"+
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    65
        "}";
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
    void compOk(String code) throws Exception {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    68
        ToolBox.javac(new ToolBox.JavaToolArgs().setSources(code));
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    69
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    70
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    71
    void compFail(String code) throws Exception {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    72
        ArrayList<String> errors = new ArrayList<>();
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    73
        ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    74
        args.setSources(code)
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    75
            .appendArgs("-cp", ".", "-XDrawDiagnostics")
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    76
            .set(ToolBox.Expect.FAIL)
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    77
            .setErrOutput(errors);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    78
        ToolBox.javac(args);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    79
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    80
        if (!errors.get(0).contains("cant.inherit.from.anon")) {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    81
            System.out.println(errors.get(0));
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    82
            throw new Exception("test failed");
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    83
        }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    84
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    85
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    86
    void run() throws Exception {
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    87
        compOk(foo);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    88
        compFail(test1);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    89
        compFail(test2);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    90
    }
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff changeset
    91
}