Fix mismatch in types of Predicates being used

We're going to need to switch away from Guava's Functions and Predicates for
everything and replace them with the java.util versions. Unfortunately there
does not appear to be an automated tool to do this all at once. Refaster got
close but doesn't seem to care about these particular types of mismatch (I
suspect we're using a different version of the JDK than the outside world;
ours is OK with Guava classes).

This also bumps up Guava to 0.23, which is needed for some new functionality
used in combination with Java 8 features.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=170531539
This commit is contained in:
mcilwain 2017-09-29 14:20:02 -07:00 committed by Ben McIlwain
parent 447b83f7db
commit a50ef39c04
9 changed files with 80 additions and 68 deletions

View file

@ -14,11 +14,10 @@
package google.registry.export; package google.registry.export;
import static com.google.common.base.Predicates.not; import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.model.EntityClasses.CLASS_TO_KIND_FUNCTION; import static google.registry.model.EntityClasses.CLASS_TO_KIND_FUNCTION;
import static google.registry.util.TypeUtils.hasAnnotation; import static google.registry.util.TypeUtils.hasAnnotation;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import google.registry.model.EntityClasses; import google.registry.model.EntityClasses;
@ -33,19 +32,21 @@ public final class ExportConstants {
public static ImmutableSet<String> getBackupKinds() { public static ImmutableSet<String> getBackupKinds() {
// Back up all entity classes that aren't annotated with @VirtualEntity (never even persisted // Back up all entity classes that aren't annotated with @VirtualEntity (never even persisted
// to Datastore, so they can't be backed up) or @NotBackedUp (intentionally omitted). // to Datastore, so they can't be backed up) or @NotBackedUp (intentionally omitted).
return FluentIterable.from(EntityClasses.ALL_CLASSES) return EntityClasses.ALL_CLASSES
.filter(not(hasAnnotation(VirtualEntity.class))) .stream()
.filter(not(hasAnnotation(NotBackedUp.class))) .filter(hasAnnotation(VirtualEntity.class).negate())
.transform(CLASS_TO_KIND_FUNCTION) .filter(hasAnnotation(NotBackedUp.class).negate())
.toSortedSet(Ordering.natural()); .map(CLASS_TO_KIND_FUNCTION)
.collect(toImmutableSortedSet(Ordering.natural()));
} }
/** Returns the names of kinds to import into reporting tools (e.g. BigQuery). */ /** Returns the names of kinds to import into reporting tools (e.g. BigQuery). */
public static ImmutableSet<String> getReportingKinds() { public static ImmutableSet<String> getReportingKinds() {
return FluentIterable.from(EntityClasses.ALL_CLASSES) return EntityClasses.ALL_CLASSES
.stream()
.filter(hasAnnotation(ReportedOn.class)) .filter(hasAnnotation(ReportedOn.class))
.filter(not(hasAnnotation(VirtualEntity.class))) .filter(hasAnnotation(VirtualEntity.class).negate())
.transform(CLASS_TO_KIND_FUNCTION) .map(CLASS_TO_KIND_FUNCTION)
.toSortedSet(Ordering.natural()); .collect(toImmutableSortedSet(Ordering.natural()));
} }
} }

View file

