Search in sources :

Example 16 with InjectionPoint

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

the class MembersInjectorStore method createWithListeners.

/** Creates a new members injector and attaches both injection listeners and method aspects. */
private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors) throws ErrorsException {
    int numErrorsBefore = errors.size();
    Set<InjectionPoint> injectionPoints;
    try {
        injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
    } catch (ConfigurationException e) {
        errors.merge(e.getErrorMessages());
        injectionPoints = e.getPartialValue();
    }
    ImmutableList<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors);
    errors.throwIfNewErrors(numErrorsBefore);
    EncounterImpl<T> encounter = new EncounterImpl<T>(errors, injector.lookups);
    Set<TypeListener> alreadySeenListeners = Sets.newHashSet();
    for (TypeListenerBinding binding : typeListenerBindings) {
        TypeListener typeListener = binding.getListener();
        if (!alreadySeenListeners.contains(typeListener) && binding.getTypeMatcher().matches(type)) {
            alreadySeenListeners.add(typeListener);
            try {
                typeListener.hear(type, encounter);
            } catch (RuntimeException e) {
                errors.errorNotifyingTypeListener(binding, type, e);
            }
        }
    }
    encounter.invalidate();
    errors.throwIfNewErrors(numErrorsBefore);
    return new MembersInjectorImpl<T>(injector, type, encounter, injectors);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) InjectionPoint(com.google.inject.spi.InjectionPoint) TypeListenerBinding(com.google.inject.spi.TypeListenerBinding) ConfigurationException(com.google.inject.ConfigurationException) TypeListener(com.google.inject.spi.TypeListener)

Example 17 with InjectionPoint

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

the class MembersInjectorStore method getInjectors.

/** Returns the injectors for the specified injection points. */
ImmutableList<SingleMemberInjector> getInjectors(Set<InjectionPoint> injectionPoints, Errors errors) {
    List<SingleMemberInjector> injectors = Lists.newArrayList();
    for (InjectionPoint injectionPoint : injectionPoints) {
        try {
            Errors errorsForMember = injectionPoint.isOptional() ? new Errors(injectionPoint) : errors.withSource(injectionPoint);
            SingleMemberInjector injector = injectionPoint.getMember() instanceof Field ? new SingleFieldInjector(this.injector, injectionPoint, errorsForMember) : new SingleMethodInjector(this.injector, injectionPoint, errorsForMember);
            injectors.add(injector);
        } catch (ErrorsException ignoredForNow) {
        // ignored for now
        }
    }
    return ImmutableList.copyOf(injectors);
}
Also used : Field(java.lang.reflect.Field) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 18 with InjectionPoint

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

the class FactoryProvider2 method findMatchingConstructorInjectionPoint.

/**
   * Finds a constructor suitable for the method. If the implementation contained any constructors
   * marked with {@link AssistedInject}, this requires all {@link Assisted} parameters to exactly
   * match the parameters (in any order) listed in the method. Otherwise, if no {@link
   * AssistedInject} constructors exist, this will default to looking for an {@literal @}{@link
   * Inject} constructor.
   */
private <T> InjectionPoint findMatchingConstructorInjectionPoint(Method method, Key<?> returnType, TypeLiteral<T> implementation, List<Key<?>> paramList) throws ErrorsException {
    Errors errors = new Errors(method);
    if (returnType.getTypeLiteral().equals(implementation)) {
        errors = errors.withSource(implementation);
    } else {
        errors = errors.withSource(returnType).withSource(implementation);
    }
    Class<?> rawType = implementation.getRawType();
    if (Modifier.isInterface(rawType.getModifiers())) {
        errors.addMessage("%s is an interface, not a concrete class.  Unable to create AssistedInject factory.", implementation);
        throw errors.toException();
    } else if (Modifier.isAbstract(rawType.getModifiers())) {
        errors.addMessage("%s is abstract, not a concrete class.  Unable to create AssistedInject factory.", implementation);
        throw errors.toException();
    } else if (Classes.isInnerClass(rawType)) {
        errors.cannotInjectInnerClass(rawType);
        throw errors.toException();
    }
    Constructor<?> matchingConstructor = null;
    boolean anyAssistedInjectConstructors = false;
    // Look for AssistedInject constructors...
    for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
        if (constructor.isAnnotationPresent(AssistedInject.class)) {
            anyAssistedInjectConstructors = true;
            if (constructorHasMatchingParams(implementation, constructor, paramList, errors)) {
                if (matchingConstructor != null) {
                    errors.addMessage("%s has more than one constructor annotated with @AssistedInject" + " that matches the parameters in method %s.  Unable to create " + "AssistedInject factory.", implementation, method);
                    throw errors.toException();
                } else {
                    matchingConstructor = constructor;
                }
            }
        }
    }
    if (!anyAssistedInjectConstructors) {
        // If none existed, use @Inject.
        try {
            return InjectionPoint.forConstructorOf(implementation);
        } catch (ConfigurationException e) {
            errors.merge(e.getErrorMessages());
            throw errors.toException();
        }
    } else {
        // Otherwise, use it or fail with a good error message.
        if (matchingConstructor != null) {
            // safe because we got the constructor from this implementation.
            @SuppressWarnings("unchecked") InjectionPoint ip = InjectionPoint.forConstructor((Constructor<? super T>) matchingConstructor, implementation);
            return ip;
        } else {
            errors.addMessage("%s has @AssistedInject constructors, but none of them match the" + " parameters in method %s.  Unable to create AssistedInject factory.", implementation, method);
            throw errors.toException();
        }
    }
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 19 with InjectionPoint

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

the class GraphvizGrapher method newDependencyEdge.

@Override
protected void newDependencyEdge(DependencyEdge edge) {
    GraphvizEdge gedge = new GraphvizEdge(edge.getFromId(), edge.getToId());
    InjectionPoint fromPoint = edge.getInjectionPoint();
    if (fromPoint == null) {
        gedge.setTailPortId("header");
    } else {
        gedge.setTailPortId(portIdFactory.getPortId(fromPoint.getMember()));
    }
    gedge.setArrowHead(ImmutableList.of(ArrowType.NORMAL));
    gedge.setTailCompassPoint(CompassPoint.EAST);
    edges.add(gedge);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 20 with InjectionPoint

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

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)

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