com.google.auto.common.AnnotationMirrors Java Examples

The following examples show how to use com.google.auto.common.AnnotationMirrors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From paperparcel with Apache License 2.0 6 votes vote down vote up
private static Optional<AnnotationMirror> findOptionsMirror(TypeElement element) {
  Optional<AnnotationMirror> result =
      MoreElements.getAnnotationMirror(element, PaperParcel.Options.class);
  if (!result.isPresent()) {
    ImmutableSet<? extends AnnotationMirror> annotatedAnnotations =
        AnnotationMirrors.getAnnotatedAnnotations(element, PaperParcel.Options.class);
    if (annotatedAnnotations.size() > 1) {
      throw new IllegalStateException("PaperParcel options applied twice.");
    } else if (annotatedAnnotations.size() == 1) {
      AnnotationMirror annotatedAnnotation = annotatedAnnotations.iterator().next();
      result = MoreElements.getAnnotationMirror(
          annotatedAnnotation.getAnnotationType().asElement(), PaperParcel.Options.class);
    } else {
      TypeMirror superType = element.getSuperclass();
      if (superType.getKind() != TypeKind.NONE) {
        TypeElement superElement = asType(asDeclared(superType).asElement());
        result = findOptionsMirror(superElement);
      }
    }
  }
  return result;
}
 
Example #2
Source File: Parameter.java    From auto with Apache License 2.0 6 votes vote down vote up
private static Parameter forVariableElement(
    VariableElement variable, TypeMirror type, Types types) {
  Optional<AnnotationMirror> nullable = Optional.absent();
  Iterable<? extends AnnotationMirror> annotations =
      Iterables.concat(variable.getAnnotationMirrors(), type.getAnnotationMirrors());
  for (AnnotationMirror annotation : annotations) {
    if (isNullable(annotation)) {
      nullable = Optional.of(annotation);
      break;
    }
  }

  Key key = Key.create(type, annotations, types);
  return new AutoValue_Parameter(
      MoreTypes.equivalence().wrap(type),
      variable.getSimpleName().toString(),
      key,
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), nullable));
}
 
Example #3
Source File: Key.java    From auto with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a key based on the type {@code type} and any {@link Qualifier}s in {@code
 * annotations}.
 *
 * <p>If {@code type} is a {@code Provider<T>}, the returned {@link Key}'s {@link #type()} is
 * {@code T}. If {@code type} is a primitive, the returned {@link Key}'s {@link #type()} is the
 * corresponding {@linkplain Types#boxedClass(PrimitiveType) boxed type}.
 *
 * <p>For example:
 * <table>
 *   <tr><th>Input type                <th>{@code Key.type()}
 *   <tr><td>{@code String}            <td>{@code String}
 *   <tr><td>{@code Provider<String>}  <td>{@code String}
 *   <tr><td>{@code int}               <td>{@code Integer}
 * </table>
 */
static Key create(
    TypeMirror type, Iterable<? extends AnnotationMirror> annotations, Types types) {
  ImmutableSet.Builder<AnnotationMirror> qualifiers = ImmutableSet.builder();
  for (AnnotationMirror annotation : annotations) {
    if (isAnnotationPresent(annotation.getAnnotationType().asElement(), Qualifier.class)) {
      qualifiers.add(annotation);
    }
  }

  // TODO(gak): check for only one qualifier rather than using the first
  Optional<AnnotationMirror> qualifier = FluentIterable.from(qualifiers.build()).first();

  TypeMirror keyType =
      isProvider(type)
          ? MoreTypes.asDeclared(type).getTypeArguments().get(0)
          : boxedType(type, types);
  return new AutoValue_Key(
      MoreTypes.equivalence().wrap(keyType),
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), qualifier));
}
 
Example #4
Source File: ProvisionBinding.java    From dagger2-sample with Apache License 2.0 6 votes vote down vote up
ProvisionBinding forComponentMethod(ExecutableElement componentMethod) {
  checkNotNull(componentMethod);
  checkArgument(componentMethod.getKind().equals(METHOD));
  checkArgument(componentMethod.getParameters().isEmpty());
  Optional<AnnotationMirror> scope = getScopeAnnotation(componentMethod);
  return new AutoValue_ProvisionBinding(
      keyFactory.forComponentMethod(componentMethod),
      componentMethod,
      ImmutableSet.<DependencyRequest>of(),
      Optional.<String>absent(),
      false /* no non-default parameter types */,
      ConfigurationAnnotations.getNullableType(componentMethod),
      Optional.<TypeElement>absent(),
      Kind.COMPONENT_PROVISION,
      Provides.Type.UNIQUE,
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), scope),
      Optional.<DependencyRequest>absent());
}
 
Example #5
Source File: ProvisionBinding.java    From dagger2-sample with Apache License 2.0 6 votes vote down vote up
ProvisionBinding forImplicitMapBinding(DependencyRequest explicitRequest,
    DependencyRequest implicitRequest) {
  checkNotNull(explicitRequest);
  checkNotNull(implicitRequest);
  ImmutableSet<DependencyRequest> dependencies = ImmutableSet.of(implicitRequest);
  Optional<AnnotationMirror> scope = getScopeAnnotation(implicitRequest.requestElement());
  return new AutoValue_ProvisionBinding(
      explicitRequest.key(),
      implicitRequest.requestElement(),
      dependencies,
      findBindingPackage(explicitRequest.key()),
      false /* no non-default parameter types */,
      Optional.<DeclaredType>absent(),
      Optional.<TypeElement>absent(),
      Kind.SYNTHETIC_PROVISON,
      Provides.Type.MAP,
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), scope),
      Optional.<DependencyRequest>absent());
}
 
Example #6
Source File: Key.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
Key forComponentMethod(ExecutableElement componentMethod) {
  checkNotNull(componentMethod);
  checkArgument(componentMethod.getKind().equals(METHOD));
  TypeMirror returnType = normalize(types, componentMethod.getReturnType());
  return new AutoValue_Key(
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(componentMethod)),
      MoreTypes.equivalence().wrap(returnType));
}
 
Example #7
Source File: Key.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
Key forProductionComponentMethod(ExecutableElement componentMethod) {
  checkNotNull(componentMethod);
  checkArgument(componentMethod.getKind().equals(METHOD));
  TypeMirror returnType = normalize(types, componentMethod.getReturnType());
  TypeMirror keyType = returnType;
  if (MoreTypes.isTypeOf(ListenableFuture.class, returnType)) {
    keyType = Iterables.getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments());
  }
  return new AutoValue_Key(
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(componentMethod)),
      MoreTypes.equivalence().wrap(keyType));
}
 
Example #8
Source File: Key.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
Key forProvidesMethod(ExecutableType executableType, ExecutableElement e) {
  checkNotNull(e);
  checkArgument(e.getKind().equals(METHOD));
  Provides providesAnnotation = e.getAnnotation(Provides.class);
  checkArgument(providesAnnotation != null);
  TypeMirror returnType = normalize(types, executableType.getReturnType());
  switch (providesAnnotation.type()) {
    case UNIQUE:
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(returnType));
    case SET:
      TypeMirror setType = types.getDeclaredType(getSetElement(), returnType);
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(setType));
    case MAP:
      AnnotationMirror mapKeyAnnotation = Iterables.getOnlyElement(getMapKeys(e));
      MapKey mapKey =
          mapKeyAnnotation.getAnnotationType().asElement().getAnnotation(MapKey.class);
      TypeElement keyTypeElement =
          mapKey.unwrapValue() ? Util.getKeyTypeElement(mapKeyAnnotation, elements)
              : (TypeElement) mapKeyAnnotation.getAnnotationType().asElement();
      TypeMirror valueType = types.getDeclaredType(getProviderElement(), returnType);
      TypeMirror mapType =
          types.getDeclaredType(getMapElement(), keyTypeElement.asType(), valueType);
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(mapType));
    case SET_VALUES:
      // TODO(gak): do we want to allow people to use "covariant return" here?
      checkArgument(returnType.getKind().equals(DECLARED));
      checkArgument(((DeclaredType) returnType).asElement().equals(getSetElement()));
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(returnType));
    default:
      throw new AssertionError();
  }
}
 
