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

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

package com.google.inject.internal;

import com.google.inject.Key;
import com.google.inject.spi.Dependency;
import com.google.inject.spi.PrivateElements;

/**
 * This factory exists in a parent injector. When invoked, it retrieves its value from a child
 * injector.
 */
final class ExposedKeyFactory implements InternalFactory, CreationListener {
    private final Key key;
    private final PrivateElements privateElements;
    private BindingImpl delegate;

    ExposedKeyFactory(Key key, PrivateElements privateElements) {
        this.key = key;
        this.privateElements = privateElements;
    }

    @Override
    public void notify(Errors errors) {
        InjectorImpl privateInjector = (InjectorImpl) privateElements.getInjector();
        BindingImpl explicitBinding = privateInjector.state.getExplicitBinding(key);

        // validate that the child injector has its own factory. If the getInternalFactory() returns
        // this, then that child injector doesn't have a factory (and getExplicitBinding has returned
        // its parent's binding instead
        if (explicitBinding.getInternalFactory() == this) {
            errors.withSource(explicitBinding.getSource()).exposedButNotBound(key);
            return;
        }

        this.delegate = explicitBinding;
    }

    public T get(InternalContext context, Dependency dependency, boolean linked)
            throws InternalProvisionException {
        return delegate.getInternalFactory().get(context, dependency, linked);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy