/** * 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 ""; } } }
private Emitter findEmitter(String emitterType, List<Binding<Emitter>> emitterBindings) { for (Binding<Emitter> binding : emitterBindings) { if (Names.named(emitterType).equals(binding.getKey().getAnnotation())) { return binding.getProvider().get(); } } return null; }
@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 TLSCertificateChecker findChecker( String checkerType, List<Binding<TLSCertificateChecker>> checkerBindings ) { for (Binding<TLSCertificateChecker> binding : checkerBindings) { if (Names.named(checkerType).equals(binding.getKey().getAnnotation())) { return binding.getProvider().get(); } } return 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)); }
boolean isIndexable(Binding<?> binding) { return binding.getKey().getAnnotation() instanceof Element; }
/** * 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()); } }
private boolean keyMatches(Key<?> key) { return key.getTypeLiteral().equals(elementType) && key.getAnnotation() instanceof Element && ((Element) key.getAnnotation()).setName().equals(getSetName()) && ((Element) key.getAnnotation()).type() == MULTIBINDER; }
/** * 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 true if the key indicates this is a value in the map. */ private boolean matchesValueKey(Key<?> key) { return key.getAnnotation() instanceof RealElement && ((RealElement) key.getAnnotation()).setName().equals(entrySetBinder.getSetName()) && ((RealElement) key.getAnnotation()).type() == MAPBINDER && ((RealElement) key.getAnnotation()).keyType().equals(keyType.toString()) && key.getTypeLiteral().equals(valueType); }
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()));
private static Key<?> getMappedKey(Injector injector, Key<?> key) { Map<TypeLiteral, BeanTypeKey> beanTypeMap = getBeanTypeMap(injector); if(key.getAnnotation() == null && beanTypeMap.containsKey(key.getTypeLiteral())) { return beanTypeMap.get(key.getTypeLiteral()).key; } else { return key; } }
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)); }
@Inject public void inject(Injector injector) { final List<Binding<Emitter>> emitterBindings = injector.findBindingsByType(new TypeLiteral<Emitter>(){}); emitter = findEmitter(emitterType, emitterBindings); if (emitter == null) { emitter = findEmitter(NoopEmitterModule.EMITTER_TYPE, emitterBindings); } if (emitter == null) { List<String> knownTypes = new ArrayList<>(); for (Binding<Emitter> binding : emitterBindings) { final Annotation annotation = binding.getKey().getAnnotation(); if (annotation != null) { knownTypes.add(((Named) annotation).value()); } } throw new ISE("Unknown emitter type[%s]=[%s], known types[%s]", EMITTER_PROPERTY, emitterType, knownTypes); } }
@SuppressWarnings("rawtypes") private static Injector proxyInjector(final ClassLoader loader, final Map<Key, Object> registry) { return Reflection.newProxy(Injector.class, (proxy, method, args) -> { if (method.getName().equals("getInstance")) { Key key = (Key) args[0]; Object value = registry.get(key); if (value == null) { Object type = key.getAnnotation() != null ? key : key.getTypeLiteral(); IllegalStateException iex = new IllegalStateException("Not found: " + type); // Skip proxy and some useless lines: Try.apply(() -> { StackTraceElement[] stacktrace = iex.getStackTrace(); return Lists.newArrayList(stacktrace).subList(CLEAN_STACK, stacktrace.length); }).onSuccess(stacktrace -> iex .setStackTrace(stacktrace.toArray(new StackTraceElement[stacktrace.size()]))); throw iex; } return value; } throw new UnsupportedOperationException(method.toString()); }); }
&& key.getAnnotation() == null && COMMON_AMBIGUOUS_TYPES.contains(key.getTypeLiteral().getRawType())) {
IndexedBinding(Binding<?> binding, BindingType type, Object scope, Object extraEquality) { this.scope = scope; this.type = type; this.extraEquality = extraEquality; this.typeLiteral = binding.getKey().getTypeLiteral(); Element annotation = (Element) binding.getKey().getAnnotation(); this.annotationName = annotation.setName(); this.annotationType = annotation.type(); }
if (e instanceof Binding) { Key key = ((Binding) e).getKey(); if (Named.class.isAssignableFrom(key.getAnnotation().annotationType()) && "configureInterceptors".equals(((Named) key.getAnnotation()).value()) && key.getTypeLiteral().getRawType().equals(Object.class)) { calledCustom = true; if (Named.class.isAssignableFrom(key.getAnnotation().annotationType()) && "configureDefaultInterceptors".equals(((Named) key.getAnnotation()).value()) && key.getTypeLiteral().getRawType().equals(Object.class)) { calledDefault = true;
@Override public boolean apply(Binding<String> input) { Annotation annotation = input.getKey().getAnnotation(); if (annotation == null) return false; return (annotation instanceof javax.inject.Named) || (annotation instanceof com.google.inject.name.Named); }
IndexedBinding(Binding<?> binding, BindingType type, Object scope, Object extraEquality) { this.scope = scope; this.type = type; this.extraEquality = extraEquality; this.typeLiteral = binding.getKey().getTypeLiteral(); Element annotation = (Element) binding.getKey().getAnnotation(); this.annotationName = annotation.setName(); this.annotationType = annotation.type(); }