Example #9
Source File: AutoOneOfProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
private DeclaredType mirrorForKindType(TypeElement autoOneOfType) {
  Optional<AnnotationMirror> oneOfAnnotation =
      getAnnotationMirror(autoOneOfType, AUTO_ONE_OF_NAME);
  if (!oneOfAnnotation.isPresent()) {
    // This shouldn't happen unless the compilation environment is buggy,
    // but it has happened in the past and can crash the compiler.
    errorReporter()
        .abortWithError(
            autoOneOfType,
            "annotation processor for @AutoOneOf was invoked with a type"
                + " that does not have that annotation; this is probably a compiler bug");
  }
  AnnotationValue kindValue =
      AnnotationMirrors.getAnnotationValue(oneOfAnnotation.get(), "value");
  Object value = kindValue.getValue();
  if (value instanceof TypeMirror) {
    TypeMirror kindType = (TypeMirror) value;
    switch (kindType.getKind()) {
      case DECLARED:
        return MoreTypes.asDeclared(kindType);
      case ERROR:
        throw new MissingTypeException(MoreTypes.asError(kindType));
      default:
        break;
    }
  }
  throw new MissingTypeException(null);
}
 
Example #10
Source File: ComponentGenerator.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
private void writeEntry(List<Object> argsBuilder, Binding binding,
    Snippet factory) {
  AnnotationMirror mapKeyAnnotationMirror =
      Iterables.getOnlyElement(getMapKeys(binding.bindingElement()));
  Map<? extends ExecutableElement, ? extends AnnotationValue> map =
      mapKeyAnnotationMirror.getElementValues();
  MapKey mapKey =
      mapKeyAnnotationMirror.getAnnotationType().asElement().getAnnotation(MapKey.class);
  if (!mapKey.unwrapValue()) {// wrapped key case
    FluentIterable<AnnotationValue> originIterable = FluentIterable.from(
        AnnotationMirrors.getAnnotationValuesWithDefaults(mapKeyAnnotationMirror).values());
    FluentIterable<Snippet> annotationValueNames =
        originIterable.transform(new Function<AnnotationValue, Snippet>() {
          @Override
          public Snippet apply(AnnotationValue value) {
            return getValueSnippet(value);
          }
        });
    ImmutableList.Builder<Snippet> snippets = ImmutableList.builder();
    for (Snippet snippet : annotationValueNames) {
      snippets.add(snippet);
    }
    argsBuilder.add(Snippet.format("%sCreator.create(%s)",
        TypeNames.forTypeMirror(mapKeyAnnotationMirror.getAnnotationType()),
        Snippet.makeParametersSnippet(snippets.build())));
    argsBuilder.add(factory);
  } else { // unwrapped key case
    argsBuilder.add(Iterables.getOnlyElement(map.entrySet()).getValue());
    argsBuilder.add(factory);
  }
}
 
Example #11
Source File: ProvisionBinding.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
ProvisionBinding forProvidesMethod(ExecutableElement providesMethod, TypeMirror contributedBy) {
  checkNotNull(providesMethod);
  checkArgument(providesMethod.getKind().equals(METHOD));
  checkArgument(contributedBy.getKind().equals(TypeKind.DECLARED));
  Provides providesAnnotation = providesMethod.getAnnotation(Provides.class);
  checkArgument(providesAnnotation != null);
  DeclaredType declaredContainer = MoreTypes.asDeclared(contributedBy);
  ExecutableType resolvedMethod =
      MoreTypes.asExecutable(types.asMemberOf(declaredContainer, providesMethod));
  Key key = keyFactory.forProvidesMethod(resolvedMethod, providesMethod);
  ImmutableSet<DependencyRequest> dependencies =
      dependencyRequestFactory.forRequiredResolvedVariables(
          declaredContainer,
          providesMethod.getParameters(),
          resolvedMethod.getParameterTypes());
  Optional<AnnotationMirror> scope = getScopeAnnotation(providesMethod);
  return new AutoValue_ProvisionBinding(
      key,
      providesMethod,
      dependencies,
      findBindingPackage(key),
      false /* no non-default parameter types */,
      ConfigurationAnnotations.getNullableType(providesMethod),
      Optional.of(MoreTypes.asTypeElement(declaredContainer)),
      Kind.PROVISION,
      providesAnnotation.type(),
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), scope),
      Optional.<DependencyRequest>absent());
}
 
