Search in sources :

Example 26 with InjectionPoint

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

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 27 with InjectionPoint

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

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 28 with InjectionPoint

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

the class ProxyFactory method create.

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) BytecodeGen.newFastClass(com.google.inject.internal.BytecodeGen.newFastClass) FastClass(net.sf.cglib.reflect.FastClass)

Example 29 with InjectionPoint

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

the class BindingBuilder method toConstructor.

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 30 with InjectionPoint

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

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 {
        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)

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