private void processFromProperties(TypeElement type, Set<TypeElement> types) {
    List<? extends Element> children = type.getEnclosedElements();
    VisitorConfig config = conf.getConfig(type, children);

    // fields
    if (config.visitFieldProperties()) {
      for (VariableElement field : ElementFilter.fieldsIn(children)) {
        TypeElement typeElement = typeExtractor.visit(field.asType());
        if (typeElement != null) {
          types.add(typeElement);
        }
      }
    }

    // getters
    if (config.visitMethodProperties()) {
      for (ExecutableElement method : ElementFilter.methodsIn(children)) {
        String name = method.getSimpleName().toString();
        if ((name.startsWith("get") || name.startsWith("is")) && method.getParameters().isEmpty()) {
          TypeElement typeElement = typeExtractor.visit(method.getReturnType());
          if (typeElement != null) {
            types.add(typeElement);
          }
        }
      }
    }
  }
Ejemplo n.º 2
0
 private void scanForMultipart(
     ExecutableElement executableElement, RestDocumentation.Resource.Method doc) {
   for (VariableElement var : executableElement.getParameters()) {
     TypeMirror varType = var.asType();
     if (varType.toString().startsWith(MultipartHttpServletRequest.class.getName())) {
       doc.setMultipartRequest(true);
       return;
     }
   }
 }
Ejemplo n.º 3
0
  private void buildUrlParameters(
      ExecutableElement executableElement, RestDocumentation.Resource.Method doc) {
    RestDocumentation.Resource.Method.UrlFields subs = doc.getUrlParameters();

    for (VariableElement var : executableElement.getParameters()) {
      RequestParam reqParam = var.getAnnotation(RequestParam.class);
      if (reqParam != null) {
        addUrlField(subs, var, reqParam.value());
      }
    }
  }
Ejemplo n.º 4
0
  private void buildPathVariables(
      ExecutableElement executableElement, RestDocumentation.Resource.Method doc) {
    RestDocumentation.Resource.Method.UrlFields subs = doc.getUrlSubstitutions();

    for (VariableElement var : executableElement.getParameters()) {
      PathVariable pathVar = var.getAnnotation(PathVariable.class);
      if (pathVar != null) {
        addUrlField(subs, var, pathVar.value());
      }
    }
  }
Ejemplo n.º 5
0
  private void buildRequestBodies(
      ExecutableElement executableElement, RestDocumentation.Resource.Method doc) {
    List<VariableElement> requestBodies = new ArrayList<VariableElement>();
    for (VariableElement var : executableElement.getParameters()) {
      if (var.getAnnotation(org.springframework.web.bind.annotation.RequestBody.class) != null) {
        requestBodies.add(var);
      }
    }

    if (requestBodies.size() > 1) {
      throw new IllegalStateException(
          String.format(
              "Method %s in class %s has multiple @RequestBody params",
              executableElement.getSimpleName(), executableElement.getEnclosingElement()));
    }

    if (requestBodies.size() == 1) {
      buildRequestBody(requestBodies.get(0), doc);
    }
  }
Ejemplo n.º 6
0
  @Override
  public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
    System.out.println("processing: " + env.toString());
    List<MethodToLog> methodToLogs = new ArrayList<>();
    List<String> classNames = new ArrayList<>();

    for (Element element : env.getElementsAnnotatedWith(XLog.class)) {
      if (element.getKind() != ElementKind.METHOD
          && element.getKind() != ElementKind.CONSTRUCTOR
          && element.getKind() != ElementKind.CLASS) {
        throw new IllegalStateException(
            String.format(
                "@%s annotation must be on as method, constructor or class.",
                element.getSimpleName()));
      }
      if (element instanceof TypeElement) {
        // class
        String pkgName = ((TypeElement) element).getQualifiedName().toString();
        if (!classNames.contains(pkgName)) {
          classNames.add(pkgName);
        }
      } else if (element instanceof ExecutableElement) {
        // method or constructor
        ExecutableElement e = (ExecutableElement) element;

        int type = XLogUtils.TYPE_METHOD;
        if (e.getKind() == ElementKind.METHOD) {
          type = XLogUtils.TYPE_METHOD;
        } else if (e.getKind() == ElementKind.CONSTRUCTOR) {
          type = XLogUtils.TYPE_CONSTRUCTOR;
        }

        TypeElement te = findEnclosingTypeElement(e);
        System.out.println(te.getQualifiedName().toString() + "." + e.getSimpleName());
        System.out.println(e.getReturnType());
        List<String> parameters = new ArrayList<>();
        List<String> parameterNames = new ArrayList<>();
        for (VariableElement ve : e.getParameters()) {
          System.out.println(ve.asType());
          System.out.println(ve.getSimpleName());
          parameters.add(ve.asType().toString());
          parameterNames.add(ve.getSimpleName().toString());
        }
        MethodToLog methodToLog =
            new MethodToLog(
                type,
                te.getQualifiedName().toString(),
                e.getSimpleName().toString(),
                parameters,
                parameterNames);
        methodToLogs.add(methodToLog);

        if (!classNames.contains(methodToLog.getClassName())) {
          classNames.add(methodToLog.getClassName());
        }
      }
    }

    if (methodToLogs.size() > 0) {
      generateXLogProcessor("_" + MD5(env.toString()), methodToLogs, classNames);
    }

    return true;
  }
Ejemplo n.º 7
0
 private void addUrlField(
     RestDocumentation.Resource.Method.UrlFields subs, VariableElement var, String annoValue) {
   String name =
       (annoValue == null || annoValue.isEmpty()) ? var.getSimpleName().toString() : annoValue;
   subs.addField(name, newJsonType(var.asType()));
 }
Ejemplo n.º 8
0
 private void buildRequestBody(VariableElement var, RestDocumentation.Resource.Method doc) {
   doc.setRequestBody(newJsonType(var.asType()));
 }