Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gissuebot/01bf29d6b02d6379983c to your computer and use it in GitHub Desktop.
Save gissuebot/01bf29d6b02d6379983c to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 760, comment 3
Description: Update internal caches to use Guava's CacheBuilder API
Author: Stuart McCulloch <mcculls@gmail.com>
Bug-Google: http://code.google.com/p/google-guice/issues/detail?id=760
Last-Update: 2013-09-12
diff --git a/core/src/com/google/inject/internal/Annotations.java b/core/src/com/google/inject/internal/Annotations.java
index 07e9a83..991a231 100644
--- a/core/src/com/google/inject/internal/Annotations.java
+++ b/core/src/com/google/inject/internal/Annotations.java
@@ -20,8 +20,10 @@ import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Joiner.MapJoiner;
import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;
import com.google.inject.BindingAnnotation;
import com.google.inject.Key;
@@ -69,11 +71,11 @@ public class Annotations {
return hasMethods;
}
- private static final Map<Class<? extends Annotation>, Annotation> cache =
- new MapMaker().weakKeys().makeComputingMap(
- new Function<Class<? extends Annotation>, Annotation>() {
+ private static final LoadingCache<Class<? extends Annotation>, Annotation> cache =
+ CacheBuilder.newBuilder().weakKeys().build(
+ new CacheLoader<Class<? extends Annotation>, Annotation>() {
@Override
- public Annotation apply(Class<? extends Annotation> input) {
+ public Annotation load(Class<? extends Annotation> input) {
return generateAnnotationImpl(input);
}
});
@@ -85,7 +87,7 @@ public class Annotations {
public static <T extends Annotation> T generateAnnotation(Class<T> annotationType) {
Preconditions.checkState(
isAllDefaultMethods(annotationType), "%s is not all default methods", annotationType);
- return (T)cache.get(annotationType);
+ return (T)cache.getUnchecked(annotationType);
}
private static <T extends Annotation> T generateAnnotationImpl(final Class<T> annotationType) {
@@ -206,9 +208,9 @@ public class Annotations {
private final Collection<Class<? extends Annotation>> annotationTypes;
/** Returns true if the given class has one of the desired annotations. */
- private Function<Class<? extends Annotation>, Boolean> hasAnnotations =
- new Function<Class<? extends Annotation>, Boolean>() {
- public Boolean apply(Class<? extends Annotation> annotationType) {
+ private CacheLoader<Class<? extends Annotation>, Boolean> hasAnnotations =
+ new CacheLoader<Class<? extends Annotation>, Boolean>() {
+ public Boolean load(Class<? extends Annotation> annotationType) {
for (Annotation annotation : annotationType.getAnnotations()) {
if (annotationTypes.contains(annotation.annotationType())) {
return true;
@@ -218,8 +220,8 @@ public class Annotations {
}
};
- final Map<Class<? extends Annotation>, Boolean> cache = new MapMaker().weakKeys()
- .makeComputingMap(hasAnnotations);
+ final LoadingCache<Class<? extends Annotation>, Boolean> cache = CacheBuilder.newBuilder().weakKeys()
+ .build(hasAnnotations);
/**
* Constructs a new checker that looks for annotations of the given types.
@@ -232,7 +234,7 @@ public class Annotations {
* Returns true if the given type has one of the desired annotations.
*/
boolean hasAnnotations(Class<? extends Annotation> annotated) {
- return cache.get(annotated);
+ return cache.getUnchecked(annotated);
}
}
diff --git a/core/src/com/google/inject/internal/BytecodeGen.java b/core/src/com/google/inject/internal/BytecodeGen.java
index 55ad161..bacbda9 100644
--- a/core/src/com/google/inject/internal/BytecodeGen.java
+++ b/core/src/com/google/inject/internal/BytecodeGen.java
@@ -16,9 +16,9 @@
package com.google.inject.internal;
-import com.google.common.base.Function;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.MapMaker;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
@@ -26,7 +26,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
-import java.util.Map;
import java.util.logging.Logger;
/**
@@ -124,7 +123,7 @@ public final class BytecodeGen {
* Weak cache of bridge class loaders that make the Guice implementation
* classes visible to various code-generated proxies of client classes.
*/
- private static final Map<ClassLoader, ClassLoader> CLASS_LOADER_CACHE;
+ private static final LoadingCache<ClassLoader, ClassLoader> CLASS_LOADER_CACHE;
static {
boolean customLoaderEnabled;
@@ -136,9 +135,9 @@ public final class BytecodeGen {
CUSTOM_LOADER_ENABLED = customLoaderEnabled;
if (CUSTOM_LOADER_ENABLED) {
- CLASS_LOADER_CACHE = new MapMaker().weakKeys().weakValues().makeComputingMap(
- new Function<ClassLoader, ClassLoader>() {
- public ClassLoader apply(final ClassLoader typeClassLoader) {
+ CLASS_LOADER_CACHE = CacheBuilder.newBuilder().weakKeys().weakValues().build(
+ new CacheLoader<ClassLoader, ClassLoader>() {
+ public ClassLoader load(final ClassLoader typeClassLoader) {
logger.fine("Creating a bridge ClassLoader for " + typeClassLoader);
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
@@ -148,7 +147,7 @@ public final class BytecodeGen {
}
});
} else {
- CLASS_LOADER_CACHE = ImmutableMap.of();
+ CLASS_LOADER_CACHE = null;
}
}
@@ -188,9 +187,9 @@ public final class BytecodeGen {
// don't try bridging private types as it won't work
if (Visibility.forType(type) == Visibility.PUBLIC) {
- if (delegate != SystemBridgeHolder.SYSTEM_BRIDGE.getParent()) {
+ if (CLASS_LOADER_CACHE != null && delegate != SystemBridgeHolder.SYSTEM_BRIDGE.getParent()) {
// delegate guaranteed to be non-null here
- return CLASS_LOADER_CACHE.get(delegate);
+ return CLASS_LOADER_CACHE.getUnchecked(delegate);
}
// delegate may or may not be null here
return SystemBridgeHolder.SYSTEM_BRIDGE;
diff --git a/core/src/com/google/inject/internal/FailableCache.java b/core/src/com/google/inject/internal/FailableCache.java
index e392e0d..acd8be5 100644
--- a/core/src/com/google/inject/internal/FailableCache.java
+++ b/core/src/com/google/inject/internal/FailableCache.java
@@ -16,10 +16,9 @@
package com.google.inject.internal;
-import com.google.common.base.Function;
-import com.google.common.collect.MapMaker;
-
-import java.util.Map;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
/**
* Lazily creates (and caches) values for keys. If creating the value fails (with errors), an
@@ -29,9 +28,9 @@ import java.util.Map;
*/
public abstract class FailableCache<K, V> {
- private final Map<K, Object> delegate = new MapMaker().makeComputingMap(
- new Function<K, Object>() {
- public Object apply(K key) {
+ private final LoadingCache<K, Object> delegate = CacheBuilder.newBuilder().build(
+ new CacheLoader<K, Object>() {
+ public Object load(K key) {
Errors errors = new Errors();
V result = null;
try {
@@ -46,7 +45,7 @@ public abstract class FailableCache<K, V> {
protected abstract V create(K key, Errors errors) throws ErrorsException;
public V get(K key, Errors errors) throws ErrorsException {
- Object resultOrError = delegate.get(key);
+ Object resultOrError = delegate.getUnchecked(key);
if (resultOrError instanceof Errors) {
errors.merge((Errors) resultOrError);
throw errors.toException();
@@ -58,6 +57,6 @@ public abstract class FailableCache<K, V> {
}
boolean remove(K key) {
- return delegate.remove(key) != null;
+ return delegate.asMap().remove(key) != null;
}
}
diff --git a/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java b/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java
index 631a05f..1074628 100644
--- a/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java
+++ b/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java
@@ -16,11 +16,12 @@
package com.google.inject.internal;
-import com.google.common.base.Function;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
-import com.google.common.collect.MapMaker;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
@@ -29,7 +30,6 @@ import com.google.inject.spi.ProvisionListener;
import com.google.inject.spi.ProvisionListenerBinding;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
@@ -47,10 +47,10 @@ final class ProvisionListenerCallbackStore {
private final ImmutableList<ProvisionListenerBinding> listenerBindings;
- private final Map<KeyBinding, ProvisionListenerStackCallback<?>> cache
- = new MapMaker().makeComputingMap(
- new Function<KeyBinding, ProvisionListenerStackCallback<?>>() {
- public ProvisionListenerStackCallback<?> apply(KeyBinding key) {
+ private final LoadingCache<KeyBinding, ProvisionListenerStackCallback<?>> cache
+ = CacheBuilder.newBuilder().build(
+ new CacheLoader<KeyBinding, ProvisionListenerStackCallback<?>>() {
+ public ProvisionListenerStackCallback<?> load(KeyBinding key) {
return create(key.binding);
}
});
@@ -65,7 +65,7 @@ final class ProvisionListenerCallbackStore {
public <T> ProvisionListenerStackCallback<T> get(Binding<T> binding) {
// Never notify any listeners for internal bindings.
if (!INTERNAL_BINDINGS.contains(binding.getKey())) {
- return (ProvisionListenerStackCallback<T>) cache.get(
+ return (ProvisionListenerStackCallback<T>) cache.getUnchecked(
new KeyBinding(binding.getKey(), binding));
}
return ProvisionListenerStackCallback.emptyListener();
@@ -81,7 +81,7 @@ final class ProvisionListenerCallbackStore {
* Returns true if the type was stored in the cache, false otherwise.
*/
boolean remove(Binding<?> type) {
- return cache.remove(type) != null;
+ return cache.asMap().remove(type) != null;
}
/**
diff --git a/core/src/com/google/inject/internal/util/StackTraceElements.java b/core/src/com/google/inject/internal/util/StackTraceElements.java
index 80580b5..4ad18cd 100644
--- a/core/src/com/google/inject/internal/util/StackTraceElements.java
+++ b/core/src/com/google/inject/internal/util/StackTraceElements.java
@@ -16,7 +16,9 @@
package com.google.inject.internal.util;
-import com.google.common.base.Function;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import com.google.common.collect.MapMaker;
import java.io.IOException;
@@ -32,9 +34,9 @@ import java.util.Map;
public class StackTraceElements {
/*if[AOP]*/
- static final Map<Class<?>, LineNumbers> lineNumbersCache = new MapMaker().weakKeys().softValues()
- .makeComputingMap(new Function<Class<?>, LineNumbers>() {
- public LineNumbers apply(Class<?> key) {
+ static final LoadingCache<Class<?>, LineNumbers> lineNumbersCache = CacheBuilder.newBuilder().weakKeys().softValues()
+ .build(new CacheLoader<Class<?>, LineNumbers>() {
+ public LineNumbers load(Class<?> key) {
try {
return new LineNumbers(key);
}
@@ -56,7 +58,7 @@ public class StackTraceElements {
Class declaringClass = member.getDeclaringClass();
/*if[AOP]*/
- LineNumbers lineNumbers = lineNumbersCache.get(declaringClass);
+ LineNumbers lineNumbers = lineNumbersCache.getUnchecked(declaringClass);
String fileName = lineNumbers.getSource();
Integer lineNumberOrNull = lineNumbers.getLineNumber(member);
int lineNumber = lineNumberOrNull == null ? lineNumbers.getFirstLine() : lineNumberOrNull;
@@ -73,7 +75,7 @@ public class StackTraceElements {
public static Object forType(Class<?> implementation) {
/*if[AOP]*/
- LineNumbers lineNumbers = lineNumbersCache.get(implementation);
+ LineNumbers lineNumbers = lineNumbersCache.getUnchecked(implementation);
int lineNumber = lineNumbers.getFirstLine();
String fileName = lineNumbers.getSource();
/*end[AOP]*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment