Search in sources :

Example 11 with InjectionPoint

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

the class ConstructorBindingImpl method applyTo.

@Override
// the raw constructor member and declaring type always agree
@SuppressWarnings("unchecked")
public void applyTo(Binder binder) {
    InjectionPoint constructor = getConstructor();
    getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).toConstructor((Constructor) getConstructor().getMember(), (TypeLiteral) constructor.getDeclaringType()));
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Constructor(java.lang.reflect.Constructor)

Example 12 with InjectionPoint

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

the class ConstructorBindingImpl method create.

/**
   * @param constructorInjector the constructor to use, or {@code null} to use the default.
   * @param failIfNotLinked true if this ConstructorBindingImpl's InternalFactory should only
   *     succeed if retrieved from a linked binding
   */
static <T> ConstructorBindingImpl<T> create(InjectorImpl injector, Key<T> key, InjectionPoint constructorInjector, Object source, Scoping scoping, Errors errors, boolean failIfNotLinked, boolean failIfNotExplicit) throws ErrorsException {
    int numErrors = errors.size();
    // constructorBinding guarantees type is consistent
    @SuppressWarnings("unchecked") Class<? super T> rawType = constructorInjector == null ? key.getTypeLiteral().getRawType() : (Class) constructorInjector.getDeclaringType().getRawType();
    // We can't inject abstract classes.
    if (Modifier.isAbstract(rawType.getModifiers())) {
        errors.missingImplementationWithHint(key, injector);
    }
    // Error: Inner class.
    if (Classes.isInnerClass(rawType)) {
        errors.cannotInjectInnerClass(rawType);
    }
    errors.throwIfNewErrors(numErrors);
    // Find a constructor annotated @Inject
    if (constructorInjector == null) {
        try {
            constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral());
            if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) {
                errors.atInjectRequired(rawType);
            }
        } catch (ConfigurationException e) {
            throw errors.merge(e.getErrorMessages()).toException();
        }
    }
    // if no scope is specified, look for a scoping annotation on the concrete class
    if (!scoping.isExplicitlyScoped()) {
        Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass();
        Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType);
        if (scopeAnnotation != null) {
            scoping = Scoping.makeInjectable(Scoping.forAnnotation(scopeAnnotation), injector, errors.withSource(rawType));
        }
    }
    errors.throwIfNewErrors(numErrors);
    Factory<T> factoryFactory = new Factory<T>(failIfNotLinked, key);
    InternalFactory<? extends T> scopedFactory = Scoping.scope(key, injector, factoryFactory, source, scoping);
    return new ConstructorBindingImpl<T>(injector, key, source, scopedFactory, scoping, factoryFactory, constructorInjector);
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 13 with InjectionPoint

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

the class ConstructorInjectorStore method createConstructor.

private <T> ConstructorInjector<T> createConstructor(InjectionPoint injectionPoint, Errors errors) throws ErrorsException {
    int numErrorsBefore = errors.size();
    SingleParameterInjector<?>[] constructorParameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
    // the injector type agrees with the injection point type
    @SuppressWarnings("unchecked") MembersInjectorImpl<T> membersInjector = (MembersInjectorImpl<T>) injector.membersInjectorStore.get(injectionPoint.getDeclaringType(), errors);
    /*if[AOP]*/
    ImmutableList<MethodAspect> injectorAspects = injector.state.getMethodAspects();
    ImmutableList<MethodAspect> methodAspects = membersInjector.getAddedAspects().isEmpty() ? injectorAspects : ImmutableList.copyOf(concat(injectorAspects, membersInjector.getAddedAspects()));
    ConstructionProxyFactory<T> factory = new ProxyFactory<T>(injectionPoint, methodAspects);
    /*end[AOP]*/
    /*if[NO_AOP]
    ConstructionProxyFactory<T> factory = new DefaultConstructionProxyFactory<T>(injectionPoint);
    end[NO_AOP]*/
    errors.throwIfNewErrors(numErrorsBefore);
    return new ConstructorInjector<T>(membersInjector.getInjectionPoints(), factory.create(), constructorParameterInjectors, membersInjector);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 14 with InjectionPoint

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

the class Errors method formatSource.

public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) {
    String modules = moduleSourceString(elementSource);
    if (source instanceof Dependency) {
        Dependency<?> dependency = (Dependency<?>) source;
        InjectionPoint injectionPoint = dependency.getInjectionPoint();
        if (injectionPoint != null) {
            formatInjectionPoint(formatter, dependency, injectionPoint, elementSource);
        } else {
            formatSource(formatter, dependency.getKey(), elementSource);
        }
    } else if (source instanceof InjectionPoint) {
        formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource);
    } else if (source instanceof Class) {
        formatter.format("  at %s%s%n", StackTraceElements.forType((Class<?>) source), modules);
    } else if (source instanceof Member) {
        formatter.format("  at %s%s%n", StackTraceElements.forMember((Member) source), modules);
    } else if (source instanceof TypeLiteral) {
        formatter.format("  while locating %s%s%n", source, modules);
    } else if (source instanceof Key) {
        Key<?> key = (Key<?>) source;
        formatter.format("  while locating %s%n", convert(key, elementSource));
    } else if (source instanceof Thread) {
        formatter.format("  in thread %s%n", source);
    } else {
        formatter.format("  at %s%s%n", source, modules);
    }
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency) Member(java.lang.reflect.Member) Key(com.google.inject.Key)

Example 15 with InjectionPoint

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

the class ProxyFactory method create.

@Override
public ConstructionProxy<T> create() throws ErrorsException {
    if (interceptors.isEmpty()) {
        return new DefaultConstructionProxyFactory<T>(injectionPoint).create();
    }
    @SuppressWarnings("unchecked") Class<? extends Callback>[] callbackTypes = new Class[callbacks.length];
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] == net.sf.cglib.proxy.NoOp.INSTANCE) {
            callbackTypes[i] = net.sf.cglib.proxy.NoOp.class;
        } else {
            callbackTypes[i] = net.sf.cglib.proxy.MethodInterceptor.class;
        }
    }
    // to this injector. Otherwise, the proxies for each injector will waste PermGen memory
    try {
        Enhancer enhancer = BytecodeGen.newEnhancer(declaringClass, visibility);
        enhancer.setCallbackFilter(new IndicesCallbackFilter(methods));
        enhancer.setCallbackTypes(callbackTypes);
        return new ProxyConstructor<T>(enhancer, injectionPoint, callbacks, interceptors);
    } catch (Throwable e) {
        throw new Errors().errorEnhancingClass(declaringClass, e).toException();
    }
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) InjectionPoint(com.google.inject.spi.InjectionPoint) Callback(net.sf.cglib.proxy.Callback) FastClass(net.sf.cglib.reflect.FastClass)

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