Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gissuebot/e411711ffb8adfe19c49 to your computer and use it in GitHub Desktop.
Save gissuebot/e411711ffb8adfe19c49 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 288, comment 101
diff --git a/core/src/com/google/inject/internal/InjectorImpl.java b/core/src/com/google/inject/internal/InjectorImpl.java
index 6faab97..5dc5013 100644
--- a/core/src/com/google/inject/internal/InjectorImpl.java
+++ b/core/src/com/google/inject/internal/InjectorImpl.java
@@ -128,7 +128,7 @@
if (parent != null) {
localContext = parent.localContext;
} else {
- localContext = new LocalContextThreadLocal();
+ localContext = new ThreadLocal<InternalContext>();
}
}
@@ -1035,22 +1035,23 @@
return getProvider(type).get();
}
- final ThreadLocal<Object[]> localContext;
+ final ThreadLocal<InternalContext> localContext;
/** Looks up thread local context. Creates (and removes) a new context if necessary. */
<T> T callInContext(ContextualCallable<T> callable) throws ErrorsException {
- Object[] reference = localContext.get();
- if (reference[0] == null) {
- reference[0] = new InternalContext();
- try {
- return callable.call((InternalContext)reference[0]);
- } finally {
- // Only clear the context if this call created it.
- reference[0] = null;
+ InternalContext reference = localContext.get();
+ boolean created = false;
+ try {
+ if (reference == null) {
+ reference = new InternalContext();
+ localContext.set(reference);
+ created = true;
+ }
+ return callable.call(reference);
+ } finally {
+ if(created){
+ localContext.remove();
}
- } else {
- // Someone else will clean up this context.
- return callable.call((InternalContext)reference[0]);
}
}
@@ -1061,10 +1062,4 @@
.toString();
}
- private static final class LocalContextThreadLocal extends ThreadLocal<Object[]> {
- @Override
- protected Object[] initialValue() {
- return new Object[1];
- }
- }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment