Exemplo n.º 1
0
  private void parseMethod(Api api, ExecutableElement executableElement) throws IOException {

    Element actionElement =
        generator
            .getProcessingEnvironment()
            .getElementUtils()
            .getTypeElement(ApiMethodDoc.class.getName());
    TypeMirror apiMethodDocType = actionElement.asType();

    for (AnnotationMirror am : executableElement.getAnnotationMirrors()) {
      if (am.getAnnotationType().equals(apiMethodDocType)) {
        api.apiMethodDoc = Maps.newHashMap();
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> ee :
            am.getElementValues().entrySet()) {
          api.apiMethodDoc.put(ee.getKey().getSimpleName().toString(), ee.getValue());
        }
        break;
      }
    }

    // generator.log("apiMethodDoc: " + api.apiMethodDoc);

    if (null == api.apiMethodDoc) {
      generator.log("Method miss @ApiMethodDoc. " + executableElement);
      return;
    }

    api.methodName = executableElement.getSimpleName().toString();
    api.methodMapping = executableElement.getAnnotation(RequestMapping.class);
    api.parameters = Lists.newArrayList();

    for (VariableElement var : executableElement.getParameters()) {
      api.parameters.add(var);
    }
  }
  /** INTERNAL: Visit an executable and create a MetadataMethod object. */
  @Override
  public MetadataMethod visitExecutable(
      ExecutableElement executableElement, MetadataClass metadataClass) {
    MetadataMethod method = new MetadataMethod(metadataClass.getMetadataFactory(), metadataClass);

    // Set the name.
    method.setName(executableElement.getSimpleName().toString());

    // Set the attribute name.
    method.setAttributeName(Helper.getAttributeNameFromMethodName(method.getName()));

    // Set the modifiers.
    method.setModifiers(getModifiers(executableElement.getModifiers()));

    // Visit executable element for the parameters, return type and generic type.
    executableElement.asType().accept(typeVisitor, method);

    // Set the annotations.
    buildMetadataAnnotations(method, executableElement.getAnnotationMirrors());

    // Handle multiple methods with the same name.
    MetadataMethod existing = metadataClass.getMethods().get(method.getName());
    if (existing == null) {
      metadataClass.addMethod(method);
    } else {
      while (existing.getNext() != null) {
        existing = existing.getNext();
      }
      existing.setNext(method);
    }

    return method;
  }
Exemplo n.º 3
0
  private void printMethod(ExecutableElement executableElement) {
    StringBuilder s = new StringBuilder(256);
    s.append("method: " + executableElement).append("\n\t");
    s.append("annotations: " + executableElement.getAnnotationMirrors()).append("\n\t");
    s.append("return: " + executableElement.getReturnType()).append("\n\t");
    for (VariableElement var : executableElement.getParameters()) {
      s.append("parameter: " + var + ", " + var.getAnnotation(ApiParameterDoc.class))
          .append("\n\t");
    }

    generator.log(s.toString());
  }
Exemplo n.º 4
0
  public static CodeExecutableElement clone(
      @SuppressWarnings("unused") ProcessingEnvironment env, ExecutableElement method) {
    CodeExecutableElement copy =
        new CodeExecutableElement(method.getReturnType(), method.getSimpleName().toString());
    for (TypeMirror thrownType : method.getThrownTypes()) {
      copy.addThrownType(thrownType);
    }
    copy.setDefaultValue(method.getDefaultValue());

    for (AnnotationMirror mirror : method.getAnnotationMirrors()) {
      copy.addAnnotationMirror(mirror);
    }
    for (VariableElement var : method.getParameters()) {
      copy.addParameter(CodeVariableElement.clone(var));
    }
    for (Element element : method.getEnclosedElements()) {
      copy.add(element);
    }
    copy.getModifiers().addAll(method.getModifiers());
    copy.setVarArgs(method.isVarArgs());
    return copy;
  }
  /**
   * Formats an ExecutableElement as if it were contained within the container, if the container is
   * present.
   */
  public String format(ExecutableElement method, Optional<DeclaredType> container) {
    StringBuilder builder = new StringBuilder();
    TypeElement type = MoreElements.asType(method.getEnclosingElement());
    ExecutableType executableType = MoreTypes.asExecutable(method.asType());
    if (container.isPresent()) {
      executableType = MoreTypes.asExecutable(types.asMemberOf(container.get(), method));
      type = MoreElements.asType(container.get().asElement());
    }

    // TODO(cgruber): AnnotationMirror formatter.
    List<? extends AnnotationMirror> annotations = method.getAnnotationMirrors();
    if (!annotations.isEmpty()) {
      Iterator<? extends AnnotationMirror> annotationIterator = annotations.iterator();
      for (int i = 0; annotationIterator.hasNext(); i++) {
        if (i > 0) {
          builder.append(' ');
        }
        builder.append(ErrorMessages.format(annotationIterator.next()));
      }
      builder.append(' ');
    }
    builder.append(nameOfType(executableType.getReturnType()));
    builder.append(' ');
    builder.append(type.getQualifiedName());
    builder.append('.');
    builder.append(method.getSimpleName());
    builder.append('(');
    checkState(method.getParameters().size() == executableType.getParameterTypes().size());
    Iterator<? extends VariableElement> parameters = method.getParameters().iterator();
    Iterator<? extends TypeMirror> parameterTypes = executableType.getParameterTypes().iterator();
    for (int i = 0; parameters.hasNext(); i++) {
      if (i > 0) {
        builder.append(", ");
      }
      appendParameter(builder, parameters.next(), parameterTypes.next());
    }
    builder.append(')');
    return builder.toString();
  }
Exemplo n.º 6
0
  public boolean hasErrors(final TypeElement typeElement) {

    isClass(typeElement);
    isNotNested(typeElement);

    final MethodValidator methodValidator = new MethodValidator(this.messager);

    for (final ExecutableElement executableElement :
        ElementFilter.methodsIn(typeElement.getEnclosedElements())) {

      boolean symbol = false;
      for (final AnnotationMirror annotationMirror : executableElement.getAnnotationMirrors()) {
        if (annotationMirror
            .getAnnotationType()
            .asElement()
            .getSimpleName()
            .toString()
            .equals(Symbol.class.getSimpleName())) {
          symbol = true;
          break;
        }
      }

      // validate as C symbol
      if (symbol) {
        methodValidator.validateSymbol(executableElement);
      } else {
        // validate as C method
        methodValidator.validateIfNative(executableElement);
      }
    }

    this.error |= methodValidator.errorRaised();

    return this.error;
  }