com.google.inject.spi.InjectionPoint Java Examples

The following examples show how to use com.google.inject.spi.InjectionPoint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuiceyInterceptor.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
private void injectValues(final Object target, final boolean sharedFields) throws IllegalAccessException {
    for (InjectionPoint point : injectionPoints) {
        if (!(point.getMember() instanceof Field)) {
            throw new GuiceyExtensionException("Method injection is not supported; use field injection instead");
        }

        final Field field = (Field) point.getMember();
        if (field.isAnnotationPresent(Shared.class) != sharedFields) {
            continue;
        }

        final Object value = injector.getInstance(point.getDependencies().get(0).getKey());
        field.setAccessible(true);
        field.set(target, value);
    }
    // ClientSupport fields
    SpecialFieldsSupport.initClients(target, clientFields, support.getClient(), sharedFields);
}
 
Example #2
Source File: GuiceyInterceptor.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
public GuiceyInterceptor(final SpecInfo spec, final EnvironmentSupport support,
                         final List<GuiceyConfigurationHook> hooks) {
    this.support = support;
    this.hooks = hooks;
    injectionPoints = InjectionPoint.forInstanceMethodsAndFields(spec.getReflection());
    clientFields = SpecialFieldsSupport.findClientFields(spec.getReflection());
}
 
Example #3
Source File: InjectableMethod.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private <D> InjectableMethod(@Nullable TypeLiteral<D> targetType, @Nullable D target, Method method, @Nullable T result) {
    final Errors errors = new Errors(method);

    if(Members.isStatic(method)) {
        checkArgument(target == null);
    } else {
        checkArgument(target != null);
    }

    targetType = targetType(targetType, target, method);

    checkArgument(method.getDeclaringClass().isAssignableFrom(targetType.getRawType()));

    this.method = method;
    this.dependencies = ImmutableSet.copyOf(InjectionPoint.forMethod(method, targetType).getDependencies());

    if(result != null) {
        this.result = result;
        this.providedKey = Keys.forInstance(result);
    } else {
        final TypeLiteral<T> returnType = (TypeLiteral<T>) targetType.getReturnType(method);
        if(!Void.class.equals(returnType.getRawType())) {
            final Annotation qualifier = Annotations.findBindingAnnotation(errors, method, method.getAnnotations());
            this.result = null;
            this.providedKey = Keys.get(returnType, qualifier);
        } else {
            this.result = (T) this;
            this.providedKey = Keys.forInstance(this.result);
        }
    }

    this.scope = Annotations.findScopeAnnotation(errors, method.getAnnotations());

    MethodHandle handle = MethodHandleUtils.privateUnreflect(method);
    if(target != null) {
        handle = handle.bindTo(target);
    }
    this.handle = handle;
}
 
Example #4
Source File: InjectionChecks.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void checkInjectableCGLibProxyBase(Class<?> cls) {
    InjectionPoint.forInstanceMethodsAndFields(cls).forEach(ip -> {
        if(ip.getMember() instanceof Method && !Modifier.isPrivate(ip.getMember().getModifiers())) {
            // CGLib proxies override all non-private methods on the base class,
            // and do not copy method annotations, so Guice will not find the
            // @Inject annotation on the base method. Declaring the method
            // private works around this. The proxy will not try to override
            // private methods, and Guice can find them just fine.
            throw new MethodFormException(
                (Method) ip.getMember(),
                "Injected method on CGLib proxied class must be private (see exception site for details)"
            );
        }
    });
}
 
Example #5
Source File: MemberInjectingFactory.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Inject public MemberInjectingFactory(TypeLiteral<T> type, MembersInjector<T> injector) {
    this.type = type;
    this.injector = injector;
    this.injectionPoint = InjectionPoint.forConstructorOf(type);
    this.constructor = (Constructor<T>) injectionPoint.getMember();
    this.constructor.setAccessible(true);

    dependencies.addAll(Dependency.forInjectionPoints(InjectionPoint.forInstanceMethodsAndFields(type)));
}
 
Example #6
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public DependencyCollector log(Logger logger, Level level) {
    logger.log(level, "Dumping all dependencies:");
    for(Map.Entry<TypeLiteral<?>, Collection<InjectionPoint>> entry : injectionPointsByType().asMap().entrySet()) {
        logger.log(level, entry.getKey().toString());
        for(InjectionPoint ip : entry.getValue()) {
            logger.log(level, "  " + ip.getMember());
            for(Dependency<?> dep : dependenciesByInjectionPoint().get(ip)) {
                logger.log(level, "    " + dep);
            }
        }
    }
    return this;
}
 
Example #7
Source File: Injection.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Return all direct dependencies injected into the given type
 */
public static Stream<Dependency<?>> dependencies(Class<?> type) {
    return Stream.concat(
        Stream.of(InjectionPoint.forConstructorOf(type)),
        InjectionPoint.forInstanceMethodsAndFields(type).stream()
    ).flatMap(ip -> ip.getDependencies().stream());
}
 
Example #8
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processInstanceInjections(TypeLiteral<?> type) {
    InjectionPoint.forInstanceMethodsAndFields(type).forEach(this::processInjectionPoint);
}
 
Example #9
Source File: InnerFactoryManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void configure() {
    final InjectionPoint point = InjectionPoint.forConstructorOf(innerType);
    final Constructor<I> constructor = (Constructor<I>) point.getMember();
    constructor.setAccessible(true);

    if(point.getDependencies().isEmpty() || !Types.isAssignable(point.getDependencies().get(0).getKey().getTypeLiteral(), outerType)) {
        addError("Expected %s to take %s as the first parameter of its injectable constructor", innerType, outerType);
        return;
    }

    final Set<Dependency<?>> dependencies = point.getDependencies()
                                                 .stream()
                                                 .skip(1)
                                                 .collect(Collectors.toImmutableSet());

    final List<Provider<?>> providers = dependencies.stream()
                                                    .map(dep -> getProvider(dep.getKey()))
                                                    .collect(Collectors.toImmutableList());

    final MembersInjector<I> membersInjector = getMembersInjector(innerType);

    class FactoryImpl implements InnerFactory<O, I>, HasDependencies {
        @Override
        public Set<Dependency<?>> getDependencies() {
            return dependencies;
        }

        public I create(O outer) {
            final Object[] args = new Object[providers.size() + 1];

            args[0] = outer;

            for(int i = 0; i < providers.size(); i++) {
                args[i + 1] = providers.get(i).get();
            }

            return Injection.wrappingExceptions(() -> {
                final I instance = constructor.newInstance(args);
                membersInjector.injectMembers(instance);
                return instance;
            });
        }
    }

    bind(factoryKey).toInstance(new FactoryImpl());
}
 
Example #10
Source File: Scoper.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Void visit(ConstructorBinding<? extends T> binding) {
    final InjectionPoint point = binding.getConstructor();
    scope(binding, rebind(binding).toConstructor((Constructor) point.getMember(), point.getDeclaringType()));
    return null;
}
 
Example #11
Source File: Dependencies.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Stream<Dependency<?>> forInstanceMethodsAndFields(TypeLiteral<?> type) {
    return InjectionPoint.forInstanceMethodsAndFields(type)
                         .stream()
                         .flatMap(point -> point.getDependencies().stream());
}
 
Example #12
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processInjections(TypeLiteral<?> type) {
    processInjectionPoint(InjectionPoint.forConstructorOf(type));
    processInstanceInjections(type);
}
 
Example #13
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processInjectionPoints(Iterable<InjectionPoint> injectionPoint) {
    injectionPoint.forEach(this::processInjectionPoint);
}
 
Example #14
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processInjectionPoint(InjectionPoint injectionPoint) {
    injectionPointsByType.put(injectionPoint.getDeclaringType(), injectionPoint);
    injectionPoint.getDependencies().forEach(this::processDependency);
}
 
Example #15
Source File: Injection.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static <T> Constructor<T> injectableConstructor(TypeLiteral<T> type) {
    return (Constructor<T>) InjectionPoint.forConstructorOf(type).getMember();
}
 
Example #16
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
public SetMultimap<TypeLiteral<?>, InjectionPoint> injectionPointsByType() { return injectionPointsByType; } 
Example #17
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
public SetMultimap<InjectionPoint, Dependency<?>> dependenciesByInjectionPoint() { return dependenciesByInjectionPoint; } 
Example #18
Source File: Injection.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
@Override public <T> Key<T> prepareMethod(Binder binder, Annotation annotation, Key<T> key, InjectionPoint injectionPoint) { return key; }