Skip to content

Commit

Permalink
Avoid calling methods whose return type changed in Java 9 -- even fro…
Browse files Browse the repository at this point in the history
…m 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 #3990.

RELNOTES=n/a
PiperOrigin-RevId: 395740030
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Sep 9, 2021
1 parent f08dfc2 commit daad0b3
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 14 deletions.
Expand Up @@ -195,8 +195,8 @@ void performAction(Random random, Iterable<? extends PrimitiveSink> 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());
Expand Down
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Expand Up @@ -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();
}
Expand Down
44 changes: 44 additions & 0 deletions 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() {}
}
Expand Up @@ -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() {}
}
4 changes: 2 additions & 2 deletions guava-tests/test/com/google/common/hash/HashTestUtils.java
Expand Up @@ -195,8 +195,8 @@ void performAction(Random random, Iterable<? extends PrimitiveSink> 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());
Expand Down
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Expand Up @@ -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();
}
Expand Down
44 changes: 44 additions & 0 deletions 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() {}
}
8 changes: 8 additions & 0 deletions guava/src/com/google/common/io/Java8Compatibility.java
Expand Up @@ -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() {}
}

0 comments on commit daad0b3

Please sign in to comment.