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+ }
0 commit comments