Code Monkey home page Code Monkey logo

javadoc-coverage's Introduction

JavaDoc Coverage Doclet

Build Status Codacy Badge Maven Central GPL licensed

The Doclet parses java source files and checks the percentage of the Java code covered by JavaDoc documentation, including:

  • packages (Java 9 modules not supported yet)
  • classes, inner classes, interfaces and enums
  • class attributes
  • methods, parameters, exceptions and return value.

A sample coverage report is available here.

Current IDEs warn about missing JavaDoc tags and documentation, allowing you to individually fix the issues, but you don't get a big the picture. Similar to code coverage tools, this plugin provides a way to get a summarized overview of your project's documentation coverage. It provides a Doclet to be used with the JavaDoc Tool to show JavaDoc documentation coverage of your project.

Usage

The easier ways to use the plugin is through Maven or Gradle. You can use the plugin calling the JavaDoc Tool directly from the command line (but this isn't handy and it isn't explained here).

Maven: Using the CoverageDoclet in a regular way

To generate the regular JavaDoc HTML files and the coverage report, you have to include two <execution> tags for the maven-javadoc-plugin inside your project's pom.xml file, as the exemple below:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.10.4</version>
            <executions>
                <!-- Exports JavaDocs to regular HTML files -->
                <execution>
                    <id>javadoc-html</id>
                    <phase>package</phase>
                    <goals>
                        <goal>javadoc</goal>
                    </goals>
                </execution>

                <!-- Generates the JavaDoc coverage report -->
                <execution>
                    <id>javadoc-coverage</id>
                    <phase>package</phase>
                    <goals>
                        <goal>javadoc</goal>
                    </goals>
                    <configuration>
                        <doclet>com.manoelcampos.javadoc.coverage.CoverageDoclet</doclet>
                        <docletArtifact>
                            <groupId>com.manoelcampos</groupId>
                            <artifactId>javadoc-coverage</artifactId>
                            <version>1.1.0</version>
                        </docletArtifact>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    <plugins>
<build>

Now, to generate the regular JavaDocs in HTML and the documentation coverage report, you can execute the package goal in Maven, using your IDE or the command line inside your project's root directory:

mvn clean package

The JavaDoc coverage report is generated by default as javadoc-coverage.html at target/site/apidocs/.

There is a sample project where you can test the plugin. Just execute the command above inside the project's directory to see the results.

Maven: Using the CoverageDoclet with the maven-site-plugin

If you are generating a maven site and want to include the regular JavaDocs HTML and the JavaDoc Coverage Report into the "Reports" section of the site, the maven-javadoc-plugin must be included with slightly different configurations into the <reporting> tag (instead of the <build> tag), as the example below:

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.10.4</version>
            <reportSets>
                <reportSet>
                    <!-- Exports JavaDocs to regular HTML files -->
                    <id>javadoc-html</id>
                    <reports>
                        <report>javadoc</report>
                    </reports>
                </reportSet>

                <reportSet>
                    <!-- Generates the JavaDoc coverage report -->
                    <id>javadoc-coverage</id>
                    <reports>
                        <report>javadoc</report>
                    </reports>
                    <configuration>
                        <name>JavaDoc Coverage</name>
                        <description>Percentage of the code coverage by JavaDoc documentation.</description>
                        <doclet>com.manoelcampos.javadoc.coverage.CoverageDoclet</doclet>
                        <docletArtifact>
                            <groupId>com.manoelcampos</groupId>
                            <artifactId>javadoc-coverage</artifactId>
                            <version>1.1.0</version>
                        </docletArtifact>
                        <!-- This is the same as using -d into the additionalparam tag -->
                        <destDir>javadoc-coverage</destDir>
                        <!-- You can also use -o instead of -outputName to define
                        the name of the generated report. -->
                        <additionalparam>-outputName "index.html"</additionalparam>
                    </configuration>
                </reportSet>
            </reportSets>
        </plugin>
    </plugins>
