aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8023945 aoqi@0: * @summary javac wrongly allows a subclass of an anonymous class aoqi@0: * @library /tools/javac/lib aoqi@0: * @build ToolBox aoqi@0: * @run main AnonymousSubclassTest aoqi@0: */ aoqi@0: aoqi@0: import java.util.ArrayList; aoqi@0: import java.io.IOException; aoqi@0: aoqi@0: public class AnonymousSubclassTest { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new AnonymousSubclassTest().run(); aoqi@0: } aoqi@0: aoqi@0: // To trigger the error we want, first we need to compile aoqi@0: // a class with an anonymous inner class: Foo$1. aoqi@0: final String foo = aoqi@0: "public class Foo {" + aoqi@0: " void m() { Foo f = new Foo() {}; }" + aoqi@0: "}"; aoqi@0: aoqi@0: // Then, we try to subclass the anonymous class aoqi@0: // Note: we must do this in two classes because a different aoqi@0: // error will be generated if we don't load Foo$1 through the aoqi@0: // class reader. aoqi@0: final String test1 = aoqi@0: "public class Test1 {" + aoqi@0: " void m() {"+ aoqi@0: " Foo f1 = new Foo();"+ aoqi@0: " Foo f2 = new Foo$1(f1) {};"+ aoqi@0: " }" + aoqi@0: "}"; aoqi@0: aoqi@0: final String test2 = aoqi@0: "public class Test2 {" + aoqi@0: " class T extends Foo$1 {" + aoqi@0: " public T(Foo f) { super(f); }" + aoqi@0: " }"+ aoqi@0: "}"; aoqi@0: aoqi@0: void compOk(String code) throws Exception { aoqi@0: ToolBox.javac(new ToolBox.JavaToolArgs().setSources(code)); aoqi@0: } aoqi@0: aoqi@0: void compFail(String code) throws Exception { aoqi@0: ArrayList errors = new ArrayList<>(); aoqi@0: ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs(); aoqi@0: args.setSources(code) aoqi@0: .appendArgs("-cp", ".", "-XDrawDiagnostics") aoqi@0: .set(ToolBox.Expect.FAIL) aoqi@0: .setErrOutput(errors); aoqi@0: ToolBox.javac(args); aoqi@0: aoqi@0: if (!errors.get(0).contains("cant.inherit.from.anon")) { aoqi@0: System.out.println(errors.get(0)); aoqi@0: throw new Exception("test failed"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: compOk(foo); aoqi@0: compFail(test1); aoqi@0: compFail(test2); aoqi@0: } aoqi@0: }