Java Code Examples for javax.lang.model.element.ExecutableElement#asType()

The following examples show how to use javax.lang.model.element.ExecutableElement#asType() . These examples are extracted from open source projects. 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
private static Optional<String> inferName(ExecutableElement element) {
  Encodable.Field annotation = element.getAnnotation(Encodable.Field.class);
  if (annotation != null && !annotation.name().isEmpty()) {
    return Optional.of(annotation.name());
  }

  String methodName = element.getSimpleName().toString();
  ExecutableType method = (ExecutableType) element.asType();

  if (methodName.startsWith("is")
      && methodName.length() != 2
      && method.getReturnType().getKind() == TypeKind.BOOLEAN) {
    return Optional.of(Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3));
  }

  if (methodName.startsWith("get")
      && methodName.length() != 3
      && method.getReturnType().getKind() != TypeKind.VOID) {
    return Optional.of(Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4));
  }
  return Optional.empty();
}
 
Example 2
Source Project: turbine   File: TurbineElements.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean overrides(
    ExecutableElement overrider, ExecutableElement overridden, TypeElement type) {
  if (!overrider.getSimpleName().contentEquals(overridden.getSimpleName())) {
    return false;
  }
  TypeMirror a = overrider.asType();
  TypeMirror b = types.asMemberOf((DeclaredType) type.asType(), overridden);
  if (b == null) {
    return false;
  }
  if (!types.isSubsignature((TurbineExecutableType) a, (TurbineExecutableType) b)) {
    return false;
  }
  return isVisible(
      packageSymbol(asSymbol(overrider)),
      packageSymbol(asSymbol(overridden)),
      TurbineVisibility.fromAccess(((TurbineExecutableElement) overridden).info().access()));
}
 
Example 3
Source Project: vertx-docgen   File: JavaDocGenerator.java    License: Apache License 2.0 6 votes vote down vote up
private String toExecutableLink(ExecutableElement elt, String name) {
  TypeElement typeElt = (TypeElement) elt.getEnclosingElement();
  String link = resolveTypeLink(typeElt, null);
  StringBuilder anchor = new StringBuilder("#");
  anchor.append(name).append('-');
  TypeMirror type = elt.asType();
  ExecutableType methodType = (ExecutableType) processingEnv.getTypeUtils().erasure(type);
  List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();
  for (int i = 0; i < parameterTypes.size(); i++) {
    if (i > 0) {
      anchor.append('-');
    }
    // We need to check whether or not the parameter is annotated. In this case, we must use the unannotated type.
    TypeMirror typeOfParameter = parameterTypes.get(i);
    if (typeOfParameter instanceof Type && ((Type) typeOfParameter).isAnnotated()) {
      anchor.append(((Type) typeOfParameter).unannotatedType().toString());
    } else {
      anchor.append(typeOfParameter.toString());
    }
  }
  anchor.append('-');
  return link + anchor;
}
 
Example 4
Source Project: j2objc   File: CastResolver.java    License: Apache License 2.0 5 votes vote down vote up
private TypeMirror getDeclaredReturnType(ExecutableElement method, TypeMirror receiverType) {
  // Check if the method is declared on the receiver type.
  if (ElementUtil.getDeclaringClass(method).equals(TypeUtil.asTypeElement(receiverType))) {
    return method.getReturnType();
  }

  // Search all inherited types for matching method declarations. Choose the
  // most narrow return type, because AbstractMethodRewriter will ensure that
  // a declaration exists with the most narrow return type.
  ExecutableType methodType = (ExecutableType) method.asType();
  String selector = nameTable.getMethodSelector(method);
  for (TypeMirror typeBound : typeUtil.getUpperBounds(receiverType)) {
    if (TypeUtil.isDeclaredType(typeBound)) {
      // Normalize any parameterized types before searching for method declarations.
      typeBound = ((DeclaredType) typeBound).asElement().asType();
    }
    TypeMirror returnType = null;
    for (DeclaredType inheritedType : typeUtil.getObjcOrderedInheritedTypes(typeBound)) {
      TypeElement inheritedElem = (TypeElement) inheritedType.asElement();
      for (ExecutableElement currentMethod : ElementUtil.getMethods(inheritedElem)) {
        ExecutableType currentMethodType = typeUtil.asMemberOf(inheritedType, currentMethod);
        if (typeUtil.isSubsignature(methodType, currentMethodType)
            && nameTable.getMethodSelector(currentMethod).equals(selector)) {
          TypeMirror newReturnType = typeUtil.erasure(currentMethodType.getReturnType());
          if (returnType == null || typeUtil.isSubtype(newReturnType, returnType)) {
            returnType = newReturnType;
          }
        }
      }
    }
    if (returnType != null) {
      return returnType;
    }
  }

  // Last resort. Might be a GeneratedExecutableElement.
  return method.getReturnType();
}
 
Example 5
private Optional<Getter> create(DeclaredType ownerType, ExecutableElement element) {
  if (element.getKind() != ElementKind.METHOD
      || element.getModifiers().contains(Modifier.STATIC)
      || !element.getModifiers().contains(Modifier.PUBLIC)) {
    return Optional.empty();
  }

  ExecutableType method = (ExecutableType) element.asType();
  if (!method.getParameterTypes().isEmpty()) {
    return Optional.empty();
  }

  if (element.getAnnotation(Encodable.Ignore.class) != null) {
    return Optional.empty();
  }
  Optional<String> fieldName = inferName(element);
  if (!fieldName.isPresent()) {
    return Optional.empty();
  }
  TypeMirror returnType = resolveTypeArguments(ownerType, element.getReturnType());
  String getterExpression = element.toString();

  // Fail to compile if Maps with non-string keys are used, if/when we add support for such maps
  // we should delete this.
  TypeMirror map = types.erasure(elements.getTypeElement("java.util.Map").asType());
  if (types.isAssignable(returnType, map)) {
    TypeMirror keyType = ((DeclaredType) returnType).getTypeArguments().get(0);
    if (!types.isSameType(keyType, elements.getTypeElement("java.lang.String").asType())) {
      messager.printMessage(
          Diagnostic.Kind.ERROR,
          "Cannot encode Maps with non-String keys.",
          ((DeclaredType) returnType).asElement());
    }
  }
  if (types.isAssignable(
      returnType, types.erasure(elements.getTypeElement("java.util.Optional").asType()))) {
    returnType = ((DeclaredType) returnType).getTypeArguments().get(0);
    getterExpression = getterExpression + ".orElse(null)";
  }

  Encodable.Field field = element.getAnnotation(Encodable.Field.class);

  return Optional.of(
      Getter.create(
          fieldName.get(), getterExpression, returnType, field != null && field.inline()));
}
 
Example 6
Source Project: j2objc   File: ExecutablePair.java    License: Apache License 2.0 4 votes vote down vote up
public ExecutablePair(ExecutableElement element) {
  this(element, (ExecutableType) element.asType());
}