From daad0b3fba2bdc317900966a2752bf7d906001f4 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Thu, 9 Sep 2021 10:54:18 -0700 Subject: [PATCH] Avoid calling methods whose return type changed in Java 9 -- even from tests. This doesn't fix a practical problem today. What it does do is unblock us from adding checking (which will run in our internal builds) that will detect the main problems described in https://github.com/google/guava/issues/3990. RELNOTES=n/a PiperOrigin-RevId: 395740030 --- .../com/google/common/hash/HashTestUtils.java | 4 +- .../common/io/CharSequenceReaderTest.java | 6 +-- .../google/common/io/SourceSinkFactories.java | 4 +- .../common/base/Java8Compatibility.java | 44 +++++++++++++++++++ .../google/common/io/Java8Compatibility.java | 8 ++++ .../com/google/common/hash/HashTestUtils.java | 4 +- .../common/io/CharSequenceReaderTest.java | 6 +-- .../google/common/io/SourceSinkFactories.java | 4 +- .../common/base/Java8Compatibility.java | 44 +++++++++++++++++++ .../google/common/io/Java8Compatibility.java | 8 ++++ 10 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 android/guava/src/com/google/common/base/Java8Compatibility.java create mode 100644 guava/src/com/google/common/base/Java8Compatibility.java diff --git a/android/guava-tests/test/com/google/common/hash/HashTestUtils.java b/android/guava-tests/test/com/google/common/hash/HashTestUtils.java index 8dfbdb0cdf03..9e9944b5abfb 100644 --- a/android/guava-tests/test/com/google/common/hash/HashTestUtils.java +++ b/android/guava-tests/test/com/google/common/hash/HashTestUtils.java @@ -195,8 +195,8 @@ void performAction(Random random, Iterable sinks) { int limit = pos + random.nextInt(value.length - pos + 1); for (PrimitiveSink sink : sinks) { ByteBuffer buffer = ByteBuffer.wrap(value); - buffer.position(pos); - buffer.limit(limit); + Java8Compatibility.position(buffer, pos); + Java8Compatibility.limit(buffer, limit); sink.putBytes(buffer); assertEquals(limit, buffer.limit()); assertEquals(limit, buffer.position()); diff --git a/android/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java b/android/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java index 12bc17e588e3..dbe94fcf71fa 100644 --- a/android/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java +++ b/android/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java @@ -211,7 +211,7 @@ private static void assertReadsCorrectly(CharSequence charSequence) throws IOExc reader = new CharSequenceReader(charSequence); CharBuffer buf2 = CharBuffer.allocate(expected.length()); assertEquals(expected.length() == 0 ? -1 : expected.length(), reader.read(buf2)); - buf2.flip(); + Java8Compatibility.flip(buf2); assertEquals(expected, buf2.toString()); assertFullyRead(reader); @@ -220,9 +220,9 @@ private static void assertReadsCorrectly(CharSequence charSequence) throws IOExc buf2 = CharBuffer.allocate(5); builder = new StringBuilder(); while (reader.read(buf2) != -1) { - buf2.flip(); + Java8Compatibility.flip(buf2); builder.append(buf2); - buf2.clear(); + Java8Compatibility.clear(buf2); } assertEquals(expected, builder.toString()); assertFullyRead(reader); diff --git a/android/guava-tests/test/com/google/common/io/SourceSinkFactories.java b/android/guava-tests/test/com/google/common/io/SourceSinkFactories.java index 8bfa6e1ba12a..cf3fad41edb8 100644 --- a/android/guava-tests/test/com/google/common/io/SourceSinkFactories.java +++ b/android/guava-tests/test/com/google/common/io/SourceSinkFactories.java @@ -407,9 +407,9 @@ public String getSinkContents() throws IOException { StringBuilder builder = new StringBuilder(); CharBuffer buffer = CharBuffer.allocate(100); while (reader.read(buffer) != -1) { - buffer.flip(); + Java8Compatibility.flip(buffer); builder.append(buffer); - buffer.clear(); + Java8Compatibility.clear(buffer); } return builder.toString(); } diff --git a/android/guava/src/com/google/common/base/Java8Compatibility.java b/android/guava/src/com/google/common/base/Java8Compatibility.java new file mode 100644 index 000000000000..edc8b73bdd10 --- /dev/null +++ b/android/guava/src/com/google/common/base/Java8Compatibility.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 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.base; + +import com.google.common.annotations.GwtIncompatible; +import java.nio.Buffer; + +/** + * Wrappers around {@link Buffer} methods that are covariantly overridden in Java 9+. See + * https://github.com/google/guava/issues/3990 + */ +@GwtIncompatible +@ElementTypesAreNonnullByDefault +final class Java8Compatibility { + static void clear(Buffer b) { + b.clear(); + } + + static void flip(Buffer b) { + b.flip(); + } + + static void limit(Buffer b, int limit) { + b.limit(limit); + } + + static void position(Buffer b, int position) { + b.position(position); + } + + private Java8Compatibility() {} +} diff --git a/android/guava/src/com/google/common/io/Java8Compatibility.java b/android/guava/src/com/google/common/io/Java8Compatibility.java index 4391e93c3b4e..705d97b4dfa5 100644 --- a/android/guava/src/com/google/common/io/Java8Compatibility.java +++ b/android/guava/src/com/google/common/io/Java8Compatibility.java @@ -36,9 +36,17 @@ static void limit(Buffer b, int limit) { b.limit(limit); } + static void mark(Buffer b) { + b.mark(); + } + static void position(Buffer b, int position) { b.position(position); } + static void reset(Buffer b) { + b.reset(); + } + private Java8Compatibility() {} } diff --git a/guava-tests/test/com/google/common/hash/HashTestUtils.java b/guava-tests/test/com/google/common/hash/HashTestUtils.java index 8dfbdb0cdf03..9e9944b5abfb 100644 --- a/guava-tests/test/com/google/common/hash/HashTestUtils.java +++ b/guava-tests/test/com/google/common/hash/HashTestUtils.java @@ -195,8 +195,8 @@ void performAction(Random random, Iterable sinks) { int limit = pos + random.nextInt(value.length - pos + 1); for (PrimitiveSink sink : sinks) { ByteBuffer buffer = ByteBuffer.wrap(value); - buffer.position(pos); - buffer.limit(limit); + Java8Compatibility.position(buffer, pos); + Java8Compatibility.limit(buffer, limit); sink.putBytes(buffer); assertEquals(limit, buffer.limit()); assertEquals(limit, buffer.position()); diff --git a/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java b/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java index 12bc17e588e3..dbe94fcf71fa 100644 --- a/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java +++ b/guava-tests/test/com/google/common/io/CharSequenceReaderTest.java @@ -211,7 +211,7 @@ private static void assertReadsCorrectly(CharSequence charSequence) throws IOExc reader = new CharSequenceReader(charSequence); CharBuffer buf2 = CharBuffer.allocate(expected.length()); assertEquals(expected.length() == 0 ? -1 : expected.length(), reader.read(buf2)); - buf2.flip(); + Java8Compatibility.flip(buf2); assertEquals(expected, buf2.toString()); assertFullyRead(reader); @@ -220,9 +220,9 @@ private static void assertReadsCorrectly(CharSequence charSequence) throws IOExc buf2 = CharBuffer.allocate(5); builder = new StringBuilder(); while (reader.read(buf2) != -1) { - buf2.flip(); + Java8Compatibility.flip(buf2); builder.append(buf2); - buf2.clear(); + Java8Compatibility.clear(buf2); } assertEquals(expected, builder.toString()); assertFullyRead(reader); diff --git a/guava-tests/test/com/google/common/io/SourceSinkFactories.java b/guava-tests/test/com/google/common/io/SourceSinkFactories.java index 9b761d37450e..d24622d0aeda 100644 --- a/guava-tests/test/com/google/common/io/SourceSinkFactories.java +++ b/guava-tests/test/com/google/common/io/SourceSinkFactories.java @@ -442,9 +442,9 @@ public String getSinkContents() throws IOException { StringBuilder builder = new StringBuilder(); CharBuffer buffer = CharBuffer.allocate(100); while (reader.read(buffer) != -1) { - buffer.flip(); + Java8Compatibility.flip(buffer); builder.append(buffer); - buffer.clear(); + Java8Compatibility.clear(buffer); } return builder.toString(); } diff --git a/guava/src/com/google/common/base/Java8Compatibility.java b/guava/src/com/google/common/base/Java8Compatibility.java new file mode 100644 index 000000000000..edc8b73bdd10 --- /dev/null +++ b/guava/src/com/google/common/base/Java8Compatibility.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 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.base; + +import com.google.common.annotations.GwtIncompatible; +import java.nio.Buffer; + +/** + * Wrappers around {@link Buffer} methods that are covariantly overridden in Java 9+. See + * https://github.com/google/guava/issues/3990 + */ +@GwtIncompatible +@ElementTypesAreNonnullByDefault +final class Java8Compatibility { + static void clear(Buffer b) { + b.clear(); + } + + static void flip(Buffer b) { + b.flip(); + } + + static void limit(Buffer b, int limit) { + b.limit(limit); + } + + static void position(Buffer b, int position) { + b.position(position); + } + + private Java8Compatibility() {} +} diff --git a/guava/src/com/google/common/io/Java8Compatibility.java b/guava/src/com/google/common/io/Java8Compatibility.java index 4391e93c3b4e..705d97b4dfa5 100644 --- a/guava/src/com/google/common/io/Java8Compatibility.java +++ b/guava/src/com/google/common/io/Java8Compatibility.java @@ -36,9 +36,17 @@ static void limit(Buffer b, int limit) { b.limit(limit); } + static void mark(Buffer b) { + b.mark(); + } + static void position(Buffer b, int position) { b.position(position); } + static void reset(Buffer b) { + b.reset(); + } + private Java8Compatibility() {} }