Skip to content

Commit

Permalink
Update Collections to support changes made in Java 9 (#9833)
Browse files Browse the repository at this point in the history
See earlier review at https://gwt-review.googlesource.com/c/gwt/+/21501.

Build changes get us ready for missing Java 10 and 11 emulation as
well.

Partial #9547
  • Loading branch information
niloc132 authored Jun 19, 2023
1 parent 0e61ca3 commit 3250dca
Show file tree
Hide file tree
Showing 10 changed files with 966 additions and 8 deletions.
46 changes: 38 additions & 8 deletions user/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
<property name="emma.merged.out" value="${junit.out}/emma-coverage"/>

<property name="gwt.junit.testcase.web.includes" value="${gwt.junit.testcase.includes}"/>
<!-- TODO once we compile with Java >8, update this to exclude tests based on running JDK version -->
<condition property="gwt.junit.testcase.web.excludes"
value="**/*JreSuite.class,**/OptimizedOnly*,**/*Java8Suite.class"
else="**/*JreSuite.class,**/OptimizedOnly*,**/*Java8Suite.class">
value="**/*JreSuite.class,**/OptimizedOnly*"
else="**/*JreSuite.class,**/OptimizedOnly*,**/*Java9Suite.class,**/*Java10Suite.class,**/*Java11Suite.class">
<isfalse value="${isJava8}" />
</condition>

Expand All @@ -31,10 +30,9 @@
</condition>

<property name="gwt.junit.testcase.dev.includes" value="${gwt.junit.testcase.includes}"/>
<!-- TODO once we compile with Java >8, update this to exclude tests based on running JDK version -->
<condition property="gwt.junit.testcase.dev.excludes"
value="**/EmulSuite.class,**/JSONSuite.class,**/RunAsyncSuite.class,**/*CompilerSuite.class,**/*JsInteropSuite.class,**/*JreSuite.class,**/OptimizedOnly*,**/*Java8Suite.class"
else="**/EmulSuite.class,**/JSONSuite.class,**/RunAsyncSuite.class,**/*CompilerSuite.class,**/*JsInteropSuite.class,**/*JreSuite.class,**/OptimizedOnly*,**/*Java8Suite.class">
value="**/EmulSuite.class,**/JSONSuite.class,**/RunAsyncSuite.class,**/*CompilerSuite.class,**/*JsInteropSuite.class,**/*JreSuite.class,**/OptimizedOnly*"
else="**/EmulSuite.class,**/JSONSuite.class,**/RunAsyncSuite.class,**/*CompilerSuite.class,**/*JsInteropSuite.class,**/*JreSuite.class,**/OptimizedOnly*,**/*Java9Suite.class,**/*Java10Suite.class,**/*Java11Suite.class">
<isfalse value="${isJava8}" />
</condition>

Expand Down Expand Up @@ -158,13 +156,13 @@
Compiles the test code for this project
-->
<target name="compile.tests"
depends="-compile.tests.java8"
depends="-compile.tests.java8,-compile.tests.java11"
unless="compile.tests.complete">
</target>

<target name="-compile.tests.java8" depends="compile.dev.tests, compile.emma.if.enabled">
<gwt.javac srcdir="test" destdir="${javac.junit.out}"
excludes="com/google/gwt/langtest/**,**/super/**"
excludes="com/google/gwt/langtest/**,**/super/**,**/java9/**,**/*Java9*.java,**/java10/**,**/*Java10*.java,**/java11/**,**/*Java11*.java"
processorpath="test.extraclasspath"
errorprone.args="
-Xep:SelfAssignment:OFF
Expand Down Expand Up @@ -194,6 +192,38 @@
<compilerarg value="com.google.web.bindery.requestfactory.apt.RfValidator"/>
</gwt.javac>
</target>
<target name="-compile.tests.java11" unless="isJava8" depends="-compile.tests.java8">
<gwt.javac srcdir="test" destdir="${javac.junit.out}"
excludes="com/google/gwt/langtest/**,**/super/**"
processorpath="test.extraclasspath"
errorprone.args="
-Xep:SelfAssignment:OFF
-Xep:MissingCasesInEnumSwitch:OFF
-Xep:SelfComparison:OFF
-Xep:SelfEquals:OFF
-Xep:FallThrough:OFF
-Xep:ReturnValueIgnored:OFF
-Xep:EqualsIncompatibleType:OFF
-Xep:IdentityBinaryExpression:OFF
-Xep:LoopConditionChecker:OFF
-Xep:JUnitAssertSameCheck:OFF
-Xep:CollectionIncompatibleType:OFF
-Xep:DeadThread:OFF
-Xep:ComplexBooleanConstant:OFF
-Xep:ComparableType:OFF
-Xep:DoNotCall:OFF
-Xep:BadAnnotationImplementation:OFF
">
<classpath>
<pathelement location="${javac.junit.out}"/>
<pathelement location="${javac.out}"/>
<pathelement location="${gwt.tools.lib}/junit/junit-4.8.2.jar"/>
<pathelement location="${gwt.tools.lib}/selenium/selenium-java-client-driver.jar"/>
<path refid="test.extraclasspath"/>
</classpath>
</gwt.javac>

</target>

<target name="build" depends="compile"
description="Build and package this project">
Expand Down
80 changes: 80 additions & 0 deletions user/super/com/google/gwt/emul/java/util/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static javaemul.internal.InternalPreconditions.checkNotNull;

import java.util.function.UnaryOperator;
import javaemul.internal.ArrayHelper;

import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsMethod;
Expand All @@ -34,6 +35,85 @@
@JsType
public interface List<E> extends Collection<E> {

@JsIgnore
static <E> List<E> of() {
return Collections.unmodifiableList(Collections.emptyList());
}

@JsIgnore
static <E> List<E> of(E e1) {
return __ofInternal((E[]) new Object[] {e1});
}

@JsIgnore
static <E> List<E> of(E e1, E e2) {
return __ofInternal((E[]) new Object[] {e1, e2});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3) {
return __ofInternal((E[]) new Object[] {e1, e2, e3});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4, e5});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4, e5, e6});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8, e9});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
return __ofInternal((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8, e9, e10});
}

