コード例 #1
0
  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);
          }
        }
      }
    }
  }
コード例 #2
0
  private Set<TypeElement> processDelegateMethods() {
    Set<Element> delegateMethods = (Set) getElements(QueryDelegate.class);
    Set<TypeElement> typeElements = new HashSet<TypeElement>();

    for (Element delegateMethod : delegateMethods) {
      ExecutableElement method = (ExecutableElement) delegateMethod;
      Element element = delegateMethod.getEnclosingElement();
      String name = method.getSimpleName().toString();
      Type delegateType = typeFactory.getType(element.asType(), true);
      Type returnType = typeFactory.getType(method.getReturnType(), true);
      List<Parameter> parameters = elementHandler.transformParams(method.getParameters());
      // remove first element
      parameters = parameters.subList(1, parameters.size());

      EntityType entityType = null;
      for (AnnotationMirror annotation : delegateMethod.getAnnotationMirrors()) {
        if (TypeUtils.isAnnotationMirrorOfType(annotation, QueryDelegate.class)) {
          TypeMirror type = TypeUtils.getAnnotationValueAsTypeMirror(annotation, "value");
          if (type != null) {
            entityType = typeFactory.getEntityType(type, true);
          }
        }
      }

      if (entityType != null) {
        registerTypeElement(entityType.getFullName(), (TypeElement) element);
        entityType.addDelegate(
            new Delegate(entityType, delegateType, name, parameters, returnType));
        TypeElement typeElement =
            processingEnv.getElementUtils().getTypeElement(entityType.getFullName());
        boolean isAnnotated = false;
        for (Class<? extends Annotation> ann : conf.getEntityAnnotations()) {
          if (typeElement.getAnnotation(ann) != null) {
            isAnnotated = true;
          }
        }
        if (isAnnotated) {
          // handle also properties of entity type
          typeElements.add(
              processingEnv.getElementUtils().getTypeElement(entityType.getFullName()));
        } else {
          // skip handling properties
          context.extensionTypes.put(entityType.getFullName(), entityType);
          context.allTypes.put(entityType.getFullName(), entityType);
        }
      }
    }

    return typeElements;
  }
コード例 #3
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;
     }
   }
 }
コード例 #4
0
  private void processRequestMappingMethod(ExecutableElement executableElement) {
    TypeElement cls = (TypeElement) executableElement.getEnclosingElement();
    String path = getClassLevelUrlPath(cls);

    RequestMapping anno = executableElement.getAnnotation(RequestMapping.class);
    path = addMethodPathComponent(executableElement, cls, path, anno);
    RequestMethod meth = getRequestMethod(executableElement, cls, anno);

    RestDocumentation.Resource.Method doc =
        _docs.getResourceDocumentation(path).newMethodDocumentation(meth);
    String docComment = processingEnv.getElementUtils().getDocComment(executableElement);
    if (StringUtils.isNotBlank(docComment)) {
      MethodStructure methodStructure = generateMethodStructure(docComment);
      doc.setCommentText(generateJavaDocHTML(methodStructure));
      doc.setCommentSummary(methodStructure.getDescription());
    }
    buildParameterData(executableElement, doc);
    buildResponseFormat(executableElement.getReturnType(), doc);
  }
コード例 #5
0
 private RequestMethod getRequestMethod(
     ExecutableElement executableElement, TypeElement cls, RequestMapping anno) {
   if (anno.method().length != 1) {
     throw new IllegalStateException(
         String.format(
             "The RequestMapping annotation for %s.%s is not parseable. Exactly one request method (GET/POST/etc) is required.",
             cls.getQualifiedName(), executableElement.getSimpleName()));
   } else {
     return anno.method()[0];
   }
 }
コード例 #6
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());
      }
    }
  }
コード例 #7
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());
      }
    }
  }
コード例 #8
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);
    }
  }
コード例 #9
0
 private String addMethodPathComponent(
     ExecutableElement executableElement, TypeElement cls, String path, RequestMapping anno) {
   if (anno == null) {
     throw new IllegalArgumentException(
         String.format(
             "Method %s should have Request mapping annotation",
             executableElement.getSimpleName()));
   }
   if (anno.value() != null && anno.value().length > 0) {
     return Utils.joinPaths(path, anno.value()[0]);
   }
   return path;
 }
コード例 #10
0
    private void addFieldFromBeanMethod(JsonObject o, ExecutableElement executableElement) {
      if (!isJsonBeanGetter(executableElement)) {
        return;
      }

      TypeMirror type = executableElement.getReturnType();
      String methodName = executableElement.getSimpleName().toString();
      int trimLength = methodName.startsWith("is") ? 2 : 3;
      String beanName = methodName.substring(trimLength + 1, methodName.length());
      beanName = methodName.substring(trimLength, trimLength + 1).toLowerCase() + beanName;

      // loop over the element's generic types, and build a concrete list from the owning context
      List<DeclaredType> concreteTypes = new ArrayList();

      // replace variables with the current concrete manifestation
      if (type instanceof TypeVariable) {
        type = getDeclaredTypeForTypeVariable((TypeVariable) type);
        if (type == null) {
          return; // couldn't find a replacement -- must be a generics-capable type with no generics
                  // info
        }
      }

      String docComment = processingEnv.getElementUtils().getDocComment(executableElement);
      if (type instanceof DeclaredType) {
        TypeElement element = (TypeElement) ((DeclaredType) type).asElement();
        for (TypeParameterElement generic : element.getTypeParameters()) {
          concreteTypes.add(_typeArguments.get(generic.getSimpleName()));
        }
        Collection<DeclaredType> types = new HashSet<DeclaredType>(_typeRecursionGuard);
        types.add(_type);
        o.addField(beanName, newJsonType((DeclaredType) type, concreteTypes, types))
            .setCommentText(docComment);
      } else {
        o.addField(beanName, newJsonType(type)).setCommentText(docComment);
      }
    }
コード例 #11
0
    private boolean isJsonBeanGetter(ExecutableElement executableElement) {
      if (executableElement.getKind() != ElementKind.METHOD) {
        return false;
      }

      if (executableElement.getReturnType().getKind() == TypeKind.NULL) {
        return false;
      }

      if (!(executableElement.getSimpleName().toString().startsWith("get")
          || executableElement.getSimpleName().toString().startsWith("is"))) {
        return false;
      }

      if (executableElement.getParameters().size() > 0) {
        return false;
      }

      return executableElement.getAnnotation(JsonIgnore.class) == null;
    }
コード例 #12
0
ファイル: XLogProcessor.java プロジェクト: hejunbinlan/XLog
  @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;
  }