-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8164714: Constructor.newInstance creates instance of inner class with null outer class #23875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3ba5e71
3778a3c
9251340
3954341
682bd6f
76ff686
6729faa
d2e10cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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. | ||
| */ | ||
|
|
||
| import java.lang.invoke.MethodHandle; | ||
| import java.lang.invoke.MethodHandles; | ||
| import java.lang.invoke.MethodType; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 8164714 | ||
| * @summary No null check for immediate enclosing instance for VM/reflective | ||
| * invocation of inner classes for older versions or on request | ||
| * | ||
| * @clean * | ||
| * @compile -XDnullCheckOuterThis=false NoOuterThisNullChecks.java | ||
| * @run junit NoOuterThisNullChecks | ||
| * | ||
| * @clean * | ||
| * @compile --release 17 NoOuterThisNullChecks.java | ||
| * @run junit NoOuterThisNullChecks | ||
| */ | ||
| class NoOuterThisNullChecks { | ||
| static Stream<Class<?>> testClasses() { | ||
| return Stream.of(NoOuterThis.class, OuterThisField.class); | ||
| } | ||
|
|
||
| @MethodSource("testClasses") | ||
| @ParameterizedTest | ||
| void testNoOuter(Class<?> clz) { | ||
| assertDoesNotThrow(() -> clz.getDeclaredConstructor(NoOuterThisNullChecks.class).newInstance((Object) null)); | ||
|
|
||
| MethodHandle mh = assertDoesNotThrow(() -> MethodHandles.lookup().findConstructor(clz, MethodType.methodType(void.class, NoOuterThisNullChecks.class))) | ||
| .asType(MethodType.methodType(Object.class, Object.class)); | ||
| assertDoesNotThrow(() -> { | ||
| Object stub = mh.invokeExact((Object) null); | ||
| }); | ||
| } | ||
|
|
||
| class NoOuterThis {} | ||
| class OuterThisField { | ||
| @Override | ||
| public String toString() { | ||
| return "outer this = " + NoOuterThisNullChecks.this; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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. | ||
| */ | ||
|
|
||
| import java.lang.invoke.MethodHandle; | ||
| import java.lang.invoke.MethodHandles; | ||
| import java.lang.invoke.MethodType; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 8164714 | ||
| * @summary Null check for immediate enclosing instance for VM/reflective | ||
| * invocation of inner classes | ||
| * | ||
| * @clean * | ||
| * @compile OuterThisNullChecks.java | ||
| * @run junit OuterThisNullChecks | ||
| * | ||
| * @clean * | ||
| * @compile --release 17 -XDnullCheckOuterThis=true OuterThisNullChecks.java | ||
| * @run junit OuterThisNullChecks | ||
| */ | ||
| class OuterThisNullChecks { | ||
| static Stream<Class<?>> testClasses() { | ||
| return Stream.of(NoOuterThis.class, OuterThisField.class); | ||
| } | ||
|
|
||
| @MethodSource("testClasses") | ||
| @ParameterizedTest | ||
| void testNoOuter(Class<?> clz) { | ||
| var ite = assertThrows(InvocationTargetException.class, () -> clz.getDeclaredConstructor(OuterThisNullChecks.class).newInstance((Object) null)); | ||
| assertInstanceOf(NullPointerException.class, ite.getCause()); | ||
|
|
||
| MethodHandle mh = assertDoesNotThrow(() -> MethodHandles.lookup().findConstructor(clz, MethodType.methodType(void.class, OuterThisNullChecks.class))) | ||
| .asType(MethodType.methodType(Object.class, Object.class)); | ||
| assertThrows(NullPointerException.class, () -> { | ||
| Object stub = mh.invokeExact((Object) null); | ||
| }); | ||
| } | ||
|
|
||
| class NoOuterThis {} | ||
| class OuterThisField { | ||
| @Override | ||
| public String toString() { | ||
| return "outer this = " + OuterThisNullChecks.this; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2009, 2025, 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 | ||
|
|
@@ -57,7 +57,8 @@ void run() throws Exception { | |
| "java/lang/Object", | ||
| "java/lang/String", | ||
| "T6887895", | ||
| "T6887895$Test" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why this test is changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now Objects is used for requireNonNull There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should then add the bug id to the test |
||
| "T6887895$Test", | ||
| "java/util/Objects", | ||
| }; | ||
|
|
||
| Set<String> expect = new TreeSet<String>(Arrays.asList(expectNames)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why changing this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CP differences when Objects.requireNonNull is emitted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then we should add the bug id to the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noted https://bugs.openjdk.org/browse/JDK-8292275 also changed many tests that this patch has changed but did not add the bug id. Is the bug id only to indicate which bug's regression is a test testing against?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jtreg supports the -bug: option to run matching tests.
Adding the bugid is an aid in verifying bugs. If it was missed being added earlier it can be added now. But it is unlikely it will be missed by anyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this test is associated to the inner class feature - it is just that the outputs this test is testing against is too closely tied to irrelevant implementation details.