Skip to content

Commit

Permalink
Remove Enums from guava-gwt at compile time.
Browse files Browse the repository at this point in the history
Previously, parts of it were present but failed at runtime when used under J2CL. And soon, J2CL may drop support for the reflective enum-related APIs that it depends on.

`guava-gwt` is used by both GWT and J2CL users, so we have to keep our code buildable against both.

If this CL causes trouble for you as a GWT user, let us know, and we can investigate options further. In the worst case, you can copy the implementations as needed, since the class contains only static methods.

RELNOTES=`collect`: Removed `Enums` from `guava-gwt` to prepare for the removal of reflective enum-related APIs from J2CL. If this causes you problems as a GWT user, let us know.
PiperOrigin-RevId: 535585561
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 26, 2023
1 parent feb83a1 commit c3a155d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
10 changes: 7 additions & 3 deletions android/guava-tests/test/com/google/common/base/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.collect.ImmutableList;
Expand All @@ -45,7 +44,7 @@
*
* @author Steve McKay
*/
@GwtCompatible(emulated = true)
@GwtIncompatible
@J2ktIncompatible
public class EnumsTest extends TestCase {

Expand Down Expand Up @@ -117,6 +116,7 @@ private WeakReference<?> doTestClassUnloading() throws Exception {
return new WeakReference<>(shadowLoader);
}

@GwtIncompatible // stringConverter
public void testStringConverter_convert() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertEquals(TestEnum.CHEETO, converter.convert("CHEETO"));
Expand All @@ -126,6 +126,7 @@ public void testStringConverter_convert() {
assertNull(converter.reverse().convert(null));
}

@GwtIncompatible // stringConverter
public void testStringConverter_convertError() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
try {
Expand All @@ -135,6 +136,7 @@ public void testStringConverter_convertError() {
}
}

@GwtIncompatible // stringConverter
public void testStringConverter_reverse() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertEquals("CHEETO", converter.reverse().convert(TestEnum.CHEETO));
Expand All @@ -143,13 +145,14 @@ public void testStringConverter_reverse() {
}

@J2ktIncompatible
@GwtIncompatible // NullPointerTester
@GwtIncompatible // stringConverter
public void testStringConverter_nullPointerTester() throws Exception {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicInstanceMethods(converter);
}

@GwtIncompatible // stringConverter
public void testStringConverter_nullConversions() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertNull(converter.convert(null));
Expand All @@ -164,6 +167,7 @@ public void testStringConverter_toString() {
Enums.stringConverter(TestEnum.class).toString());
}

@GwtIncompatible // stringConverter
public void testStringConverter_serialization() {
SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class));
}
Expand Down
5 changes: 3 additions & 2 deletions android/guava/src/com/google/common/base/Enums.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import java.io.Serializable;
Expand All @@ -34,7 +33,7 @@
* @author Steve McKay
* @since 9.0
*/
@GwtCompatible(emulated = true)
@GwtIncompatible
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
public final class Enums {
Expand Down Expand Up @@ -108,10 +107,12 @@ static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnum
*
* @since 16.0
*/
@GwtIncompatible
public static <T extends Enum<T>> Converter<String, T> stringConverter(Class<T> enumClass) {
return new StringConverter<>(enumClass);
}

@GwtIncompatible
private static final class StringConverter<T extends Enum<T>> extends Converter<String, T>
implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ static CharMatcher precomputeCharMatcher(CharMatcher matcher) {
return matcher;
}

static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
try {
return Optional.of(Enum.valueOf(enumClass, value));
} catch (IllegalArgumentException iae) {
return Optional.absent();
}
}

static String formatCompact4Digits(double value) {
return "" + ((Number) (Object) value).toPrecision(4);
}
Expand Down
10 changes: 7 additions & 3 deletions guava-tests/test/com/google/common/base/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.collect.ImmutableList;
Expand All @@ -45,7 +44,7 @@
*
* @author Steve McKay
*/
@GwtCompatible(emulated = true)
@GwtIncompatible
@J2ktIncompatible
public class EnumsTest extends TestCase {

Expand Down Expand Up @@ -117,6 +116,7 @@ private WeakReference<?> doTestClassUnloading() throws Exception {
return new WeakReference<>(shadowLoader);
}

@GwtIncompatible // stringConverter
public void testStringConverter_convert() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertEquals(TestEnum.CHEETO, converter.convert("CHEETO"));
Expand All @@ -126,6 +126,7 @@ public void testStringConverter_convert() {
assertNull(converter.reverse().convert(null));
}

@GwtIncompatible // stringConverter
public void testStringConverter_convertError() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
try {
Expand All @@ -135,6 +136,7 @@ public void testStringConverter_convertError() {
}
}

@GwtIncompatible // stringConverter
public void testStringConverter_reverse() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertEquals("CHEETO", converter.reverse().convert(TestEnum.CHEETO));
Expand All @@ -143,13 +145,14 @@ public void testStringConverter_reverse() {
}

@J2ktIncompatible
@GwtIncompatible // NullPointerTester
@GwtIncompatible // stringConverter
public void testStringConverter_nullPointerTester() throws Exception {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicInstanceMethods(converter);
}

@GwtIncompatible // stringConverter
public void testStringConverter_nullConversions() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertNull(converter.convert(null));
Expand All @@ -164,6 +167,7 @@ public void testStringConverter_toString() {
Enums.stringConverter(TestEnum.class).toString());
}

@GwtIncompatible // stringConverter
public void testStringConverter_serialization() {
SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class));
}
Expand Down
5 changes: 3 additions & 2 deletions guava/src/com/google/common/base/Enums.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import java.io.Serializable;
Expand All @@ -34,7 +33,7 @@
* @author Steve McKay
* @since 9.0
*/
@GwtCompatible(emulated = true)
@GwtIncompatible
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
public final class Enums {
Expand Down Expand Up @@ -108,10 +107,12 @@ static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnum
*
* @since 16.0
*/
@GwtIncompatible
public static <T extends Enum<T>> Converter<String, T> stringConverter(Class<T> enumClass) {
return new StringConverter<>(enumClass);
}

@GwtIncompatible
private static final class StringConverter<T extends Enum<T>> extends Converter<String, T>
implements Serializable {

Expand Down

0 comments on commit c3a155d

Please sign in to comment.