Skip to content

Commit d6aa7c8

Browse files
author
Vicente Romero
committed
8314621: ClassNotFoundException due to lambda reference to elided anonymous inner class
Reviewed-by: jlahoda
1 parent 52e2878 commit d6aa7c8

File tree

5 files changed

+74
-148
lines changed

5 files changed

+74
-148
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,18 @@ ClassSymbol accessClass(Symbol sym, boolean protAccess, JCTree tree) {
11961196
}
11971197
}
11981198

1199+
private boolean noClassDefIn(JCTree tree) {
1200+
var scanner = new TreeScanner() {
1201+
boolean noClassDef = true;
1202+
@Override
1203+
public void visitClassDef(JCClassDecl tree) {
1204+
noClassDef = false;
1205+
}
1206+
};
1207+
scanner.scan(tree);
1208+
return scanner.noClassDef;
1209+
}
1210+
11991211
private void addPrunedInfo(JCTree tree) {
12001212
List<JCTree> infoList = prunedTree.get(currentClass);
12011213
infoList = (infoList == null) ? List.of(tree) : infoList.prepend(tree);
@@ -3030,10 +3042,10 @@ private Boolean expValueIsNull(boolean eq, JCTree t) {
30303042
@Override
30313043
public void visitConditional(JCConditional tree) {
30323044
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
3033-
if (isTrue(cond)) {
3045+
if (isTrue(cond) && noClassDefIn(tree.falsepart)) {
30343046
result = convert(translate(tree.truepart, tree.type), tree.type);
30353047
addPrunedInfo(cond);
3036-
} else if (isFalse(cond)) {
3048+
} else if (isFalse(cond) && noClassDefIn(tree.truepart)) {
30373049
result = convert(translate(tree.falsepart, tree.type), tree.type);
30383050
addPrunedInfo(cond);
30393051
} else {
@@ -3057,10 +3069,10 @@ private JCExpression convert(JCExpression tree, Type pt) {
30573069
*/
30583070
public void visitIf(JCIf tree) {
30593071
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
3060-
if (isTrue(cond)) {
3072+
if (isTrue(cond) && noClassDefIn(tree.elsepart)) {
30613073
result = translate(tree.thenpart);
30623074
addPrunedInfo(cond);
3063-
} else if (isFalse(cond)) {
3075+
} else if (isFalse(cond) && noClassDefIn(tree.thenpart)) {
30643076
if (tree.elsepart != null) {
30653077
result = translate(tree.elsepart);
30663078
} else {

test/langtools/tools/javac/6917288/GraphicalInstallerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,6 +47,7 @@ void run() throws Exception {
4747
}
4848

4949
check(classes,
50+
"GraphicalInstaller$1.class",
5051
"GraphicalInstaller$1X$1.class",
5152
"GraphicalInstaller$1X.class",
5253
"GraphicalInstaller$BackgroundInstaller.class",

test/langtools/tools/javac/6917288/T6917288.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ void test(Kind k) throws Exception {
7070
}
7171

7272
switch (k) {
73+
case NONE:
74+
check(classesDir, "Test.class", "Test$Inner.class");
75+
break;
7376
case ALWAYS:
7477
case TRUE:
7578
check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
7679
break;
7780
default:
78-
check(classesDir, "Test.class", "Test$Inner.class");
81+
check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
7982
}
8083
}
8184

test/langtools/tools/javac/7199823/InnerClassCannotBeVerified.java

Lines changed: 0 additions & 142 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2023, 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 8314621
27+
* @summary ClassNotFoundException due to lambda reference to elided anonymous inner class
28+
*/
29+
30+
public class ClassNotFoundExceptionDueToPrunedCodeTest {
31+
public static void main(String... args) {
32+
var o1 = false ? new Object() {} : null;
33+
Runnable r = () -> {
34+
System.out.println(o1 == o1);
35+
};
36+
r.run();
37+
38+
var o2 = true ? null : new Object() {};
39+
r = () -> {
40+
System.out.println(o2 == o2);
41+
};
42+
r.run();
43+
44+
var o3 = switch (0) { default -> { if (false) yield new Object() { }; else yield null; } };
45+
r = () -> System.out.println(o3);
46+
r.run();
47+
48+
var o4 = switch (0) { default -> { if (true) yield null; else yield new Object() { }; } };
49+
r = () -> System.out.println(o4);
50+
r.run();
51+
}
52+
}

0 commit comments

Comments
 (0)