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). */ private static boolean isMembersInjector(Key<?> key) { return key.getTypeLiteral().getRawType().equals(MembersInjector.class) && key.getAnnotationType() == null; }
@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.ofInstance(instance)); }
/** * Returns the name the binding should use. This is based on the annotation. If the annotation has * an instance and is not a marker annotation, we ask the annotation for its toString. If it was a * marker annotation or just an annotation type, we use the annotation's name. Otherwise, the name * is the empty string. */ public static String nameOf(Key<?> key) { Annotation annotation = key.getAnnotation(); Class<? extends Annotation> annotationType = key.getAnnotationType(); if (annotation != null && !isMarker(annotationType)) { return key.getAnnotation().toString(); } else if (key.getAnnotationType() != null) { return "@" + key.getAnnotationType().getName(); } else { return ""; } } }
protected void checkNotAnnotated() { if (binding.getKey().getAnnotationType() != null) { binder.addError(ANNOTATION_ALREADY_SPECIFIED); } }
/** * 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) { Class<? extends Annotation> annotationType = interfaceKey.getAnnotationType(); return MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), interfaceType, annotationType); } else { return MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), interfaceType); } }
/** * Returns a key that doesn't hold any references to parent classes. This is necessary for * anonymous keys, so ensure we don't hold a ref to the containing module (or class) forever. */ public static <T> Key<T> canonicalizeKey(Key<T> key) { // If we know this isn't a subclass, return as-is. // Otherwise, recreate the key to avoid the subclass if (key.getClass() == Key.class) { return key; } else if (key.getAnnotation() != null) { return Key.get(key.getTypeLiteral(), key.getAnnotation()); } else if (key.getAnnotationType() != null) { return Key.get(key.getTypeLiteral(), key.getAnnotationType()); } else { return Key.get(key.getTypeLiteral()); } }
@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(); } } });
if (key.getAnnotation() != null) { implsMap = (Map<String, Provider<T>>) injector.getInstance(Key.get(mapType, key.getAnnotation())); } else if (key.getAnnotationType() != null) { implsMap = (Map<String, Provider<T>>) injector.getInstance(Key.get(mapType, key.getAnnotationType())); } else { implsMap = (Map<String, Provider<T>>) injector.getInstance(Key.get(mapType));
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<T>( base.getSource(), key, base.getScoping(), ImmutableSet.<InjectionPoint>of(), instanceAsT)); }
@Override public Boolean visit(UntargettedBinding<? extends T> untargetted) { prepareBinding(); // Error: Missing implementation. // Example: bind(Date.class).annotatedWith(Red.class); // We can't assume abstract types aren't injectable. They may have an // @ImplementedBy annotation or something. if (key.getAnnotationType() != null) { errors.missingImplementationWithHint(key, injector); putBinding(invalidBinding(injector, key, source)); return true; } // This cast is safe after the preceeding check. try { BindingImpl<T> binding = injector.createUninitializedBinding(key, scoping, source, errors, false); scheduleInitialization(binding); putBinding(binding); } catch (ErrorsException e) { errors.merge(e.getErrors()); putBinding(invalidBinding(injector, key, source)); } return true; }
if (key.getAnnotationType() != null) {
protected void checkNotAnnotated() { if (key.getAnnotationType() != null) { binder.addError(AbstractBindingBuilder.ANNOTATION_ALREADY_SPECIFIED); } }
/** Return all non-assisted dependencies. */ private Set<Dependency<?>> removeAssistedDeps(Set<Dependency<?>> deps) { ImmutableSet.Builder<Dependency<?>> builder = ImmutableSet.builder(); for (Dependency<?> dep : deps) { Class<?> annotationType = dep.getKey().getAnnotationType(); if (annotationType == null || !annotationType.equals(Assisted.class)) { builder.add(dep); } } return builder.build(); }
/** Returns true if the key type is MembersInjector (but not a subclass of MembersInjector). */ private static boolean isMembersInjector(Key<?> key) { return key.getTypeLiteral().getRawType().equals(MembersInjector.class) && !(key.getAnnotationType() != null); }
protected void checkNotAnnotated() { if (key.getAnnotationType() != null) { binder.addError(AbstractBindingBuilder.ANNOTATION_ALREADY_SPECIFIED); } }
protected void checkNotAnnotated() { if (binding.getKey().getAnnotationType() != null) { binder.addError(ANNOTATION_ALREADY_SPECIFIED); } }
protected void checkNotAnnotated() { if (binding.getKey().getAnnotationType() != null) { binder.addError(ANNOTATION_ALREADY_SPECIFIED); } }
@Override public Boolean visit( final LinkedKeyBinding<?> binding ) { final Key<?> linkedKey = binding.getLinkedKey(); if ( linkedKey.getAnnotationType() == null ) { return verifyImplementation( linkedKey.getTypeLiteral() ); } return Boolean.TRUE; // indirect binding, don't scan }
@Override public Boolean visit( final ProviderKeyBinding<?> binding ) { final Key<?> providerKey = binding.getProviderKey(); if ( providerKey.getAnnotationType() == null ) { return analyzeImplementation( providerKey.getTypeLiteral(), true ); } return Boolean.TRUE; // indirect binding, don't scan }