Skip to content
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

Internal change. #7544

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6
uses: github/codeql-action/upload-sarif@f09c1c0a94de965c15400f5634aa42fac8fb8f88 # v3.27.5
with:
sarif_file: results.sarif
91 changes: 34 additions & 57 deletions android/guava-tests/test/com/google/common/base/JoinerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@
package com.google.common.base;

import static com.google.common.base.ReflectionFreeAssertThrows.assertThrows;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.unmodifiableList;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Joiner.MapJoiner;
import com.google.common.collect.ForwardingList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.testing.NullPointerTester;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -62,56 +60,12 @@ public class JoinerTest extends TestCase {
private static final Iterable<@Nullable Integer> ITERABLE_FOUR_NULLS =
Arrays.asList((Integer) null, null, null, null);

/*
* Both of these fields *are* immutable/constant. They don't use the type ImmutableList because
* they need to behave slightly differently.
*/
@SuppressWarnings("ConstantCaseForConstants")
private static final List<Integer> UNDERREPORTING_SIZE_LIST;

@SuppressWarnings("ConstantCaseForConstants")
private static final List<Integer> OVERREPORTING_SIZE_LIST;

static {
List<Integer> collection123 = Arrays.asList(1, 2, 3);
UNDERREPORTING_SIZE_LIST = unmodifiableList(new MisleadingSizeList<>(collection123, -1));
OVERREPORTING_SIZE_LIST = unmodifiableList(new MisleadingSizeList<>(collection123, 1));
}

/*
* c.g.c.collect.testing.Helpers.misleadingSizeList has a broken Iterator, so we can't use it. (I
* mean, ideally we'd fix it....) Also, we specifically need a List so that we trigger the fast
* path in join(Iterable).
*/
private static final class MisleadingSizeList<E extends @Nullable Object>
extends ForwardingList<E> {
final List<E> delegate;
final int delta;

MisleadingSizeList(List<E> delegate, int delta) {
this.delegate = delegate;
this.delta = delta;
}

@Override
protected List<E> delegate() {
return delegate;
}

@Override
public int size() {
return delegate.size() + delta;
}
}

@SuppressWarnings("JoinIterableIterator") // explicitly testing iterator overload, too
public void testNoSpecialNullBehavior() {
checkNoOutput(J, ITERABLE_);
checkResult(J, ITERABLE_1, "1");
checkResult(J, ITERABLE_12, "1-2");
checkResult(J, ITERABLE_123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");

assertThrows(NullPointerException.class, () -> J.join(ITERABLE_NULL));
assertThrows(NullPointerException.class, () -> J.join(ITERABLE_1_NULL_2));
Expand All @@ -126,8 +80,6 @@ public void testOnCharOverride() {
checkResult(onChar, ITERABLE_1, "1");
checkResult(onChar, ITERABLE_12, "1-2");
checkResult(onChar, ITERABLE_123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");
}

public void testSkipNulls() {
Expand All @@ -139,8 +91,6 @@ public void testSkipNulls() {
checkResult(skipNulls, ITERABLE_1, "1");
checkResult(skipNulls, ITERABLE_12, "1-2");
checkResult(skipNulls, ITERABLE_123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");
checkResult(skipNulls, ITERABLE_NULL_1, "1");
checkResult(skipNulls, ITERABLE_1_NULL, "1");
checkResult(skipNulls, ITERABLE_1_NULL_2, "1-2");
Expand All @@ -152,8 +102,6 @@ public void testUseForNull() {
checkResult(zeroForNull, ITERABLE_1, "1");
checkResult(zeroForNull, ITERABLE_12, "1-2");
checkResult(zeroForNull, ITERABLE_123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");
checkResult(zeroForNull, ITERABLE_NULL, "0");
checkResult(zeroForNull, ITERABLE_NULL_NULL, "0-0");
checkResult(zeroForNull, ITERABLE_NULL_1, "0-1");
Expand All @@ -166,7 +114,7 @@ private static void checkNoOutput(Joiner joiner, Iterable<Integer> set) {
assertEquals("", joiner.join(set));
assertEquals("", joiner.join(set.iterator()));

Object[] array = newArrayList(set).toArray(new Integer[0]);
Object[] array = Lists.newArrayList(set).toArray(new Integer[0]);
assertEquals("", joiner.join(array));

StringBuilder sb1FromIterable = new StringBuilder();
Expand Down Expand Up @@ -231,8 +179,7 @@ private static void checkResult(Joiner joiner, Iterable<Integer> parts, String e
joiner.appendTo(sb1FromIterator, parts.iterator());
assertEquals("x" + expected, sb1FromIterator.toString());

// The use of iterator() works around J2KT b/381065164.
Integer[] partsArray = newArrayList(parts.iterator()).toArray(new Integer[0]);
Integer[] partsArray = Lists.newArrayList(parts).toArray(new Integer[0]);
assertEquals(expected, joiner.join(partsArray));

StringBuilder sb2 = new StringBuilder().append('x');
Expand Down Expand Up @@ -322,6 +269,36 @@ public void test_skipNulls_onMap() {
assertThrows(UnsupportedOperationException.class, () -> j.withKeyValueSeparator("/"));
}

private static class DontStringMeBro implements CharSequence {
@Override
public int length() {
return 3;
}

@Override
public char charAt(int index) {
return "foo".charAt(index);
}

@Override
public CharSequence subSequence(int start, int end) {
return "foo".subSequence(start, end);
}

@Override
public String toString() {
throw new AssertionFailedError("shouldn't be invoked");
}
}

@GwtIncompatible // StringBuilder.append in GWT invokes Object.toString(), unlike the JRE version.
public void testDontConvertCharSequenceToString() {
assertEquals("foo,foo", Joiner.on(",").join(new DontStringMeBro(), new DontStringMeBro()));
assertEquals(
"foo,bar,foo",
Joiner.on(",").useForNull("bar").join(new DontStringMeBro(), null, new DontStringMeBro()));
}

@J2ktIncompatible
@GwtIncompatible // NullPointerTester
public void testNullPointers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,6 @@ public void testAddOverflowCollection() {
assertThrows(
IllegalArgumentException.class,
() -> builder.addAll(nCopies(Integer.MAX_VALUE - 50, "a")));
assertThat(expected)
.hasMessageThat()
.contains("cannot store more than Integer.MAX_VALUE elements");
assertThat(expected).hasMessageThat().contains("cannot store more than MAX_VALUE elements");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public static TestSuite suite() {
suite.addTest(
NavigableMapTestSuiteBuilder.using(
new TestStringSortedMapGenerator() {
private final Object mutex = new Object[0]; // something Serializable
private final Object mutex = new Integer(1);

@Override
protected SortedMap<String, String> create(Entry<String, String>[] entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ public void testLog2NaNInfinity() {
}

@GwtIncompatible // StrictMath
@SuppressWarnings("strictfp") // Guava still supports Java 8
private strictfp double trueLog2(double d) {
double trueLog2 = StrictMath.log(d) / StrictMath.log(2);
// increment until it's >= the true value
Expand Down
38 changes: 1 addition & 37 deletions android/guava/src/com/google/common/base/Joiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,10 @@ public final StringBuilder appendTo(
* Returns a string containing the string representation of each of {@code parts}, using the
* previously configured separator between each.
*/
public String join(Iterable<? extends @Nullable Object> parts) {
// We don't use the same optimization here as in the JRE flavor.
// TODO: b/381289911 - Evaluate the performance impact of doing so.
public final String join(Iterable<? extends @Nullable Object> parts) {
return join(parts.iterator());
}

/*
* TODO: b/381289911 - Make the Iterator overload use StringJoiner (including Android or not)—or
* some other optimization, given that StringJoiner can over-allocate:
* https://bugs.openjdk.org/browse/JDK-8305774
*/

// TODO: b/381289911 - Optimize MapJoiner similarly to Joiner (including Android or not).

/**
* Returns a string containing the string representation of each of {@code parts}, using the
* previously configured separator between each.
Expand Down Expand Up @@ -278,12 +268,6 @@ public Joiner skipNulls() {
*/
public Joiner skipNulls() {
return new Joiner(this) {
@Override
@SuppressWarnings("JoinIterableIterator") // suggests infinite recursion
public String join(Iterable<? extends @Nullable Object> parts) {
return join(parts.iterator());
}

@Override
public <A extends Appendable> A appendTo(
A appendable, Iterator<? extends @Nullable Object> parts) throws IOException {
Expand Down Expand Up @@ -486,7 +470,6 @@ public MapJoiner useForNull(String nullText) {
}
}

// TODO(cpovirk): Rename to "toCharSequence."
CharSequence toString(@CheckForNull Object part) {
/*
* requireNonNull is not safe: Joiner.on(...).join(somethingThatContainsNull) will indeed throw.
Expand Down Expand Up @@ -532,23 +515,4 @@ public Object get(int index) {
}
};
}

// cloned from ImmutableCollection
private static int expandedCapacity(int oldCapacity, int minCapacity) {
if (minCapacity < 0) {
throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements");
} else if (minCapacity <= oldCapacity) {
return oldCapacity;
}
// careful of overflow!
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
if (newCapacity < minCapacity) {
newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
}
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
// guaranteed to be >= newCapacity
}
return newCapacity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public abstract static class Builder<E> {

static int expandedCapacity(int oldCapacity, int minCapacity) {
if (minCapacity < 0) {
throw new IllegalArgumentException("cannot store more than Integer.MAX_VALUE elements");
throw new IllegalArgumentException("cannot store more than MAX_VALUE elements");
} else if (minCapacity <= oldCapacity) {
return oldCapacity;
}
Expand Down
Loading
Loading