com.google.inject.spi.Dependency Java Examples

The following examples show how to use com.google.inject.spi.Dependency. 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: ModuleDependencyTransformer.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
public ModuleDependencyTransformer(TypeLiteral<M> type) {
    final ModuleDescription annotation = type.getRawType().getAnnotation(ModuleDescription.class);
    if(annotation == null) {
        requiresKeys = ImmutableSet.of();
        dependsKeys = followsKeys = ImmutableSet.of();
    } else {
        requiresKeys = Keys.get(annotation.requires());
        dependsKeys = Keys.optional(annotation.depends());
        followsKeys = Keys.optional(annotation.follows());
    }

    dependencies = Streams.concat(requiresKeys.stream(),
                                  dependsKeys.stream(),
                                  followsKeys.stream())
                          .map(Dependency::get)
                          .collect(Collectors.toImmutableSet());
}
 
Example #2
Source File: AbstractDynamicProvider.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private Class<?> getRequiredType() {
 	
 		Reflect context = Reflect.on(injector).call("enterContext");
try {
	Dependency<?> dependency = context.call("getDependency").get();
	return dependency.getKey().getTypeLiteral().getRawType();
} finally {
	context.call("close");
}
 }
 
Example #3
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 #4
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 #5
Source File: InjectableMethod.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public Provider<T> asProvider() {
    return new ProviderWithDependencies<T>() {
        @Override
        public T get() {
            return Injection.wrappingExceptions(asSupplier());
        }

        @Override
        public Set<Dependency<?>> getDependencies() {
            return dependencies;
        }
    };
}
 
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: ModuleDependencyTransformer.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<Dependency<?>> getDependencies() {
    return dependencies;
}
 
Example #8
Source File: MemberInjectingFactory.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<Dependency<?>> getDependencies() {
    return dependencies;
}
 
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: 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 #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(Class<?> type) {
    return forInstanceMethodsAndFields(TypeLiteral.get(type));
}
 
Example #12
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processDependency(Dependency<?> dependency) {
    dependenciesByKey.put(dependency.getKey(), dependency);
    dependenciesByInjectionPoint.put(dependency.getInjectionPoint(), dependency);
    requireKey(dependency.getKey());
}
 
Example #13
Source File: InjectableMethod.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<Dependency<?>> getDependencies() {
    return dependencies;
}
 
Example #14
Source File: ModuleManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<Dependency<?>> getDependencies() {
    return dependencies;
}
 
Example #15
Source File: ModuleManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
<T> void addDependency(List<Provider<? extends ProvisionWrapper<?>>> list, Class<T> module) {
    final Key<ProvisionWrapper<T>> key = ProvisionWrapper.keyOf(TypeLiteral.get(module));
    final Provider<ProvisionWrapper<T>> provider = getProvider(key);
    dependencies.add(Dependency.get(key));
    list.add(provider);
}
 
Example #16
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 #17
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
public SetMultimap<Key<?>, Dependency<?>> dependenciesByKey() { return dependenciesByKey; }