1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/AnonymousSubclassTest.java Tue Apr 15 17:09:56 2014 -0400 1.3 @@ -0,0 +1,91 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8023945 1.30 + * @summary javac wrongly allows a subclass of an anonymous class 1.31 + * @library /tools/javac/lib 1.32 + * @build ToolBox 1.33 + * @run main AnonymousSubclassTest 1.34 + */ 1.35 + 1.36 +import java.util.ArrayList; 1.37 +import java.io.IOException; 1.38 + 1.39 +public class AnonymousSubclassTest { 1.40 + public static void main(String... args) throws Exception { 1.41 + new AnonymousSubclassTest().run(); 1.42 + } 1.43 + 1.44 + // To trigger the error we want, first we need to compile 1.45 + // a class with an anonymous inner class: Foo$1. 1.46 + final String foo = 1.47 + "public class Foo {" + 1.48 + " void m() { Foo f = new Foo() {}; }" + 1.49 + "}"; 1.50 + 1.51 + // Then, we try to subclass the anonymous class 1.52 + // Note: we must do this in two classes because a different 1.53 + // error will be generated if we don't load Foo$1 through the 1.54 + // class reader. 1.55 + final String test1 = 1.56 + "public class Test1 {" + 1.57 + " void m() {"+ 1.58 + " Foo f1 = new Foo();"+ 1.59 + " Foo f2 = new Foo$1(f1) {};"+ 1.60 + " }" + 1.61 + "}"; 1.62 + 1.63 + final String test2 = 1.64 + "public class Test2 {" + 1.65 + " class T extends Foo$1 {" + 1.66 + " public T(Foo f) { super(f); }" + 1.67 + " }"+ 1.68 + "}"; 1.69 + 1.70 + void compOk(String code) throws Exception { 1.71 + ToolBox.javac(new ToolBox.JavaToolArgs().setSources(code)); 1.72 + } 1.73 + 1.74 + void compFail(String code) throws Exception { 1.75 + ArrayList<String> errors = new ArrayList<>(); 1.76 + ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs(); 1.77 + args.setSources(code) 1.78 + .appendArgs("-cp", ".", "-XDrawDiagnostics") 1.79 + .set(ToolBox.Expect.FAIL) 1.80 + .setErrOutput(errors); 1.81 + ToolBox.javac(args); 1.82 + 1.83 + if (!errors.get(0).contains("cant.inherit.from.anon")) { 1.84 + System.out.println(errors.get(0)); 1.85 + throw new Exception("test failed"); 1.86 + } 1.87 + } 1.88 + 1.89 + void run() throws Exception { 1.90 + compOk(foo); 1.91 + compFail(test1); 1.92 + compFail(test2); 1.93 + } 1.94 +}