private boolean shouldBeInjected( Element injectionSite, SetMultimap<String, ExecutableElement> overriddenMethodMap) { if (!isAnnotationPresent(injectionSite, Inject.class) || injectionSite.getModifiers().contains(PRIVATE) || injectionSite.getModifiers().contains(STATIC)) { return false; } if (injectionSite.getKind().isField()) { // Inject all fields (self and ancestors) return true; } // For each method with the same name belonging to any descendant class, return false if any // method has already overridden the injectionSite method. To decrease the number of methods // that are checked, we store the already injected methods in a SetMultimap and only // check the methods with the same name. ExecutableElement injectionSiteMethod = MoreElements.asExecutable(injectionSite); TypeElement injectionSiteType = MoreElements.asType(injectionSite.getEnclosingElement()); for (ExecutableElement method : overriddenMethodMap.get(injectionSiteMethod.getSimpleName().toString())) { if (elements.overrides(method, injectionSiteMethod, injectionSiteType)) { return false; } } return true; }
/** * Returns an {@link AnnotationMirror} for the annotation of type {@code annotationClass} on * {@code element}, or {@link Optional#empty()} if no such annotation exists. This method is a * safer alternative to calling {@link Element#getAnnotation} as it avoids any interaction with * annotation proxies. */ static Optional<AnnotationMirror> getAnnotationMirror( Element element, Class<? extends Annotation> annotationClass) { return Optional.ofNullable(MoreElements.getAnnotationMirror(element, annotationClass).orNull()); }
ImmutableSet<ExecutableElement> getUnimplementedMethods(TypeElement type) { return FluentIterable.from(getLocalAndInheritedMethods(type, types, elements)) .filter(hasModifiers(ABSTRACT)) .toSet(); }
private String implicitlyIncludedModuleName(ExecutableElement method) { return getPackage(method).getQualifiedName() + "." + classFileName(ClassName.get(MoreElements.asType(method.getEnclosingElement()))) + "_" + LOWER_CAMEL.to(UPPER_CAMEL, method.getSimpleName().toString()); } }
@Override public Void visitExecutableAsMethod(ExecutableElement methodElement, Void aVoid) { injectBindingRegistry.tryRegisterMembersInjectedType( MoreElements.asType(methodElement.getEnclosingElement())); return null; } };
/** * Returns {@code true} iff the given element has an {@link AnnotationMirror} whose * {@linkplain AnnotationMirror#getAnnotationType() annotation type} has the same canonical name * as any of that of {@code annotationClasses}. */ static boolean isAnyAnnotationPresent( Element element, Iterable<? extends Class<? extends Annotation>> annotationClasses) { for (Class<? extends Annotation> annotation : annotationClasses) { if (MoreElements.isAnnotationPresent(element, annotation)) { return true; } } return false; }
currentClass = MoreElements.asType(types.asElement(currentClass.getSuperclass())); List<ExecutableElement> superclassMethods = ElementFilter.methodsIn(currentClass.getEnclosedElements()); if (isAnnotationPresent(superclassMethod, moduleKind.methodAnnotation())) { for (ExecutableElement method : allMethodsByName.get(name)) { if (!failedMethods.contains(method)
TypeElement enclosingElement = MoreElements.asType(method.getEnclosingElement()); if (!isAnnotationPresent(enclosingElement, Module.class)) { reporter.reportError("@ContributesAndroidInjector methods must be in a @Module"); 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)); } else {
/** If {@link #bindingElement()} is a method that returns a primitive type, returns that type. */ Optional<TypeMirror> contributedPrimitiveType() { return bindingElement() .filter(bindingElement -> bindingElement instanceof ExecutableElement) .map(bindingElement -> MoreElements.asExecutable(bindingElement).getReturnType()) .filter(type -> type.getKind().isPrimitive()); }
/** * When a type has an inaccessible member from a supertype (e.g. an @Inject field in a parent * that's in a different package), a method in the supertype's package must be generated to give * the subclass's members injector a way to inject it. Each potentially inaccessible member * receives its own method, as the subclass may need to inject them in a different order from * the parent class. */ static InjectionMethod create(InjectionSite injectionSite, DaggerElements elements) { String methodName = methodName(injectionSite); ClassName proxyEnclosingClass = membersInjectorNameForType( MoreElements.asType(injectionSite.element().getEnclosingElement())); switch (injectionSite.kind()) { case METHOD: return methodProxy( proxyEnclosingClass, MoreElements.asExecutable(injectionSite.element()), methodName, ReceiverAccessibility.CAST_IF_NOT_PUBLIC, CheckNotNullPolicy.IGNORE, elements); case FIELD: return fieldProxy( proxyEnclosingClass, MoreElements.asVariable(injectionSite.element()), methodName, elements); default: throw new AssertionError(injectionSite); } }
if (isAnnotationPresent(subject, CancellationPolicy.class) && !componentKind.isProducer()) { report.addError( "@CancellationPolicy may only be applied to production components and subcomponents", getLocalAndInheritedMethods(subject, types, elements).stream() .filter(method -> method.getModifiers().contains(ABSTRACT)) .forEachOrdered( Sets.difference(referencedSubcomponents.keySet(), validatedSubcomponents)) { ComponentValidationReport subreport = validate(asType(subcomponent), validatedSubcomponents, validatedSubcomponentCreators); report.addItems(subreport.report().items()); allSubcomponents.addAll(subreport.referencedSubcomponents());
currentType = types.nonObjectSuperclass(currentType.get())) { DeclaredType type = currentType.get(); ancestors.add(MoreElements.asType(type.asElement())); for (Element enclosedElement : type.asElement().getEnclosedElements()) { Optional<InjectionSite> maybeInjectionSite = MoreElements.asExecutable(injectionSite.element()); overriddenMethodMap.put( injectionSiteMethod.getSimpleName().toString(), injectionSiteMethod);
boolean accessibleModifiers(Element element) { if (element.getModifiers().contains(PUBLIC)) { return true; } else if (element.getModifiers().contains(PRIVATE)) { return false; } else if (packageName.isPresent() && getPackage(element).getQualifiedName().contentEquals(packageName.get())) { return true; } else { return false; } }
getLocalAndInheritedMethods(typeElement(), types, elements); boolean foundInstanceMethod = false; for (ExecutableElement method : methods) {
/** * Returns {@code true} if {@code overrider} overrides {@code overridden} considered from within * the type that declares {@code overrider}. */ // TODO(dpb): Does this break for ECJ? private boolean overridesAsDeclared(ExecutableElement overridder, ExecutableElement overridden) { return elements.overrides(overridder, overridden, asType(overridder.getEnclosingElement())); }
/** * The contribution type from a binding method annotations. Presumes a well-formed binding method * (at most one of @IntoSet, @IntoMap, @ElementsIntoSet and @Provides.type). {@link * ProvidesMethodValidator} and {@link ProducesMethodValidator} validate correctness on their own. */ static ContributionType fromBindingMethod(ExecutableElement method) { if (isAnnotationPresent(method, IntoMap.class)) { return ContributionType.MAP; } else if (isAnnotationPresent(method, IntoSet.class)) { return ContributionType.SET; } else if (isAnnotationPresent(method, ElementsIntoSet.class)) { return ContributionType.SET_VALUES; } return ContributionType.UNIQUE; } }
ExecutableElement constructorElement, Optional<TypeMirror> resolvedType) { checkArgument(constructorElement.getKind().equals(CONSTRUCTOR)); checkArgument(isAnnotationPresent(constructorElement, Inject.class)); checkArgument(!getQualifier(constructorElement).isPresent()); .scope(uniqueScopeOf(constructorElement.getEnclosingElement())); TypeElement bindingTypeElement = MoreElements.asType(constructorElement.getEnclosingElement()); if (hasNonDefaultTypeParameters(bindingTypeElement, key.type(), types)) { builder.unresolved(injectionBinding(constructorElement, Optional.empty()));
@Override public String visitVariableAsParameter(VariableElement parameter, Void aVoid) { ExecutableElement methodOrConstructor = asExecutable(parameter.getEnclosingElement()); return enclosingTypeAndMemberName(methodOrConstructor) .append('(') .append( formatArgumentInList( methodOrConstructor.getParameters().indexOf(parameter), methodOrConstructor.getParameters().size(), parameter.getSimpleName())) .append(')') .toString(); }
/** Returns true if the given element can be referenced from other code in its own package. */ static boolean isElementAccessibleFromOwnPackage(Element element) { return isElementAccessibleFrom( element, MoreElements.getPackage(element).getQualifiedName().toString()); }
.ifPresent(this::addCreatorClass); getLocalAndInheritedMethods(graph.componentTypeElement(), types, elements) .forEach(method -> componentImplementation.claimMethodName(method.getSimpleName())); componentImplementation