Example #12
Source File: ProvisionBinding.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a ProvisionBinding for the given element. If {@code resolvedType} is present, this
 * will return a resolved binding, with the key & type resolved to the given type (using
 * {@link Types#asMemberOf(DeclaredType, Element)}).
 */
ProvisionBinding forInjectConstructor(ExecutableElement constructorElement,
    Optional<TypeMirror> resolvedType) {
  checkNotNull(constructorElement);
  checkArgument(constructorElement.getKind().equals(CONSTRUCTOR));
  checkArgument(isAnnotationPresent(constructorElement, Inject.class));
  checkArgument(!getQualifier(constructorElement).isPresent());

  ExecutableType cxtorType = MoreTypes.asExecutable(constructorElement.asType());
  DeclaredType enclosingCxtorType =
      MoreTypes.asDeclared(constructorElement.getEnclosingElement().asType());
  // If the class this is constructing has some type arguments, resolve everything.
  if (!enclosingCxtorType.getTypeArguments().isEmpty() && resolvedType.isPresent()) {
    DeclaredType resolved = MoreTypes.asDeclared(resolvedType.get());
    // Validate that we're resolving from the correct type.
    checkState(types.isSameType(types.erasure(resolved), types.erasure(enclosingCxtorType)),
        "erased expected type: %s, erased actual type: %s",
        types.erasure(resolved), types.erasure(enclosingCxtorType));
    cxtorType = MoreTypes.asExecutable(types.asMemberOf(resolved, constructorElement));
    enclosingCxtorType = resolved;
  }

  Key key = keyFactory.forInjectConstructorWithResolvedType(enclosingCxtorType);
  checkArgument(!key.qualifier().isPresent());
  ImmutableSet<DependencyRequest> dependencies =
      dependencyRequestFactory.forRequiredResolvedVariables(enclosingCxtorType,
          constructorElement.getParameters(),
          cxtorType.getParameterTypes());
  Optional<DependencyRequest> membersInjectionRequest =
      membersInjectionRequest(enclosingCxtorType);
  Optional<AnnotationMirror> scope =
      getScopeAnnotation(constructorElement.getEnclosingElement());

  TypeElement bindingTypeElement =
      MoreElements.asType(constructorElement.getEnclosingElement());

  return new AutoValue_ProvisionBinding(
      key,
      constructorElement,
      dependencies,
      findBindingPackage(key),
      hasNonDefaultTypeParameters(bindingTypeElement, key.type(), types),
      Optional.<DeclaredType>absent(),
      Optional.<TypeElement>absent(),
      Kind.INJECTION,
      Provides.Type.UNIQUE,
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), scope),
      membersInjectionRequest);
}
 
Example #13
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
static OptionsDescriptor getModuleOptions(AnnotationMirror processorConfig) {
  AnnotationValue optionsAnnotationValue =
      AnnotationMirrors.getAnnotationValue(processorConfig, "options");
  AnnotationMirror optionsMirror = optionsAnnotationValue.accept(TO_ANNOTATION, null);
  return parseOptions(optionsMirror);
}
 
Example #14
Source File: ProviderField.java    From auto with Apache License 2.0 4 votes vote down vote up
static ProviderField create(String name, Key key, Optional<AnnotationMirror> nullable) {
  return new AutoValue_ProviderField(
      name, key, wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), nullable));
}
 
Example #15
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
private static ImmutableList<Set<Modifier>> getExcludeModifiers(AnnotationMirror mirror) {
  AnnotationValue excludeFieldsWithModifiers =
      AnnotationMirrors.getAnnotationValue(mirror, "excludeModifiers");
  return convertModifiers(excludeFieldsWithModifiers.accept(INT_ARRAY_VISITOR, null));
}
 
Example #16
Source File: ComponentMethodDescriptor.java    From bullet with Apache License 2.0 4 votes vote down vote up
static boolean hasQualifier(Element e) {
  return !AnnotationMirrors.getAnnotatedAnnotations(e, Qualifier.class).isEmpty();
}
 
Example #17
Source File: BindingGraphValidator.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
/**
 * Validates that scopes do not participate in a scoping cycle - that is to say, scoped
 * components are in a hierarchical relationship terminating with Singleton.
 *
 * <p>As a side-effect, this means scoped components cannot have a dependency cycle between
 * themselves, since a component's presence within its own dependency path implies a cyclical
 * relationship between scopes.
 */
private void validateScopeHierarchy(TypeElement rootComponent,
    TypeElement componentType,
    Builder<BindingGraph> reportBuilder,
    Deque<Equivalence.Wrapper<AnnotationMirror>> scopeStack,
    Deque<TypeElement> scopedDependencyStack) {
  Optional<AnnotationMirror> scope = getScopeAnnotation(componentType);
  if (scope.isPresent()) {
    Equivalence.Wrapper<AnnotationMirror> wrappedScope =
        AnnotationMirrors.equivalence().wrap(scope.get());
    if (scopeStack.contains(wrappedScope)) {
      scopedDependencyStack.push(componentType);
      // Current scope has already appeared in the component chain.
      StringBuilder message = new StringBuilder();
      message.append(rootComponent.getQualifiedName());
      message.append(" depends on scoped components in a non-hierarchical scope ordering:\n");
      appendIndentedComponentsList(message, scopedDependencyStack);
      if (scopeCycleValidationType.diagnosticKind().isPresent()) {
        reportBuilder.addItem(message.toString(),
            scopeCycleValidationType.diagnosticKind().get(),
            rootComponent, getAnnotationMirror(rootComponent, Component.class).get());
      }
      scopedDependencyStack.pop();
    } else {
      Optional<AnnotationMirror> componentAnnotation =
          getAnnotationMirror(componentType, Component.class);
      if (componentAnnotation.isPresent()) {
        ImmutableSet<TypeElement> scopedDependencies = scopedTypesIn(
            MoreTypes.asTypeElements(getComponentDependencies(componentAnnotation.get())));
        if (scopedDependencies.size() == 1) {
          // empty can be ignored (base-case), and > 1 is a different error reported separately.
          scopeStack.push(wrappedScope);
          scopedDependencyStack.push(componentType);
          validateScopeHierarchy(rootComponent, getOnlyElement(scopedDependencies),
              reportBuilder, scopeStack, scopedDependencyStack);
          scopedDependencyStack.pop();
          scopeStack.pop();
        }
      } // else: we skip component dependencies which are not components
    }
  }
}
 
Example #18
Source File: ConfigurationAnnotations.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
static ImmutableSet<? extends AnnotationMirror> getMapKeys(Element element) {
  return AnnotationMirrors.getAnnotatedAnnotations(element, MapKey.class);
}
 
Example #19
Source File: ComponentDescriptor.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
private ComponentDescriptor create(TypeElement componentDefinitionType, Kind kind) {
  AnnotationMirror componentMirror =
      getAnnotationMirror(componentDefinitionType, kind.annotationType())
          .or(getAnnotationMirror(componentDefinitionType, Subcomponent.class))
          .get();
  ImmutableSet<TypeElement> componentDependencyTypes =
      isComponent(componentDefinitionType)
          ? MoreTypes.asTypeElements(getComponentDependencies(componentMirror))
          : ImmutableSet.<TypeElement>of();

  ImmutableMap.Builder<ExecutableElement, TypeElement> dependencyMethodIndex =
      ImmutableMap.builder();

  for (TypeElement componentDependency : componentDependencyTypes) {
    List<ExecutableElement> dependencyMethods =
        ElementFilter.methodsIn(elements.getAllMembers(componentDependency));
    for (ExecutableElement dependencyMethod : dependencyMethods) {
      if (isComponentContributionMethod(elements, dependencyMethod)) {
        dependencyMethodIndex.put(dependencyMethod, componentDependency);
      }
    }
  }

  Optional<TypeElement> executorDependency =
      kind.equals(Kind.PRODUCTION_COMPONENT)
          ? Optional.of(elements.getTypeElement(Executor.class.getCanonicalName()))
          : Optional.<TypeElement>absent();

  ImmutableSet<ExecutableElement> unimplementedMethods =
      getUnimplementedMethods(elements, componentDefinitionType);

  ImmutableSet.Builder<ComponentMethodDescriptor> componentMethodsBuilder =
      ImmutableSet.builder();

  ImmutableMap.Builder<ExecutableElement, ComponentDescriptor> subcomponentDescriptors =
      ImmutableMap.builder();
  for (ExecutableElement componentMethod : unimplementedMethods) {
    ComponentMethodDescriptor componentMethodDescriptor =
        getDescriptorForComponentMethod(componentDefinitionType, kind, componentMethod);
    componentMethodsBuilder.add(componentMethodDescriptor);
    if (componentMethodDescriptor.kind().equals(ComponentMethodKind.SUBCOMPONENT)) {
      subcomponentDescriptors.put(componentMethod,
          create(MoreElements.asType(MoreTypes.asElement(componentMethod.getReturnType())),
              Kind.COMPONENT));
    }
  }

  Optional<AnnotationMirror> scope = getScopeAnnotation(componentDefinitionType);
  return new AutoValue_ComponentDescriptor(
      kind,
      componentMirror,
      componentDefinitionType,
      componentDependencyTypes,
      dependencyMethodIndex.build(),
      executorDependency,
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), scope),
      subcomponentDescriptors.build(),
      componentMethodsBuilder.build());
}
 
