mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
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:
parent
447b83f7db
commit
a50ef39c04
9 changed files with 80 additions and 68 deletions
|
@ -14,11 +14,10 @@
|
|||
|
||||
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.util.TypeUtils.hasAnnotation;
|
||||
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Ordering;
|
||||
import google.registry.model.EntityClasses;
|
||||
|
@ -33,19 +32,21 @@ public final class ExportConstants {
|
|||
public static ImmutableSet<String> getBackupKinds() {
|
||||
// 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).
|
||||
return FluentIterable.from(EntityClasses.ALL_CLASSES)
|
||||
.filter(not(hasAnnotation(VirtualEntity.class)))
|
||||
.filter(not(hasAnnotation(NotBackedUp.class)))
|
||||
.transform(CLASS_TO_KIND_FUNCTION)
|
||||
.toSortedSet(Ordering.natural());
|
||||
return EntityClasses.ALL_CLASSES
|
||||
.stream()
|
||||
.filter(hasAnnotation(VirtualEntity.class).negate())
|
||||
.filter(hasAnnotation(NotBackedUp.class).negate())
|
||||
.map(CLASS_TO_KIND_FUNCTION)
|
||||
.collect(toImmutableSortedSet(Ordering.natural()));
|
||||
}
|
||||
|
||||
/** Returns the names of kinds to import into reporting tools (e.g. BigQuery). */
|
||||
public static ImmutableSet<String> getReportingKinds() {
|
||||
return FluentIterable.from(EntityClasses.ALL_CLASSES)
|
||||
return EntityClasses.ALL_CLASSES
|
||||
.stream()
|
||||
.filter(hasAnnotation(ReportedOn.class))
|
||||
.filter(not(hasAnnotation(VirtualEntity.class)))
|
||||
.transform(CLASS_TO_KIND_FUNCTION)
|
||||
.toSortedSet(Ordering.natural());
|
||||
.filter(hasAnnotation(VirtualEntity.class).negate())
|
||||
.map(CLASS_TO_KIND_FUNCTION)
|
||||
.collect(toImmutableSortedSet(Ordering.natural()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
package google.registry.model.ofy;
|
||||
|
||||
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 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.common.annotations.VisibleForTesting;
|
||||
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.Objectify;
|
||||
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.UpdateAutoTimestampTranslatorFactory;
|
||||
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
|
||||
|
@ -133,13 +134,16 @@ public class ObjectifyService {
|
|||
|
||||
/** Register classes that can be persisted via Objectify as Datastore entities. */
|
||||
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
|
||||
// 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.
|
||||
for (Class<?> clazz : Iterables.concat(
|
||||
Iterables.filter(entityClasses, hasAnnotation(Entity.class)),
|
||||
Iterables.filter(entityClasses, not(hasAnnotation(Entity.class))))) {
|
||||
|
||||
for (Class<?> clazz :
|
||||
Stream.concat(
|
||||
entityClasses.stream().filter(hasAnnotation(Entity.class)),
|
||||
entityClasses.stream().filter(hasAnnotation(Entity.class).negate()))
|
||||
.collect(toImmutableSet())) {
|
||||
String kind = Key.getKind(clazz);
|
||||
boolean registered = factory().getMetadata(kind) != null;
|
||||
if (clazz.isAnnotationPresent(Entity.class)) {
|
||||
|
|
|
@ -957,11 +957,10 @@ def com_google_gdata_core():
|
|||
def com_google_guava():
|
||||
java_import_external(
|
||||
name = "com_google_guava",
|
||||
jar_sha256 = "36a666e3b71ae7f0f0dca23654b67e086e6c93d192f60ba5dfd5519db6c288c8",
|
||||
jar_sha256 = "7baa80df284117e5b945b19b98d367a85ea7b7801bd358ff657946c3bd1b6596",
|
||||
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/20.0/guava-20.0.jar",
|
||||
"http://maven.ibiblio.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://domain-registry-maven.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava/23.0/guava-23.0.jar",
|
||||
],
|
||||
licenses = ["notice"], # The Apache Software License, Version 2.0
|
||||
exports = [
|
||||
|
@ -973,11 +972,10 @@ def com_google_guava():
|
|||
def com_google_guava_testlib():
|
||||
java_import_external(
|
||||
name = "com_google_guava_testlib",
|
||||
jar_sha256 = "a9f52f328ac024e420c8995a107ea0dbef3fc169ddf97b3426e634f28d6b3663",
|
||||
jar_sha256 = "7e328d0f89a5ea103de4f9b689130eb555ff277e83bf86294bc14c2c40a59a80",
|
||||
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://maven.ibiblio.org/maven2/com/google/guava/guava-testlib/20.0/guava-testlib-20.0.jar",
|
||||
"http://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://domain-registry-maven.storage.googleapis.com/repo1.maven.org/maven2/com/google/guava/guava-testlib/23.0/guava-testlib-23.0.jar",
|
||||
],
|
||||
licenses = ["notice"], # The Apache Software License, Version 2.0
|
||||
testonly_ = True,
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.google.common.annotations.VisibleForTesting;
|
|||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import google.registry.model.server.Lock;
|
||||
import google.registry.util.AppEngineTimeLimiter;
|
||||
import google.registry.util.FormattingLogger;
|
||||
|
@ -28,6 +29,7 @@ import google.registry.util.RequestStatusChecker;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
@ -67,8 +69,12 @@ public class LockHandlerImpl implements LockHandler {
|
|||
return AppEngineTimeLimiter.create().callWithTimeout(
|
||||
new LockingCallable(callable, Strings.emptyToNull(tld), leaseLength, lockNames),
|
||||
leaseLength.minus(LOCK_TIMEOUT_FUDGE).getMillis(),
|
||||
TimeUnit.MILLISECONDS,
|
||||
true);
|
||||
TimeUnit.MILLISECONDS);
|
||||
} 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) {
|
||||
throwIfUnchecked(e);
|
||||
throw new RuntimeException(e);
|
||||
|
|
|
@ -76,6 +76,6 @@ public class AppEngineTimeLimiter {
|
|||
}
|
||||
|
||||
public static TimeLimiter create() {
|
||||
return new SimpleTimeLimiter(new NewRequestThreadExecutorService());
|
||||
return SimpleTimeLimiter.create(new NewRequestThreadExecutorService());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,13 +19,13 @@ import static google.registry.util.CollectionUtils.difference;
|
|||
import static java.lang.reflect.Modifier.isFinal;
|
||||
import static java.lang.reflect.Modifier.isStatic;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Utilities methods related to reflection. */
|
||||
public class TypeUtils {
|
||||
|
@ -103,12 +103,7 @@ public class TypeUtils {
|
|||
/** Returns a predicate that tests whether classes are annotated with the given annotation. */
|
||||
public static final Predicate<Class<?>> hasAnnotation(
|
||||
final Class<? extends Annotation> annotation) {
|
||||
return new Predicate<Class<?>>() {
|
||||
@Override
|
||||
public boolean apply(Class<?> clazz) {
|
||||
return clazz.isAnnotationPresent(annotation);
|
||||
}
|
||||
};
|
||||
return (Class<?> clazz) -> clazz.isAnnotationPresent(annotation);
|
||||
}
|
||||
|
||||
public static void checkNoInheritanceRelationships(ImmutableSet<Class<?>> resourceClasses) {
|
||||
|
|
|
@ -14,13 +14,13 @@
|
|||
|
||||
package google.registry.model;
|
||||
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.model.EntityClasses.ALL_CLASSES;
|
||||
import static google.registry.model.EntityClasses.CLASS_TO_KIND_FUNCTION;
|
||||
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.googlecode.objectify.annotation.Entity;
|
||||
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.
|
||||
private static final Ordering<Class<?>> QUALIFIED_CLASS_NAME_ORDERING =
|
||||
Ordering.natural().onResultOf(new Function<Class<?>, String>() {
|
||||
@Override
|
||||
public String apply(Class<?> clazz) {
|
||||
// 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());
|
||||
}
|
||||
});
|
||||
Ordering.natural()
|
||||
.onResultOf(
|
||||
clazz -> clazz.getCanonicalName().substring(clazz.getPackage().getName().length()));
|
||||
|
||||
@Test
|
||||
public void testEntityClasses_inAlphabeticalOrder() throws Exception {
|
||||
|
@ -51,23 +46,30 @@ public class EntityClassesTest {
|
|||
|
||||
@Test
|
||||
public void testEntityClasses_baseEntitiesHaveUniqueKinds() throws Exception {
|
||||
assertThat(FluentIterable.from(ALL_CLASSES)
|
||||
assertThat(
|
||||
ALL_CLASSES
|
||||
.stream()
|
||||
.filter(hasAnnotation(Entity.class))
|
||||
.transform(CLASS_TO_KIND_FUNCTION))
|
||||
.map(CLASS_TO_KIND_FUNCTION)
|
||||
.collect(toImmutableSet()))
|
||||
.named("base entity kinds")
|
||||
.containsNoDuplicates();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntityClasses_entitySubclassesHaveKindsMatchingBaseEntities() throws Exception {
|
||||
Set<String> baseEntityKinds = FluentIterable.from(ALL_CLASSES)
|
||||
Set<String> baseEntityKinds =
|
||||
ALL_CLASSES
|
||||
.stream()
|
||||
.filter(hasAnnotation(Entity.class))
|
||||
.transform(CLASS_TO_KIND_FUNCTION)
|
||||
.toSet();
|
||||
Set<String> entitySubclassKinds = FluentIterable.from(ALL_CLASSES)
|
||||
.map(CLASS_TO_KIND_FUNCTION)
|
||||
.collect(toSet());
|
||||
Set<String> entitySubclassKinds =
|
||||
ALL_CLASSES
|
||||
.stream()
|
||||
.filter(hasAnnotation(EntitySubclass.class))
|
||||
.transform(CLASS_TO_KIND_FUNCTION)
|
||||
.toSet();
|
||||
.map(CLASS_TO_KIND_FUNCTION)
|
||||
.collect(toSet());
|
||||
assertThat(baseEntityKinds).named("base entity kinds").containsAllIn(entitySubclassKinds);
|
||||
}
|
||||
|
||||
|
|
|
@ -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.util.concurrent.Runnables.doNothing;
|
||||
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.ImmutableMap;
|
||||
|
@ -128,14 +129,18 @@ public final class TestServer {
|
|||
/** Stops the HTTP server. */
|
||||
public void stop() {
|
||||
try {
|
||||
new SimpleTimeLimiter().callWithTimeout(new Callable<Void>() {
|
||||
SimpleTimeLimiter.create(newCachedThreadPool())
|
||||
.callWithTimeout(
|
||||
new Callable<Void>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
server.stop();
|
||||
return null;
|
||||
}
|
||||
}, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS, true);
|
||||
},
|
||||
SHUTDOWN_TIMEOUT_MS,
|
||||
TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
throwIfUnchecked(e);
|
||||
throw new RuntimeException(e);
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package google.registry.server;
|
||||
|
||||
import static com.google.common.base.Throwables.throwIfUnchecked;
|
||||
import static java.util.concurrent.Executors.newCachedThreadPool;
|
||||
|
||||
import com.google.common.util.concurrent.SimpleTimeLimiter;
|
||||
import java.io.IOException;
|
||||
|
@ -32,7 +33,7 @@ final class UrlChecker {
|
|||
/** Probes {@code url} until it becomes available. */
|
||||
static void waitUntilAvailable(final URL url, int timeoutMs) {
|
||||
try {
|
||||
new SimpleTimeLimiter().callWithTimeout(new Callable<Void>() {
|
||||
SimpleTimeLimiter.create(newCachedThreadPool()).callWithTimeout(new Callable<Void>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public Void call() throws InterruptedException, IOException {
|
||||
|
@ -44,7 +45,7 @@ final class UrlChecker {
|
|||
Thread.sleep(exponentialBackoffMs *= 2);
|
||||
}
|
||||
}
|
||||
}, timeoutMs, TimeUnit.MILLISECONDS, true);
|
||||
}, timeoutMs, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
throwIfUnchecked(e);
|
||||
throw new RuntimeException(e);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue