Skip to content

Commit

Permalink
Fix cases where GWT sometimes couldn't infer which types are serialized.
Browse files Browse the repository at this point in the history
And finally revise our (internal-only) tests to catch these problems and the problem fixed by CL 147488537.

As far as I can tell, our old tests passed only because of a GWT bug.
The fixes are mostly to create dummy superclasses with dummy fields, as usual.
The exception is ImmutableSortedSet. For some reason, the fix there is to move the existing dummy fields to RegularImmutableSortedSet. My completely wild guess (without investigating at all) is that GWT ignores the ImmutableSortedSet fields because ImmutableSortedSet_CustomFieldSerializer doesn't have the expected methods. This guess makes no sense, especially because GWT appears to be fine with the *Comparator<E>* field on ImmutableSortedSet, just not the E field. Basically nothing makes any sense, but I'm just happy it works now.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=147607863
  • Loading branch information
cpovirk committed Feb 15, 2017
1 parent 217659f commit 89a8e3f
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Map;

@GwtCompatible(emulated = true)
abstract class ArrayListMultimapGwtSerializationDependencies<K, V>
extends AbstractListMultimap<K, V> {
ArrayListMultimapGwtSerializationDependencies(Map<K, Collection<V>> map) {
super(map);
}

K dummyKey;
V dummyValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Map;

@GwtCompatible(emulated = true)
abstract class HashMultimapGwtSerializationDependencies<K, V> extends AbstractSetMultimap<K, V> {
HashMultimapGwtSerializationDependencies(Map<K, Collection<V>> map) {
super(map);
}

K dummyKey;
V dummyValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;

@GwtCompatible(emulated = true)
abstract class ImmutableMultisetGwtSerializationDependencies<E> extends ImmutableCollection<E> {
E dummy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,6 @@ static <E> ImmutableSortedSet<E> unsafeDelegateSortedSet(
: new RegularImmutableSortedSet<E>(delegate, isSubset);
}

// This reference is only used by GWT compiler to infer the elements of the
// set that needs to be serialized.
private Comparator<E> unusedComparatorForSerialization;
private E unusedElementForSerialization;

private transient final SortedSet<E> sortedDelegate;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Map;

@GwtCompatible(emulated = true)
abstract class LinkedHashMultimapGwtSerializationDependencies<K, V>
extends AbstractSetMultimap<K, V> {
LinkedHashMultimapGwtSerializationDependencies(Map<K, Collection<V>> map) {
super(map);
}

K dummyKey;
V dummyValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.common.collect;

import java.util.Comparator;
import java.util.SortedSet;

/**
Expand All @@ -28,6 +29,9 @@ final class RegularImmutableSortedSet<E> extends ImmutableSortedSet<E> {
/** true if this set is a subset of another immutable sorted set. */
final boolean isSubset;

private Comparator<E> unusedComparatorForSerialization;
private E unusedElementForSerialization;

RegularImmutableSortedSet(SortedSet<E> delegate, boolean isSubset) {
super(delegate);
this.isSubset = isSubset;
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/ArrayListMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
* @since 2.0
*/
@GwtCompatible(serializable = true, emulated = true)
public final class ArrayListMultimap<K, V> extends AbstractListMultimap<K, V> {
public final class ArrayListMultimap<K, V>
extends ArrayListMultimapGwtSerializationDependencies<K, V> {
// Default from ArrayList
private static final int DEFAULT_VALUES_PER_KEY = 3;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Map;

/**
* A dummy superclass to support GWT serialization of the element types of an {@link
* ArrayListMultimap}. The GWT supersource for this class contains a field for each type.
*
* <p>For details about this hack, see {@link GwtSerializationDependencies}, which takes the same
* approach but with a subclass rather than a superclass.
*
* <p>TODO(cpovirk): Consider applying this subclass approach to our other types.
*/
@GwtCompatible(emulated = true)
abstract class ArrayListMultimapGwtSerializationDependencies<K, V>
extends AbstractListMultimap<K, V> {
ArrayListMultimapGwtSerializationDependencies(Map<K, Collection<V>> map) {
super(map);
}
// TODO(cpovirk): Maybe I should have just one shared superclass for AbstractMultimap itself?
}
2 changes: 1 addition & 1 deletion guava/src/com/google/common/collect/HashMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* @since 2.0
*/
@GwtCompatible(serializable = true, emulated = true)
public final class HashMultimap<K, V> extends AbstractSetMultimap<K, V> {
public final class HashMultimap<K, V> extends HashMultimapGwtSerializationDependencies<K, V> {
private static final int DEFAULT_VALUES_PER_KEY = 2;

@VisibleForTesting transient int expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Map;

/**
* A dummy superclass to support GWT serialization of the element types of a {@link HashMultimap}.
* The GWT supersource for this class contains a field for each type.
*
* <p>For details about this hack, see {@link GwtSerializationDependencies}, which takes the same
* approach but with a subclass rather than a superclass.
*
* <p>TODO(cpovirk): Consider applying this subclass approach to our other types.
*/
@GwtCompatible(emulated = true)
abstract class HashMultimapGwtSerializationDependencies<K, V> extends AbstractSetMultimap<K, V> {
HashMultimapGwtSerializationDependencies(Map<K, Collection<V>> map) {
super(map);
}
}
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/ImmutableMultiset.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
@GwtCompatible(serializable = true, emulated = true)
@SuppressWarnings("serial") // we're overriding default serialization
// TODO(lowasser): write an efficient asList() implementation
public abstract class ImmutableMultiset<E> extends ImmutableCollection<E> implements Multiset<E> {
public abstract class ImmutableMultiset<E> extends ImmutableMultisetGwtSerializationDependencies<E>
implements Multiset<E> {

/**
* Returns a {@code Collector} that accumulates the input elements into a new
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;

/**
* A dummy superclass to support GWT serialization of the element type of an {@link
* ImmutableMultiset}. The GWT supersource for this class contains a field of type {@code E}.
*
* <p>For details about this hack, see {@link GwtSerializationDependencies}, which takes the same
* approach but with a subclass rather than a superclass.
*
* <p>TODO(cpovirk): Consider applying this subclass approach to our other types.
*
* <p>For {@code ImmutableMultiset} in particular, I ran into a problem with the {@code
* GwtSerializationDependencies} approach: When autogenerating a serializer for the new class, GWT
* tries to refer to our dummy serializer for the superclass,
* ImmutableMultiset_CustomFieldSerializer. But that type has no methods (since it's never actually
* used). We could probably fix the problem by adding dummy methods to that class, but that is
* starting to sound harder than taking the superclass approach, which I've been coming to like,
* anyway, since it doesn't require us to declare dummy methods (though occasionally constructors)
* and make types non-final.
*/
@GwtCompatible(emulated = true)
abstract class ImmutableMultisetGwtSerializationDependencies<E> extends ImmutableCollection<E> {}
7 changes: 4 additions & 3 deletions guava/src/com/google/common/collect/LinkedHashMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
* @since 2.0
*/
@GwtCompatible(serializable = true, emulated = true)
public final class LinkedHashMultimap<K, V> extends AbstractSetMultimap<K, V> {
public final class LinkedHashMultimap<K, V>
extends LinkedHashMultimapGwtSerializationDependencies<K, V> {

/**
* Creates a new, empty {@code LinkedHashMultimap} with the default initial
Expand Down Expand Up @@ -300,7 +301,7 @@ public Set<Map.Entry<K, V>> entries() {
* Returns a view collection of all <i>distinct</i> keys contained in this
* multimap. Note that the key set contains a key if and only if this multimap
* maps that key to at least one value.
*
*
* <p>The iterator generated by the returned set traverses the keys in the
* order they were first added to the multimap.
*
Expand Down Expand Up @@ -419,7 +420,7 @@ public void remove() {
}
};
}

@Override
public void forEach(Consumer<? super V> action) {
checkNotNull(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Map;

/**
* A dummy superclass to support GWT serialization of the element types of a {@link
* LinkedHashMultimap}. The GWT supersource for this class contains a field for each type.
*
* <p>For details about this hack, see {@link GwtSerializationDependencies}, which takes the same
* approach but with a subclass rather than a superclass.
*
* <p>TODO(cpovirk): Consider applying this subclass approach to our other types.
*/
@GwtCompatible(emulated = true)
abstract class LinkedHashMultimapGwtSerializationDependencies<K, V>
extends AbstractSetMultimap<K, V> {
LinkedHashMultimapGwtSerializationDependencies(Map<K, Collection<V>> map) {
super(map);
}
}

0 comments on commit 89a8e3f

Please sign in to comment.