private static String annotationToString(AnnotationDesc annDesc, String annName, String def) { for (AnnotationDesc.ElementValuePair e : annDesc.elementValues()) { if (StringUtils.equals(e.element().name(), annName)) return annotationToString(e.value().value()); } return def; }
private static void scanClassDoc(List<ApiDoc> apiDocs, ClassDoc classDoc) { AnnotationDesc[] annDescs = classDoc.annotations(); for (AnnotationDesc annDesc : annDescs) { if (IgnoreDocument.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) return; } MethodDoc[] methodDocs = classDoc.methods(); if (ArrayUtils.isNotEmpty(methodDocs)) { for (MethodDoc methodDoc : methodDocs) { ApiDoc apiDoc = scanMethodDoc(classDoc, methodDoc); if (apiDoc != null) apiDocs.add(apiDoc); } } ClassDoc[] innerClassDocs = classDoc.innerClasses(); if (ArrayUtils.isNotEmpty(innerClassDocs)) { for (ClassDoc innerClassDoc : innerClassDocs) scanClassDoc(apiDocs, innerClassDoc); } }
/** * 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; }