Search in sources :

Example 21 with InjectionPoint

use of com.google.inject.spi.InjectionPoint in project guice by google.

the class ProviderMethodsModule method createProviderMethod.

private <T> ProviderMethod<T> createProviderMethod(Binder binder, Method method, Annotation annotation) {
    binder = binder.withSource(method);
    Errors errors = new Errors(method);
    // prepare the parameter providers
    InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
    // Define T as the method's return type.
    @SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
    Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
    try {
        key = scanner.prepareMethod(binder, annotation, key, point);
    } catch (Throwable t) {
        binder.addError(t);
    }
    Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
    for (Message message : errors.getMessages()) {
        binder.addError(message);
    }
    return ProviderMethod.create(key, method, delegate, ImmutableSet.copyOf(point.getDependencies()), scopeAnnotation, skipFastClassGeneration, annotation);
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) Message(com.google.inject.spi.Message) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 22 with InjectionPoint

use of com.google.inject.spi.InjectionPoint in project guice by google.

the class InjectorImpl method cleanup.

/**
   * Iterates through the binding's dependencies to clean up any stray bindings that were leftover
   * from a failed JIT binding. This is required because the bindings are eagerly & optimistically
   * added to allow circular dependency support, so dependencies may pass where they should have
   * failed.
   */
private boolean cleanup(BindingImpl<?> binding, Set<Key> encountered) {
    boolean bindingFailed = false;
    Set<Dependency<?>> deps = getInternalDependencies(binding);
    for (Dependency dep : deps) {
        Key<?> depKey = dep.getKey();
        InjectionPoint ip = dep.getInjectionPoint();
        if (encountered.add(depKey)) {
            // only check if we haven't looked at this key yet
            BindingImpl depBinding = jitBindings.get(depKey);
            if (depBinding != null) {
                // if the binding still exists, validate
                // if children fail, we fail
                boolean failed = cleanup(depBinding, encountered);
                if (depBinding instanceof ConstructorBindingImpl) {
                    ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl) depBinding;
                    ip = ctorBinding.getInternalConstructor();
                    if (!ctorBinding.isInitialized()) {
                        failed = true;
                    }
                }
                if (failed) {
                    removeFailedJitBinding(depBinding, ip);
                    bindingFailed = true;
                }
            } else if (state.getExplicitBinding(depKey) == null) {
                // ignore keys if they were explicitly bound, but if neither JIT
                // nor explicit, it's also invalid & should let parent know.
                bindingFailed = true;
            }
        }
    }
    return bindingFailed;
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency)

Example 23 with InjectionPoint

use of com.google.inject.spi.InjectionPoint in project guice by google.

the class DefaultConstructionProxyFactory method create.

@Override
public ConstructionProxy<T> create() {
    // the injection point is for a constructor of T
    @SuppressWarnings("unchecked") final Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember();
    /*if[AOP]*/
    try {
        net.sf.cglib.reflect.FastClass fc = BytecodeGen.newFastClassForMember(constructor);
        if (fc != null) {
            int index = fc.getIndex(constructor.getParameterTypes());
            // We could just fall back to reflection in this case but I believe this should actually
            // be impossible.
            Preconditions.checkArgument(index >= 0, "Could not find constructor %s in fast class", constructor);
            return new FastClassProxy<T>(injectionPoint, constructor, fc, index);
        }
    } catch (net.sf.cglib.core.CodeGenerationException e) {
    /* fall-through */
    }
    return new ReflectiveProxy<T>(injectionPoint, constructor);
}
Also used : Constructor(java.lang.reflect.Constructor) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 24 with InjectionPoint

use of com.google.inject.spi.InjectionPoint in project guice by google.

the class BindingBuilder method toConstructor.

@Override
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor, TypeLiteral<? extends S> type) {
    checkNotNull(constructor, "constructor");
    checkNotNull(type, "type");
    checkNotTargetted();
    BindingImpl<T> base = getBinding();
    Set<InjectionPoint> injectionPoints;
    try {
        injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
    } catch (ConfigurationException e) {
        copyErrorsToBinder(e);
        injectionPoints = e.getPartialValue();
    }
    try {
        InjectionPoint constructorPoint = InjectionPoint.forConstructor(constructor, type);
        setBinding(new ConstructorBindingImpl<T>(base.getKey(), base.getSource(), base.getScoping(), constructorPoint, injectionPoints));
    } catch (ConfigurationException e) {
        copyErrorsToBinder(e);
    }
    return this;
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) ConfigurationException(com.google.inject.ConfigurationException)

Example 25 with InjectionPoint

use of com.google.inject.spi.InjectionPoint in project guice by google.

the class FactoryProvider2 method getDependencies.

/** Calculates all dependencies required by the implementation and constructor. */
private Set<Dependency<?>> getDependencies(InjectionPoint ctorPoint, TypeLiteral<?> implementation) {
    ImmutableSet.Builder<Dependency<?>> builder = ImmutableSet.builder();
    builder.addAll(ctorPoint.getDependencies());
    if (!implementation.getRawType().isInterface()) {
        for (InjectionPoint ip : InjectionPoint.forInstanceMethodsAndFields(implementation)) {
            builder.addAll(ip.getDependencies());
        }
    }
    return builder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency)

Aggregations

InjectionPoint (com.google.inject.spi.InjectionPoint)31 ConfigurationException (com.google.inject.ConfigurationException)8 Dependency (com.google.inject.spi.Dependency)8 TypeLiteral (com.google.inject.TypeLiteral)5 Constructor (java.lang.reflect.Constructor)3 Field (java.lang.reflect.Field)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Key (com.google.inject.Key)2 Provider (com.google.inject.Provider)2 Errors (com.google.inject.internal.Errors)2 TypeListener (com.google.inject.spi.TypeListener)2 TypeListenerBinding (com.google.inject.spi.TypeListenerBinding)2 Member (java.lang.reflect.Member)2 Callback (net.sf.cglib.proxy.Callback)2 Enhancer (net.sf.cglib.proxy.Enhancer)2 FastClass (net.sf.cglib.reflect.FastClass)2 BytecodeGen.newFastClass (com.google.inject.internal.BytecodeGen.newFastClass)1 Message (com.google.inject.spi.Message)1 Shared (spock.lang.Shared)1