8023945: javac wrongly allows a subclass of an anonymous class

Tue, 15 Apr 2014 17:09:56 -0400

author
pgovereau
date
Tue, 15 Apr 2014 17:09:56 -0400
changeset 2361
d75c4adbc698
parent 2360
9d2a88a9e69a
child 2363
7ad480b982bf

8023945: javac wrongly allows a subclass of an anonymous class
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/resources/compiler.properties file | annotate | diff | comparison | revisions
test/tools/javac/AnonymousSubclassTest.java file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples.not-yet.txt file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Apr 11 09:36:46 2014 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Apr 15 17:09:56 2014 -0400
     1.3 @@ -812,6 +812,10 @@
     1.4                     boolean classExpected,
     1.5                     boolean interfaceExpected,
     1.6                     boolean checkExtensible) {
     1.7 +        if (t.tsym.isAnonymous()) {
     1.8 +            log.error(tree.pos(), "cant.inherit.from.anon");
     1.9 +            return types.createErrorType(t);
    1.10 +        }
    1.11          if (t.isErroneous())
    1.12              return t;
    1.13          if (t.hasTag(TYPEVAR) && !classExpected && !interfaceExpected) {
     2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 11 09:36:46 2014 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Apr 15 17:09:56 2014 -0400
     2.3 @@ -123,6 +123,9 @@
     2.4  compiler.err.anon.class.impl.intf.no.qual.for.new=\
     2.5      anonymous class implements interface; cannot have qualifier for new
     2.6  
     2.7 +compiler.err.cant.inherit.from.anon=\
     2.8 +    cannot inherit from anonymous class
     2.9 +
    2.10  # 0: symbol, 1: symbol, 2: symbol
    2.11  compiler.err.array.and.varargs=\
    2.12      cannot declare both {0} and {1} in {2}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/AnonymousSubclassTest.java	Tue Apr 15 17:09:56 2014 -0400
     3.3 @@ -0,0 +1,91 @@
     3.4 +/*
     3.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 8023945
    3.30 + * @summary javac wrongly allows a subclass of an anonymous class
    3.31 + * @library /tools/javac/lib
    3.32 + * @build ToolBox
    3.33 + * @run main AnonymousSubclassTest
    3.34 + */
    3.35 +
    3.36 +import java.util.ArrayList;
    3.37 +import java.io.IOException;
    3.38 +
    3.39 +public class AnonymousSubclassTest {
    3.40 +    public static void main(String... args) throws Exception {
    3.41 +        new AnonymousSubclassTest().run();
    3.42 +    }
    3.43 +
    3.44 +    // To trigger the error we want, first we need to compile
    3.45 +    // a class with an anonymous inner class: Foo$1.
    3.46 +    final String foo =
    3.47 +        "public class Foo {" +
    3.48 +        "  void m() { Foo f = new Foo() {}; }" +
    3.49 +        "}";
    3.50 +
    3.51 +    // Then, we try to subclass the anonymous class
    3.52 +    // Note: we must do this in two classes because a different
    3.53 +    // error will be generated if we don't load Foo$1 through the
    3.54 +    // class reader.
    3.55 +    final String test1 =
    3.56 +        "public class Test1 {" +
    3.57 +        "  void m() {"+
    3.58 +        "    Foo f1 = new Foo();"+
    3.59 +        "    Foo f2 = new Foo$1(f1) {};"+
    3.60 +        "  }" +
    3.61 +        "}";
    3.62 +
    3.63 +    final String test2 =
    3.64 +        "public class Test2 {" +
    3.65 +        "  class T extends Foo$1 {" +
    3.66 +        "    public T(Foo f) { super(f); }" +
    3.67 +        "  }"+
    3.68 +        "}";
    3.69 +
    3.70 +    void compOk(String code) throws Exception {
    3.71 +        ToolBox.javac(new ToolBox.JavaToolArgs().setSources(code));
    3.72 +    }
    3.73 +
    3.74 +    void compFail(String code) throws Exception {
    3.75 +        ArrayList<String> errors = new ArrayList<>();
    3.76 +        ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
    3.77 +        args.setSources(code)
    3.78 +            .appendArgs("-cp", ".", "-XDrawDiagnostics")
    3.79 +            .set(ToolBox.Expect.FAIL)
    3.80 +            .setErrOutput(errors);
    3.81 +        ToolBox.javac(args);
    3.82 +
    3.83 +        if (!errors.get(0).contains("cant.inherit.from.anon")) {
    3.84 +            System.out.println(errors.get(0));
    3.85 +            throw new Exception("test failed");
    3.86 +        }
    3.87 +    }
    3.88 +
    3.89 +    void run() throws Exception {
    3.90 +        compOk(foo);
    3.91 +        compFail(test1);
    3.92 +        compFail(test2);
    3.93 +    }
    3.94 +}
     4.1 --- a/test/tools/javac/diags/examples.not-yet.txt	Fri Apr 11 09:36:46 2014 -0700
     4.2 +++ b/test/tools/javac/diags/examples.not-yet.txt	Tue Apr 15 17:09:56 2014 -0400
     4.3 @@ -110,4 +110,4 @@
     4.4  compiler.warn.unknown.enum.constant                     # in bad class file
     4.5  compiler.warn.unknown.enum.constant.reason              # in bad class file
     4.6  compiler.warn.override.equals.but.not.hashcode          # when a class overrides equals but not hashCode method from Object
     4.7 -
     4.8 +compiler.err.cant.inherit.from.anon                     # error for subclass of anonymous class

mercurial