/** * Returns all of the {@link MapKey} annotations that annotate {@code bindingElement}. */ static ImmutableSet<? extends AnnotationMirror> getMapKeys(Element bindingElement) { return getAnnotatedAnnotations(bindingElement, MapKey.class); }
/** * Returns the value named {@code name} from {@code annotation}. * * @throws IllegalArgumentException unless that member represents a single type */ static TypeMirror getTypeValue(AnnotationMirror annotation, String name) { return MoreAnnotationValues.asType(getAnnotationValue(annotation, name)); }
/** * Wraps an {@link Optional} of a type in an {@code Optional} of a {@link Equivalence.Wrapper} for * that type. */ static Optional<Equivalence.Wrapper<AnnotationMirror>> wrapOptionalInEquivalence( Optional<AnnotationMirror> optional) { return optional.map(AnnotationMirrors.equivalence()::wrap); }
getAnnotationMirror(method, ContributesAndroidInjector.class).get(); for (TypeMirror module : getAnnotationValue(annotation, "modules").accept(new AllTypesVisitor(), null)) { if (isAnnotationPresent(MoreTypes.asElement(module), Module.class)) { builder.modulesBuilder().add((ClassName) TypeName.get(module)); for (AnnotationMirror scope : getAnnotatedAnnotations(method, Scope.class)) { builder.scopesBuilder().add(AnnotationSpec.get(scope)); for (AnnotationMirror qualifier : getAnnotatedAnnotations(method, Qualifier.class)) { reporter.reportError( "@ContributesAndroidInjector methods cannot have qualifiers", qualifier);
/** * Returns the annotation value if {@code mapKey}'s type is annotated with * {@link MapKey @MapKey(unwrapValue = true)}. * * @throws IllegalArgumentException if {@code mapKey}'s type is not annotated with * {@link MapKey @MapKey} at all. */ static Optional<? extends AnnotationValue> unwrapValue(AnnotationMirror mapKey) { MapKey mapKeyAnnotation = mapKey.getAnnotationType().asElement().getAnnotation(MapKey.class); checkArgument( mapKeyAnnotation != null, "%s is not annotated with @MapKey", mapKey.getAnnotationType()); return mapKeyAnnotation.unwrapValue() ? Optional.of(getOnlyElement(getAnnotationValuesWithDefaults(mapKey).values())) : Optional.empty(); }
ImmutableSet<SubcomponentDeclaration> forModule(TypeElement module) { ImmutableSet.Builder<SubcomponentDeclaration> declarations = ImmutableSet.builder(); AnnotationMirror moduleAnnotation = getModuleAnnotation(module).get(); Element subcomponentAttribute = getAnnotationElementAndValue(moduleAnnotation, "subcomponents").getKey(); for (TypeElement subcomponent : MoreTypes.asTypeElements(getModuleSubcomponents(moduleAnnotation))) { declarations.add( new AutoValue_SubcomponentDeclaration( Optional.of(subcomponentAttribute), Optional.of(module), keyFactory.forSubcomponentCreator( getSubcomponentCreator(subcomponent).get().asType()), subcomponent, moduleAnnotation)); } return declarations.build(); } }
private CodeBlock getAnnotationInstanceExpression(AnnotationMirror annotation) { return CodeBlock.of( "$T.$L($L)", creatorClass, createMethodName( MoreElements.asType(annotation.getAnnotationType().asElement())), makeParametersCodeBlock( getAnnotationValuesWithDefaults(annotation) .entrySet() .stream() .map(entry -> getValueExpression(entry.getKey().getReturnType(), entry.getValue())) .collect(toList()))); }
static ImmutableSet<? extends AnnotationMirror> getQualifiers(Element element) { return AnnotationMirrors.getAnnotatedAnnotations(element, Qualifier.class); }
/** * If {@code mapKey} is {@link AndroidInjectionKey}, returns the string value for the map key. If * it's {@link dagger.multibindings.ClassKey}, returns the fully-qualified class name of the * annotation value. Otherwise returns {@link Optional#empty()}. */ static Optional<String> injectedTypeFromMapKey(AnnotationMirror mapKey) { Object mapKeyClass = getAnnotationValue(mapKey, "value").getValue(); if (mapKeyClass instanceof String) { return Optional.of((String) mapKeyClass); } else if (mapKeyClass instanceof TypeMirror) { TypeElement type = MoreTypes.asTypeElement((TypeMirror) mapKeyClass); return Optional.of(type.getQualifiedName().toString()); } else { return Optional.empty(); } } }
getOnlyElement(getAnnotationValuesWithDefaults(mapKey).keySet()).getReturnType(); return annotationExpression.getValueExpression(unwrappedValueType, unwrappedValue.get()); } else {
private Optional<AnnotationMirror> mapKey(Binding binding) { return binding .bindingElement() .map(bindingElement -> getAnnotatedAnnotations(bindingElement, MapKey.class)) .flatMap( annotations -> annotations.isEmpty() ? Optional.empty() : Optional.of(getOnlyElement(annotations))); }
/** * Returns the list of types that is the value named {@code name} from {@code annotationMirror}. * * @throws IllegalArgumentException unless that member represents an array of types */ static ImmutableList<TypeMirror> getTypeListValue( AnnotationMirror annotationMirror, String name) { return asAnnotationValues(getAnnotationValue(annotationMirror, name)) .stream() .map(MoreAnnotationValues::asType) .collect(toImmutableList()); } }
/** Returns all of the associated scopes for a source code element. */ static ImmutableSet<Scope> scopesOf(Element element) { return AnnotationMirrors.getAnnotatedAnnotations(element, javax.inject.Scope.class) .stream() .map(Scope::scope) .collect(toImmutableSet()); } }
/** * Returns the annotation values for the modules directly installed into a component or included * in a module. * * @param annotatedType the component or module type * @param annotation the component or module annotation */ static ImmutableList<AnnotationValue> getModules( TypeElement annotatedType, AnnotationMirror annotation) { if (ComponentKind.forAnnotatedElement(annotatedType) .filter(kind -> !kind.isForModuleValidation()) .isPresent()) { return asAnnotationValues(getAnnotationValue(annotation, MODULES_ATTRIBUTE)); } if (ModuleKind.forAnnotatedElement(annotatedType).isPresent()) { return asAnnotationValues(getAnnotationValue(annotation, INCLUDES_ATTRIBUTE)); } throw new IllegalArgumentException(String.format("unsupported annotation: %s", annotation)); }
private void validateNoScopeAnnotationsOnModuleElement( TypeElement module, ModuleKind moduleKind, ValidationReport.Builder<TypeElement> report) { for (AnnotationMirror scope : getAnnotatedAnnotations(module, Scope.class)) { report.addError( String.format( "@%ss cannot be scoped. Did you mean to scope a method instead?", moduleKind.annotation().getSimpleName()), module, scope); } }
private void validateSelfCycles( TypeElement module, ValidationReport.Builder<TypeElement> builder) { AnnotationMirror moduleAnnotation = getModuleAnnotation(module).get(); getAnnotationValue(moduleAnnotation, "includes") .accept( new SimpleAnnotationValueVisitor8<Void, AnnotationValue>() { @Override public Void visitType(TypeMirror includedModule, AnnotationValue value) { if (MoreTypes.equivalence().equivalent(module.asType(), includedModule)) { Name moduleKind = moduleAnnotation.getAnnotationType().asElement().getSimpleName(); builder.addError( String.format("@%s cannot include themselves.", moduleKind), module, moduleAnnotation, value); } return null; } @Override public Void visitArray(List<? extends AnnotationValue> values, AnnotationValue p) { values.stream().forEach(value -> value.accept(this, value)); return null; } }, null); }
private void validateMethod(Class<? extends Annotation> annotation, ExecutableElement method) { if (!getAnnotatedAnnotations(method, Qualifier.class).isEmpty()) { return; if (!getAnnotatedAnnotations(method, Scope.class).isEmpty()) { SuppressWarnings suppressedWarnings = method.getAnnotation(SuppressWarnings.class); if (suppressedWarnings == null .contains("dagger.android.ScopedInjectorFactory")) { AnnotationMirror mapKeyAnnotation = getOnlyElement(getAnnotatedAnnotations(method, MapKey.class)); TypeElement mapKeyValueElement = elements.getTypeElement(injectedTypeFromMapKey(mapKeyAnnotation).get());