private static Tag getSingleTagOrNull(final MethodDoc methodDoc, final String tagName) { final Tag[] tags = methodDoc.tags(tagName); if (tags != null && tags.length == 1) { return tags[0]; } return null; }
private static List<MethodInspect> getInspects(ExecutableMemberDoc[] memberDocs) { List<MethodInspect> methodInspects = new ArrayList<>(memberDocs.length); for (ExecutableMemberDoc memberDoc : memberDocs) { List<String> signature = new ArrayList<>(); for (Parameter parameter : memberDoc.parameters()) { signature.add(parameter.type().qualifiedTypeName() + " " + parameter.name()); } MethodInspect methodInspect = new MethodInspect(memberDoc.name(), memberDoc.getRawCommentText(), String.join(", ", signature)); methodInspects.add(methodInspect); } return methodInspects; } }
public class ExtractCommentsDoclet { public static boolean start(RootDoc root) throws IOException { for (ClassDoc c : root.classes()) { print(c.qualifiedName(), c.commentText()); for (FieldDoc f : c.fields(false)) { print(f.qualifiedName(), f.commentText()); } for (MethodDoc m : c.methods(false)) { print(m.qualifiedName(), m.commentText()); if (m.commentText() != null && m.commentText().length() > 0) { for (ParamTag p : m.paramTags()) print(m.qualifiedName() + "@" + p.parameterName(), p.parameterComment()); for (Tag t : m.tags("return")) { if (t.text() != null && t.text().length() > 0) print(m.qualifiedName() + "@return", t.text()); } } } } return true; } private static void print(String name, String comment) throws IOException { if (comment != null && comment.length() > 0) { new FileWriter(name + ".txt").append(comment).close(); } } }
private static void addParamDocs(final MethodDoc methodDoc, final MethodDocType methodDocType, final DocProcessor docProcessor) { final Parameter[] parameters = methodDoc.parameters(); final ParamTag[] paramTags = methodDoc.paramTags(); paramDocType.setParamName(paramTag.parameterName()); paramDocType.setCommentText(paramTag.parameterComment()); docProcessor.processParamTag(paramTag, parameter, paramDocType); final AnnotationDesc[] annotations = parameter.annotations(); if (annotations != null) { for (final AnnotationDesc annotationDesc : annotations) { final AnnotationDocType annotationDocType = new AnnotationDocType(); final String typeName = annotationDesc.annotationType().qualifiedName(); annotationDocType.setAnnotationTypeName(typeName); for (final ElementValuePair elementValuePair : annotationDesc.elementValues()) { final NamedValueType namedValueType = new NamedValueType(); namedValueType.setName(elementValuePair.element().name()); namedValueType.setValue(elementValuePair.value().value().toString()); annotationDocType.getAttributeDocs().add(namedValueType);
public static boolean start(RootDoc root) { //iterate over all classes. HashMap<String, ClassInspect> inspects = new HashMap<>(root.classes().length); ClassDoc[] classes = root.classes(); for (ClassDoc classDoc : classes) { ClassInspect classInspect = new ClassInspect(classDoc.name(), ((ClassDocImpl) classDoc).type.toString(), classDoc.commentText()); inspects.put(classInspect.getFullName(), classInspect); classInspect.setMethods(getInspects(classDoc.methods())); classInspect.setConstructors(getInspects(classDoc.constructors())); } SerializeInspect serializeInspect = new SerializeInspect(); String json = serializeInspect.toJson(inspects); serializeInspect.saveToFile(json); return true; }
/** * 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 static String print(final Tag tag) { return String.valueOf(tag.getClass()) + "[" + "firstSentenceTags=" + toCSV(tag.firstSentenceTags()) + ", inlineTags=" + toCSV(tag.inlineTags()) + ", kind=" + tag.kind() + ", name=" + tag.name() + ", text=" + tag.text() + "]"; }
String[][] options = root.options(); root.printError("Internal Error: No output file"); return false; root.printNotice("Generating " + outputFile); pw.println(""); ClassDoc[] classes = root.classes(); Arrays.sort(classes, new Comparator() { @Override for (int i = 0; i < classes.length; i++) { ClassDoc c = classes[i]; if (!c.isAbstract() && isUnitTest(c)) { document(c, pw); StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw, true)); root.printError(sw.toString()); return false;
/** * Summarizes the test methods of the given class */ public static void document(ClassDoc c, PrintWriter pw) throws IOException { pw.println(c.qualifiedName()); { String comment = c.commentText(); if (comment != null && !comment.equals("")) { pw.println(""); indent(comment, 4, pw); pw.println(""); } } MethodDoc[] methods = getTestMethods(c); for (int i = 0; i < methods.length; i++) { MethodDoc method = methods[i]; pw.print(" "); pw.println(method.name()); String comment = method.commentText(); if (comment != null && !comment.equals("")) { pw.println(""); indent(comment, 6, pw); pw.println(""); } } pw.println(""); }
final Tag[] inlineTags = tag.inlineTags(); if (inlineTags != null && inlineTags.length > 0) { for (final Tag inlineTag : inlineTags) { LOG.fine("Have inline tag: " + print(inlineTag)); if ("@link".equals(inlineTag.name())) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Have link: " + print(inlineTag)); } else if (!isEmpty(inlineTag.text())) { return inlineTag.text(); return tag.text();
private static String getSerializedLinkFromTag(final SeeTag linkTag) { final MemberDoc referencedMember = linkTag.referencedMember(); if (!referencedMember.isStatic()) { LOG.warning("Referenced member of @link " + print(linkTag) + " is not static." + " Right now only references to static members are supported."); final ClassDoc containingClass = referencedMember.containingClass(); final Object object; try { final Field declaredField = Class.forName(containingClass.qualifiedName(), false, Thread.currentThread() .getContextClassLoader()).getDeclaredField(referencedMember.name()); if (referencedMember.isFinal()) { declaredField.setAccessible(true); LOG.info("Have thread classloader " + Thread.currentThread().getContextClassLoader().getClass()); LOG.info("Have system classloader " + ClassLoader.getSystemClassLoader().getClass()); LOG.log(Level.SEVERE, "Could not get field " + referencedMember.qualifiedName(), e); return null;
private static void addRequestRepresentationDoc(final MethodDoc methodDoc, final MethodDocType methodDocType) { final Tag requestElement = getSingleTagOrNull(methodDoc, "request.representation.qname"); final Tag requestExample = getSingleTagOrNull(methodDoc, "request.representation.example"); if (requestElement != null || requestExample != null) { final RequestDocType requestDoc = new RequestDocType(); final RepresentationDocType representationDoc = new RepresentationDocType(); /* requestElement exists */ if (requestElement != null) { representationDoc.setElement(QName.valueOf(requestElement.text())); } /* requestExample exists */ if (requestExample != null) { final String example = getSerializedExample(requestExample); if (!isEmpty(example)) { representationDoc.setExample(example); } else { LOG.warning("Could not get serialized example for method " + methodDoc.qualifiedName()); } } requestDoc.setRepresentationDoc(representationDoc); methodDocType.setRequestDoc(requestDoc); } }
@Override public String toString(final Tag[] tags) { if (tags.length == 0) { return null; } StringBuilder buf = new StringBuilder(String.format("\n<dl><dt><span class=\"strong\">%s</span></dt>\n", getHeader())); for (Tag t : tags) { buf.append(" <dd>").append(genLink(t.text())).append("</dd>\n"); } return buf.toString(); }
/** * 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()); } }
private static Map<String, List<Tag>> getResponseRepresentationTags(final MethodDoc methodDoc) { final Map<String, List<Tag>> tagsByStatus = new HashMap<>(); for (final Tag tag : methodDoc.tags()) { final Matcher matcher = PATTERN_RESPONSE_REPRESENTATION.matcher(tag.name()); if (matcher.matches()) { final String status = matcher.group(1); List<Tag> tags = tagsByStatus.get(status); if (tags == null) { tags = new ArrayList<>(); tagsByStatus.put(status, tags); } tags.add(tag); } } return tagsByStatus; }
@Override public int compare(Object o1, Object o2) { ClassDoc c1 = (ClassDoc) o1; ClassDoc c2 = (ClassDoc) o2; return c1.qualifiedName().compareTo(c2.qualifiedName()); } });
static String toCSV(final Collection<Tag> items, final String separator, final String delimiter) { if (items == null) { return null; } if (items.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); for (final Iterator<Tag> iter = items.iterator(); iter.hasNext(); ) { if (delimiter != null) { sb.append(delimiter); } final Tag item = iter.next(); sb.append(item.name()); if (delimiter != null) { sb.append(delimiter); } if (iter.hasNext()) { sb.append(separator); } } return sb.toString(); }
private static boolean validOption(final String optionName, final String reportOptionName, final String[][] options, final DocErrorReporter reporter) { final String option = getOptionArg(options, optionName); final boolean foundOption = option != null && !option.trim().isEmpty(); if (!foundOption) { reporter.printError(optionName + " " + reportOptionName + " must be specified."); } return foundOption; }
public static boolean validOptions(String[][] options, DocErrorReporter reporter) { boolean sawOutput = false; for (int i = 0; i < options.length; i++) { String[] option = options[i]; if (option[0].equals("-output")) { File output = new File(option[1]); if (output.exists() && output.isDirectory()) { reporter.printError("Output file " + output + " is a directory"); return false; } else { sawOutput = true; } } } if (!sawOutput) { reporter.printError("Missing -output"); return false; } return true; }