Example #20
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
private static ImmutableList<String> getExcludeAnnotations(AnnotationMirror mirror) {
  AnnotationValue excludeAnnotationNames =
      AnnotationMirrors.getAnnotationValue(mirror, "excludeAnnotations");
  return excludeAnnotationNames.accept(TYPE_NAME_ARRAY_VISITOR, null);
}
 
Example #21
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
private static ImmutableList<String> getExposeAnnotations(AnnotationMirror mirror) {
  AnnotationValue exposeAnnotationNames =
      AnnotationMirrors.getAnnotationValue(mirror, "exposeAnnotations");
  return exposeAnnotationNames.accept(TYPE_NAME_ARRAY_VISITOR, null);
}
 
Example #22
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
private static boolean getExcludeNonExposedFields(AnnotationMirror mirror) {
  AnnotationValue excludeNonExposedFields =
      AnnotationMirrors.getAnnotationValue(mirror, "excludeNonExposedFields");
  return excludeNonExposedFields.accept(TO_BOOLEAN, null);
}
 
Example #23
Source File: InjectionAnnotations.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
static ImmutableSet<? extends AnnotationMirror> getScopes(Element element) {
  return AnnotationMirrors.getAnnotatedAnnotations(element, Scope.class);
}
 
Example #24
Source File: InjectionAnnotations.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
static ImmutableSet<? extends AnnotationMirror> getQualifiers(Element element) {
  return AnnotationMirrors.getAnnotatedAnnotations(element, Qualifier.class);
}
 
Example #25
Source File: Key.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
Key forQualifiedType(Optional<AnnotationMirror> qualifier, TypeMirror type) {
  return new AutoValue_Key(
      wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), qualifier),
      MoreTypes.equivalence().wrap(normalize(types, type)));
}
 
Example #26
Source File: Key.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
Key forProducesMethod(ExecutableType executableType, ExecutableElement e) {
  checkNotNull(e);
  checkArgument(e.getKind().equals(METHOD));
  Produces producesAnnotation = e.getAnnotation(Produces.class);
  checkArgument(producesAnnotation != null);
  TypeMirror returnType = normalize(types, executableType.getReturnType());
  TypeMirror keyType = returnType;
  if (MoreTypes.isTypeOf(ListenableFuture.class, returnType)) {
    keyType = Iterables.getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments());
  }
  switch (producesAnnotation.type()) {
    case UNIQUE:
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(keyType));
    case SET:
      TypeMirror setType = types.getDeclaredType(getSetElement(), keyType);
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(setType));
    case MAP:
      AnnotationMirror mapKeyAnnotation = Iterables.getOnlyElement(getMapKeys(e));
      MapKey mapKey =
          mapKeyAnnotation.getAnnotationType().asElement().getAnnotation(MapKey.class);
      TypeElement keyTypeElement =
          mapKey.unwrapValue() ? Util.getKeyTypeElement(mapKeyAnnotation, elements)
              : (TypeElement) mapKeyAnnotation.getAnnotationType().asElement();
      TypeMirror valueType = types.getDeclaredType(getProducerElement(), keyType);
      TypeMirror mapType =
          types.getDeclaredType(getMapElement(), keyTypeElement.asType(), valueType);
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(mapType));
    case SET_VALUES:
      // TODO(gak): do we want to allow people to use "covariant return" here?
      checkArgument(keyType.getKind().equals(DECLARED));
      checkArgument(((DeclaredType) keyType).asElement().equals(getSetElement()));
      return new AutoValue_Key(
          wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)),
          MoreTypes.equivalence().wrap(keyType));
    default:
      throw new AssertionError();
  }
}
 
Example #27
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
private static boolean getAllowSerializable(AnnotationMirror mirror) {
  AnnotationValue allowSerializable =
      AnnotationMirrors.getAnnotationValue(mirror, "allowSerializable");
  return allowSerializable.accept(TO_BOOLEAN, null);
}
 
Example #28
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
private static ImmutableList<String> getReflectAnnotations(AnnotationMirror mirror) {
  AnnotationValue exposeAnnotationNames =
      AnnotationMirrors.getAnnotationValue(mirror, "reflectAnnotations");
  return exposeAnnotationNames.accept(TYPE_NAME_ARRAY_VISITOR, null);
}