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

The following examples show how to use com.google.inject.Key#getAnnotationType() . 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: LinStorScope.java    From linstor-server with GNU General Public License v3.0 6 votes vote down vote up
private <T> Map<Key<?>, Object> getScopedObjectMap(Key<T> key)
{
    Map<Key<?>, Object> scopedObjects = values.get();
    if (scopedObjects == null)
    {
        Annotation annotation = key.getAnnotation();
        Class<? extends Annotation> annotationType = key.getAnnotationType();

        boolean isErrorReporterContext = annotation != null && (annotation instanceof ErrorReporterContext);
        isErrorReporterContext |= annotationType != null && annotationType.equals(ErrorReporterContext.class);
        if (!isErrorReporterContext)
        {
            throw new OutOfScopeException(
                "Cannot access " + key + " outside of a scoping block"
            );
        }
    }
    return scopedObjects;
}
 
Example 2
Source File: JsonConfigProvider.java    From druid-api with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> void bindInstance(
    Binder binder,
    Key<T> bindKey,
    T instance
)
{
  binder.bind(bindKey).toInstance(instance);

  final ParameterizedType supType = Types.newParameterizedType(Supplier.class, bindKey.getTypeLiteral().getType());
  final Key supplierKey;

  if (bindKey.getAnnotationType() != null) {
    supplierKey = Key.get(supType, bindKey.getAnnotationType());
  }
  else if (bindKey.getAnnotation() != null) {
    supplierKey = Key.get(supType, bindKey.getAnnotation());
  }
  else {
    supplierKey = Key.get(supType);
  }

  binder.bind(supplierKey).toInstance(Suppliers.<T>ofInstance(instance));
}
 
Example 3
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 4
Source File: GuiceModelUtils.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
/**
 * @param key guice binding key
 * @return string representation for key or "-" if key is null
 */
public static String renderKey(final Key key) {
    if (key == null) {
        return "-";
    }
    final StringBuilder res = new StringBuilder();
    if (key.getAnnotationType() != null) {
        res.append('@').append(key.getAnnotationType().getSimpleName());
        for (Method method : key.getAnnotationType().getMethods()) {
            if (method.getName().equals("value") && method.getReturnType().equals(String.class)) {
                final boolean accessible = method.isAccessible();
                try {
                    method.setAccessible(true);
                    final String qualifier = (String) method.invoke(key.getAnnotation());
                    if (qualifier != null && !qualifier.isEmpty()) {
                        res.append("(\"").append(qualifier).append("\")");
                    }
                    break;
                } catch (Exception e) {
                    throw new IllegalStateException("Failed to inspect annotation", e);
                } finally {
                    method.setAccessible(accessible);
                }
            }
        }
        res.append(' ');
    }
    res.append(TypeToStringUtils.toStringType(key.getTypeLiteral().getType(), EmptyGenericsMap.getInstance()));
    return res.toString();
}
 
Example 5
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;
}