Search in sources :

Example 11 with MethodDoc

use of com.sun.javadoc.MethodDoc in project cxf by apache.

the class DumpJavaDoc method start.

public static boolean start(RootDoc root) throws IOException {
    String dumpFileName = readOptions(root.options());
    OutputStream os = Files.newOutputStream(Paths.get(dumpFileName));
    Properties javaDocMap = new Properties();
    for (ClassDoc classDoc : root.classes()) {
        javaDocMap.put(classDoc.toString(), classDoc.commentText());
        for (MethodDoc method : classDoc.methods()) {
            javaDocMap.put(method.qualifiedName(), method.commentText());
            for (ParamTag paramTag : method.paramTags()) {
                Parameter[] parameters = method.parameters();
                for (int i = 0; i < parameters.length; ++i) {
                    if (parameters[i].name().equals(paramTag.parameterName())) {
                        javaDocMap.put(method.qualifiedName() + ".paramCommentTag." + i, paramTag.parameterComment());
                    }
                }
            }
            Tag[] retTags = method.tags("return");
            if (retTags != null && retTags.length == 1) {
                Tag retTag = method.tags("return")[0];
                javaDocMap.put(method.qualifiedName() + "." + "returnCommentTag", retTag.text());
            }
        }
    }
    javaDocMap.store(os, "");
    os.flush();
    os.close();
    return true;
}
Also used : ParamTag(com.sun.javadoc.ParamTag) MethodDoc(com.sun.javadoc.MethodDoc) OutputStream(java.io.OutputStream) Parameter(com.sun.javadoc.Parameter) ParamTag(com.sun.javadoc.ParamTag) Tag(com.sun.javadoc.Tag) Properties(java.util.Properties) ClassDoc(com.sun.javadoc.ClassDoc)

Example 12 with MethodDoc

use of com.sun.javadoc.MethodDoc in project jersey by jersey.

the class ResourceDoclet method start.

/**
     * Start the doclet.
     *
     * @param root the root JavaDoc document.
     * @return true if no exception is thrown.
     */
public static boolean start(final RootDoc root) {
    final String output = getOptionArg(root.options(), OPTION_OUTPUT);
    final String classpath = getOptionArg(root.options(), OPTION_CLASSPATH);
    // LOG.info( "Have classpath: " + classpath );
    final String[] classpathElements = classpath.split(File.pathSeparator);
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    final ClassLoader ncl = new Loader(classpathElements, ResourceDoclet.class.getClassLoader());
    Thread.currentThread().setContextClassLoader(ncl);
    final String docProcessorOption = getOptionArg(root.options(), OPTION_DOC_PROCESSORS);
    final String[] docProcessors = docProcessorOption != null ? docProcessorOption.split(":") : null;
    final DocProcessorWrapper docProcessor = new DocProcessorWrapper();
    try {
        if (docProcessors != null && docProcessors.length > 0) {
            final Class<?> clazz = Class.forName(docProcessors[0], true, Thread.currentThread().getContextClassLoader());
            final Class<? extends DocProcessor> dpClazz = clazz.asSubclass(DocProcessor.class);
            docProcessor.add(dpClazz.newInstance());
        }
    } catch (final Exception e) {
        LOG.log(Level.SEVERE, "Could not load docProcessors " + docProcessorOption, e);
    }
    try {
        final ResourceDocType result = new ResourceDocType();
        final ClassDoc[] classes = root.classes();
        for (final ClassDoc classDoc : classes) {
            LOG.fine("Writing class " + classDoc.qualifiedTypeName());
            final ClassDocType classDocType = new ClassDocType();
            classDocType.setClassName(classDoc.qualifiedTypeName());
            classDocType.setCommentText(classDoc.commentText());
            docProcessor.processClassDoc(classDoc, classDocType);
            for (final MethodDoc methodDoc : classDoc.methods()) {
                final MethodDocType methodDocType = new MethodDocType();
                methodDocType.setMethodName(methodDoc.name());
                methodDocType.setMethodSignature(methodDoc.signature());
                methodDocType.setCommentText(methodDoc.commentText());
                docProcessor.processMethodDoc(methodDoc, methodDocType);
                addParamDocs(methodDoc, methodDocType, docProcessor);
                addRequestRepresentationDoc(methodDoc, methodDocType);
                addResponseDoc(methodDoc, methodDocType);
                classDocType.getMethodDocs().add(methodDocType);
            }
            result.getDocs().add(classDocType);
        }
        try {
            final Class<?>[] clazzes = getJAXBContextClasses(result, docProcessor);
            final JAXBContext c = JAXBContext.newInstance(clazzes);
            final Marshaller m = c.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            final OutputStream out = new BufferedOutputStream(new FileOutputStream(output));
            final String[] cdataElements = getCDataElements(docProcessor);
            final XMLSerializer serializer = getXMLSerializer(out, cdataElements);
            m.marshal(result, serializer);
            out.close();
            LOG.info("Wrote " + output);
        } catch (final Exception e) {
            LOG.log(Level.SEVERE, "Could not serialize ResourceDoc.", e);
            return false;
        }
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
    return true;
}
Also used : Marshaller(javax.xml.bind.Marshaller) XMLSerializer(org.apache.xml.serialize.XMLSerializer) ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) JAXBContext(javax.xml.bind.JAXBContext) MalformedURLException(java.net.MalformedURLException) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType) MethodDoc(com.sun.javadoc.MethodDoc) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) BufferedOutputStream(java.io.BufferedOutputStream) ClassDoc(com.sun.javadoc.ClassDoc)

