Java Code Examples for com.google.auto.common.MoreTypes#asTypeElement()

The following examples show how to use com.google.auto.common.MoreTypes#asTypeElement() . 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 5 votes vote down vote up
@Override public TypeMirror visitDeclared(DeclaredType type, Types types) {
  TypeElement element = MoreTypes.asTypeElement(type);
  List<? extends TypeMirror> args = type.getTypeArguments();
  TypeMirror[] strippedArgs = new TypeMirror[args.size()];
  for (int i = 0; i < args.size(); i++) {
    TypeMirror arg = args.get(i);
    strippedArgs[i] = arg.accept(this, types);
  }
  return types.getDeclaredType(element, strippedArgs);
}
 
Example 2
Source File: MemoizedValidator.java    From auto with Apache License 2.0 5 votes vote down vote up
static Optional<AnnotationMirror> getAnnotationMirror(Element element, String annotationName) {
  for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
    TypeElement annotationElement = MoreTypes.asTypeElement(annotation.getAnnotationType());
    if (annotationElement.getQualifiedName().contentEquals(annotationName)) {
      return Optional.of(annotation);
    }
  }
  return Optional.empty();
}
 
Example 3
Source File: AutoValueOrOneOfProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
/**
 * Determines which of the three public non-final methods from {@code java.lang.Object}, if any,
 * is overridden by the given method.
 */
static ObjectMethod objectMethodToOverride(ExecutableElement method) {
  String name = method.getSimpleName().toString();
  switch (method.getParameters().size()) {
    case 0:
      if (name.equals("toString")) {
        return ObjectMethod.TO_STRING;
      } else if (name.equals("hashCode")) {
        return ObjectMethod.HASH_CODE;
      }
      break;
    case 1:
      if (name.equals("equals")) {
        TypeMirror param = getOnlyElement(method.getParameters()).asType();
        if (param.getKind().equals(TypeKind.DECLARED)) {
          TypeElement paramType = MoreTypes.asTypeElement(param);
          if (paramType.getQualifiedName().contentEquals("java.lang.Object")) {
            return ObjectMethod.EQUALS;
          }
        }
      }
      break;
    default:
      // No relevant Object methods have more than one parameter.
  }
  return ObjectMethod.NONE;
}
 
Example 4
Source File: AutoValueOrOneOfProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
static Optional<AnnotationMirror> getAnnotationMirror(Element element, String annotationName) {
  for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
    TypeElement annotationElement = MoreTypes.asTypeElement(annotation.getAnnotationType());
    if (annotationElement.getQualifiedName().contentEquals(annotationName)) {
      return Optional.of(annotation);
    }
  }
  return Optional.empty();
}
 
Example 5
Source File: EclipseHack.java    From auto with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the enclosing type of {@code type}, if {@code type} is an inner class. Otherwise
 * returns a {@code NoType}. This is what {@link DeclaredType#getEnclosingType()} is supposed to
 * do. However, some versions of Eclipse have a bug where, for example, asking for the enclosing
 * type of {@code PrimitiveIterator.OfInt} will return {@code PrimitiveIterator<T, T_CONS>} rather
 * than plain {@code PrimitiveIterator}, as if {@code OfInt} were an inner class rather than a
 * static one. This would lead us to write a reference to {@code OfInt} as {@code
 * PrimitiveIterator<T, T_CONS>.OfInt}, which would obviously break. We attempt to avert this by
 * detecting that:
 *
 * <ul>
 *   <li>there is an enclosing type that is a {@code DeclaredType}, which should mean that {@code
 *       type} is an inner class;
 *   <li>we are in the Eclipse compiler;
 *   <li>the type arguments of the purported enclosing type are all type variables with the same
 *       names as the corresponding type parameters.
 * </ul>
 *
 * <p>If all these conditions are met, we assume we're hitting the Eclipse bug, and we return no
 * enclosing type instead. That does mean that in the unlikely event where we really do have an
 * inner class of an instantiation of the outer class with type arguments that happen to be type
 * variables with the same names as the corresponding parameters, we will do the wrong thing on
 * Eclipse. But doing the wrong thing in that case is better than doing the wrong thing in the
 * usual case.
 */
static TypeMirror getEnclosingType(DeclaredType type) {
  TypeMirror enclosing = type.getEnclosingType();
  if (!enclosing.getKind().equals(TypeKind.DECLARED)
      || !enclosing.getClass().getName().contains("eclipse")) {
    // If the class representing the enclosing type comes from the Eclipse compiler, it will be
    // something like org.eclipse.jdt.internal.compiler.apt.model.DeclaredTypeImpl. If we're not
    // in the Eclipse compiler then we don't expect to see "eclipse" in the name of this
    // implementation class.
    return enclosing;
  }
  DeclaredType declared = MoreTypes.asDeclared(enclosing);
  List<? extends TypeMirror> arguments = declared.getTypeArguments();
  if (!arguments.isEmpty()) {
    boolean allVariables = arguments.stream().allMatch(t -> t.getKind().equals(TypeKind.TYPEVAR));
    if (allVariables) {
      List<Name> argumentNames =
          arguments.stream()
              .map(t -> MoreTypes.asTypeVariable(t).asElement().getSimpleName())
              .collect(toList());
      TypeElement enclosingElement = MoreTypes.asTypeElement(declared);
      List<Name> parameterNames =
          enclosingElement.getTypeParameters().stream()
              .map(Element::getSimpleName)
              .collect(toList());
      if (argumentNames.equals(parameterNames)) {
        // We're going to return a NoType. We don't have a Types to hand so we can't call
        // Types.getNoType(). Instead, just keep going through outer types until we get to
        // the outside, which will be a NoType.
        while (enclosing.getKind().equals(TypeKind.DECLARED)) {
          enclosing = MoreTypes.asDeclared(enclosing).getEnclosingType();
        }
        return enclosing;
      }
    }
  }
  return declared;
}
 
Example 6
Source File: AutoServiceProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
private void processAnnotations(Set<? extends TypeElement> annotations,
    RoundEnvironment roundEnv) {

  Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(AutoService.class);

  log(annotations.toString());
  log(elements.toString());

  for (Element e : elements) {
    // TODO(gak): check for error trees?
    TypeElement providerImplementer = (TypeElement) e;
    AnnotationMirror annotationMirror = getAnnotationMirror(e, AutoService.class).get();
    Set<DeclaredType> providerInterfaces = getValueFieldOfClasses(annotationMirror);
    if (providerInterfaces.isEmpty()) {
      error(MISSING_SERVICES_ERROR, e, annotationMirror);
      continue;
    }
    for (DeclaredType providerInterface : providerInterfaces) {
      TypeElement providerType = MoreTypes.asTypeElement(providerInterface);

      log("provider interface: " + providerType.getQualifiedName());
      log("provider implementer: " + providerImplementer.getQualifiedName());

      if (checkImplementer(providerImplementer, providerType)) {
        providers.put(getBinaryName(providerType), getBinaryName(providerImplementer));
      } else {
        String message = "ServiceProviders must implement their service provider interface. "
            + providerImplementer.getQualifiedName() + " does not implement "
            + providerType.getQualifiedName();
        error(message, e, annotationMirror);
      }
    }
  }
}
 
Example 7
Source File: MoreApt.java    From j2cl with Apache License 2.0 4 votes vote down vote up
public static TypeElement asTypeElement(TypeMirror type) {
  return type.getKind() != TypeKind.DECLARED ? null : MoreTypes.asTypeElement(type);
}