@Override public String toString(Key key) { if (key.getAnnotationType() != null) { return key.getTypeLiteral() + " annotated with " + (key.getAnnotation() != null ? key.getAnnotation() : key.getAnnotationType()); } else { return key.getTypeLiteral().toString(); } } }
private <T> void validateKey(Object source, Key<T> key) { Annotations.checkForMisplacedScopeAnnotations(key.getRawType(), source, errors); }
/** * Gets a key for an injection type and an annotation. */ public static <T> Key<T> get(Class<T> type, Annotation annotation) { return new Key<>(type, strategyFor(annotation)); }
@Override public void annotatedWith(Class<? extends Annotation> annotationType) { Objects.requireNonNull(annotationType, "annotationType"); checkNotAnnotated(); key = Key.get(key.getTypeLiteral(), annotationType); }
/** * Gets the strategy for an annotation type. */ static AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType) { Objects.requireNonNull(annotationType, "annotation type"); ensureRetainedAtRuntime(annotationType); ensureIsBindingAnnotation(annotationType); return new AnnotationTypeStrategy(annotationType, null); }
private boolean keyMatches(Key<?> key) { return key.getTypeLiteral().equals(elementType) && key.getAnnotation() instanceof Element && ((Element) key.getAnnotation()).setName().equals(setName); }
private void toConstant(Class<?> type, Object instance) { // this type will define T, so these assignments are safe @SuppressWarnings("unchecked") Class<T> typeAsClassT = (Class<T>) type; @SuppressWarnings("unchecked") T instanceAsT = (T) instance; if (keyTypeIsSet()) { binder.addError(CONSTANT_VALUE_ALREADY_SET); return; } BindingImpl<T> base = getBinding(); Key<T> key; if (base.getKey().getAnnotation() != null) { key = Key.get(typeAsClassT, base.getKey().getAnnotation()); } else if (base.getKey().getAnnotationType() != null) { key = Key.get(typeAsClassT, base.getKey().getAnnotationType()); } else { key = Key.get(typeAsClassT); } if (instanceAsT == null) { binder.addError(BINDING_TO_NULL); } setBinding(new InstanceBindingImpl<>( base.getSource(), key, base.getScoping(), emptySet(), instanceAsT)); }
@Override public boolean equals(Object o) { return o instanceof RealMapBinder && ((RealMapBinder<?, ?>) o).mapKey.equals(mapKey); }
/** * Replace annotation instances with annotation types, this is only * appropriate for testing if a key is bound and not for injecting. * <p> * See Guice bug 125, * http://code.google.com/p/google-guice/issues/detail?id=125 */ public Key<?> fixAnnotations(Key<?> key) { return key.getAnnotation() == null ? key : Key.get(key.getTypeLiteral(), key.getAnnotation().annotationType()); }
/** * Constructs a key from a manually specified type. */ private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes.makeKeySafe(typeLiteral); this.hashCode = computeHashCode(); }
protected void checkNotAnnotated() { if (key.getAnnotationType() != null) { binder.addError(AbstractBindingBuilder.ANNOTATION_ALREADY_SPECIFIED); } }
/** * Returns true if the key type is MembersInjector (but not a subclass of MembersInjector). */ static boolean isMembersInjector(Key<?> key) { return key.getTypeLiteral().getRawType().equals(MembersInjector.class) && !key.hasAnnotationType(); }
@Override public String toString() { return "Provider<" + lookup.key.getTypeLiteral() + ">"; }
/** * Constructs a new key. Derives the type from this class's type parameter. * <p> * Clients create an empty anonymous subclass. Doing so embeds the type * parameter in the anonymous class's type hierarchy so we can reconstitute it * at runtime despite erasure. * <p> * Example usage for a binding of type {@code Foo} annotated with * {@code @Bar}: * <p> * {@code new Key<Foo>(Bar.class) {}}. */ @SuppressWarnings("unchecked") protected Key(Class<? extends Annotation> annotationType) { this.annotationStrategy = strategyFor(annotationType); this.typeLiteral = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); this.hashCode = computeHashCode(); }
/** * Gets a key for an injection type. */ public static <T> Key<T> get(Class<T> type) { return new Key<>(type, NullAnnotationStrategy.INSTANCE); }
final Object source = command.getSource(); if (Void.class.equals(command.getKey().getRawType())) { if (command instanceof ProviderInstanceBinding && ((ProviderInstanceBinding) command).getProviderInstance() instanceof ProviderMethod) { Class<? super T> rawType = key.getTypeLiteral().getRawType();
/** * Gets the strategy for an annotation. */ static AnnotationStrategy strategyFor(Annotation annotation) { Objects.requireNonNull(annotation, "annotation"); Class<? extends Annotation> annotationType = annotation.annotationType(); ensureRetainedAtRuntime(annotationType); ensureIsBindingAnnotation(annotationType); if (annotationType.getMethods().length == 0) { return new AnnotationTypeStrategy(annotationType, annotation); } return new AnnotationInstanceStrategy(annotation); }
private boolean keyMatches(Key<?> key) { return key.getTypeLiteral().equals(elementType) && key.getAnnotation() instanceof Element && ((Element) key.getAnnotation()).setName().equals(setName); }
@Override public void annotatedWith(Annotation annotation) { Objects.requireNonNull(annotation, "annotation"); checkNotAnnotated(); key = Key.get(key.getTypeLiteral(), annotation); }