@Override public boolean matches(TypeLiteral<?> typeLiteral) { Type type = typeLiteral.getType(); if (!(type instanceof Class)) { return false; } Class<?> clazz = (Class<?>) type; return typeMatcher.matches(clazz); }
@SuppressWarnings("unchecked") // a provider map <K, V> is safely a Map<K, Provider<V>> private static <K, V> TypeLiteral<Map<K, Provider<V>>> mapOfProviderOf( TypeLiteral<K> keyType, TypeLiteral<V> valueType) { return (TypeLiteral<Map<K, Provider<V>>>) TypeLiteral.get( Types.mapOf(keyType.getType(), newParameterizedType(Provider.class, valueType.getType()))); }
@SuppressWarnings("unchecked") // a provider entry <K, V> is safely a Map.Entry<K, Provider<V>> private static <K, V> TypeLiteral<Map.Entry<K, Provider<V>>> entryOfProviderOf( TypeLiteral<K> keyType, TypeLiteral<V> valueType) { return (TypeLiteral<Entry<K, Provider<V>>>) TypeLiteral.get(newParameterizedTypeWithOwner( Map.class, Entry.class, keyType.getType(), Types.providerOf(valueType.getType()))); }
protected boolean keyTypeIsSet() { return !Void.class.equals(binding.getKey().getTypeLiteral().getType()); }
@SuppressWarnings("unchecked") AssistedConstructor(Constructor<T> constructor, List<TypeLiteral<?>> parameterTypes) { this.constructor = constructor; Annotation[][] annotations = constructor.getParameterAnnotations(); List<Type> typeList = new ArrayList<>(); allParameters = new ArrayList<>(parameterTypes.size()); // categorize params as @Assisted or @Injected for (int i = 0; i < parameterTypes.size(); i++) { Parameter parameter = new Parameter(parameterTypes.get(i).getType(), annotations[i]); allParameters.add(parameter); if (parameter.isProvidedByFactory()) { typeList.add(parameter.getType()); } } this.assistedParameters = new ParameterListKey(typeList); }
/** * Gets the type of this type's provider. */ @SuppressWarnings("unchecked") final TypeLiteral<Provider<T>> providerType() { // This cast is safe and wouldn't generate a warning if Type had a type // parameter. return (TypeLiteral<Provider<T>>) get(Types.providerOf(getType())); }
@SuppressWarnings("unchecked") // wrapping a T in a Set safely returns a Set<T> private static <T> TypeLiteral<Set<T>> setOf(TypeLiteral<T> elementType) { Type type = Types.setOf(elementType.getType()); return (TypeLiteral<Set<T>>) TypeLiteral.get(type); }
/** * Returns an equivalent type that's safe for use in a key. The returned type will be free of * primitive types. Type literals of primitives will return the corresponding wrapper types. * * @throws ConfigurationException if {@code type} contains a type variable */ public static <T> TypeLiteral<T> makeKeySafe(TypeLiteral<T> type) { if (!isFullySpecified(type.getType())) { String message = type + " cannot be used as a key; It is not fully specified."; throw new ConfigurationException(singleton(new Message(message))); } @SuppressWarnings("unchecked") TypeLiteral<T> wrappedPrimitives = (TypeLiteral<T>) PRIMITIVE_TO_WRAPPER.get(type); return wrappedPrimitives != null ? wrappedPrimitives : type; }
private static <M extends Member & AnnotatedElement> void addInjectionPoints(TypeLiteral<?> type, Factory<M> factory, boolean statics, Collection<InjectionPoint> injectionPoints, Errors errors) { if (type.getType() == Object.class) { return; } // Add injectors for superclass first. TypeLiteral<?> superType = type.getSupertype(type.getRawType().getSuperclass()); addInjectionPoints(superType, factory, statics, injectionPoints, errors); // Add injectors for all members next addInjectorsForMembers(type, factory, statics, injectionPoints, errors); }
private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding( Key<MembersInjector<T>> key, Errors errors) throws ErrorsException { Type membersInjectorType = key.getTypeLiteral().getType(); if (!(membersInjectorType instanceof ParameterizedType)) { throw errors.cannotInjectRawMembersInjector().toException(); } @SuppressWarnings("unchecked") // safe because T came from Key<MembersInjector<T>> TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get( ((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]); MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors); InternalFactory<MembersInjector<T>> factory = new ConstantFactory<>( Initializables.of(membersInjector)); return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), membersInjector); }
private static <M extends Member & AnnotatedElement> void addInjectorsForMembers( TypeLiteral<?> typeLiteral, Factory<M> factory, boolean statics, Collection<InjectionPoint> injectionPoints, Errors errors) { for (M member : factory.getMembers(getRawType(typeLiteral.getType()))) { if (isStatic(member) != statics) { continue; } Inject inject = member.getAnnotation(Inject.class); if (inject == null) { continue; } try { injectionPoints.add(factory.create(typeLiteral, member, errors)); } catch (ConfigurationException ignorable) { if (!inject.optional()) { errors.merge(ignorable.getErrorMessages()); } } } }
/** * Creates a synthetic binding to {@code Provider<T>}, i.e. a binding to the provider from * {@code Binding<T>}. */ private <T> BindingImpl<Provider<T>> createProviderBinding(Key<Provider<T>> key, Errors errors) throws ErrorsException { Type providerType = key.getTypeLiteral().getType(); // If the Provider has no type parameter (raw Provider)... if (!(providerType instanceof ParameterizedType)) { throw errors.cannotInjectRawProvider().toException(); } Type entryType = ((ParameterizedType) providerType).getActualTypeArguments()[0]; @SuppressWarnings("unchecked") // safe because T came from Key<Provider<T>> Key<T> providedKey = (Key<T>) key.ofType(entryType); BindingImpl<T> delegate = getBindingOrThrow(providedKey, errors); return new ProviderBindingImpl<>(this, key, delegate); }
/** * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's * a bit awkward because we have to pull out the inner type in the type literal. */ private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding( Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException { Type typeLiteralType = key.getTypeLiteral().getType(); if (!(typeLiteralType instanceof ParameterizedType)) { throw errors.cannotInjectRawTypeLiteral().toException(); } ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType; Type innerType = parameterizedType.getActualTypeArguments()[0]; // this is unfortunate. We don't support building TypeLiterals for type variable like 'T'. If // this proves problematic, we can probably fix TypeLiteral to support type variables if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) { throw errors.cannotInjectTypeLiteralOf(innerType).toException(); } @SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType); InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>( Initializables.of(value)); return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), value); }
Class<?> rawType = getRawType(type.getType()); Errors errors = new Errors(rawType);
@SuppressWarnings("unchecked") // a provider entry <K, V> is safely a Map.Entry<K, Provider<V>> private static <K, V> TypeLiteral<Map.Entry<K, Provider<V>>> entryOfProviderOf( TypeLiteral<K> keyType, TypeLiteral<V> valueType) { return (TypeLiteral<Entry<K, Provider<V>>>) TypeLiteral.get(newParameterizedTypeWithOwner( Map.class, Entry.class, keyType.getType(), Types.providerOf(valueType.getType()))); }
@SuppressWarnings("unchecked") // a provider map <K, V> is safely a Map<K, Provider<V>> private static <K, V> TypeLiteral<Map<K, Provider<V>>> mapOfProviderOf( TypeLiteral<K> keyType, TypeLiteral<V> valueType) { return (TypeLiteral<Map<K, Provider<V>>>) TypeLiteral.get( Types.mapOf(keyType.getType(), newParameterizedType(Provider.class, valueType.getType()))); }
/** * Gets the type of this type's provider. */ @SuppressWarnings("unchecked") final TypeLiteral<Provider<T>> providerType() { // This cast is safe and wouldn't generate a warning if Type had a type // parameter. return (TypeLiteral<Provider<T>>) get(Types.providerOf(getType())); }
/** * Gets the type of this type's provider. */ @SuppressWarnings("unchecked") final TypeLiteral<Provider<T>> providerType() { // This cast is safe and wouldn't generate a warning if Type had a type // parameter. return (TypeLiteral<Provider<T>>) get(Types.providerOf(getType())); }
@SuppressWarnings("unchecked") // wrapping a T in a Set safely returns a Set<T> private static <T> TypeLiteral<Set<T>> setOf(TypeLiteral<T> elementType) { Type type = Types.setOf(elementType.getType()); return (TypeLiteral<Set<T>>) TypeLiteral.get(type); }