</reporting>

Notice that in this case, the coverage report is being generated into the target/site/javadoc-coverage (as defined by the destDir tag) with the name of index.html (as defined by the <additionalparam> tag), as required for the maven site. More details of additional parameters is provided in the next section.

Now, to generate the site you can execute:

mvn clean site

The list of project's reports will be included into the target/site/project-reports.html file.

Gradle

To use the Doclet with Gradle, add the following code to your build.gradle file.

configurations {
// Other configuration lines might be in here
    javadocCoverage
}
dependencies {
// Your application's other dependencies go here.
    javadocCoverage "com.manoelcampos:javadoc-coverage:1.1.0"
}
// This generates the Javadoc coverage report into build/reports/javadoc/javadoc-coverage.html
task javadocCoverageReport(type: Javadoc, dependsOn: javadoc) {
    source = sourceSets.main.allJava
    destinationDir = reporting.file("javadoc")
    options.docletpath = configurations.javadocCoverage.files.asType(List)
    options.doclet = "com.manoelcampos.javadoc.coverage.CoverageDoclet"
}
// Optionally you can add the dependsOn here so that when you generate the javadoc
// jar, e.g. if you include the javadocJar in a publishing configuration, the javadoc
// coverage report will be generated automatically.
task javadocJar(type: Jar, dependsOn: javadocCoverageReport) {
    classifier "javadoc"
    from javadoc.destinationDir
}

Additional Configuration (optional)

You can define additional configurations for the plugin. The examples below are presented only for Maven (but the same parameters work for Gradle).

Changing the name of the coverage report file

The CoverageDoclet accepts the command line parameter -outputName (-o for short) to set the name of the report. The following example shows the code to be added to the <configuration> tag of the maven-javadoc-plugin:

<additionalparam>-outputName "my-project-javadoc-coverage-report.html"</additionalparam>

Excluding packages from the coverage report

You can exclude some packages from the coverage report by adding the code example below into the <configuration> tag of the maven-javadoc-plugin.

<configuration>
    <doclet>com.manoelcampos.javadoc.coverage.CoverageDoclet</doclet>
    <docletArtifact>
        <groupId>com.manoelcampos</groupId>
        <artifactId>javadoc-coverage</artifactId>
        <version>1.1.0</version>
    </docletArtifact>
    <!-- Excludes packages from the coverage report. -->
    <excludePackageNames>com.manoelcampos.sample2</excludePackageNames>
</configuration>

The example shows how to ignore the package com.manoelcampos.sample2 from the coverage report. The <excludePackageNames> tag accepts a list of packages separated by : and also wildcards such as *. For more details, check this link.

If you are generating the regular JavaDoc HTML files, you have to include this configuration only where the CoverageDoclet is being used into your pom.xml, unless you want these packages to be excluded from the regular JavaDocs too.

Building the Doclet from Sources

The Doclet is a Java Maven project which can be built directly from any IDE or using the following maven command:

mvn clean install

The command builds the Doclet and install it at your local maven repository.

javadoc-coverage's People

Contributors

dependabot[bot] avatar manoelcampos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javadoc-coverage's Issues

Bugs in MethodDocStats

Hi,

by going through the code I found some suspicious code snippets:

  • The method isReturnValueDocumented is unused and should be removed
  • The method getDocumentedMembers returns 0, and should instead return the number of documented members (extract from getDocumentedMembersPercent)
  • there is a TODO for the @return annotation regarding void methods and constructors, this can easily be integrated, see the sample code below:
    @Override
    public long getDocumentedMembers() {
        int countReturnPartial = 0;
        if (!isVoidMethod() && !doc.isConstructor()) {
            if (Arrays.stream(doc.tags()).filter(t -> t.name().equals("@return")).map(Tag::text).anyMatch(Utils::isNotStringEmpty)) {
                countReturnPartial = 1;
            }
        }
        return Utils.boolToInt(hasDocumentation()) + paramsStats.getDocumentedMembers() + thrownExceptions.getDocumentedMembers()
                + countReturnPartial;
    }

    @Override
    public double getDocumentedMembersPercent() {
        int countReturnTotal = 1;
        if (isVoidMethod() || doc.isConstructor()) {
            countReturnTotal = 0;
        }

        // this 1 is used to count the method as a element which may be documented or not
        return Utils.computePercentage(getDocumentedMembers(), 1 + getMembersNumber() + countReturnTotal);
    }

    private boolean isVoidMethod() {
        return doc.isMethod() && ((MethodDoc) doc).returnType() == null;
    }

