Refine search
InjectionPoint(TypeLiteral<?> declaringType, Field field, boolean optional) { this.member = field; this.declaringType = declaringType; this.optional = optional; Annotation[] annotations = field.getAnnotations(); Errors errors = new Errors(field); Key<?> key = null; try { key = Annotations.getKey(declaringType.getFieldType(field), field, annotations, errors); } catch (ConfigurationException e) { errors.merge(e.getErrorMessages()); } catch (ErrorsException e) { errors.merge(e.getErrors()); } errors.throwConfigurationExceptionIfErrorsExist(); this.dependencies = ImmutableList.<Dependency<?>>of( newDependency(key, Nullability.allowsNull(annotations), -1)); }
private static void ensureIsBindingAnnotation(Class<? extends Annotation> annotationType) { checkArgument( Annotations.isBindingAnnotation(annotationType), "%s is not a binding annotation. Please annotate it with @BindingAnnotation.", annotationType.getName()); }
boolean failIfNotExplicit) throws ErrorsException { int numErrors = errors.size(); ? key.getTypeLiteral().getRawType() : (Class) constructorInjector.getDeclaringType().getRawType(); errors.missingImplementationWithHint(key, injector); errors.cannotInjectInnerClass(rawType); constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral()); if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) { errors.atInjectRequired(rawType); Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType); if (scopeAnnotation != null) { scoping =
/** Gets the strategy for an annotation type. */ static AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType) { annotationType = Annotations.canonicalizeIfNamed(annotationType); if (isAllDefaultMethods(annotationType)) { return strategyFor(generateAnnotation(annotationType)); } checkNotNull(annotationType, "annotation type"); ensureRetainedAtRuntime(annotationType); ensureIsBindingAnnotation(annotationType); return new AnnotationTypeStrategy(annotationType, null); }
/** * Adds an error if there is a misplaced annotations on {@code type}. Scoping annotations are not * allowed on abstract classes or interfaces. */ public static void checkForMisplacedScopeAnnotations( Class<?> type, Object source, Errors errors) { if (Classes.isConcrete(type)) { return; } Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, type); if (scopeAnnotation != null // We let Dagger Components through to aid migrations. && !containsComponentAnnotation(type.getAnnotations())) { errors.withSource(type).scopeAnnotationOnAbstractType(scopeAnnotation, type, source); } }
/** Gets the strategy for an annotation. */ static AnnotationStrategy strategyFor(Annotation annotation) { checkNotNull(annotation, "annotation"); Class<? extends Annotation> annotationType = annotation.annotationType(); ensureRetainedAtRuntime(annotationType); ensureIsBindingAnnotation(annotationType); if (Annotations.isMarker(annotationType)) { return new AnnotationTypeStrategy(annotationType, annotation); } return new AnnotationInstanceStrategy(Annotations.canonicalizeIfNamed(annotation)); }
/** Gets a key for the given type, member and annotations. */ public static Key<?> getKey( TypeLiteral<?> type, Member member, Annotation[] annotations, Errors errors) throws ErrorsException { int numErrorsBefore = errors.size(); Annotation found = findBindingAnnotation(errors, member, annotations); errors.throwIfNewErrors(numErrorsBefore); return found == null ? Key.get(type) : Key.get(type, found); }
public static <F> Provider<F> newFactory( TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) { Map<Method, AssistedConstructor<?>> factoryMethodToConstructor = createMethodMapping(factoryType, implementationType); if (!factoryMethodToConstructor.isEmpty()) { return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor); } else { BindingCollector collector = new BindingCollector(); // Preserving backwards-compatibility: Map all return types in a factory // interface to the passed implementation type. Errors errors = new Errors(); Key<?> implementationKey = Key.get(implementationType); try { for (Method method : factoryType.getRawType().getMethods()) { Key<?> returnType = getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors); if (!implementationKey.equals(returnType)) { collector.addBinding(returnType, implementationType); } } } catch (ErrorsException e) { throw new ConfigurationException(e.getErrors().getMessages()); } return new FactoryProvider2<F>(Key.get(factoryType), collector); } }
@Override public Key<?> getKey(TypeLiteral<?> injectedType, Field injectedMember, A resourceAnnotation) { for (Annotation annotation : injectedMember.getAnnotations()) if (Annotations.isBindingAnnotation(annotation.annotationType())) return Key.get(injectedType.getFieldType(injectedMember), annotation); return Key.get(injectedType.getFieldType(injectedMember)); }
private <T> void validateKey(Object source, Key<T> key) { Annotations.checkForMisplacedScopeAnnotations( key.getTypeLiteral().getRawType(), source, errors); }
<T> Key<T> getKey(Errors errors, TypeLiteral<T> type, Member member, Annotation[] annotations) { Annotation bindingAnnotation = Annotations.findBindingAnnotation(errors, member, annotations); return bindingAnnotation == null ? Key.get(type) : Key.get(type, bindingAnnotation); }
private static Key<?> buildKey(TypeLiteral<?> type, Annotation[] annotations) { for (Annotation annotation : annotations) if (Annotations.isBindingAnnotation(annotation.annotationType())) return Key.get(type, annotation); return Key.get(type); }
public void testGetAnnotationName_annotationInstanceWithParameters() throws Exception { Key<?> key = Key.get(String.class, Names.named("name")); assertEquals( "@Named(value=" + Annotations.memberValueString("name") + ")", nameFactory.getAnnotationName(key)); }
Class<?> rawType = key.getTypeLiteral().getRawType(); throw errors.missingImplementation(key).toException(); Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createImplementedByBinding(key, scoping, implementedBy, errors); Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createProvidedByBinding(key, scoping, providedBy, errors);
Key<T> key, Scoping scoping, Object source, Errors errors, boolean jitBinding) throws ErrorsException { Class<?> rawType = key.getTypeLiteral().getRawType(); throw errors.missingImplementationWithHint(key, this).toException(); Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createImplementedByBinding(key, scoping, implementedBy, errors); Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createProvidedByBinding(key, scoping, providedBy, errors);
@Override public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) { if (injectee.getRequiredType() instanceof Class) { TypeLiteral<?> typeLiteral = TypeLiteral.get(injectee.getRequiredType()); Errors errors = new Errors(injectee.getParent()); Key<?> key; try { key = Annotations.getKey(typeLiteral, (Member) injectee.getParent(), injectee.getParent().getDeclaredAnnotations(), errors); } catch (ErrorsException e) { errors.merge(e.getErrors()); throw new ConfigurationException(errors.getMessages()); } return injector.getInstance(key); } throw new IllegalStateException("Can't process injection point: " + injectee.getRequiredType()); }
public static List<Key<?>> getMethodKeys(Method method, Errors errors) { Annotation allParameterAnnotations[][] = method.getParameterAnnotations(); List<Key<?>> result = new ArrayList<Key<?>>(allParameterAnnotations.length); Iterator<Annotation[]> annotationsIterator = Arrays.asList(allParameterAnnotations).iterator(); TypeLiteral<?> type = TypeLiteral.get(method.getDeclaringClass()); for (TypeLiteral<?> parameterType : type.getParameterTypes(method)) { try { Annotation[] parameterAnnotations = annotationsIterator.next(); result.add(Annotations.getKey(parameterType, method, parameterAnnotations, errors)); } catch (ConfigurationException e) { errors.merge(e.getErrorMessages()); } catch (ErrorsException e) { errors.merge(e.getErrors()); } } return result; } }
private Key<?> getKey(ParameterInfo parameter) { try { // Get TypeLiteral for this parameter TypeLiteral<?> declaringType = TypeLiteral.get(parameter.getDeclaringClass()); List<TypeLiteral<?>> parameterTypes = declaringType.getParameterTypes(parameter.getConstructor()); TypeLiteral<?> parameterType = parameterTypes.get(parameter.getParameterIndex()); // Create Key object for this parameter Errors errors = new Errors(parameter.getConstructor()); return Annotations.getKey( parameterType, parameter.getConstructor(), parameter.getParameterAnnotations(), errors); } catch (ErrorsException e) { throw new MgnlInstantiationException(e.getMessage(), e); } } }
/** * 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 ImmutableList<Dependency<?>> forMember( Member member, TypeLiteral<?> type, Annotation[][] paramterAnnotations) { Errors errors = new Errors(member); List<Dependency<?>> dependencies = Lists.newArrayList(); int index = 0; for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) { try { Annotation[] parameterAnnotations = paramterAnnotations[index]; Key<?> key = Annotations.getKey(parameterType, member, parameterAnnotations, errors); dependencies.add(newDependency(key, Nullability.allowsNull(parameterAnnotations), index)); index++; } catch (ConfigurationException e) { errors.merge(e.getErrorMessages()); } catch (ErrorsException e) { errors.merge(e.getErrors()); } } errors.throwConfigurationExceptionIfErrorsExist(); return ImmutableList.copyOf(dependencies); }