Example 13 with MethodDoc

use of com.sun.javadoc.MethodDoc in project RESTdoclet by IG-Group.

the class DocTypeUtils method getTypeDoc.

/**
    * Returns as a string a list of attributes plus comments for the given
    * iggroup complex type (or empty string if not iggroup), formatted in an
    * HTML table. This method will recurse if attributes are iggroup complex
    * types
    * 
    * @param type
    * @param processedTypes
    * @param leafType
    * @return
    */
private static String getTypeDoc(final Type type, HashMap<String, String> processedTypes, Boolean leafType) {
    LOG.info("getTypeDoc " + type + " leafType=" + leafType);
    String typeInfo = "";
    if (isRelevantType(type)) {
        ClassDoc typeDoc = type.asClassDoc();
        typeInfo = processedTypes.get(type.toString());
        if (typeInfo != null) {
            LOG.debug("Found cached typedoc for " + type.typeName() + " - " + typeInfo);
        }
        if (typeInfo == null && typeDoc != null) {
            typeInfo = "";
            // if this is a generic type then recurse with the first type argument
            if (isParameterisedType(type)) {
                LOG.debug("Parameterised type");
                if (type.asClassDoc() != null) {
                    typeInfo = getTypeDoc(type.asParameterizedType().typeArguments()[type.asParameterizedType().typeArguments().length - 1], processedTypes, true);
                }
            } else {
                logType(type);
                // put placeholder to stop recursion for self-referential types
                LOG.debug("Starting to cache: " + type.typeName());
                processedTypes.put(type.toString(), "");
                LOG.debug(typeDoc.commentText() + "  " + leafType);
                if (leafType && !typeDoc.commentText().isEmpty()) {
                    typeInfo += "<tr><span class=\"javadoc-header\">" + typeDoc.commentText() + "</span></tr>";
                    LOG.debug(typeInfo);
                }
                if (typeDoc.isEnum()) {
                    LOG.debug("Enum type");
                    typeInfo += getEnumDoc(type);
                } else {
                    // class
                    LOG.debug("Class");
                    // first do base class
                    if (typeDoc.superclass() != null) {
                        LOG.debug("base type = " + typeDoc.superclass().qualifiedName());
                        String baseTypeDoc = getTypeDoc(type.asClassDoc().superclassType(), processedTypes, false);
                        if (!baseTypeDoc.isEmpty()) {
                            LOG.debug("base type DOC = " + baseTypeDoc);
                            typeInfo += baseTypeDoc;
                        }
                    }
                    typeInfo += getPublicConstantDoc(type);
                    Collection<String> getterNames = getGetterNames(type);
                    for (MethodDoc method : typeDoc.methods()) {
                        if (method.isPublic() && getterNames.contains(method.name()) && !ignore(method)) {
                            String attributeInfo = "";
                            String attributeType = method.returnType().simpleTypeName();
                            // check if is this a parameterised type
                            ParameterizedType pt = method.returnType().asParameterizedType();
                            if (pt != null && pt.typeArguments().length > 0) {
                                attributeType += "[";
                                for (int i = 0; i < pt.typeArguments().length; i++) {
                                    attributeType += pt.typeArguments()[i].simpleTypeName();
                                    if (i < pt.typeArguments().length - 1) {
                                        attributeType += ", ";
                                    }
                                }
                                attributeType += "]";
                            }
                            // Check if this is an array
                            attributeType += method.returnType().dimension();
                            final String attributeName = getAttributeNameFromMethod(method.name());
                            attributeInfo += "<td>" + attributeType + " " + attributeName + "</td>";
                            // If type or parameterised type then recurse
                            LOG.debug("Generating attribute doc for " + method.returnType());
                            String attributeTypeDoc = getTypeDoc(method.returnType(), processedTypes, true);
                            if (!attributeTypeDoc.isEmpty()) {
                                LOG.debug("Found attribute doc for " + method.returnType());
                                attributeInfo += "<td>" + attributeTypeDoc + "</td>";
                            } else {
                                // no useful type information, so use whatever's on the attribute doc
                                LOG.debug("Found no attribute doc for " + method.returnType());
                                String fieldDoc = getFieldDoc(type, attributeName, method.commentText());
                                attributeInfo += "<td>" + fieldDoc + "</td>";
                            }
                            if (!attributeInfo.isEmpty()) {
                                typeInfo += "<tr>" + attributeInfo + "</tr>";
                            }
                        }
                    }
                }
                // Wrap in a table tag if this is concrete type
                if (leafType && !typeInfo.isEmpty()) {
                    typeInfo = "<table>" + typeInfo + "</table>";
                }
            }
        }
        LOG.debug("Caching: " + type);
        processedTypes.put(type.toString(), typeInfo);
    }
    if (typeInfo == null) {
        typeInfo = "";
    }
    LOG.debug("XXX " + type.typeName() + " XXX " + typeInfo);
    return typeInfo;
}
Also used : ParameterizedType(com.sun.javadoc.ParameterizedType) MethodDoc(com.sun.javadoc.MethodDoc) ClassDoc(com.sun.javadoc.ClassDoc)

