Skip to content

Commit d209237

Browse files
authored
Merge pull request #45 from secure-software-engineering/fix/update-coverage-doclet
Update coverage doclet
2 parents 33a27a3 + a01c66a commit d209237

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+319
-4699
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The project contains the following modules:
1212
* **swan-pipeline**: core machine learning implementation for SWAN with components for data collection and preparation, feature engineering and model selection phases
1313
* **swan-assist**: IntelliJ plugin provides GUI support for SWAN and enables active machine learning.
1414
* **swan-javadoc-exporter**: Doclet exports doc comments to XML files so that they can be analyzed by the Natural Language Processing (NLP) module
15-
* **swan-javadoc-coverage**: Doclet calculates the software documentation coverage of Java programs based on the presence of doc comments for classes, methods, and other objects.
15+
* **doc-coverage-doclet**: Doclet calculates the software documentation coverage of Java programs based on the presence of doc comments for classes, methods, and other objects.
1616

1717
How do I get started with SWAN?
1818
-------------

doc-coverage-doclet/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.fraunhofer.iem</groupId>
8+
<artifactId>coverage-doclet</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
</project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package de.fraunhofer.iem.coveragedoclet;
2+
3+
import com.sun.source.doctree.DocCommentTree;
4+
import com.sun.source.util.DocTrees;
5+
import jdk.javadoc.doclet.Doclet;
6+
import jdk.javadoc.doclet.DocletEnvironment;
7+
import jdk.javadoc.doclet.Reporter;
8+
9+
import javax.lang.model.SourceVersion;
10+
import javax.lang.model.element.Element;
11+
import javax.lang.model.element.TypeElement;
12+
import javax.lang.model.util.ElementFilter;
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.Locale;
16+
import java.util.Set;
17+
import java.util.logging.Logger;
18+
19+
/**
20+
* A minimal doclet that just prints out the names of the
21+
* selected elements. It goes on and on
22+
*/
23+
public class CoverageDoclet implements Doclet {
24+
25+
final private Logger log = Logger.getLogger(CoverageDoclet.class.getName());
26+
27+
@Override
28+
public void init(Locale locale, Reporter reporter) {
29+
}
30+
31+
@Override
32+
public String getName() {
33+
// For this doclet, the name of the doclet is just the
34+
// simple name of the class. The name may be used in
35+
// messages related to this doclet, such as in command-line
36+
// help when doclet-specific options are provided.
37+
return getClass().getSimpleName();
38+
}
39+
40+
@Override
41+
public Set<? extends Option> getSupportedOptions() {
42+
// This doclet does not support any options.
43+
return Collections.emptySet();
44+
}
45+
46+
@Override
47+
public SourceVersion getSupportedSourceVersion() {
48+
// This doclet supports all source versions.
49+
// More sophisticated doclets may use a more
50+
// specific version, to ensure that they do not
51+
// encounter more recent language features that
52+
// they may not be able to handle.
53+
return SourceVersion.latest();
54+
}
55+
56+
private static final boolean OK = true;
57+
58+
@Override
59+
public boolean run(DocletEnvironment environment) {
60+
// This method is called to perform the work of the doclet.
61+
// In this case, it just prints out the names of the
62+
// elements specified on the command line.
63+
//environment.getSpecifiedElements().forEach(System.out::println);
64+
65+
DocTrees docTrees = environment.getDocTrees();
66+
67+
CoverageReport docReport = new CoverageReport();
68+
69+
70+
ArrayList<String> classnames = new ArrayList<>();
71+
72+
for (TypeElement typeElement : ElementFilter.typesIn(environment.getIncludedElements())) {
73+
74+
docReport.incrementTotalClasses();
75+
classnames.add(typeElement.getQualifiedName().toString());
76+
77+
if (isDocumented(docTrees.getDocCommentTree(typeElement)))
78+
docReport.incrementDocumentedClasses();
79+
80+
for (Element element : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
81+
docReport.incrementTotalMethods();
82+
83+
if (isDocumented(docTrees.getDocCommentTree(element)))
84+
docReport.incrementDocumentedMethods();
85+
}
86+
}
87+
log.info(classnames.size() + " classes found");
88+
log.info("Documentation coverage report: " + docReport);
89+
return OK;
90+
}
91+
92+
public boolean isDocumented(DocCommentTree docCommentTree) {
93+
94+
return !(docCommentTree == null || docCommentTree.getFullBody().isEmpty());
95+
}
96+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package de.fraunhofer.iem.coveragedoclet;
2+
3+
import java.text.DecimalFormat;
4+
5+
public class CoverageReport {
6+
7+
private int totalClasses;
8+
private int documentedClasses;
9+
private int totalMethods;
10+
private int documentedMethods;
11+
12+
public int getTotalClasses() {
13+
return totalClasses;
14+
}
15+
16+
public void incrementTotalClasses() {
17+
this.totalClasses++;
18+
}
19+
20+
public int getDocumentedClasses() {
21+
return documentedClasses;
22+
}
23+
24+
public void incrementDocumentedClasses() {
25+
this.documentedClasses++;
26+
}
27+
28+
public int getTotalMethods() {
29+
return totalMethods;
30+
}
31+
32+
public void incrementTotalMethods() {
33+
this.totalMethods++;
34+
}
35+
36+
public int getDocumentedMethods() {
37+
return documentedMethods;
38+
}
39+
40+
public void incrementDocumentedMethods() {
41+
this.documentedMethods++;
42+
}
43+
44+
public double getCoveragePercent() {
45+
46+
double covPercent = (double) (documentedClasses + documentedMethods) / (totalClasses + totalMethods) * 100.0;
47+
DecimalFormat format_2Places = new DecimalFormat("0.00");
48+
49+
return Double.parseDouble(format_2Places.format(covPercent));
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "DocCoverageReport{" +
55+
"totalClasses=" + totalClasses +
56+
", documentedClasses=" + documentedClasses +
57+
", totalMethods=" + totalMethods +
58+
", documentedMethods=" + documentedMethods +
59+
", coveragePercent=" + getCoveragePercent() +
60+
'}';
61+
}
62+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.fraunhofer.iem.coveragedoclet;
2+
3+
import javax.tools.DocumentationTool;
4+
import javax.tools.ToolProvider;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
String[] docletArgs = new String[]{
11+
"-doclet", CoverageDoclet.class.getName(),
12+
"-docletpath", "target/classes/",
13+
"-sourcepath", "source-jars/",
14+
"-subpackages", "org.owasp.encoder",
15+
};
16+
17+
DocumentationTool docTool = ToolProvider.getSystemDocumentationTool();
18+
docTool.run(System.in, System.out, System.err, docletArgs);
19+
}
20+
}

swan-javadoc-coverage/.travis.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)