Java Code Examples for com.google.inject.Key#getTypeLiteral()

The following examples show how to use com.google.inject.Key#getTypeLiteral() . 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: ComponentGraph.java    From vespa with Apache License 2.0 6 votes vote down vote up
private Optional<Node> lookupGlobalComponent(Key<?> key) {
    if (!(key.getTypeLiteral().getType() instanceof Class)) {
        throw new RuntimeException("Type not supported " + key.getTypeLiteral());
    }
    Class<?> clazz = key.getTypeLiteral().getRawType();

    Collection<ComponentNode> components = matchingComponentNodes(nodes(), key);
    if (components.isEmpty()) {
        return Optional.empty();
    } else if (components.size() == 1) {
        return Optional.ofNullable(Iterables.get(components, 0));
    } else {

        List<Node> nonProviderComponents = components.stream().filter(c -> !Provider.class.isAssignableFrom(c.instanceType()))
                .collect(Collectors.toList());
        if (nonProviderComponents.isEmpty()) {
            throw new IllegalStateException("Multiple global component providers for class '" + clazz.getName() + "' found");
        } else if (nonProviderComponents.size() == 1) {
            return Optional.of(nonProviderComponents.get(0));
        } else {
            throw new IllegalStateException("Multiple global components with class '" + clazz.getName() + "' found");
        }
    }
}
 
Example 2
Source File: PolyBind.java    From druid-api with Apache License 2.0 6 votes vote down vote up
/**
 * Binds an option for a specific choice.  The choice must already be registered on the injector for this to work.
 *
 * @param binder the binder for the injector that is being configured
 * @param interfaceKey the interface that will have an option added to it.  This must equal the
 *                     Key provided to createChoice
 * @param <T> interface type
 * @return A MapBinder that can be used to create the actual option bindings.
 */
public static <T> MapBinder<String, T> optionBinder(Binder binder, Key<T> interfaceKey)
{
  final TypeLiteral<T> interfaceType = interfaceKey.getTypeLiteral();

  if (interfaceKey.getAnnotation() != null) {
    return MapBinder.newMapBinder(
        binder, TypeLiteral.get(String.class), interfaceType, interfaceKey.getAnnotation()
    );
  }
  else if (interfaceKey.getAnnotationType() != null) {
    return MapBinder.newMapBinder(
        binder, TypeLiteral.get(String.class), interfaceType, interfaceKey.getAnnotationType()
    );
  }
  else {
    return MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), interfaceType);
  }
}
 
Example 3
Source File: MultipleSingletonPluginUITest.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void getN4JSSingletonsOfInjector(Injector injector, Multimap<Class<?>, Injector> singletonInstances) {
	for (Binding<?> b : injector.getAllBindings().values()) {
		if (isSingleton(b)) {
			Key<?> key = b.getKey();
			TypeLiteral<?> typeLiteral = key.getTypeLiteral();
			String typeName = typeLiteral.toString();
			if (typeName.contains("n4js")) {
				singletonInstances.put(typeLiteral.getRawType(), injector);
			}
		}
	}
}
 
Example 4
Source File: InnerFactoryManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public InnerFactoryManifest(Key<I> innerKey, TypeLiteral<O> outerType) {
    if(innerKey == null) {
        innerKey = Key.get(new ResolvableType<I>(){}.in(getClass()));
    }
    this.innerType = innerKey.getTypeLiteral();
    this.outerType = outerType != null ? outerType : new ResolvableType<O>(){}.in(getClass());

    this.factoryKey = innerKey.ofType(new ResolvableType<InnerFactory<O, I>>(){}
                                          .with(new TypeArgument<O>(this.outerType){},
                                                new TypeArgument<I>(this.innerType){}));
}
 
Example 5
Source File: SetBinder.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
protected SetBinder(Binder binder, @Nullable Key<T> key) {
    if(key == null) {
        key = Key.get(new ResolvableType<T>(){}.in(getClass()));
    }

    this.binder = binder.skipSources(SetBinder.class);
    this.elementType = key.getTypeLiteral();
    this.collectionType = new ResolvableType<Set<T>>(){}.with(new TypeArgument<T>(this.elementType){});
    this.multibinder = Multibinder.newSetBinder(binder, key);
}
 
Example 6
Source File: SettingsBinder.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
private <T> void bindSetting(Key<T> key, Object value) {
  if (value instanceof Key) {
    @SuppressWarnings("unchecked")
    Key<T> valueKey = (Key<T>) value;
    binder.bind(key).to(valueKey);
  } else {
    TypeLiteral<T> typeLiteral = key.getTypeLiteral();
    Class<? super T> rawType = typeLiteral.getRawType();
    @SuppressWarnings("unchecked")
    T cast = (T) rawType.cast(value);
    binder.bind(key).toInstance(cast);
  }
}
 
Example 7
Source File: DefaultApplicationContextTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private boolean isPinpointBinding(Key<?> key) {
    TypeLiteral<?> typeLiteral = key.getTypeLiteral();
    if (typeLiteral != null && typeLiteral.toString().startsWith(PINPOINT_PACKAGE_PREFIX)) {
        return true;
    }
    Class<? extends Annotation> annotationType = key.getAnnotationType();
    if (annotationType != null) {
        return annotationType.getName().startsWith(PINPOINT_PACKAGE_PREFIX);
    }
    return false;
}
 
Example 8
Source File: GuiceBeans.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private Set<Object> getBeans(Collection<Class<? extends Annotation>> annotations) {
	Set<Object> beans = U.set();

	for (Map.Entry<Key<?>, Binding<?>> e : injector.getAllBindings().entrySet()) {

		Key<?> key = e.getKey();
		Binding<?> value = e.getValue();

		boolean include = false;
		if (U.notEmpty(annotations)) {
			if (key.getTypeLiteral() != null && key.getTypeLiteral().getRawType() != null) {

				Class<?> type = key.getTypeLiteral().getRawType();
				if (Metadata.isAnnotatedAny(type, annotations)) {
					include = true;
				}
			}
		} else {
			include = true;
		}

		if (include) {
			beans.add(value.getProvider().get());
		}
	}

	return beans;
}