Search in sources :

Example 1 with InjectionPoint

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

the class ProxyFactoryTest method testSimpleCase.

public void testSimpleCase() throws NoSuchMethodException, InvocationTargetException, ErrorsException {
    SimpleInterceptor interceptor = new SimpleInterceptor();
    InjectionPoint injectionPoint = InjectionPoint.forConstructorOf(Simple.class);
    aspects.add(new MethodAspect(any(), any(), interceptor));
    ProxyFactory<Simple> factory = new ProxyFactory<Simple>(injectionPoint, aspects);
    ConstructionProxy<Simple> constructionProxy = factory.create();
    Simple simple = constructionProxy.newInstance();
    simple.invoke();
    assertTrue(simple.invoked);
    assertTrue(interceptor.invoked);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 2 with InjectionPoint

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

the class Providers method guicify.

/**
   * Returns a Guice-friendly {@code com.google.inject.Provider} for the given
   * JSR-330 {@code javax.inject.Provider}. The converse method is unnecessary,
   * since Guice providers directly implement the JSR-330 interface.
   * 
   * @since 3.0
   */
public static <T> Provider<T> guicify(javax.inject.Provider<T> provider) {
    if (provider instanceof Provider) {
        return (Provider<T>) provider;
    }
    final javax.inject.Provider<T> delegate = checkNotNull(provider, "provider");
    // Ensure that we inject all injection points from the delegate provider.
    Set<InjectionPoint> injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass());
    if (injectionPoints.isEmpty()) {
        return new GuicifiedProvider<T>(delegate);
    } else {
        Set<Dependency<?>> mutableDeps = Sets.newHashSet();
        for (InjectionPoint ip : injectionPoints) {
            mutableDeps.addAll(ip.getDependencies());
        }
        final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps);
        return new GuicifiedProviderWithDependencies<T>(dependencies, delegate);
    }
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency) Provider(com.google.inject.Provider)

Example 3 with InjectionPoint

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

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.missingImplementation(key);
    }
    // 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 4 with InjectionPoint

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

the class ConstructorBindingImpl method applyTo.

// 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 5 with InjectionPoint

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

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)

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