Skip to content

Commit 39c2d16

Browse files
committed
Roll back Objects.rNN for now
1 parent 807f3d3 commit 39c2d16

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

src/java.base/share/classes/java/util/Objects.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525

2626
package java.util;
2727

28-
import jdk.internal.access.SharedSecrets;
2928
import jdk.internal.util.Preconditions;
30-
import jdk.internal.vm.annotation.DontInline;
3129
import jdk.internal.vm.annotation.ForceInline;
32-
import jdk.internal.vm.annotation.Hidden;
3330

3431
import java.util.function.Supplier;
3532

@@ -220,18 +217,10 @@ public static <T> int compare(T a, T b, Comparator<? super T> c) {
220217
@ForceInline
221218
public static <T> T requireNonNull(T obj) {
222219
if (obj == null)
223-
throw extendedNullPointerException();
220+
throw new NullPointerException();
224221
return obj;
225222
}
226223

227-
/// Squeeze code here to avoid blowing up ForceInline above with two calls
228-
@Hidden
229-
@DontInline
230-
private static NullPointerException extendedNullPointerException() {
231-
// 1 offset for explicit requireNonNull, 0 slot for incoming argument
232-
return SharedSecrets.getJavaLangAccess().extendedNullPointerException(1, 0);
233-
}
234-
235224
/**
236225
* Checks that the specified object reference is not {@code null} and
237226
* throws a customized {@link NullPointerException} if it is. This method

test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/MethodParametersTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @summary Test the MethodParameters-based NPE messages.
2727
* @bug 8233268
2828
* @library /test/lib
29+
* @modules java.base/jdk.internal.access
2930
* @clean MethodParametersTest InnerClass
3031
* @compile -parameters -g:none MethodParametersTest.java
3132
* @run junit/othervm -XX:+ShowCodeDetailsInExceptionMessages MethodParametersTest
@@ -34,14 +35,26 @@
3435
import java.lang.reflect.InvocationTargetException;
3536
import java.util.Objects;
3637

38+
import jdk.internal.access.JavaLangAccess;
39+
import jdk.internal.access.SharedSecrets;
40+
import org.junit.jupiter.api.Disabled;
3741
import org.junit.jupiter.api.Test;
3842

3943
import static org.junit.jupiter.api.Assertions.*;
4044

4145
public class MethodParametersTest {
46+
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
47+
48+
// An arbitrary null-checking API
49+
static void nullCheck(Object arg) {
50+
if (arg == null) {
51+
throw JLA.extendedNullPointerException(1, 0);
52+
}
53+
}
4254

4355
class InnerClass {}
4456

57+
@Disabled("Requires javac's API support")
4558
@Test
4659
void testOuterThis() {
4760
var npe = assertThrows(NullPointerException.class, () -> {
@@ -54,10 +67,10 @@ void testOuterThis() {
5467
assertEquals("\"this$0\" is null", npe.getMessage());
5568
}
5669

57-
// Random slot to param index mappings, both raw and requireNonNull NPEs
70+
// Random slot to param index mappings, both raw and null-check API NPEs
5871
// 0, 1, 3, 5
5972
static void myMethod(String firstArg, long l1, double l2, int[] lastArg) {
60-
Objects.requireNonNull(firstArg);
73+
nullCheck(firstArg);
6174
System.out.println(lastArg.length);
6275
Object a = l1 > 100 ? null : ""; // 6
6376
a.toString();

test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/RequireNonNullTest.java renamed to test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/NullCheckAPITest.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,48 @@
2323

2424
/*
2525
* @test
26-
* @summary Test the messages for 1-arg requireNonNull.
26+
* @summary Test the messages for arbitrary null-check APIs.
2727
* @bug 8233268
2828
* @library /test/lib
29-
* @compile -g RequireNonNullTest.java
30-
* @run junit/othervm -XX:+ShowCodeDetailsInExceptionMessages RequireNonNullTest
29+
* @modules java.base/jdk.internal.access
30+
* @compile -g NullCheckAPITest.java
31+
* @run junit/othervm -DnullCheckAPI.nestedThrow=true -XX:+ShowCodeDetailsInExceptionMessages NullCheckAPITest
32+
* @run junit/othervm -DnullCheckAPI.nestedThrow=false -XX:+ShowCodeDetailsInExceptionMessages NullCheckAPITest
3133
*/
3234

3335
import java.lang.reflect.InvocationTargetException;
3436
import java.lang.reflect.ParameterizedType;
35-
import java.util.Objects;
3637

38+
import jdk.internal.access.JavaLangAccess;
39+
import jdk.internal.access.SharedSecrets;
40+
import org.junit.jupiter.api.Disabled;
3741
import org.junit.jupiter.api.Test;
3842
import org.junit.jupiter.api.function.Executable;
3943

4044
import static org.junit.jupiter.api.Assertions.assertEquals;
4145
import static org.junit.jupiter.api.Assertions.assertThrows;
4246

43-
public class RequireNonNullTest {
47+
public class NullCheckAPITest {
48+
49+
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
50+
private static final boolean NESTED_THROW = Boolean.getBoolean("nullCheckAPI.nestedThrow");
51+
52+
// An arbitrary null-checking API
53+
static void nullCheck(Object arg) {
54+
if (arg == null) {
55+
if (NESTED_THROW) {
56+
// 2 offset: nullCheck, throwNpe;
57+
throwNpe();
58+
} else {
59+
// 1 offset: nullCheck
60+
throw JLA.extendedNullPointerException(1, 0);
61+
}
62+
}
63+
}
64+
65+
static void throwNpe() {
66+
throw JLA.extendedNullPointerException(2, 0);
67+
}
4468

4569
/// A simple NPE message for an expression
4670
static String simpleMessage(String cause) {
@@ -64,32 +88,38 @@ static void checkInvocationMessage(Executable action, String cause) {
6488

6589
class Dummy { Object field; }
6690

91+
@Test
92+
void test() {
93+
checkSimpleMessage(() -> generateVariableNpe(null), "myA");
94+
checkSimpleMessage(() -> generateVariableNpe(new Dummy()), "myA.field");
95+
96+
checkInvocationMessage(() -> nullCheck(int.class.getSuperclass()), "java.lang.Class.getSuperclass()");
97+
}
98+
6799
static class One extends Dummy {
68-
One(RequireNonNullTest rnnt) {
100+
One(NullCheckAPITest rnnt) {
69101
rnnt.super();
70102
}
71103
}
72104

73105
@Test
74-
void test() {
75-
checkSimpleMessage(() -> generateVariableNpe(null), "myA");
76-
checkSimpleMessage(() -> generateVariableNpe(new Dummy()), "myA.field");
106+
@Disabled("Requires javac's API support")
107+
void testRequireNonNull() {
77108
checkSimpleMessage(() -> {
78-
RequireNonNullTest t = null;
109+
NullCheckAPITest t = null;
79110
t.new Dummy();
80111
}, "t");
81112
checkSimpleMessage(() -> new One(null), "rnnt");
82113

83114
var npe = assertThrows(NullPointerException.class, () -> {
84115
try {
85-
Dummy.class.getDeclaredConstructor(RequireNonNullTest.class).newInstance((Object) null);
116+
Dummy.class.getDeclaredConstructor(NullCheckAPITest.class).newInstance((Object) null);
86117
} catch (InvocationTargetException ex) {
87118
throw ex.getCause();
88119
}
89120
});
90121
assertEquals("\"this$0\" is null", npe.getMessage());
91122

92-
checkInvocationMessage(() -> Objects.requireNonNull(int.class.getSuperclass()), "java.lang.Class.getSuperclass()");
93123
checkInvocationMessage(() -> {
94124
switch (int.class.getGenericSuperclass()) {
95125
case ParameterizedType pt -> {}
@@ -101,7 +131,7 @@ void test() {
101131

102132
// A method that generate NPE from variables
103133
static void generateVariableNpe(Dummy myA) {
104-
Objects.requireNonNull(myA);
105-
Objects.requireNonNull(myA.field);
134+
nullCheck(myA);
135+
nullCheck(myA.field);
106136
}
107137
}

test/jdk/java/util/Objects/BasicObjectsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private static int testRequireNonNull() {
208208
errors += testRNN_NonNull(rnn2, RNN_2);
209209
errors += testRNN_NonNull(rnn3, RNN_3);
210210

211-
// No message constraint for 1-arg
211+
errors += testRNN_Null(rnn1, RNN_1, null);
212212
errors += testRNN_Null(rnn2, RNN_2, "trousers");
213213
errors += testRNN_Null(rnn3, RNN_3, "trousers");
214214
return errors;

0 commit comments

Comments
 (0)