Skip to content

Commit

Permalink
Use imports instead of fully qualified types.
Browse files Browse the repository at this point in the history
I suspect that many of these fully qualified types come from fears that our tools for stripping (e.g.) `@GwtIncompatible` elements will fail to strip imports when they should. (I think that there may specifically have been a problem in Make Open Easy (whose acronym is M-O-E, which would trigger a presubmit error if I removed those hyphens :)) when a comment or Javadoc referred to the simple name of a class that was imported but not actually used.)

Some others come from trying to remain in sync with an upstream whose subsequent changes we've probably never actually pulled in (e.g., `JSR166TestCase`). Still others may come from trying to write code that can be copied and pasted from one file to another without touching imports. And some of it probably comes from attempts to suppress `sunapi` warnings (which can't be done on imports)—but those went from "hard to suppress" to "impossible to suppress" a while back, as discussed in cl/637073596. We _might_ actually be better off with the imports, since _maaaaaaybe_ that will lead to only one warning instead of _n_? But I doubt we'd be so lucky.

PiperOrigin-RevId: 643394004
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jun 14, 2024
1 parent a5f9bca commit 808e079
Show file tree
Hide file tree
Showing 35 changed files with 221 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.annotations.GwtCompatible;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -29,7 +30,7 @@
*
* @author George van den Driessche
*/
@Target(value = {java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Target(value = {ElementType.ANNOTATION_TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@GwtCompatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -135,7 +136,7 @@ public void execute(Runnable command) {
public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
Preconditions.checkNotNull(command, "command must not be null");
Preconditions.checkNotNull(unit, "unit must not be null!");
return schedule(java.util.concurrent.Executors.callable(command), delay, unit);
return schedule(Executors.callable(command), delay, unit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.AbstractFutureBenchmarks.OldAbstractFuture;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
Expand All @@ -39,6 +43,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
import sun.misc.Unsafe;

/** Benchmarks for {@link ExecutionList}. */
@VmOptions({"-Xms8g", "-Xmx8g"})
Expand Down Expand Up @@ -577,7 +582,7 @@ private static final class RunnableExecutorPair {
private static final class ExecutionListCAS {
static final Logger log = Logger.getLogger(ExecutionListCAS.class.getName());

private static final sun.misc.Unsafe UNSAFE;
private static final Unsafe UNSAFE;
private static final long HEAD_OFFSET;

/**
Expand All @@ -596,26 +601,26 @@ private static final class ExecutionListCAS {
}

/** TODO(lukes): This was copied verbatim from Striped64.java... standardize this? */
private static sun.misc.Unsafe getUnsafe() {
private static Unsafe getUnsafe() {
try {
return sun.misc.Unsafe.getUnsafe();
return Unsafe.getUnsafe();
} catch (SecurityException tryReflectionInstead) {
}
try {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<Unsafe>() {
@Override
public sun.misc.Unsafe run() throws Exception {
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
public Unsafe run() throws Exception {
Class<Unsafe> k = Unsafe.class;
for (Field f : k.getDeclaredFields()) {
f.setAccessible(true);
Object x = f.get(null);
if (k.isInstance(x)) return k.cast(x);
}
throw new NoSuchFieldError("the Unsafe");
}
});
} catch (java.security.PrivilegedActionException e) {
} catch (PrivilegedActionException e) {
throw new RuntimeException("Could not initialize intrinsics", e.getCause());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.collect.Lists;
import com.google.common.testing.ClassSanityTester;
import com.google.common.testing.EqualsTester;
import java.io.NotSerializableException;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
Expand Down Expand Up @@ -145,7 +146,7 @@ public void testMemoizeNonSerializable() throws Exception {

// Should get an exception when we try to serialize.
RuntimeException ex = assertThrows(RuntimeException.class, () -> reserialize(memoizedSupplier));
assertThat(ex).hasCauseThat().isInstanceOf(java.io.NotSerializableException.class);
assertThat(ex).hasCauseThat().isInstanceOf(NotSerializableException.class);
}

@J2ktIncompatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import sun.misc.Unsafe;

/**
* Tests our AtomicHelper fallback strategies in AbstractFuture.
Expand Down Expand Up @@ -55,7 +56,7 @@ public class AbstractFutureFallbackAtomicHelperTest extends TestCase {
*/
@SuppressWarnings({"SunApi", "removal"}) // b/345822163
private static final ClassLoader NO_UNSAFE =
getClassLoader(ImmutableSet.of(sun.misc.Unsafe.class.getName()));
getClassLoader(ImmutableSet.of(Unsafe.class.getName()));

/**
* This classloader disallows {@link sun.misc.Unsafe} and {@link AtomicReferenceFieldUpdater},
Expand All @@ -64,8 +65,7 @@ public class AbstractFutureFallbackAtomicHelperTest extends TestCase {
@SuppressWarnings({"SunApi", "removal"}) // b/345822163
private static final ClassLoader NO_ATOMIC_REFERENCE_FIELD_UPDATER =
getClassLoader(
ImmutableSet.of(
sun.misc.Unsafe.class.getName(), AtomicReferenceFieldUpdater.class.getName()));
ImmutableSet.of(Unsafe.class.getName(), AtomicReferenceFieldUpdater.class.getName()));

public static TestSuite suite() {
// we create a test suite containing a test for every AbstractFutureTest test method and we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilePermission;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.CodeSource;
Expand Down Expand Up @@ -590,7 +591,7 @@ public void runWithoutPermissions(Runnable r) {
}

/** A security policy where new permissions can be dynamically added or all cleared. */
public static class AdjustablePolicy extends java.security.Policy {
public static class AdjustablePolicy extends Policy {
Permissions perms = new Permissions();

AdjustablePolicy(Permission... permissions) {
Expand Down Expand Up @@ -639,7 +640,7 @@ public static Policy permissivePolicy() {
// Permissions needed by the junit test harness
new RuntimePermission("accessDeclaredMembers"),
new PropertyPermission("*", "read"),
new java.io.FilePermission("<<ALL FILES>>", "read"));
new FilePermission("<<ALL FILES>>", "read"));
}

/** Sleeps until the given time has elapsed. Throws AssertionFailedError if interrupted. */
Expand Down
25 changes: 15 additions & 10 deletions android/guava/src/com/google/common/cache/Striped64.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
package com.google.common.cache;

import com.google.common.annotations.GwtIncompatible;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Random;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import sun.misc.Unsafe;

/**
* A package-local class holding common representation and mechanics for classes supporting dynamic
Expand Down Expand Up @@ -109,7 +114,7 @@ final boolean cas(long cmp, long val) {
}

// Unsafe mechanics
private static final sun.misc.Unsafe UNSAFE;
private static final Unsafe UNSAFE;
private static final long valueOffset;

static {
Expand Down Expand Up @@ -267,7 +272,7 @@ final void internalReset(long initialValue) {
}

// Unsafe mechanics
private static final sun.misc.Unsafe UNSAFE;
private static final Unsafe UNSAFE;
private static final long baseOffset;
private static final long busyOffset;

Expand All @@ -288,26 +293,26 @@ final void internalReset(long initialValue) {
*
* @return a sun.misc.Unsafe
*/
private static sun.misc.Unsafe getUnsafe() {
private static Unsafe getUnsafe() {
try {
return sun.misc.Unsafe.getUnsafe();
return Unsafe.getUnsafe();
} catch (SecurityException tryReflectionInstead) {
}
try {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<Unsafe>() {
@Override
public sun.misc.Unsafe run() throws Exception {
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
public Unsafe run() throws Exception {
Class<Unsafe> k = Unsafe.class;
for (Field f : k.getDeclaredFields()) {
f.setAccessible(true);
Object x = f.get(null);
if (k.isInstance(x)) return k.cast(x);
}
throw new NoSuchFieldError("the Unsafe");
}
});
} catch (java.security.PrivilegedActionException e) {
} catch (PrivilegedActionException e) {
throw new RuntimeException("Could not initialize intrinsics", e.getCause());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public boolean hasNext() {
}

@Override
public java.util.Map.Entry<K, V> next() {
public Entry<K, V> next() {
if (nextOrNull == null) {
throw new NoSuchElementException();
}
Expand Down
16 changes: 8 additions & 8 deletions android/guava/src/com/google/common/collect/TableCollectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ final class TableCollectors {
V,
I extends Table<R, C, V>>
Collector<T, ?, I> toTable(
java.util.function.Function<? super T, ? extends R> rowFunction,
java.util.function.Function<? super T, ? extends C> columnFunction,
java.util.function.Function<? super T, ? extends V> valueFunction,
java.util.function.Supplier<I> tableSupplier) {
Function<? super T, ? extends R> rowFunction,
Function<? super T, ? extends C> columnFunction,
Function<? super T, ? extends V> valueFunction,
Supplier<I> tableSupplier) {
return TableCollectors.<T, R, C, V, I>toTable(
rowFunction,
columnFunction,
Expand All @@ -110,11 +110,11 @@ final class TableCollectors {
V,
I extends Table<R, C, V>>
Collector<T, ?, I> toTable(
java.util.function.Function<? super T, ? extends R> rowFunction,
java.util.function.Function<? super T, ? extends C> columnFunction,
java.util.function.Function<? super T, ? extends V> valueFunction,
Function<? super T, ? extends R> rowFunction,
Function<? super T, ? extends C> columnFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction,
java.util.function.Supplier<I> tableSupplier) {
Supplier<I> tableSupplier) {
checkNotNull(rowFunction);
checkNotNull(columnFunction);
checkNotNull(valueFunction);
Expand Down
2 changes: 1 addition & 1 deletion android/guava/src/com/google/common/hash/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class BloomFilter<T extends @Nullable Object> implements Predicate<
*
* <p>Implementations should be collections of pure functions (i.e. stateless).
*/
interface Strategy extends java.io.Serializable {
interface Strategy extends Serializable {

/**
* Sets {@code numHashFunctions} bits of the given bit array, by hashing a user element.
Expand Down
25 changes: 15 additions & 10 deletions android/guava/src/com/google/common/hash/Striped64.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
package com.google.common.hash;

import com.google.common.annotations.GwtIncompatible;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Random;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import sun.misc.Unsafe;

/**
* A package-local class holding common representation and mechanics for classes supporting dynamic
Expand Down Expand Up @@ -109,7 +114,7 @@ final boolean cas(long cmp, long val) {
}

// Unsafe mechanics
private static final sun.misc.Unsafe UNSAFE;
private static final Unsafe UNSAFE;
private static final long valueOffset;

static {
Expand Down Expand Up @@ -267,7 +272,7 @@ final void internalReset(long initialValue) {
}

// Unsafe mechanics
private static final sun.misc.Unsafe UNSAFE;
private static final Unsafe UNSAFE;
private static final long baseOffset;
private static final long busyOffset;

Expand All @@ -288,26 +293,26 @@ final void internalReset(long initialValue) {
*
* @return a sun.misc.Unsafe
*/
private static sun.misc.Unsafe getUnsafe() {
private static Unsafe getUnsafe() {
try {
return sun.misc.Unsafe.getUnsafe();
return Unsafe.getUnsafe();
} catch (SecurityException tryReflectionInstead) {
}
try {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<Unsafe>() {
@Override
public sun.misc.Unsafe run() throws Exception {
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
public Unsafe run() throws Exception {
Class<Unsafe> k = Unsafe.class;
for (Field f : k.getDeclaredFields()) {
f.setAccessible(true);
Object x = f.get(null);
if (k.isInstance(x)) return k.cast(x);
}
throw new NoSuchFieldError("the Unsafe");
}
});
} catch (java.security.PrivilegedActionException e) {
} catch (PrivilegedActionException e) {
throw new RuntimeException("Could not initialize intrinsics", e.getCause());
}
}
Expand Down
20 changes: 12 additions & 8 deletions android/guava/src/com/google/common/primitives/UnsignedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.Field;
import java.nio.ByteOrder;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.Comparator;
import sun.misc.Unsafe;
Expand Down Expand Up @@ -334,19 +338,19 @@ enum UnsafeComparator implements Comparator<byte[]> {
*
* @return a sun.misc.Unsafe
*/
private static sun.misc.Unsafe getUnsafe() {
private static Unsafe getUnsafe() {
try {
return sun.misc.Unsafe.getUnsafe();
return Unsafe.getUnsafe();
} catch (SecurityException e) {
// that's okay; try reflection instead
}
try {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<Unsafe>() {
@Override
public sun.misc.Unsafe run() throws Exception {
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
public Unsafe run() throws Exception {
Class<Unsafe> k = Unsafe.class;
for (Field f : k.getDeclaredFields()) {
f.setAccessible(true);
Object x = f.get(null);
if (k.isInstance(x)) {
Expand All @@ -356,7 +360,7 @@ public sun.misc.Unsafe run() throws Exception {
throw new NoSuchFieldError("the Unsafe");
}
});
} catch (java.security.PrivilegedActionException e) {
} catch (PrivilegedActionException e) {
throw new RuntimeException("Could not initialize intrinsics", e.getCause());
}
}
Expand Down
Loading

0 comments on commit 808e079

Please sign in to comment.