Support Java 9+

Hi,
calling "site" with Java 9+ fails with the attached exception, with java 8 however works fine. Is there a workaround?

Thanks in advance

Attachment

Stacktrace.txt

javadoc: error - invalid flag: -d

Tried using javadoc-coverage with gradle as per the steps in the README.
Get the following error : javadoc: error - invalid flag: -d

Note: Running javdaoc without the coverage doclet works fine.

Is this a known issue? Couldn't find much support elsewhere.

Wrong Coverage-Percentage computed

When having additional tags (e.g. @throws IllegalArgumentException or IllegalStateException without declaring the exception, which is not necessary as it is a runtime exception, we just want to tell the user under which circumstances this can occur) the computed percentage is wrong.

Example class where wrong numbers occur (150% for the constructor):

public class TestJavadocCoverage {

    /**
     * @param param
     *            unimportant
     * @throws IllegalStateException
     *             unimportant
     */
    public TestJavadocCoverage(int param) {
       // not important
    }
}

we used version 1.1.0 from maven central

Exclude certain classes

Not sure if I missed it, but I couldnt find it in the readme nor in the code. We have several internal packages where javadoc is not required. But our public API does require it. Could you add an option to exclude classes based on a regex?

Allow to compute coverage only for public methods/fields

I have a rather large codebase and want to make assumptions about the javadoc-coverage only from the public (and therefore accessible from everywhere) part of the code. It would be ok for me to send you a merge request for that, but at first I wanted to know if you like the idea of having this option, too?

Spot useless javadocs

Sometimes, developers write useless javadocs such as the example below

/**
 * Some Foo Method.
 * @param id the id
 * @return 
*/
int someFooMethod(int id){ return 0; }

The plugin should spot this kind of "documentation" and present them into the report.
A common pattern to detect such useless javadocs are:

  • the method documentation includes only the name of the method, accordingly separating words by space
  • param documentation just includes the name or type of the param, preceded by an article (the, a, an): the id, an id, an int
  • method or param documentation includes just one word.
  • after removing connectors (the, a, and, of, to, for, etc) and spaces from the documentation, the text remaining is equal to the element name, for instance:
/**
 * Compute the statistics.
*/
double computeStatistics(){ return 0; }

However, it may be difficult to detect verbs in the 3rd person of the singular, such as "Computes" instead of "Compute".

Regular expressions should be defined to detect such issues. A set of regex should be defined by default, but the plugin should enable developers to specify their own regex and if they want to use both the given regular expressions and the standard ones, or just the given ones.

Use the javadoc-coverage report to be generated under the reporting tag in pom.xml

Reports are created from the plugins added under the reporting tag in the pom.xml .

I found out that I can't add it under the reporting tag in pom.xml because it contains the execution tag. I have to put it under the build tag and because of that it does not get added to the list of project reports for a project.

The project reports are generated by the plugins placed under the reporting tag but I can't put this plugin there.

Overridden Interface/Abstract Methods potentially don't need separate Documentation

For interface methods it is clear that they should be documented in the interface itself and not in the implementing classes (no duplicate documentation necessary!)

For methods overridden from super classes this might as well be the case, however behavior could be different in them, so there is potentially the need for additional documentation.

In general, I would ignore the second case, and count them separately, but all interface methods shouldn't be counted in the implementing classes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.