// CHECKSTYLE_OFF: Internal only method that cannot collide with future JRE changes
/**
* Internal-only helper to avoid copying the incoming array, and instead just wrap it with an
* immutable List after checking there are no nulls.
*/
@JsIgnore
static <E> List<E> __ofInternal(E[] elements) {
for (int i = 0; i < elements.length; i++) {
checkNotNull(elements[i]);
}
return Collections.unmodifiableList(Arrays.asList(elements));
}
// CHECKSTYLE_ON

@JsIgnore
static <E> List<E> of(E... elements) {
for (int i = 0; i < elements.length; i++) {
checkNotNull(elements[i]);
}
return Collections.unmodifiableList(
Arrays.asList((E[]) ArrayHelper.unsafeClone(elements, 0, elements.length))
);
}

@JsMethod(name = "addAtIndex")
void add(int index, E element);

Expand Down
206 changes: 206 additions & 0 deletions user/super/com/google/gwt/emul/java/util/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkArgument;
import static javaemul.internal.InternalPreconditions.checkNotNull;

import java.io.Serializable;
Expand All @@ -35,6 +36,211 @@
@JsType
public interface Map<K, V> {

@JsIgnore
static <K, V> Map<K, V> of() {
return Collections.unmodifiableMap(Collections.emptyMap());
}

@JsIgnore
static <K, V> Map<K, V> of(K key, V value) {
return ofEntries(
entry(key, value)
);
}

@JsIgnore
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
return ofEntries(
entry(k1, v1),
entry(k2, v2)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7,
K k8, V v8
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7),
entry(k8, v8)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7,
K k8, V v8,
K k9, V v9
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7),
entry(k8, v8),
entry(k9, v9)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7,
K k8, V v8,
K k9, V v9,
K k10, V v10
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7),
entry(k8, v8),
entry(k9, v9),
entry(k10, v10)
);
}

@JsIgnore
static <K, V> Entry<K, V> entry(K key, V value) {
// This isn't quite consistent with the javadoc, since this is serializable, while entry()
// need not be serializable.
return new AbstractMap.SimpleImmutableEntry<>(
checkNotNull(key),
checkNotNull(value)
);
}

@JsIgnore
static <K, V> Map<K, V> ofEntries(Entry<? extends K,? extends V>... entries) {
Map<K, V> map = new HashMap<>();

for (int i = 0; i < entries.length; i++) {
// TODO this perhaps can be optimized if we know the entry is an instance of
// AbstractMap.SimpleImmutableEntry, or something more specialized?
Entry<? extends K, ? extends V> entry = checkNotNull(entries[i]);
checkArgument(map.put(checkNotNull(entry.getKey()), checkNotNull(entry.getValue())) == null,
"Can't add multiple entries with the same key");
}

return Collections.unmodifiableMap(map);
}

/**
* Represents an individual map entry.
*/
Expand Down
Loading

0 comments on commit 3250dca

Please sign in to comment.