@ -15,7 +15,7 @@
package google.registry.model.ofy; package google.registry.model.ofy;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.not; import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.googlecode.objectify.ObjectifyService.factory; import static com.googlecode.objectify.ObjectifyService.factory;
import static google.registry.util.TypeUtils.hasAnnotation; import static google.registry.util.TypeUtils.hasAnnotation;
@ -24,7 +24,7 @@ import com.google.appengine.api.datastore.DatastoreServiceConfig;
import com.google.appengine.api.datastore.DatastoreServiceFactory; import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify; import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory; import com.googlecode.objectify.ObjectifyFactory;
@ -45,6 +45,7 @@ import google.registry.model.translators.InetAddressTranslatorFactory;
import google.registry.model.translators.ReadableInstantUtcTranslatorFactory; import google.registry.model.translators.ReadableInstantUtcTranslatorFactory;
import google.registry.model.translators.UpdateAutoTimestampTranslatorFactory; import google.registry.model.translators.UpdateAutoTimestampTranslatorFactory;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
/** /**
* An instance of Ofy, obtained via {@code #ofy()}, should be used to access all persistable * An instance of Ofy, obtained via {@code #ofy()}, should be used to access all persistable
@ -133,13 +134,16 @@ public class ObjectifyService {
/** Register classes that can be persisted via Objectify as Datastore entities. */ /** Register classes that can be persisted via Objectify as Datastore entities. */
private static void registerEntityClasses( private static void registerEntityClasses(
Iterable<Class<? extends ImmutableObject>> entityClasses) { ImmutableSet<Class<? extends ImmutableObject>> entityClasses) {
// Register all the @Entity classes before any @EntitySubclass classes so that we can check // Register all the @Entity classes before any @EntitySubclass classes so that we can check
// that every @Entity registration is a new kind and every @EntitySubclass registration is not. // that every @Entity registration is a new kind and every @EntitySubclass registration is not.
// This is future-proofing for Objectify 5.x where the registration logic gets less lenient. // This is future-proofing for Objectify 5.x where the registration logic gets less lenient.
for (Class<?> clazz : Iterables.concat(
Iterables.filter(entityClasses, hasAnnotation(Entity.class)), for (Class<?> clazz :
Iterables.filter(entityClasses, not(hasAnnotation(Entity.class))))) { Stream.concat(
entityClasses.stream().filter(hasAnnotation(Entity.class)),
entityClasses.stream().filter(hasAnnotation(Entity.class).negate()))
.collect(toImmutableSet())) {
String kind = Key.getKind(clazz); String kind = Key.getKind(clazz);
boolean registered = factory().getMetadata(kind) != null; boolean registered = factory().getMetadata(kind) != null;
if (clazz.isAnnotationPresent(Entity.class)) { if (clazz.isAnnotationPresent(Entity.class)) {

View file

@ -957,11 +957,10 @@ def com_google_gdata_core():
def com_google_guava(): def com_google_guava():
java_import_external( java_import_external(
name = "com_google_guava", name = "com_google_guava",
jar_sha256 = "36a666e3b71ae7f0f0dca23654b67e086e6c93d192f60ba5dfd5519db6c288c8", jar_sha256 = "7baa80df284117e5b945b19b98d367a85ea7b7801bd358ff657946c3bd1b6596",
jar_urls = [ jar_urls = [
"http://domain-registry-maven.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar", "http://repo1.maven.org/maven2/com/google/guava/guava/23.0/guava-23.0.jar",
"http://repo1.maven.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar", "http://domain-registry-maven.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava/23.0/guava-23.0.jar",
"http://maven.ibiblio.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar",
], ],
licenses = ["notice"], # The Apache Software License, Version 2.0 licenses = ["notice"], # The Apache Software License, Version 2.0
exports = [ exports = [
@ -973,11 +972,10 @@ def com_google_guava():
def com_google_guava_testlib(): def com_google_guava_testlib():
java_import_external( java_import_external(
name = "com_google_guava_testlib", name = "com_google_guava_testlib",
jar_sha256 = "a9f52f328ac024e420c8995a107ea0dbef3fc169ddf97b3426e634f28d6b3663", jar_sha256 = "7e328d0f89a5ea103de4f9b689130eb555ff277e83bf86294bc14c2c40a59a80",
jar_urls = [ jar_urls = [
"http://domain-registry-maven.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava-testlib/20.0/guava-testlib-20.0.jar", "http://repo1.maven.org/maven2/com/google/guava/guava-testlib/23.0/guava-testlib-23.0.jar",
"http://maven.ibiblio.org/maven2/com/google/guava/guava-testlib/20.0/guava-testlib-20.0.jar", "http://domain-registry-maven.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava-testlib/23.0/guava-testlib-23.0.jar",
"http://repo1.maven.org/maven2/com/google/guava/guava-testlib/20.0/guava-testlib-20.0.jar",
], ],
licenses = ["notice"], # The Apache Software License, Version 2.0 licenses = ["notice"], # The Apache Software License, Version 2.0
testonly_ = True, testonly_ = True,

View file

@ -21,6 +21,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.ImmutableSortedSet;
import com.google.common.util.concurrent.UncheckedExecutionException;
import google.registry.model.server.Lock; import google.registry.model.server.Lock;
import google.registry.util.AppEngineTimeLimiter; import google.registry.util.AppEngineTimeLimiter;
import google.registry.util.FormattingLogger; import google.registry.util.FormattingLogger;
@ -28,6 +29,7 @@ import google.registry.util.RequestStatusChecker;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
@ -67,8 +69,12 @@ public class LockHandlerImpl implements LockHandler {
return AppEngineTimeLimiter.create().callWithTimeout( return AppEngineTimeLimiter.create().callWithTimeout(
new LockingCallable(callable, Strings.emptyToNull(tld), leaseLength, lockNames), new LockingCallable(callable, Strings.emptyToNull(tld), leaseLength, lockNames),
leaseLength.minus(LOCK_TIMEOUT_FUDGE).getMillis(), leaseLength.minus(LOCK_TIMEOUT_FUDGE).getMillis(),
TimeUnit.MILLISECONDS, TimeUnit.MILLISECONDS);
true); } catch (ExecutionException | UncheckedExecutionException e) {
// Unwrap the execution exception and throw its root cause.
Throwable cause = e.getCause();
throwIfUnchecked(cause);
throw new RuntimeException(cause);
} catch (Exception e) { } catch (Exception e) {
throwIfUnchecked(e); throwIfUnchecked(e);
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -76,6 +76,6 @@ public class AppEngineTimeLimiter {
} }
public static TimeLimiter create() { public static TimeLimiter create() {
return new SimpleTimeLimiter(new NewRequestThreadExecutorService()); return SimpleTimeLimiter.create(new NewRequestThreadExecutorService());
} }
} }

View file

@ -19,13 +19,13 @@ import static google.registry.util.CollectionUtils.difference;
import static java.lang.reflect.Modifier.isFinal; import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isStatic; import static java.lang.reflect.Modifier.isStatic;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.function.Predicate;
/** Utilities methods related to reflection. */ /** Utilities methods related to reflection. */
public class TypeUtils { public class TypeUtils {
@ -103,12 +103,7 @@ public class TypeUtils {
/** Returns a predicate that tests whether classes are annotated with the given annotation. */ /** Returns a predicate that tests whether classes are annotated with the given annotation. */
public static final Predicate<Class<?>> hasAnnotation( public static final Predicate<Class<?>> hasAnnotation(
final Class<? extends Annotation> annotation) { final Class<? extends Annotation> annotation) {
return new Predicate<Class<?>>() { return (Class<?> clazz) -> clazz.isAnnotationPresent(annotation);
@Override
public boolean apply(Class<?> clazz) {
return clazz.isAnnotationPresent(annotation);
}
};
} }
public static void checkNoInheritanceRelationships(ImmutableSet<Class<?>> resourceClasses) { public static void checkNoInheritanceRelationships(ImmutableSet<Class<?>> resourceClasses) {

View file

@ -14,13 +14,13 @@
package google.registry.model; package google.registry.model;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.EntityClasses.ALL_CLASSES; import static google.registry.model.EntityClasses.ALL_CLASSES;
import static google.registry.model.EntityClasses.CLASS_TO_KIND_FUNCTION; import static google.registry.model.EntityClasses.CLASS_TO_KIND_FUNCTION;
import static google.registry.util.TypeUtils.hasAnnotation; import static google.registry.util.TypeUtils.hasAnnotation;
import static java.util.stream.Collectors.toSet;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.EntitySubclass; import com.googlecode.objectify.annotation.EntitySubclass;
@ -35,14 +35,9 @@ public class EntityClassesTest {
// This implements the manual ordering we've been using for the EntityClasses class lists. // This implements the manual ordering we've been using for the EntityClasses class lists.
private static final Ordering<Class<?>> QUALIFIED_CLASS_NAME_ORDERING = private static final Ordering<Class<?>> QUALIFIED_CLASS_NAME_ORDERING =
Ordering.natural().onResultOf(new Function<Class<?>, String>() { Ordering.natural()
@Override .onResultOf(
public String apply(Class<?> clazz) { clazz -> clazz.getCanonicalName().substring(clazz.getPackage().getName().length()));
// Return only the part of the class name after the package name, which is the class
// name plus any outer class names.
return clazz.getCanonicalName().substring(clazz.getPackage().getName().length());
}
});
@Test @Test
public void testEntityClasses_inAlphabeticalOrder() throws Exception { public void testEntityClasses_inAlphabeticalOrder() throws Exception {
@ -51,23 +46,30 @@ public class EntityClassesTest {
@Test @Test
public void testEntityClasses_baseEntitiesHaveUniqueKinds() throws Exception { public void testEntityClasses_baseEntitiesHaveUniqueKinds() throws Exception {
assertThat(FluentIterable.from(ALL_CLASSES) assertThat(
ALL_CLASSES
.stream()
.filter(hasAnnotation(Entity.class)) .filter(hasAnnotation(Entity.class))
.transform(CLASS_TO_KIND_FUNCTION)) .map(CLASS_TO_KIND_FUNCTION)
.collect(toImmutableSet()))
.named("base entity kinds") .named("base entity kinds")
.containsNoDuplicates(); .containsNoDuplicates();
} }
@Test @Test
public void testEntityClasses_entitySubclassesHaveKindsMatchingBaseEntities() throws Exception { public void testEntityClasses_entitySubclassesHaveKindsMatchingBaseEntities() throws Exception {
Set<String> baseEntityKinds = FluentIterable.from(ALL_CLASSES) Set<String> baseEntityKinds =
ALL_CLASSES
.stream()
.filter(hasAnnotation(Entity.class)) .filter(hasAnnotation(Entity.class))
.transform(CLASS_TO_KIND_FUNCTION) .map(CLASS_TO_KIND_FUNCTION)
.toSet(); .collect(toSet());
Set<String> entitySubclassKinds = FluentIterable.from(ALL_CLASSES) Set<String> entitySubclassKinds =
ALL_CLASSES
.stream()
.filter(hasAnnotation(EntitySubclass.class)) .filter(hasAnnotation(EntitySubclass.class))
.transform(CLASS_TO_KIND_FUNCTION) .map(CLASS_TO_KIND_FUNCTION)
.toSet(); .collect(toSet());
assertThat(baseEntityKinds).named("base entity kinds").containsAllIn(entitySubclassKinds); assertThat(baseEntityKinds).named("base entity kinds").containsAllIn(entitySubclassKinds);
} }

View file

@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Throwables.throwIfUnchecked; import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.util.concurrent.Runnables.doNothing; import static com.google.common.util.concurrent.Runnables.doNothing;
import static google.registry.util.NetworkUtils.getCanonicalHostName; import static google.registry.util.NetworkUtils.getCanonicalHostName;
import static java.util.concurrent.Executors.newCachedThreadPool;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -128,14 +129,18 @@ public final class TestServer {
/** Stops the HTTP server. */ /** Stops the HTTP server. */
public void stop() { public void stop() {
try { try {
new SimpleTimeLimiter().callWithTimeout(new Callable<Void>() { SimpleTimeLimiter.create(newCachedThreadPool())
.callWithTimeout(
new Callable<Void>() {
@Nullable @Nullable
@Override @Override
public Void call() throws Exception { public Void call() throws Exception {
server.stop(); server.stop();
return null; return null;
} }
}, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS, true); },
SHUTDOWN_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
} catch (Exception e) { } catch (Exception e) {
throwIfUnchecked(e); throwIfUnchecked(e);
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -15,6 +15,7 @@
package google.registry.server; package google.registry.server;
import static com.google.common.base.Throwables.throwIfUnchecked; import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.util.concurrent.Executors.newCachedThreadPool;
import com.google.common.util.concurrent.SimpleTimeLimiter; import com.google.common.util.concurrent.SimpleTimeLimiter;
import java.io.IOException; import java.io.IOException;
@ -32,7 +33,7 @@ final class UrlChecker {
/** Probes {@code url} until it becomes available. */ /** Probes {@code url} until it becomes available. */
static void waitUntilAvailable(final URL url, int timeoutMs) { static void waitUntilAvailable(final URL url, int timeoutMs) {
try { try {
new SimpleTimeLimiter().callWithTimeout(new Callable<Void>() { SimpleTimeLimiter.create(newCachedThreadPool()).callWithTimeout(new Callable<Void>() {
@Nullable @Nullable
@Override @Override
public Void call() throws InterruptedException, IOException { public Void call() throws InterruptedException, IOException {
@ -44,7 +45,7 @@ final class UrlChecker {
Thread.sleep(exponentialBackoffMs *= 2); Thread.sleep(exponentialBackoffMs *= 2);
} }
} }
}, timeoutMs, TimeUnit.MILLISECONDS, true); }, timeoutMs, TimeUnit.MILLISECONDS);
} catch (Exception e) { } catch (Exception e) {
throwIfUnchecked(e); throwIfUnchecked(e);
throw new RuntimeException(e); throw new RuntimeException(e);