Пример #1
0
  private void _addParamElements(Element methodElement, JavaMethod javaMethod) {

    JavaParameter[] javaParameters = javaMethod.getParameters();

    DocletTag[] paramDocletTags = javaMethod.getTagsByName("param");

    for (JavaParameter javaParameter : javaParameters) {
      _addParamElement(methodElement, javaParameter, paramDocletTags);
    }
  }
Пример #2
0
  // Processes parameters of javaMethod and enters the proper key-values into the methodNode
  private void processParameters(JavaMethod javaMethod, ObjectNode methodNode) {
    ArrayNode parameters = mapper.createArrayNode();
    methodNode.set("parameters", parameters);
    boolean required = true;

    for (JavaParameter javaParameter : javaMethod.getParameters()) {
      ObjectNode individualParameterNode = mapper.createObjectNode();
      Optional<JavaAnnotation> optional =
          javaParameter
              .getAnnotations()
              .stream()
              .filter(
                  annotation ->
                      annotation.getType().getName().equals(PATH_PARAM)
                          || annotation.getType().getName().equals(QUERY_PARAM))
              .findAny();
      JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;

      String annotationName = javaParameter.getName();

      if (pathType != null) { // the parameter is a path or query parameter
        individualParameterNode.put(
            "name", pathType.getNamedParameter("value").toString().replace("\"", ""));
        if (pathType.getType().getName().equals(PATH_PARAM)) {
          individualParameterNode.put("in", "path");
        } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
          individualParameterNode.put("in", "query");
        }
        individualParameterNode.put("type", getType(javaParameter.getType()));
      } else { // the parameter is a body parameter
        individualParameterNode.put("name", annotationName);
        individualParameterNode.put("in", "body");

        // TODO add actual hardcoded schemas and a type
        // body parameters must have a schema associated with them
        ArrayNode schema = mapper.createArrayNode();
        individualParameterNode.set("schema", schema);
      }
      for (DocletTag p : javaMethod.getTagsByName("param")) {
        if (p.getValue().contains(annotationName)) {
          try {
            String description = p.getValue().split(" ", 2)[1].trim();
            if (description.contains("optional")) {
              required = false;
            }
            individualParameterNode.put("description", description);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      individualParameterNode.put("required", required);
      parameters.add(individualParameterNode);
    }
  }
Пример #3
0
  private void buildQueryAndPathDocuments(
      Method method, JavaMethod javaMethod, MethodDocument document) {
    if (method.getParameterTypes().length != javaMethod.getParameters().length) {
      LOG.error(
          "Unable to generate argument documentation: number of method arguments is not "
              + "the same as number of @param found in javadoc.");
      return;
    }

    List<ParamDocument> queryParams = new ArrayList<ParamDocument>();
    List<ParamDocument> pathParams = new ArrayList<ParamDocument>();

    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < annotations.length; i++) {
      JavaParameter javaParameter = javaMethod.getParameters()[i];
      DocletTag javaParameterTag = javaMethod.getTagsByName("param")[i];

      // Check annotations at parameter i
      for (Annotation a : annotations[i]) {
        if (a.annotationType() == QueryParam.class) {
          queryParams.add(
              new ParamDocument(
                  javaParameter.getName(),
                  extractParameterValue(javaParameterTag, javaParameter.getName())));
        } else if (a.annotationType() == PathParam.class) {
          pathParams.add(
              new ParamDocument(
                  javaParameter.getName(),
                  extractParameterValue(javaParameterTag, javaParameter.getName())));
        }
      }
    }

    LOG.info("Found {} query params for method {}", queryParams.size(), method.getName());
    LOG.info("Found {}  path params for method {}", pathParams.size(), method.getName());
    document.setQueryParams(ImmutableList.copyOf(queryParams));
    document.setPathParams(ImmutableList.copyOf(pathParams));
  }
Пример #4
0
  private String _getMethodKey(JavaMethod javaMethod) {
    StringBundler sb = new StringBundler();

    sb.append(javaMethod.getName());
    sb.append(StringPool.OPEN_PARENTHESIS);

    JavaParameter[] javaParameters = javaMethod.getParameters();

    for (JavaParameter javaParameter : javaParameters) {
      sb.append(javaParameter.getName());
      sb.append("|");
      sb.append(_getTypeValue(javaParameter));
      sb.append(",");
    }

    sb.append(StringPool.CLOSE_PARENTHESIS);

    return sb.toString();
  }
  private String _getMethodKey(JavaMethod javaMethod) {
    StringBuilder sb = new StringBuilder();

    sb.append(javaMethod.getName());
    sb.append("(");

    JavaParameter[] javaParameters = javaMethod.getParameters();

    for (JavaParameter javaParameter : javaParameters) {
      sb.append(javaParameter.getName());
      sb.append("|");
      sb.append(javaParameter.getType().getValue());
      sb.append(",");
    }

    sb.append(")");

    return sb.toString();
  }
Пример #6
0
  private void _copyInterface(String parentDir, String srcFile) throws IOException {

    JavaClass javaClass = _getJavaClass(parentDir, srcFile);

    JavaMethod[] methods = javaClass.getMethods();

    Arrays.sort(methods, new JavaMethodComparator());

    StringMaker sm = new StringMaker();

    // Package

    sm.append("package " + javaClass.getPackage() + ";");

    // Imports

    sm.append("[$IMPORTS$]");

    // Class declaration

    sm.append(
        "public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {");

    String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I);

    // Methods

    Set imports = new TreeSet();

    for (int i = 0; i < methods.length; i++) {
      JavaMethod javaMethod = methods[i];

      String methodName = javaMethod.getName();

      if (javaMethod.isPublic()) {
        String returnValueName = javaMethod.getReturns().getValue();

        imports.add(returnValueName);

        sm.append(
            "public "
                + javaMethod.getReturns().getJavaClass().getName()
                + _getDimensions(javaMethod.getReturns())
                + " "
                + methodName
                + "(");

        JavaParameter[] parameters = javaMethod.getParameters();

        for (int j = 0; j < parameters.length; j++) {
          JavaParameter javaParameter = parameters[j];

          sm.append(
              javaParameter.getType().getJavaClass().getName()
                  + _getDimensions(javaParameter.getType())
                  + " "
                  + javaParameter.getName());

          imports.add(javaParameter.getType().getValue());

          if ((j + 1) != parameters.length) {
            sm.append(", ");
          }
        }

        sm.append(")");

        Type[] thrownExceptions = javaMethod.getExceptions();

        Set newExceptions = new LinkedHashSet();

        for (int j = 0; j < thrownExceptions.length; j++) {
          Type thrownException = thrownExceptions[j];

          newExceptions.add(thrownException.getJavaClass().getName());

          imports.add(thrownException.getValue());
        }

        if (newExceptions.size() > 0) {
          sm.append(" throws ");

          Iterator itr = newExceptions.iterator();

          while (itr.hasNext()) {
            sm.append(itr.next());

            if (itr.hasNext()) {
              sm.append(", ");
            }
          }
        }

        sm.append("{");

        if (!returnValueName.equals("void")) {
          sm.append("return ");
        }

        sm.append(varName + "." + methodName + "(");

        for (int j = 0; j < parameters.length; j++) {
          JavaParameter javaParameter = parameters[j];

          sm.append(javaParameter.getName());

          if ((j + 1) != parameters.length) {
            sm.append(", ");
          }
        }

        sm.append(");");
        sm.append("}");
      }
    }

    // Fields

    sm.append("private " + javaClass.getName() + " " + varName + ";");

    // Class close brace

    sm.append("}");

    // Imports

    String content = sm.toString();

    sm = new StringMaker();

    Iterator itr = imports.iterator();

    while (itr.hasNext()) {
      String importClass = (String) itr.next();

      if (!importClass.equals("boolean")
          && !importClass.equals("double")
          && !importClass.equals("int")
          && !importClass.equals("long")
          && !importClass.equals("short")
          && !importClass.equals("void")) {
        sm.append("import " + importClass + ";");
      }
    }

    content = StringUtil.replace(content, "[$IMPORTS$]", sm.toString());

    // Write file

    File file =
        new File(
            parentDir
                + "/"
                + StringUtil.replace(javaClass.getPackage(), ".", "/")
                + "/Copy"
                + javaClass.getName()
                + ".java");

    ServiceBuilder.writeFile(file, content);
  }
Пример #7
0
  private boolean _isOverrideMethod(
      JavaClass javaClass, JavaMethod javaMethod, Collection<Tuple> ancestorJavaClassTuples) {

    if (javaMethod.isConstructor() || javaMethod.isPrivate() || javaMethod.isStatic()) {

      return false;
    }

    String methodName = javaMethod.getName();

    JavaParameter[] javaParameters = javaMethod.getParameters();

    Type[] types = new Type[javaParameters.length];

    for (int i = 0; i < javaParameters.length; i++) {
      types[i] = javaParameters[i].getType();
    }

    // Check for matching method in each ancestor

    for (Tuple ancestorJavaClassTuple : ancestorJavaClassTuples) {
      JavaClass ancestorJavaClass = (JavaClass) ancestorJavaClassTuple.getObject(0);

      JavaMethod ancestorJavaMethod = null;

      if (ancestorJavaClassTuple.getSize() > 1) {

        // LPS-35613

        Type[] ancestorActualTypeArguments = (Type[]) ancestorJavaClassTuple.getObject(1);

        Type[] genericTypes = new Type[types.length];

        for (int i = 0; i < types.length; i++) {
          Type type = types[i];

          String typeValue = type.getValue();

          boolean useGenericType = false;

          for (int j = 0; j < ancestorActualTypeArguments.length; j++) {

            if (typeValue.equals(ancestorActualTypeArguments[j].getValue())) {

              useGenericType = true;

              break;
            }
          }

          if (useGenericType) {
            genericTypes[i] = new Type("java.lang.Object");
          } else {
            genericTypes[i] = type;
          }
        }

        ancestorJavaMethod = ancestorJavaClass.getMethodBySignature(methodName, genericTypes);
      } else {
        ancestorJavaMethod = ancestorJavaClass.getMethodBySignature(methodName, types);
      }

      if (ancestorJavaMethod == null) {
        continue;
      }

      boolean samePackage = false;

      JavaPackage ancestorJavaPackage = ancestorJavaClass.getPackage();

      if (ancestorJavaPackage != null) {
        samePackage = ancestorJavaPackage.equals(javaClass.getPackage());
      }

      // Check if the method is in scope

      if (samePackage) {
        return !ancestorJavaMethod.isPrivate();
      } else {
        if (ancestorJavaMethod.isProtected() || ancestorJavaMethod.isPublic()) {

          return true;
        } else {
          return false;
        }
      }
    }

    return false;
  }
