All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.internal.InternalContext Maven / Gradle / Ivy

package com.google.inject.internal;

import com.google.inject.Key;
import com.google.inject.internal.InjectorImpl.InjectorOptions;
import com.google.inject.spi.Dependency;

import java.util.IdentityHashMap;
import java.util.Map;

/**
 * Internal context. Used to coordinate injections and support circular
 * dependencies.
 */
final class InternalContext implements AutoCloseable {

    private final InjectorOptions options;

    private final Map> constructionContexts =
            new IdentityHashMap<>();

    /**
     * Keeps track of the type that is currently being requested for injection.
     */
    private Dependency dependency;

    /**
     * Keeps track of the hierarchy of types needed during injection.
     *
     * 

This is a pairwise combination of dependencies and sources, with dependencies or keys on * even indices, and sources on odd indices. This structure is to avoid the memory overhead of * DependencyAndSource objects, which can add to several tens of megabytes in large applications. */ private Object[] dependencyStack = new Object[16]; private int dependencyStackSize = 0; /** * The number of times {@link #enter()} has been called + 1 for initial construction. */ private int enterCount; /** * A single element array to clear when the {@link #enterCount} hits {@code 0}. * *

This is the value stored in the {@code InjectorImpl.localContext} thread local. */ private final Object[] toClear; InternalContext(InjectorOptions options, Object[] toClear) { this.options = options; this.toClear = toClear; this.enterCount = 1; } /** Should only be called by InjectorImpl.enterContext(). */ void enter() { enterCount++; } /** Should be called any any method that received an instance via InjectorImpl.enterContext(). */ @Override public void close() { int newCount = --enterCount; if (newCount < 0) { throw new IllegalStateException("Called close() too many times"); } if (newCount == 0) { toClear[0] = null; } } public InjectorOptions getInjectorOptions() { return options; } @SuppressWarnings("unchecked") public ConstructionContext getConstructionContext(Object key) { ConstructionContext constructionContext = (ConstructionContext) constructionContexts.get(key); if (constructionContext == null) { constructionContext = new ConstructionContext(); constructionContexts.put(key, constructionContext); } return constructionContext; } public Dependency getDependency() { return dependency; } /** * Sets the new current dependency & adds it to the state. */ public Dependency pushDependency(Dependency dependency, Object source) { Dependency previous = this.dependency; this.dependency = dependency; doPushState(dependency, source); return previous; } /** * Pops the current state & sets the new dependency. */ public void popStateAndSetDependency(Dependency newDependency) { popState(); this.dependency = newDependency; } /** * Adds to the state without setting the dependency. */ public void pushState(Key key, Object source) { doPushState(key, source); } private void doPushState(Object dependencyOrKey, Object source) { int localSize = dependencyStackSize; Object[] localStack = dependencyStack; if (localStack.length < localSize + 2) { localStack = dependencyStack = java.util.Arrays.copyOf(localStack, (localStack.length * 3) / 2 + 2); } localStack[localSize++] = dependencyOrKey; localStack[localSize++] = source; dependencyStackSize = localSize; } /** * Pops from the state without setting a dependency. */ public void popState() { // N.B. we don't null out the array entries. It isn't necessary since all the objects in the // array (Key, Dependency, or Binding source objects) are all tied to the lifetime of the // injector, which is greater than the lifetime of this object. So removing them from the array // doesn't matter. dependencyStackSize -= 2; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy