wrappingElement, mapType.unwrappedValueType(currentWrappingClass)); return Optional.of( possibleMapKey.toBuilder().type(mapOf(mapType.keyType(), wrappedValueType)).build());
/** * If {@code key}'s type is {@code Optional<T>} for some {@code T}, returns a key with the same * qualifier whose type is {@linkplain RequestKinds#extractKeyType(RequestKind, TypeMirror)} * extracted} from {@code T}. */ Optional<Key> unwrapOptional(Key key) { if (!OptionalType.isOptional(key)) { return Optional.empty(); } TypeMirror optionalValueType = OptionalType.from(key).valueType(); return Optional.of( key.toBuilder() .type(extractKeyType(getRequestKind(optionalValueType), optionalValueType)) .build()); } }
/** * If {@code key}'s type is {@code Set<WrappingClass<Bar>>}, returns a key with type {@code Set * <Bar>} with the same qualifier. Otherwise returns {@link Optional#empty()}. */ Optional<Key> unwrapSetKey(Key key, Class<?> wrappingClass) { if (SetType.isSet(key)) { SetType setType = SetType.from(key); if (!setType.isRawType() && setType.elementsAreTypeOf(wrappingClass)) { return Optional.of( key.toBuilder().type(setOf(setType.unwrappedElementType(wrappingClass))).build()); } } return Optional.empty(); }
/** * If {@code key}'s type is {@code Map<K, Provider<V>>}, {@code Map<K, Producer<V>>}, or {@code * Map<K, Produced<V>>}, returns a key with the same qualifier and {@link * Key#multibindingContributionIdentifier()} whose type is simply {@code Map<K, V>}. * * <p>Otherwise, returns {@code key}. */ Key unwrapMapValueType(Key key) { if (MapType.isMap(key)) { MapType mapType = MapType.from(key); if (!mapType.isRawType()) { for (Class<?> frameworkClass : asList(Provider.class, Producer.class, Produced.class)) { if (mapType.valuesAreTypeOf(frameworkClass)) { return key.toBuilder() .type(mapOf(mapType.keyType(), mapType.unwrappedValueType(frameworkClass))) .build(); } } } } return key; }
/** * If {@code key}'s type is {@code Map<K, Foo>} and {@code Foo} is not {@code WrappingClass * <Bar>}, returns a key with type {@code Map<K, WrappingClass<Foo>>} with the same qualifier. * Otherwise returns {@link Optional#empty()}. * * <p>Returns {@link Optional#empty()} if {@code WrappingClass} is not in the classpath. */ private Optional<Key> wrapMapKey(Key possibleMapKey, Class<?> wrappingClass) { if (MapType.isMap(possibleMapKey)) { MapType mapType = MapType.from(possibleMapKey); if (!mapType.isRawType() && !mapType.valuesAreTypeOf(wrappingClass)) { TypeElement wrappingElement = elements.getTypeElement(wrappingClass); if (wrappingElement == null) { // This target might not be compiled with Producers, so wrappingClass might not have an // associated element. return Optional.empty(); } DeclaredType wrappedValueType = types.getDeclaredType(wrappingElement, mapType.valueType()); return Optional.of( possibleMapKey.toBuilder().type(mapOf(mapType.keyType(), wrappedValueType)).build()); } } return Optional.empty(); }