Пример #8
0
  // Processes parameters of javaMethod and enters the proper key-values into the methodNode
  private void processParameters(
      JavaMethod javaMethod, ObjectNode methodNode, String method, DocletTag tag) {
    ArrayNode parameters = mapper.createArrayNode();
    methodNode.set("parameters", parameters);
    boolean required = true;

    for (JavaParameter javaParameter : javaMethod.getParameters()) {
      ObjectNode individualParameterNode = mapper.createObjectNode();
      Optional<JavaAnnotation> optional =
          javaParameter
              .getAnnotations()
              .stream()
              .filter(
                  annotation ->
                      annotation.getType().getName().equals(PATH_PARAM)
                          || annotation.getType().getName().equals(QUERY_PARAM))
              .findAny();
      JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;

      String annotationName = javaParameter.getName();

      if (pathType != null) { // the parameter is a path or query parameter
        individualParameterNode.put(
            "name", pathType.getNamedParameter("value").toString().replace("\"", ""));
        if (pathType.getType().getName().equals(PATH_PARAM)) {
          individualParameterNode.put("in", "path");
        } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
          individualParameterNode.put("in", "query");
        }
        individualParameterNode.put("type", getType(javaParameter.getType()));
      } else { // the parameter is a body parameter
        individualParameterNode.put("name", annotationName);
        individualParameterNode.put("in", "body");

        // Adds the reference to the Json model for the input
        // that goes in the post or put operation
        if (tag != null
            && (method.toLowerCase().equals("post") || method.toLowerCase().equals("put"))) {
          ObjectNode schema = mapper.createObjectNode();
          tag.getParameters()
              .stream()
              .forEach(
                  param -> {
                    schema.put("$ref", "#/definitions/" + param);
                  });
          individualParameterNode.set("schema", schema);
        }
      }
      for (DocletTag p : javaMethod.getTagsByName("param")) {
        if (p.getValue().contains(annotationName)) {
          try {
            String description = p.getValue().split(" ", 2)[1].trim();
            if (description.contains("optional")) {
              required = false;
            }
            individualParameterNode.put("description", description);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      individualParameterNode.put("required", required);
      parameters.add(individualParameterNode);
    }
  }