Skip to content

Instantly share code, notes, and snippets.

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/b9ae362bf8737e92d5fa to your computer and use it in GitHub Desktop.
Save gissuebot/b9ae362bf8737e92d5fa to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 288, comment 88
Description: Decouple the context ThreadLocal from the containing Injector/ClassLoader
Author: Stuart McCulloch <mcculls@gmail.com>
Bug-Google: http://code.google.com/p/google-guice/issues/detail?id=288
Last-Update: 2012-08-07
diff --git a/core/src/com/google/inject/internal/InjectorImpl.java b/core/src/com/google/inject/internal/InjectorImpl.java
index ee5e2d6..f40bc0f 100644
--- a/core/src/com/google/inject/internal/InjectorImpl.java
+++ b/core/src/com/google/inject/internal/InjectorImpl.java
@@ -125,12 +125,7 @@ final class InjectorImpl implements Injector, Lookups {
if (parent != null) {
localContext = parent.localContext;
} else {
- localContext = new ThreadLocal<Object[]>() {
- @Override
- protected Object[] initialValue() {
- return new Object[1];
- }
- };
+ localContext = new ThreadLocal<Object[]>();
}
}
@@ -1037,11 +1032,15 @@ final class InjectorImpl implements Injector, Lookups {
return getProvider(type).get();
}
- final ThreadLocal<Object[]> localContext;
+ private final ThreadLocal<Object[]> 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 == null) {
+ reference = new Object[1];
+ localContext.set(reference);
+ }
if (reference[0] == null) {
reference[0] = new InternalContext();
try {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment