/**
  * Get ITestNGMethod author(s) string, or class author(s) if no method author is present. Default
  * return value is "unknown".
  *
  * @param className
  * @param method
  * @return
  * @author hzjingcheng
  */
 private String getAuthors(String className, ITestNGMethod method) {
   JavaClass cls = builder.getClassByName(className);
   DocletTag[] authors = cls.getTagsByName("author");
   // get class authors as default author name
   String allAuthors = "";
   if (authors.length == 0) {
     allAuthors = "unknown";
   } else {
     for (DocletTag author : authors) {
       allAuthors += author.getValue() + " ";
     }
   }
   // get method author name
   JavaMethod[] mtds = cls.getMethods();
   for (JavaMethod mtd : mtds) {
     if (mtd.getName().equals(method.getMethodName())) {
       authors = mtd.getTagsByName("author");
       if (authors.length != 0) {
         allAuthors = "";
         for (DocletTag author : authors) {
           allAuthors += author.getValue() + " ";
         }
       }
       break;
     }
   }
   return allAuthors.trim();
 }
  private void _addMethodElement(Element rootElement, JavaMethod javaMethod) {
    Element methodElement = rootElement.addElement("method");

    DocUtil.add(methodElement, "name", javaMethod.getName());

    Element commentElement = methodElement.addElement("comment");

    commentElement.addCDATA(_getCDATA(javaMethod));

    _addDocletElements(methodElement, javaMethod, "deprecated");
    _addParamElements(methodElement, javaMethod);
    _addReturnElement(methodElement, javaMethod);
    _addDocletElements(methodElement, javaMethod, "see");
    _addDocletElements(methodElement, javaMethod, "since");
    _addThrowsElements(methodElement, javaMethod);
    _addDocletElements(methodElement, javaMethod, "version");
  }
  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();
  }
  private void _addMethodElement(Element rootElement, JavaMethod javaMethod) throws Exception {

    Element methodElement = rootElement.addElement("method");

    DocUtil.add(methodElement, "name", javaMethod.getName());

    String comment = _getCDATA(javaMethod);

    if (Validator.isNotNull(comment)) {
      Element commentElement = methodElement.addElement("comment");

      commentElement.addCDATA(_getCDATA(javaMethod));
    }

    _addDocletElements(methodElement, javaMethod, "version");
    _addParamElements(methodElement, javaMethod);
    _addReturnElement(methodElement, javaMethod);
    _addThrowsElements(methodElement, javaMethod);
    _addDocletElements(methodElement, javaMethod, "see");
    _addDocletElements(methodElement, javaMethod, "since");
    _addDocletElements(methodElement, javaMethod, "deprecated");
  }
  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);
  }
  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;
  }