Example 14 with MethodDoc

use of com.sun.javadoc.MethodDoc in project geode by apache.

the class UnitTestDoclet method getTestMethods.

/**
   * 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]);
}
Also used : MethodDoc(com.sun.javadoc.MethodDoc)

Example 15 with MethodDoc

use of com.sun.javadoc.MethodDoc in project geode by apache.

the class UnitTestDoclet method document.

/**
   * 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("");
}
Also used : MethodDoc(com.sun.javadoc.MethodDoc)

Aggregations

MethodDoc (com.sun.javadoc.MethodDoc)19 ClassDoc (com.sun.javadoc.ClassDoc)10 Type (com.sun.javadoc.Type)4 FieldDoc (com.sun.javadoc.FieldDoc)2 ParamTag (com.sun.javadoc.ParamTag)2 Parameter (com.sun.javadoc.Parameter)2 Tag (com.sun.javadoc.Tag)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 FieldParameter (com.iggroup.oss.restdoclet.doclet.type.FieldParameter)1 FieldParameterBuilder (com.iggroup.oss.restdoclet.doclet.type.builder.FieldParameterBuilder)1 ParameterizedType (com.sun.javadoc.ParameterizedType)1 ApiClassDocumentation (com.zimbra.doc.soap.ApiClassDocumentation)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1