@Override public Void visitDeclared(DeclaredType t, Set<TypeElement> p) { TypeElement typeElement = MoreTypes.asTypeElement(t); if (typeElement.getKind() == ElementKind.ANNOTATION_TYPE) { nestedAnnotationElements(typeElement, p); } return null; } };
@Override public TypeName visitDeclared(DeclaredType t, Void p) { return ClassName.get(MoreTypes.asTypeElement(t)); }
/** Adds an annotation to a method. */ static void addAnnotation(MethodSpec.Builder method, DeclaredType nullableType) { method.addAnnotation(ClassName.get(MoreTypes.asTypeElement(nullableType))); }
private static Predicate<AnnotationMirror> hasAnnotationTypeIn( Collection<? extends Class<? extends Annotation>> annotations) { Set<String> annotationClassNames = annotations.stream().map(Class::getCanonicalName).collect(toSet()); return annotation -> annotationClassNames.contains( MoreTypes.asTypeElement(annotation.getAnnotationType()).getQualifiedName().toString()); }
AnnotationExpression(AnnotationMirror annotation) { this.annotation = annotation; this.creatorClass = getAnnotationCreatorClassName( MoreTypes.asTypeElement(annotation.getAnnotationType())); }
/** The element associated with the type of this requirement. */ TypeElement typeElement() { return MoreTypes.asTypeElement(type()); }
private TypeMirror accessibleType( TypeMirror type, Predicate<TypeMirror> accessibilityPredicate, Predicate<TypeMirror> rawTypeAccessibilityPredicate) { if (accessibilityPredicate.test(type)) { return type; } else if (type.getKind().equals(TypeKind.DECLARED) && rawTypeAccessibilityPredicate.test(type)) { return getDeclaredType(MoreTypes.asTypeElement(type)); } else { return elements.getTypeElement(Object.class).asType(); } }
@Override public Optional<TypeElement> visitDeclared(DeclaredType t, Void p) { for (ModuleKind moduleKind : subcomponentKind.legalModuleKinds()) { if (isAnnotationPresent(t.asElement(), moduleKind.annotation())) { return Optional.of(MoreTypes.asTypeElement(t)); } } return Optional.empty(); } },
private static <T extends Element> void validateComponentDependencies( ValidationReport.Builder<T> report, Iterable<TypeMirror> types) { validateTypesAreDeclared(report, types, "component dependency"); for (TypeMirror type : types) { if (getModuleAnnotation(MoreTypes.asTypeElement(type)).isPresent()) { report.addError( String.format("%s is a module, which cannot be a component dependency", type)); } } }
static ComponentRequirement forDependency(TypeMirror type) { return new AutoValue_ComponentRequirement( Kind.DEPENDENCY, MoreTypes.equivalence().wrap(checkNotNull(type)), Optional.empty(), Optional.empty(), simpleVariableName(MoreTypes.asTypeElement(type))); }
boolean isValidType(TypeMirror type) { if (!type.getKind().equals(DECLARED)) { return true; } return validateType(MoreTypes.asTypeElement(type)).isClean(); }
static ComponentRequirement forModule(TypeMirror type) { return new AutoValue_ComponentRequirement( Kind.MODULE, MoreTypes.equivalence().wrap(checkNotNull(type)), Optional.empty(), Optional.empty(), simpleVariableName(MoreTypes.asTypeElement(type))); }
/** * When a binding is resolved for a {@link SubcomponentDeclaration}, adds corresponding {@link * ComponentDescriptor subcomponent} to a queue in the owning component's resolver. The queue * will be used to detect which subcomponents need to be resolved. */ private void addSubcomponentToOwningResolver(ProvisionBinding subcomponentCreatorBinding) { checkArgument(subcomponentCreatorBinding.kind().equals(SUBCOMPONENT_BUILDER)); Resolver owningResolver = getOwningResolver(subcomponentCreatorBinding).get(); TypeElement builderType = MoreTypes.asTypeElement(subcomponentCreatorBinding.key().type()); owningResolver.subcomponentsToResolve.add( owningResolver.componentDescriptor.getChildComponentWithBuilderType(builderType)); }
@CanIgnoreReturnValue @Override public Optional<MembersInjectionBinding> getOrFindMembersInjectionBinding(Key key) { checkNotNull(key); // TODO(gak): is checking the kind enough? checkArgument(isValidMembersInjectionKey(key)); MembersInjectionBinding binding = membersInjectionBindings.getBinding(key); if (binding != null) { return Optional.of(binding); } Optional<MembersInjectionBinding> newBinding = tryRegisterMembersInjectedType( MoreTypes.asTypeElement(key.type()), Optional.of(key.type()), true); return newBinding; }
@Override public Void visitDeclared(DeclaredType declaredType, Void aVoid) { TypeElement attributeType = MoreTypes.asTypeElement(declaredType); if (isAnyAnnotationPresent(attributeType, SUBCOMPONENT_TYPES)) { validateSubcomponentHasBuilder(attributeType, moduleAnnotation, builder); } else { builder.addError( isAnyAnnotationPresent(attributeType, SUBCOMPONENT_BUILDER_TYPES) ? moduleSubcomponentsIncludesBuilder(attributeType) : moduleSubcomponentsIncludesNonSubcomponent(attributeType), subject, moduleAnnotation); } return null; } },
private void validateSubcomponentCreatorMethod( ValidationReport.Builder<TypeElement> report, ExecutableElement method, List<? extends VariableElement> parameters, TypeMirror returnType, Set<? extends Element> validatedSubcomponentCreators) { if (!parameters.isEmpty()) { report.addError(SubcomponentCreatorMessages.INSTANCE.builderMethodRequiresNoArgs(), method); } // If we haven't already validated the subcomponent creator itself, validate it now. TypeElement creatorElement = MoreTypes.asTypeElement(returnType); if (!validatedSubcomponentCreators.contains(creatorElement)) { // TODO(sameb): The creator validator right now assumes the element is being compiled // in this pass, which isn't true here. We should change error messages to spit out // this method as the subject and add the original subject to the message output. report.addItems(creatorValidator.validate(creatorElement).items()); } }
@Override public CodeBlock creationExpression() { TypeMirror membersInjectedType = getOnlyElement(MoreTypes.asDeclared(binding.key().type()).getTypeArguments()); CodeBlock membersInjector = binding.injectionSites().isEmpty() ? CodeBlock.of("$T.<$T>noOp()", MEMBERS_INJECTORS, membersInjectedType) : CodeBlock.of( "$T.create($L)", membersInjectorNameForType(MoreTypes.asTypeElement(membersInjectedType)), componentBindingExpressions.getCreateMethodArgumentsCodeBlock(binding)); // TODO(ronshapiro): consider adding a MembersInjectorBindingExpression to return this directly // (as it's rarely requested as a Provider). return CodeBlock.of("$T.create($L)", INSTANCE_FACTORY, membersInjector); }
private ComponentNode subcomponentNode(TypeMirror subcomponentBuilderType, BindingGraph graph) { TypeElement subcomponentBuilderElement = asTypeElement(subcomponentBuilderType); ComponentDescriptor subcomponent = graph.componentDescriptor().getChildComponentWithBuilderType(subcomponentBuilderElement); return ComponentNodeImpl.create( componentTreePath().childPath(subcomponent.typeElement()).toComponentPath(), subcomponent); }
private void validateInjectionBinding( dagger.model.Binding node, DiagnosticReporter diagnosticReporter) { ValidationReport<TypeElement> typeReport = injectValidator.validateType(MoreTypes.asTypeElement(node.key().type())); for (Item item : typeReport.allItems()) { diagnosticReporter.reportBinding(item.kind(), node, item.message()); } } }
/** Returns a {@link dagger.model.BindingKind#MEMBERS_INJECTOR} binding. */ ProvisionBinding membersInjectorBinding( Key key, MembersInjectionBinding membersInjectionBinding) { return ProvisionBinding.builder() .key(key) .contributionType(ContributionType.UNIQUE) .kind(MEMBERS_INJECTOR) .bindingElement(MoreTypes.asTypeElement(membersInjectionBinding.key().type())) .provisionDependencies(membersInjectionBinding.dependencies()) .injectionSites(membersInjectionBinding.injectionSites()) .build(); }