author | lana |
Tue, 25 Apr 2017 07:38:50 +0000 | |
changeset 44909 | b53c770dc04f |
parent 36778 | e04318f39f92 |
permissions | -rw-r--r-- |
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
1 |
/* |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
23809
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 |
26100 | 28 |
* @library /tools/lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
26100
diff
changeset
|
29 |
* @modules jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
26100
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.javac.main |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
31 |
* @build toolbox.ToolBox toolbox.JavacTask |
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
32 |
* @run main AnonymousSubclassTest |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
33 |
*/ |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
34 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
35 |
import toolbox.JavacTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
36 |
import toolbox.Task; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
37 |
import toolbox.ToolBox; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
38 |
|
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
39 |
public class AnonymousSubclassTest { |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
40 |
public static void main(String... args) throws Exception { |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
41 |
new AnonymousSubclassTest().run(); |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
42 |
} |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
43 |
|
26100 | 44 |
ToolBox tb = new ToolBox(); |
45 |
||
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
46 |
// 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
|
47 |
// a class with an anonymous inner class: Foo$1. |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
48 |
final String foo = |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
49 |
"public class Foo {" + |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
50 |
" void m() { Foo f = new Foo() {}; }" + |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
51 |
"}"; |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
52 |
|
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
53 |
// Then, we try to subclass the anonymous class |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
54 |
// 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
|
55 |
// 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
|
56 |
// class reader. |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
57 |
final String test1 = |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
58 |
"public class Test1 {" + |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
59 |
" void m() {"+ |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
60 |
" Foo f1 = new Foo();"+ |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
61 |
" Foo f2 = new Foo$1(f1) {};"+ |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
62 |
" }" + |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
63 |
"}"; |
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 |
final String test2 = |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
66 |
"public class Test2 {" + |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
67 |
" class T extends Foo$1 {" + |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
68 |
" public T(Foo f) { super(f); }" + |
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 |
|
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
72 |
void compOk(String code) throws Exception { |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
73 |
new JavacTask(tb) |
26100 | 74 |
.sources(code) |
75 |
.run(); |
|
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
76 |
} |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
77 |
|
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
78 |
void compFail(String code) throws Exception { |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
79 |
String errs = new JavacTask(tb) |
26100 | 80 |
.sources(code) |
81 |
.classpath(".") |
|
82 |
.options("-XDrawDiagnostics") |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
83 |
.run(Task.Expect.FAIL) |
26100 | 84 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
85 |
.getOutput(Task.OutputKind.DIRECT); |
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
86 |
|
26100 | 87 |
if (!errs.contains("cant.inherit.from.anon")) { |
23809
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
88 |
throw new Exception("test failed"); |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
89 |
} |
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 |
|
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
92 |
void run() throws Exception { |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
93 |
compOk(foo); |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
94 |
compFail(test1); |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
95 |
compFail(test2); |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
96 |
} |
9405883da95f
8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents:
diff
changeset
|
97 |
} |