public List<String> getMethods() { List<String> httpMethods = new ArrayList<String>(methods.size()); for (AnnotationDesc method : methods) { httpMethods.add(method.annotationType().name()); } return httpMethods; }
private static void processParameterAnnotations(MethodDoc methodDoc, MethodModel methodModel) { Parameter[] params = methodDoc.parameters(); // Start with false here. If we find the userId parameter this will be changed to true. methodModel.setIsAuthenticationRequired(false); Map<String, ParameterModel> paramMap = new HashMap<String, ParameterModel>(); if (params != null) { for (Parameter param : params) { AnnotationDesc[] paramAnnos = param.annotations(); if (paramAnnos != null) { for (AnnotationDesc ad : paramAnnos) { String qualifiedName = ad.annotationType().qualifiedName(); Map<String, Object> annotationMap = mapAnnotation(ad); System.out.println(annotationMap); if (RequestBody.class.getName().equals(qualifiedName)) { // Request body String schema = SchemaUtils.getEffectiveSchema(param.type().qualifiedTypeName()); if (schema != null) { Link link = new Link("${" + param.type().qualifiedTypeName() + "}", param.typeName()); methodModel.setRequestBody(link); } } else if (PathVariable.class.getName().equals(qualifiedName)) { // Path parameter ParameterModel paramModel = new ParameterModel(); paramModel.setName(param.name()); methodModel.addPathVariable(paramModel); paramMap.put(param.name(), paramModel); } else if (RequestParam.class.getName().equals(qualifiedName)) { // if this is the userId parameter then we do now show it, // rather it means this method requires authentication. if (AuthorizationConstants.USER_ID_PARAM.equals( annotationMap.get(REQUEST_PARAMETER_VALUE))) { methodModel.setIsAuthenticationRequired(true); } else { ParameterModel paramModel = new ParameterModel(); paramModel.setName(param.name()); paramModel.setIsOptional(!(isRequired(annotationMap))); methodModel.addParameter(paramModel); paramMap.put(param.name(), paramModel); } } } } } } ParamTag[] paramTags = methodDoc.paramTags(); if (paramTags != null) { for (ParamTag paramTag : paramTags) { ParameterModel paramModel = paramMap.get(paramTag.parameterName()); if (paramModel != null) { paramModel.setDescription(paramTag.parameterComment()); } } } System.out.println(methodModel); // Lookup the parameter descriptions }
protected boolean isBusinessMeaningful(ProgramElementDoc doc) { final AnnotationDesc[] annotations = doc.annotations(); for (AnnotationDesc annotation : annotations) { if (isBusinessMeaningful(annotation.annotationType())) { return true; } } return false; }
public String toString() { StringBuilder strbuf = new StringBuilder(path); strbuf.append(" "); for (AnnotationDesc method : methods) { strbuf.append(method.annotationType().name()); strbuf.append(" "); } return strbuf.toString(); }
public static AnnotationDesc findAnnotation( final AnnotationDesc[] annotations, final String... soughtAnnotations) { for (final AnnotationDesc annotation : annotations) { final AnnotationTypeDoc annotationType = annotation.annotationType(); for (final String soughtAnnotation : soughtAnnotations) { if (annotationType.qualifiedTypeName().equals(soughtAnnotation)) { return annotation; } } } return null; }
public static List<AnnotationDesc> findAnnotations( final AnnotationDesc[] annotations, final Class<?>... soughtAnnotations) { List<AnnotationDesc> ret = new LinkedList<AnnotationDesc>(); for (final AnnotationDesc annotation : annotations) { final AnnotationTypeDoc annotationType = annotation.annotationType(); for (final Class<?> soughtAnnotation : soughtAnnotations) { if (annotationType.qualifiedTypeName().equals(soughtAnnotation.getName())) { ret.add(annotation); } } } return ret; }
private static void processMethodAnnotations(MethodDoc methodDoc, MethodModel methodModel) { AnnotationDesc[] annos = methodDoc.annotations(); if (annos != null) { for (AnnotationDesc ad : annos) { String qualifiedName = ad.annotationType().qualifiedName(); System.out.println(qualifiedName); if (RequestMapping.class.getName().equals(qualifiedName)) { extractRequestMapping(methodModel, ad); } else if (ResponseBody.class.getName().equals(qualifiedName)) { Link link = extractResponseLink(methodDoc); methodModel.setResponseBody(link); } } } }
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; }
public static Object getAnnotationValue(AnnotationDesc annotation, String name) { for (final ElementValuePair elementValuePair : annotation.elementValues()) { if (elementValuePair.element().name().equals(name)) { return elementValuePair.value().value(); } } return null; }
/** * Put all annotation value key pairs into a map for easier lookup. * * @param ad * @param map */ public static void mapAnnotation(AnnotationDesc ad, Map<String, Object> map) { ElementValuePair[] pairs = ad.elementValues(); if (pairs != null) { for (ElementValuePair evp : pairs) { String name = evp.element().qualifiedName(); Object value = evp.value().value(); map.put(name, value); } } }
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); } }
/** * Extract the request mapping data from the annotation. * * @param methodModel * @param ad */ private static void extractRequestMapping(MethodModel methodModel, AnnotationDesc ad) { for (ElementValuePair pair : ad.elementValues()) { String pairName = pair.element().qualifiedName(); if (REQUEST_MAPPING_VALUE.equals(pairName)) { String rawValue = pair.value().toString(); if (rawValue != null) { methodModel.setUrl(rawValue.substring(1, rawValue.length() - 1)); } } else if (REQUEST_MAPPING_METHOD.equals(pairName)) { String value = pair.value().toString(); if (value != null) { int inxed = RequestMethod.class.getName().length(); methodModel.setHttpType(value.substring(inxed + 1)); } } } }
public InfoAnnotation(String clazzName, AnnotationDesc annotation) { items.put("package", new StringValue(clazzName)); for (ElementValuePair elementValuePair : annotation.elementValues()) { AnnotationTypeElementDoc elementDoc = elementValuePair.element(); AnnotationValue aValue = elementValuePair.value(); String elementName = elementDoc.name(); if (elementName.equals(AUTHOR)) { items.put(AUTHOR, new AnnotationSingleValue(aValue)); } else if (elementName.equals(DISPLAY)) { items.put("display", new AnnotationSingleValue(aValue)); } else if (elementName.equals(SUMMARY)) { items.put(SUMMARY, new AnnotationSingleValue(aValue)); } else if (elementName.equals(SEND)) { items.put(SEND, new AnnotationSingleValue(aValue)); } else if (elementName.equals(RECEIVE)) { items.put(RECEIVE, new AnnotationArrayValue(aValue)); } } }
/** * 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; }