Search in sources :

Example 1 with ProvidesIntoMap

use of com.google.inject.multibindings.ProvidesIntoMap in project guice by google.

the class ProvidesMethodScanner method prepareMethod.

// mapKey doesn't know its key type
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <T> Key<T> prepareMethod(Binder binder, Annotation annotation, Key<T> key, InjectionPoint injectionPoint) {
    Method method = (Method) injectionPoint.getMember();
    AnnotationOrError mapKey = findMapKeyAnnotation(binder, method);
    if (annotation instanceof Provides) {
        if (mapKey.annotation != null) {
            binder.addError("Found a MapKey annotation on non map binding at %s.", method);
        }
        // no key rewriting for plain old @Provides
        return key;
    }
    if (annotation instanceof ProvidesIntoSet) {
        if (mapKey.annotation != null) {
            binder.addError("Found a MapKey annotation on non map binding at %s.", method);
        }
        return RealMultibinder.newRealSetBinder(binder, key).getKeyForNewItem();
    } else if (annotation instanceof ProvidesIntoMap) {
        if (mapKey.error) {
            // Already failed on the MapKey, don't bother doing more work.
            return key;
        }
        if (mapKey.annotation == null) {
            // If no MapKey, make an error and abort.
            binder.addError("No MapKey found for map binding at %s.", method);
            return key;
        }
        TypeAndValue typeAndValue = typeAndValueOfMapKey(mapKey.annotation);
        return RealMapBinder.newRealMapBinder(binder, typeAndValue.type, key).getKeyForNewValue(typeAndValue.value);
    } else if (annotation instanceof ProvidesIntoOptional) {
        if (mapKey.annotation != null) {
            binder.addError("Found a MapKey annotation on non map binding at %s.", method);
        }
        switch(((ProvidesIntoOptional) annotation).value()) {
            case DEFAULT:
                return RealOptionalBinder.newRealOptionalBinder(binder, key).getKeyForDefaultBinding();
            case ACTUAL:
                return RealOptionalBinder.newRealOptionalBinder(binder, key).getKeyForActualBinding();
        }
    }
    throw new IllegalStateException("Invalid annotation: " + annotation);
}
Also used : ProvidesIntoMap(com.google.inject.multibindings.ProvidesIntoMap) ProvidesIntoOptional(com.google.inject.multibindings.ProvidesIntoOptional) Method(java.lang.reflect.Method) Provides(com.google.inject.Provides) ProvidesIntoSet(com.google.inject.multibindings.ProvidesIntoSet)

Aggregations

Provides (com.google.inject.Provides)1 ProvidesIntoMap (com.google.inject.multibindings.ProvidesIntoMap)1 ProvidesIntoOptional (com.google.inject.multibindings.ProvidesIntoOptional)1 ProvidesIntoSet (com.google.inject.multibindings.ProvidesIntoSet)1 Method (java.lang.reflect.Method)1