1 /*
2  * Copyright (c) 2014, 2016, 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 8023945
27  * @summary javac wrongly allows a subclass of an anonymous class
28  * @library /tools/lib
29  * @modules jdk.compiler/com.sun.tools.javac.api
30  *          jdk.compiler/com.sun.tools.javac.main
31  * @build toolbox.ToolBox toolbox.JavacTask
32  * @run main AnonymousSubclassTest
33  */
34 
35 import toolbox.JavacTask;
36 import toolbox.Task;
37 import toolbox.ToolBox;
38 
39 public class AnonymousSubclassTest {
main(String... args)40     public static void main(String... args) throws Exception {
41         new AnonymousSubclassTest().run();
42     }
43 
44     ToolBox tb = new ToolBox();
45 
46     // To trigger the error we want, first we need to compile
47     // a class with an anonymous inner class: Foo$1.
48     final String foo =
49         "public class Foo {" +
50         "  void m() { Foo f = new Foo() {}; }" +
51         "}";
52 
53     // Then, we try to subclass the anonymous class
54     // Note: we must do this in two classes because a different
55     // error will be generated if we don't load Foo$1 through the
56     // class reader.
57     final String test1 =
58         "public class Test1 {" +
59         "  void m() {"+
60         "    Foo f1 = new Foo();"+
61         "    Foo f2 = new Foo$1(f1) {};"+
62         "  }" +
63         "}";
64 
65     final String test2 =
66         "public class Test2 {" +
67         "  class T extends Foo$1 {" +
68         "    public T(Foo f) { super(f); }" +
69         "  }"+
70         "}";
71 
compOk(String code)72     void compOk(String code) throws Exception {
73         new JavacTask(tb)
74                 .sources(code)
75                 .run();
76     }
77 
compFail(String code)78     void compFail(String code) throws Exception {
79         String errs = new JavacTask(tb)
80                 .sources(code)
81                 .classpath(".")
82                 .options("-XDrawDiagnostics")
83                 .run(Task.Expect.FAIL)
84                 .writeAll()
85                 .getOutput(Task.OutputKind.DIRECT);
86 
87         if (!errs.contains("cant.inherit.from.anon")) {
88             throw new Exception("test failed");
89         }
90     }
91 
run()92     void run() throws Exception {
93         compOk(foo);
94         compFail(test1);
95         compFail(test2);
96     }
97 }
98