Пример #1
0
  @SuppressWarnings("deprecation")
  public static Type resolveType(String typeName, ClassDoc klass, JAXDoclet<?> doclet) {
    log("resolving " + typeName + " in " + klass.qualifiedTypeName());
    // first look in inner classes
    for (ClassDoc innerClass : klass.innerClasses(false)) {
      if (innerClass.simpleTypeName().equals(typeName)) return innerClass;
    }
    // then the class itself
    if (klass.typeName().equals(typeName)) return klass;
    try {
      // then go through the named imports
      for (ClassDoc importedClass : klass.importedClasses()) {
        if (importedClass.typeName().equals(typeName)) return importedClass;
      }
      // then the package imports
      for (PackageDoc importedPackage : klass.importedPackages()) {
        for (ClassDoc importedClass : importedPackage.allClasses(false)) {
          if (importedClass.typeName().equals(typeName)) return importedClass;
        }
      }
    } catch (NullPointerException e) {

    }
    // now try FQDN
    Type type = doclet.forName(typeName);
    if (type != null) return type;
    log("resolving failed for " + typeName + " in " + klass.qualifiedTypeName());
    return null;
  }
Пример #2
0
  public static boolean start(RootDoc root) {
    try {
      init(root);
      ClassDoc[] classes = root.classes();
      ArrayList<String> generatesTestClasses = new ArrayList<String>();
      for (int i = 0; i < classes.length; i++) {
        ClassDoc classDoc = classes[i];
        if (classDoc.isAbstract()
            || classDoc.isInterface()
            || classDoc.isEnum()
            || classDoc.isPrivate()
            || classDoc.isAnnotationType()) {
        } else if (!hasDefaultConstructor(classDoc)) {
          System.out.println(classDoc.qualifiedTypeName() + " does not have default Constructor.");
        } else {
          ClassBean testClassBean = ClassBeanCreator.create(classDoc);
          if (testClassBean.hasMethods()) {
            JavaCodeWriter writer = getWriter(testClassBean);
            if (writer != null) {
              JavaCodeGenerator.generate(testClassBean, writer);
              generatesTestClasses.add(classDoc.qualifiedTypeName());
              writer.close();
            }
          } else {
            System.out.println(
                classDoc.qualifiedTypeName() + " does not have any methods to cover.");
          }
        }
      }
      System.out.println("---------REPORT-----------------------------------------------");
      for (Iterator iterator = generatesTestClasses.iterator(); iterator.hasNext(); ) {
        String clsName = (String) iterator.next();
        System.out.println("\t" + clsName);
      }
      System.out.println("-------------------------------------------------------------");

      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
Пример #3
0
 private void handleJPAClass(final ClassDoc klass) {
   ClassDoc superDoc = klass.superclass();
   if (!registry.isJPAClass(klass.qualifiedTypeName())
       && !klass.isPrimitive()
       && !klass.qualifiedTypeName().startsWith("java.")
       && !klass.isEnum()) {
     String fqName = klass.qualifiedTypeName();
     JPAClass jpaClass = new JPAClass(klass, registry, this);
     jpaClasses.add(jpaClass);
     registry.addJPAClass(jpaClass);
     // load all used types
     List<JPAMember> members = jpaClass.getMembers();
     for (JPAMember member : members) {
       Type type = member.getJavaType();
       ClassDoc doc = type.asClassDoc();
       if (doc != null && Utils.findAnnotatedClass(doc, jpaAnnotations) != null) {
         handleJPAClass(doc);
       }
     }
   }
 }
Пример #4
0
  public static Type findSuperTypeFromClass(final ClassDoc klass, String typeName) {
    if (klass.qualifiedTypeName().equals(typeName)) return klass;

    // find it in the interfaces
    final Type foundType = findSuperTypeFromInterface(klass, typeName);
    if (foundType != null) {
      return foundType;
    }

    final Type superclass = klass.superclassType();
    if (superclass != null && superclass.asClassDoc() != null) {
      return findSuperTypeFromClass(superclass.asClassDoc(), typeName);
    }
    return null;
  }
Пример #5
0
 public static Type findSuperTypeFromInterface(final ClassDoc klass, String typeName) {
   // find it in the interfaces
   final Type[] interfaceTypes = klass.interfaceTypes();
   for (final Type interfaceType : interfaceTypes) {
     final ClassDoc interfaceClassDoc = interfaceType.asClassDoc();
     if (interfaceClassDoc != null) {
       if (interfaceClassDoc.qualifiedTypeName().equals(typeName)) return interfaceClassDoc;
       final Type foundType = findSuperTypeFromInterface(interfaceClassDoc, typeName);
       if (foundType != null) {
         return foundType;
       }
     }
   }
   return null;
 }
Пример #6
0
  /**
   * Parses annotation instances from the javadoc annotation instance type
   *
   * @param annotationDocs Annotations decorated on some type
   * @return Serializable representation of annotations
   */
  protected static AnnotationInstance[] ParseAnnotationInstances(
      AnnotationDesc[] annotationDocs, String origin) {
    AnnotationInstance[] annotations = null;

    if (annotationDocs != null && annotationDocs.length > 0) {
      ArrayList<AnnotationInstance> list = new ArrayList<AnnotationInstance>();

      for (AnnotationDesc annot : annotationDocs) {
        AnnotationInstance instance = new AnnotationInstance();

        AnnotationTypeDoc annotTypeInfo = null;
        try {
          annotTypeInfo = annot.annotationType();
          instance.name = annot.annotationType().name();
          instance.qualifiedName = annot.annotationType().qualifiedTypeName();

        } catch (ClassCastException castException) {
          log.error("Unable to obtain type data about an annotation found on: " + origin);
          log.error("Add to the -cp parameter the class/jar that defines this annotation.");
          instance.name = null;
          instance.qualifiedName = null;
        }

        AnnotationDesc.ElementValuePair[] arguments = annot.elementValues();
        if (arguments != null && arguments.length > 0) {
          ArrayList<AnnotationArgument> argumentList = new ArrayList<AnnotationArgument>();

          for (AnnotationDesc.ElementValuePair pair : arguments) {
            AnnotationArgument annotationArgument = new AnnotationArgument();
            annotationArgument.name = pair.element().name();

            Type annotationArgumentType = pair.element().returnType();
            annotationArgument.type = annotationArgumentType.qualifiedTypeName();
            annotationArgument.isPrimitive = annotationArgumentType.isPrimitive();
            annotationArgument.isArray = annotationArgumentType.dimension().length() > 0;

            Object objValue = pair.value().value();
            if (objValue instanceof AnnotationValue[]) {
              AnnotationValue[] realValues = (AnnotationValue[]) objValue;
              String[] values = new String[realValues.length];

              for (int i = 0; i < realValues.length; i++) {
                values[i] = realValues[i].value().toString();
              }
              annotationArgument.value = values;
            } else if (objValue instanceof Number) {
              Number number = (Number) objValue;
              annotationArgument.value = new String[] {number.toString()};
            } else if (objValue instanceof Character) {
              Character character = (Character) objValue;
              annotationArgument.value = new String[] {character.toString()};
            } else if (objValue instanceof Boolean) {
              Boolean booleanValue = (Boolean) objValue;
              annotationArgument.value = new String[] {booleanValue.toString()};
            } else if (objValue instanceof String) {
              String stringValue = (String) objValue;
              annotationArgument.value = new String[] {stringValue};
            } else if (objValue instanceof FieldDoc) {
              FieldDoc field = (FieldDoc) objValue;
              annotationArgument.value = new String[] {field.name()};
            } else if (objValue instanceof ClassDoc) {
              ClassDoc classDoc = (ClassDoc) objValue;
              annotationArgument.value = new String[] {classDoc.qualifiedTypeName()};
            }
            argumentList.add(annotationArgument);
          }

          instance.arguments = argumentList.toArray(new AnnotationArgument[] {});
        }

        list.add(instance);
      }

      annotations = list.toArray(new AnnotationInstance[] {});
    }

    return annotations;
  }
  private static ApiDoc scanMethodDoc(ClassDoc classDoc, MethodDoc methodDoc) {
    String routePrefix = "";

    // RoutePrefix for class
    for (AnnotationDesc annDesc : classDoc.annotations()) {
      if (RoutePrefix.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        routePrefix = annotationToString(annDesc, "value", "");
        break;
      }
    }

    AnnotationDesc[] annDescs = methodDoc.annotations();
    if (ArrayUtils.isEmpty(annDescs)) return null;

    for (AnnotationDesc annDesc : annDescs) {
      if (IgnoreDocument.class.getName().equals(annDesc.annotationType().qualifiedTypeName()))
        return null;
    }

    // RoutePrefix for method
    for (AnnotationDesc annDesc : annDescs) {
      if (RoutePrefix.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        routePrefix = annotationToString(annDesc, "value", "");
        break;
      }
    }

    Class httpExamplePackageClass = null;
    String[] routes = null;
    String[] httpMethods = {"GET", "POST"};
    boolean deprecated = false;
    for (AnnotationDesc annDesc : annDescs) {
      if (Route.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        for (AnnotationDesc.ElementValuePair annValue : annDesc.elementValues()) {
          String name = annValue.element().name();
          if ("url".equals(name)) {
            routes = annotationToStringArray(annValue.value().value());
          } else if ("method".equals(name)) {
            httpMethods = annotationToStringArray(annValue.value().value());
          }
        }
      }

      if (Deprecated.class.getName().equals(annDesc.annotationType().qualifiedTypeName()))
        deprecated = true;

      if (HttpExamplePackage.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        for (AnnotationDesc.ElementValuePair annValue : annDesc.elementValues()) {
          String name = annValue.element().name();
          if ("value".equals(name)) {
            ClassDoc httpExamplePackageClassAnn = (ClassDoc) annValue.value().value();
            httpExamplePackageClass =
                ClassHelper.forName(httpExamplePackageClassAnn.qualifiedTypeName());
          }
        }
      }
    }
    if (routes == null) return null;

    if (httpExamplePackageClass == null) {
      annDescs = classDoc.annotations();
      for (AnnotationDesc annDesc : annDescs) {
        if (HttpExamplePackage.class
            .getName()
            .equals(annDesc.annotationType().qualifiedTypeName())) {
          for (AnnotationDesc.ElementValuePair annValue : annDesc.elementValues()) {
            String name = annValue.element().name();
            if ("value".equals(name)) {
              ClassDoc httpExamplePackageClassAnn = (ClassDoc) annValue.value().value();
              httpExamplePackageClass =
                  ClassHelper.forName(httpExamplePackageClassAnn.qualifiedTypeName());
            }
          }
        }
      }
    }

    ApiDoc apiDoc = new ApiDoc();
    apiDoc.group = getTag(methodDoc, GROUP_TAG, "");
    apiDoc.routes = addRoutePrefix(routes, routePrefix);
    apiDoc.httpMethods = httpMethods;
    apiDoc.description = expandText(methodDoc.commentText());
    apiDoc.login = parseBoolean(getTag(methodDoc, LOGIN_TAG, "y"));
    apiDoc.deprecated = deprecated;
    apiDoc.httpReturn = getTag(methodDoc, HTTP_RETURN, "");

    apiDoc.remark = expandText(getTag(methodDoc, REMARK_TAG, ""));
    apiDoc.className = classDoc.qualifiedTypeName();
    apiDoc.httpExamplePackageClass = httpExamplePackageClass;
    String[] httpExamples = expandTexts(getTags(methodDoc, HTTP_EXAMPLE_TAG));
    for (int i = 0; i < httpExamples.length; i++) {
      String httpExample = httpExamples[i];
      if (httpExample.startsWith("@")) {
        if (apiDoc.httpExamplePackageClass != null) {
          httpExamples[i] =
              VfsHelper.loadTextInClasspath(
                  apiDoc.httpExamplePackageClass, StringUtils.removeStart(httpExample, "@"));
        } else {
          httpExamples[i] = "";
        }
      }
    }

    apiDoc.httpExamples = httpExamples;

    ArrayList<ParamDoc> l = new ArrayList<ParamDoc>();
    for (String httpParamTag : getTags(methodDoc, HTTP_PARAM_TAG)) {
      ParamDoc httpParamDoc = parseParamDoc(httpParamTag);
      if (httpParamDoc != null) l.add(httpParamDoc);
    }
    apiDoc.httpParams = l.toArray(new ParamDoc[l.size()]);
    return apiDoc;
  }