Skip to content

Add emulation for Java 9 additions to Objects class #10107

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions user/super/com/google/gwt/emul/java/util/Objects.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
return obj;
}

public static <T> T requireNonNullElse(T obj, T defaultObj) {
return obj != null ? obj : requireNonNull(defaultObj);
}

public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) {
return obj != null ? obj : requireNonNull(requireNonNull(supplier, "supplier").get());
}

public static int checkIndex(int index, int length) {
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + length);
Copy link
Member

Choose a reason for hiding this comment

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

I'm unsure about this exception message. Can't remember if it'll be pruned in production builds (in which case this is OK) or not (in which case we should just throw the exception without message)

Alternatively, we could emulate the IndexOufOfBoundsException(int) constructor and use it here.

Copy link
Member

@niloc132 niloc132 Mar 26, 2025

Choose a reason for hiding this comment

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

I think if we are exposing user methods to call, we should leave it up to them to prune or not - GWT might prune null checks inside of collections and such if directed, but we wouldn't want to actually rewrite if (foo == null) etc, and likewise shouldn't optimize these out?

EDIT: you're specifically talking about the error message being pruned, while I'm speaking more broadly, the runtime error at all, and the expected error message (which the developer explicitly wants).

Copy link
Member

Choose a reason for hiding this comment

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

The information should be transported to the user. Some months ago I came across an IOOBE in GWT which did not provide any information and it was annoying.

GWT already has a bunch of different exception messages for IOOBE so I am fine with the text.

I guess we would save more if GWT would provide some internal ctors or factory methods in IOOBE each providing a fixed, short error message and then use these ctors everywhere to get rid of all the different messages.

}
return index;
}

public static int checkFromToIndex(int fromIndex, int toIndex, int length) {
if (fromIndex < 0 || fromIndex > toIndex || toIndex > length) {
throw new IndexOutOfBoundsException("Range [" + fromIndex + ", " + toIndex
+ ") out of bounds for length " + length);
}
return fromIndex;
}

public static int checkFromIndexSize(int fromIndex, int size, int length) {
// in JS fromIndex + size cannot overflow because int is not limited to 32 bits
if (fromIndex < 0 || size < 0 || fromIndex + size > length) {
throw new IndexOutOfBoundsException("Range [" + fromIndex + ", " + (fromIndex + size)
+ ") out of bounds for length " + length);
}
return fromIndex;
}

public static String toString(Object o) {
return String.valueOf(o);
}
Expand Down
74 changes: 74 additions & 0 deletions user/test/com/google/gwt/emultest/java/util/ObjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,78 @@ public void testHashCode() {
Object obj = new Object();
assertEquals(obj.hashCode(), Objects.hashCode(obj));
}

public void testRequireNonNull() {
assertEquals("foo", Objects.requireNonNull(hideFromCompiler("foo")));
assertThrows(NullPointerException.class,
() -> Objects.requireNonNull(hideFromCompiler(null)));
}

public void testRequireNonNullElse() {
assertEquals("foo", Objects.requireNonNullElse(hideFromCompiler("foo"), "bar"));
assertEquals("bar", Objects.requireNonNullElse(hideFromCompiler(null), "bar"));
assertThrows(NullPointerException.class,
() -> Objects.requireNonNullElse(hideFromCompiler(null), null));
}

public void testRequireNonNullElseGet() {
assertEquals("foo",
Objects.requireNonNullElseGet(hideFromCompiler("foo"), () -> "bar"));
assertEquals("bar",
Objects.requireNonNullElseGet(hideFromCompiler(null), () -> "bar"));
assertThrows(NullPointerException.class,
() -> Objects.requireNonNullElseGet(hideFromCompiler(null), null));
assertThrows(NullPointerException.class,
() -> Objects.requireNonNullElseGet(hideFromCompiler(null), () -> null));
}

private String hideFromCompiler(String value) {
if (Math.random() > 2) {
return "unreachable";
}
return value;
}

public void testCheckIndex() {
assertEquals(5, Objects.checkIndex(5, 10));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkIndex(-5, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkIndex(10, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkIndex(5, 5));
}

public void testCheckFromToIndex() {
assertEquals(5, Objects.checkFromToIndex(5, 7, 10));
assertEquals(0, Objects.checkFromToIndex(0, 10, 10));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromToIndex(-5, 1, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromToIndex(10, 1, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromToIndex(1, 10, 5));
}

public void testCheckFromIndexSize() {
assertEquals(5, Objects.checkFromIndexSize(5, 2, 10));
assertEquals(0, Objects.checkFromIndexSize(0, 10, 10));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromIndexSize(-5, 1, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromIndexSize(10, 1, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromIndexSize(1, 10, 5));
assertThrows(IndexOutOfBoundsException.class,
() -> Objects.checkFromIndexSize(1, -5, 5));
}

private void assertThrows(Class<? extends Exception> thrownCheck, Runnable toTest) {
try {
toTest.run();
fail("Should have failed");
} catch (Exception ex) {
assertEquals(thrownCheck, ex.getClass());
}
}
}