private PSItemDoc(ClassDoc classDoc) { this.classDoc = classDoc; this.name = classDoc.name(); this.description = classDoc.commentText(); this.itemFieldDocs = new ArrayList<>(); this.providerDocs = new ArrayList<>(); List<FieldDoc> allFields = new ArrayList<>(); this.getAllFieldDocs(classDoc, allFields); for (FieldDoc fieldDoc : allFields) { PSItemFieldDoc itemFieldDoc = PSItemFieldDoc.build(this, fieldDoc); if (itemFieldDoc != null) this.itemFieldDocs.add(itemFieldDoc); } for (MethodDoc methodDoc : classDoc.methods()) { PSOperatorDoc providerDoc = PSOperatorDoc.build(classDoc, methodDoc); if (providerDoc != null) this.providerDocs.add(providerDoc); } }
@Override protected APIParameter getInputParams(ClassDoc classDoc) { if(classDoc.tags(WRMqConsumerTaglet.NAME).length > 0) { APIParameter apiParameter = new APIParameter(); apiParameter.setParameterOccurs(ParameterOccurs.REQUIRED); apiParameter.setType(classDoc.qualifiedTypeName()); apiParameter.setName(classDoc.name()); HashSet<String> processingClasses = new HashSet<String>(); apiParameter.setFields(this.getFields(classDoc, ParameterType.Request, processingClasses)); apiParameter.setDescription(classDoc.commentText()); return apiParameter; } else { return null; } }
@Override protected void processOpenAPIClasses(ClassDoc[] classes, Configuration configuration) { for (int i = 0; i < classes.length; i++) { if (configuration.nodeprecated && (Util.isDeprecated(classes[i]) || Util .isDeprecated(classes[i].containingPackage()))) { continue; } if (this.isServiceInterface(classes[i])) { this.processServiceClass(classes[i], configuration); MethodDoc[] methods = classes[i].methods(); for (int l = 0; l < methods.length; l++) { if (configuration.nodeprecated && Util.isDeprecated(methods[l])) { continue; } this.processOpenAPIMethod(methods[l], configuration); } } } }
protected boolean isInStopClasses(ClassDoc classDoc) { String property = ApplicationContextConfig.getStopClasses(); if (property != null) { String[] stopClasses = property.split(","); String[] cdParts = classDoc.qualifiedTypeName().split("\\."); for (String stopClass : stopClasses) { String[] scParts = stopClass.trim().split("\\."); if (scParts.length <= cdParts.length) { boolean hasDiffPart = false; for (int i = 0; i < scParts.length; i++) { if (scParts[i].equals("*")) { return true; } else if (!scParts[i].equalsIgnoreCase(cdParts[i])) { hasDiffPart = true; break; } } if (scParts.length == cdParts.length && !hasDiffPart) { return true; } } } } return false; }
protected LinkedList<String> processAnnotationDubboInterfaces(ClassDoc[] classes) { LinkedList<String> result = new LinkedList<String>(); for (int i = 0; i < classes.length; i++) { // implementation class which used com.alibaba.dubbo.config.annotation.Service if(isDubboService(classes[i])) { for(ClassDoc interfaceClassDoc : classes[i].interfaces()) { result.add(interfaceClassDoc.qualifiedName()); // mapping the method in interface to the method in implementation class for(MethodDoc implMethodDoc : classes[i].methods()) { MethodDoc overriddenMethod = implMethodDoc.overriddenMethod(); if(overriddenMethod != null) { methodMap.put(overriddenMethod, implMethodDoc); } else { //It seems that MethodDoc.overriddenMethod() doesn't work, but MethodDoc.overrides() works fine. for(MethodDoc interfaceMethodDoc : interfaceClassDoc.methods()) { if(implMethodDoc.overrides(interfaceMethodDoc)) { methodMap.put(interfaceMethodDoc, implMethodDoc); } } } } } } } return result; }
protected boolean isDubboService(ClassDoc classDoc) { AnnotationDesc[] annotations = classDoc.annotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].annotationType().qualifiedTypeName().equals("com.alibaba.dubbo.config.annotation.Service")) { AnnotationDesc.ElementValuePair[] elementValuePairs = annotations[i].elementValues(); for (AnnotationDesc.ElementValuePair elementValuePair : elementValuePairs) { if("protocol".equals(elementValuePair.element().name()) && "http".equals(protocolMap.get(elementValuePair.value().toString().replace("\"", "")))) { return false; } } return true; } } return false; }
/** * Returns a new Method object that is a legal combination of * this Method object and another one. * * Doing this requires determining the exceptions declared by * the combined method, which must be (only) all of the * exceptions declared in both old Methods that may thrown in * either of them. **/ Method mergeWith(Method other) { if (!nameAndDescriptor().equals(other.nameAndDescriptor())) { throw new AssertionError( "attempt to merge method \"" + other.nameAndDescriptor() + "\" with \"" + nameAndDescriptor()); } List<ClassDoc> legalExceptions = new ArrayList<ClassDoc>(); collectCompatibleExceptions( other.exceptionTypes, exceptionTypes, legalExceptions); collectCompatibleExceptions( exceptionTypes, other.exceptionTypes, legalExceptions); Method merged = clone(); merged.exceptionTypes = legalExceptions.toArray(new ClassDoc[legalExceptions.size()]); return merged; }
/** * Adds to the supplied list all exceptions in the "froms" * array that are subclasses of an exception in the "withs" * array. **/ private void collectCompatibleExceptions(ClassDoc[] froms, ClassDoc[] withs, List<ClassDoc> list) { for (ClassDoc from : froms) { if (!list.contains(from)) { for (ClassDoc with : withs) { if (from.subclassOf(with)) { list.add(from); break; } } } } }
/** * Returns an array containing all of the "test" methods (including those that are inherited) for * the given class. */ private static MethodDoc[] getTestMethods(ClassDoc c) { Set set = new TreeSet(); while (c != null) { MethodDoc[] methods = c.methods(); for (int i = 0; i < methods.length; i++) { MethodDoc method = methods[i]; if (method.isPublic() && method.parameters().length == 0 && method.name().startsWith("test")) { set.add(method); } } c = c.superclass(); } return (MethodDoc[]) set.toArray(new MethodDoc[0]); }
private PSOperatorDoc(ClassDoc classDoc, MethodDoc methodDoc) { this.declaringClassDoc = classDoc; this.methodDoc = methodDoc; this.description = methodDoc.commentText().replace('\n', ' '); this.paramDesc = ""; Tag[] paramTags = methodDoc.tags("param"); for (Tag paramTag : paramTags) { String paraStr = paramTag.text(); String paraName = paraStr.substring(0, paraStr.indexOf(' ')).replace('\n', ' ');; String paraDesc = paraStr.substring(paraStr.indexOf(' ') + 1).replace('\n', ' ');; this.paramDesc += "<br> - `" + paraName + "`: " + paraDesc; } this.returnType = methodDoc.returnType(); // ParameterizedType returnType = methodDoc.returnType().asParameterizedType(); // this.inputType = returnType.typeArguments()[0]; // this.outputType = returnType.typeArguments()[1]; // this.completeSignature = "Function<" + this.inputType + ", " + this.outputType + "> " + methodDoc.toString(); String shortSignature = classDoc.name() + "." + methodDoc.name() + "("; boolean firstParameter = true; for (Parameter parameter : methodDoc.parameters()) { if (firstParameter) { shortSignature += Utils.getSimpleTypeName(parameter.type()) + " " + parameter.name(); firstParameter = false; } else { shortSignature += ", " + Utils.getSimpleTypeName(parameter.type()) + " " + parameter.name(); } } shortSignature += ")"; this.shortSignature = shortSignature; }
public static PSOperatorDoc build(ClassDoc classDoc, MethodDoc methodDoc) { if (methodDoc.isStatic() && methodDoc.isPublic() && Utils.instanceOf(methodDoc.returnType().asClassDoc(), Consts.TYPE_FUNCTION)) { return new PSOperatorDoc(classDoc, methodDoc); } return null; }
public static PSOperatorWrapperDoc build(ClassDoc classDoc) { AnnotationDesc[] annotations = classDoc.annotations(); for (AnnotationDesc annotation : annotations) { AnnotationTypeDoc annotationType = annotation.annotationType(); if (Consts.OPERATOR_WRAPPER_ANNOTATION.equals(annotationType.toString())) { return new PSOperatorWrapperDoc(classDoc); } } return null; }
public static PSPipelineDoc build(ClassDoc classDoc, MethodDoc methodDoc) { AnnotationDesc[] annotations = methodDoc.annotations(); for (AnnotationDesc annotation : annotations) { AnnotationTypeDoc annotationType = annotation.annotationType(); if (Consts.TRANSFORMATION_ANNOTATION.equals(annotationType.toString())) { return new PSPipelineDoc(classDoc, methodDoc, annotation, TYPE_TRANSFORMATION); } else if (Consts.ACTION_ANNOTATION.equals(annotationType.toString())) { return new PSPipelineDoc(classDoc, methodDoc, annotation, TYPE_ACTION); } } return null; }
private boolean build(RootDoc rootDoc) { this.readOptions(rootDoc.options()); ClassDoc[] classes = rootDoc.classes(); for (int i = 0; i < classes.length; ++i) { ClassDoc classDoc = classes[i]; PSItemDoc itemDoc = PSItemDoc.build(classDoc); if (itemDoc != null) this.psItems.add(itemDoc); PSOperatorWrapperDoc operatorWrapperDoc = PSOperatorWrapperDoc.build(classDoc); if (operatorWrapperDoc != null) this.psOperatorWrappers.add(operatorWrapperDoc); if (Utils.instanceOf(classDoc, Consts.TYPE_P_STREAM)) { for (MethodDoc methodDoc : classDoc.methods()) { PSPipelineDoc pipelineDoc = PSPipelineDoc.build(classDoc, methodDoc); if (pipelineDoc != null) { this.psPipelines.add(pipelineDoc); } } } } this.dump(); return true; }
private void getAllFieldDocs(ClassDoc classDoc, List<FieldDoc> fieldDocs) { if (classDoc.superclass() != null) { this.getAllFieldDocs(classDoc.superclass(), fieldDocs); } if (isValidPSItem(classDoc)) { fieldDocs.addAll(Arrays.asList(classDoc.fields())); } }
public static boolean isValidPSItem(ClassDoc classDoc) { AnnotationDesc[] annotations = classDoc.annotations(); for (AnnotationDesc annotation : annotations) { AnnotationTypeDoc annotationType = annotation.annotationType(); if (Consts.ITEM_ANNOTATION.equals(annotationType.toString())) { return true; } } return false; }
public static boolean instanceOf(ClassDoc classDoc, String superClassName) { if (classDoc == null || superClassName == null) return false; String className = classDoc.containingPackage().name() + "." + classDoc.name(); // System.out.println(className + " " + superClassName); if (className.startsWith(superClassName)) { return true; } return instanceOf(classDoc.superclass(), superClassName); }
protected boolean isMQMessageClasses(ClassDoc classDoc) { Tag[] t = classDoc.tags(WRMqConsumerTaglet.NAME); if(t.length > 0) { return true; } t = classDoc.tags(WRMqProducerTaglet.NAME); return t.length > 0; }
@Override protected RequestMapping parseRequestMapping(ClassDoc classDoc) { RequestMapping mapping = new RequestMapping(); if(classDoc.tags(WRMqConsumerTaglet.NAME).length > 0) { mapping.setUrl(this.getMQConsumerTopic(classDoc)); } else { mapping.setUrl(this.getMQProducerTopic(classDoc)); } mapping.setTooltip(classDoc.simpleTypeName()); mapping.setContainerName(classDoc.simpleTypeName()); return mapping; }
protected ModificationHistory getModificationHistory(ClassDoc classDoc) { ModificationHistory history = new ModificationHistory(); if (classDoc != null) { LinkedList<ModificationRecord> list = this.getModificationRecords(classDoc); history.addModificationRecords(list); } return history; }
protected ModificationHistory getModificationHistory(Type type) { ModificationHistory history = new ModificationHistory(); ClassDoc classDoc = this.wrDoc.getConfiguration().root.classNamed(type.qualifiedTypeName()); if (classDoc != null) { LinkedList<ModificationRecord> list = this.getModificationRecords(classDoc); history.addModificationRecords(list); } return history; }
protected LinkedList<ModificationRecord> getModificationRecords(ClassDoc classDoc) { ClassDoc superClass = classDoc.superclass(); if (superClass == null) { return new LinkedList<ModificationRecord>(); } LinkedList<ModificationRecord> result = this.getModificationRecords(superClass); result.addAll(this.parseModificationRecords(classDoc.tags())); return result; }
protected String getMQConsumerTopic(ClassDoc classDoc) { Tag[] tags = classDoc.tags(WRMqConsumerTaglet.NAME); if (tags.length == 0) { return ""; } return StringUtils.substringBefore(tags[0].text(), "\n"); }
protected String getMQProducerTopic(ClassDoc classDoc) { Tag[] tags = classDoc.tags(WRMqProducerTaglet.NAME); if (tags.length == 0) { return ""; } return StringUtils.substringBefore(tags[0].text(), "\n"); }
protected List<APIParameter> getFields(Type type, ParameterType paramType, HashSet<String> processingClasses) { processingClasses.add(type.toString()); List<APIParameter> result = new LinkedList<APIParameter>(); if (!type.isPrimitive()) { ParameterizedType pt = type.asParameterizedType(); if (pt != null && pt.typeArguments().length > 0) { for (Type arg : pt.typeArguments()) { if (!this.isParameterizedTypeInStopClasses(arg)) { APIParameter tmp = new APIParameter(); tmp.setName(arg.simpleTypeName()); tmp.setType(this.getTypeName(arg, false)); tmp.setDescription(""); tmp.setParentTypeArgument(true); if (!processingClasses.contains(arg.qualifiedTypeName())) { tmp.setFields(this.getFields(arg, paramType, processingClasses)); } result.add(tmp); } } } ClassDoc classDoc = this.wrDoc.getConfiguration().root.classNamed(type.qualifiedTypeName()); if (classDoc != null) { result.addAll(this.getFields(classDoc, paramType, processingClasses)); } } return result; }
protected boolean checkForDeprecation(RootDoc root) { ClassDoc[] classarr = root.classes(); for (int i = 0; i < classarr.length; i++) { if (isGeneratedDoc(classarr[i])) { return true; } } return false; }
@Override protected void processOpenAPIClasses(ClassDoc[] classes, Configuration configuration) { LinkedList<String> annotationDubboInterfaces = processAnnotationDubboInterfaces(classes); dubboInterfaces.addAll(annotationDubboInterfaces); this.logger.debug("dubbo annotation interface list:"); for (String s : annotationDubboInterfaces) { this.logger.debug("interface: " + s); } super.processOpenAPIClasses(classes, configuration); }
protected ClassDoc getValidClass(ClassDoc[] classarr) { if (!nodeprecated) { return classarr[0]; } for (int i = 0; i < classarr.length; i++) { if (classarr[i].tags("deprecated").length == 0) { return classarr[i]; } } return null; }
public static boolean start(RootDoc root) throws ClassNotFoundException, NoSuchMethodException { ClassDoc[] classDocs = root.classes(); List<ApiModule> apiModules = new LinkedList<>(); for (int i = 0; i < classDocs.length; i++) { ClassDoc aClass = classDocs[i]; Class<?> moduleType = Class.forName(aClass.qualifiedTypeName()); ClassFilter classFilter = ClassFilterFactory.getDefaultFilter(); if (!classFilter.filter(moduleType)) { continue; } ApiModule apiModule = new ApiModule(); apiModule.setType(moduleType); apiModule.setComment(aClass.commentText()); MethodDoc[] methods = aClass.methods(false); for (MethodDoc method : methods) { Class[] paramTypes = paramTypes(method); Method m = moduleType.getDeclaredMethod(method.name(), paramTypes); DocTags docTags = SunDocUtils.getDocsForTag(method); ApiAction apiAction = new ApiAction(); apiAction.setComment(method.commentText()); apiAction.setName(method.name()); apiAction.setDocTags(docTags); apiAction.setMethod(m); apiModule.getApiActions().add(apiAction); } apiModules.add(apiModule); } ApiModulesHolder.setCurrentApiModules(apiModules);//设置当前的解析结果 return true; }
/** * Returns whether or not a class is a unit test. That is, whether or not it is a subclass of * {@link junit.framework.TestCase}. */ private static boolean isUnitTest(ClassDoc c) { if (c == null) { return false; } else if (c.qualifiedName().equals(TestCase.class.getName())) { return true; } else { return isUnitTest(c.superclass()); } }
@Override protected void generateClassFiles(ClassDoc[] arr, ClassTree classtree) { Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { if (!(configurationEx.isGeneratedDoc(arr[i]) && arr[i].isIncluded())) { continue; } ClassDoc prev = (i == 0) ? null : arr[i - 1]; ClassDoc curr = arr[i]; ClassDoc next = (i + 1 == arr.length) ? null : arr[i + 1]; try { if (curr.isAnnotationType()) { AbstractBuilder annotationTypeBuilder = configurationEx .getBuilderFactory().getAnnotationTypeBuilder( (AnnotationTypeDoc) curr, prev, next); annotationTypeBuilder.build(); } else { AbstractBuilder classBuilder = configurationEx .getBuilderFactory().getClassBuilder(curr, prev, next, classtree); classBuilder.build(); } } catch (Exception e) { e.printStackTrace(); throw new DocletAbortException(e.getMessage()); } } }
String dump(ClassDoc cd) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); dump(pw, "", cd); String out = sw.toString(); System.err.println(out); return out; }
void dump(PrintWriter out, String prefix, ClassDoc cd) { out.println(prefix + "class: " + cd); for (FieldDoc fd: cd.fields()) { out.println(prefix + " " + fd); if (fd.type().asClassDoc() != null) { dump(out, prefix + " ", fd.type().asClassDoc()); } } }
static void printClassMembers(RootDoc root, ClassDoc cls) { root.printNotice("Members for: " + cls); root.printNotice(" extends " + Arrays.asList(cls.superclass())); root.printNotice(" Fields: "); printMembers(root, cls.fields()); root.printNotice(" Constructor: "); printMembers(root, cls.constructors()); root.printNotice(" Method: "); printMembers(root, cls.methods()); if (cls.superclass() != null && !cls.superclassType().toString().equals("java.lang.Object")) printClassMembers(root, cls.superclass()); }