Skip to content

Commit

Permalink
8314621: ClassNotFoundException due to lambda reference to elided ano…
Browse files Browse the repository at this point in the history
…nymous inner class

Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Nov 16, 2023
1 parent 52e2878 commit d6aa7c8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 148 deletions.
20 changes: 16 additions & 4 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,18 @@ ClassSymbol accessClass(Symbol sym, boolean protAccess, JCTree tree) {
}
}

private boolean noClassDefIn(JCTree tree) {
var scanner = new TreeScanner() {
boolean noClassDef = true;
@Override
public void visitClassDef(JCClassDecl tree) {
noClassDef = false;
}
};
scanner.scan(tree);
return scanner.noClassDef;
}

private void addPrunedInfo(JCTree tree) {
List<JCTree> infoList = prunedTree.get(currentClass);
infoList = (infoList == null) ? List.of(tree) : infoList.prepend(tree);
Expand Down Expand Up @@ -3030,10 +3042,10 @@ private Boolean expValueIsNull(boolean eq, JCTree t) {
@Override
public void visitConditional(JCConditional tree) {
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
if (isTrue(cond)) {
if (isTrue(cond) && noClassDefIn(tree.falsepart)) {
result = convert(translate(tree.truepart, tree.type), tree.type);
addPrunedInfo(cond);
} else if (isFalse(cond)) {
} else if (isFalse(cond) && noClassDefIn(tree.truepart)) {
result = convert(translate(tree.falsepart, tree.type), tree.type);
addPrunedInfo(cond);
} else {
Expand All @@ -3057,10 +3069,10 @@ private JCExpression convert(JCExpression tree, Type pt) {
*/
public void visitIf(JCIf tree) {
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
if (isTrue(cond)) {
if (isTrue(cond) && noClassDefIn(tree.elsepart)) {
result = translate(tree.thenpart);
addPrunedInfo(cond);
} else if (isFalse(cond)) {
} else if (isFalse(cond) && noClassDefIn(tree.thenpart)) {
if (tree.elsepart != null) {
result = translate(tree.elsepart);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -47,6 +47,7 @@ void run() throws Exception {
}

check(classes,
"GraphicalInstaller$1.class",
"GraphicalInstaller$1X$1.class",
"GraphicalInstaller$1X.class",
"GraphicalInstaller$BackgroundInstaller.class",
Expand Down
5 changes: 4 additions & 1 deletion test/langtools/tools/javac/6917288/T6917288.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ void test(Kind k) throws Exception {
}

switch (k) {
case NONE:
check(classesDir, "Test.class", "Test$Inner.class");
break;
case ALWAYS:
case TRUE:
check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
break;
default:
check(classesDir, "Test.class", "Test$Inner.class");
check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
}
}

Expand Down
142 changes: 0 additions & 142 deletions test/langtools/tools/javac/7199823/InnerClassCannotBeVerified.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8314621
* @summary ClassNotFoundException due to lambda reference to elided anonymous inner class
*/

public class ClassNotFoundExceptionDueToPrunedCodeTest {
public static void main(String... args) {
var o1 = false ? new Object() {} : null;
Runnable r = () -> {
System.out.println(o1 == o1);
};
r.run();

var o2 = true ? null : new Object() {};
r = () -> {
System.out.println(o2 == o2);
};
r.run();

var o3 = switch (0) { default -> { if (false) yield new Object() { }; else yield null; } };
r = () -> System.out.println(o3);
r.run();

var o4 = switch (0) { default -> { if (true) yield null; else yield new Object() { }; } };
r = () -> System.out.println(o4);
r.run();
}
}

1 comment on commit d6aa7c8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.