1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
|
---|
5 | import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
---|
6 | import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
---|
7 | import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Checks that there is Javadoc for every top level class, interface or enum.
|
---|
11 | */
|
---|
12 | public class TopLevelJavadocCheck extends AbstractCheck
|
---|
13 | {
|
---|
14 | private boolean foundTopLevelClass;
|
---|
15 |
|
---|
16 | @Override
|
---|
17 | public int[] getDefaultTokens()
|
---|
18 | {
|
---|
19 | return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF};
|
---|
20 | }
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | public boolean isCommentNodesRequired() {
|
---|
24 | return true;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public void beginTree(DetailAST rootAST) {
|
---|
29 | foundTopLevelClass = false;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public void finishTree(DetailAST rootAST) {
|
---|
34 | if (!foundTopLevelClass) {
|
---|
35 | this.log(rootAST.getLineNo(), "assertion failure: unable to find toplevel class or interface");
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private boolean hasJavadoc(DetailAST ast) {
|
---|
40 | DetailAST blockCommentBegin = ast.findFirstToken(TokenTypes.BLOCK_COMMENT_BEGIN);
|
---|
41 | if (blockCommentBegin == null) {
|
---|
42 | DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
|
---|
43 | if (modifiers == null)
|
---|
44 | return false;
|
---|
45 | blockCommentBegin = modifiers.findFirstToken(TokenTypes.BLOCK_COMMENT_BEGIN);
|
---|
46 | if (blockCommentBegin == null) {
|
---|
47 | DetailAST annotation = modifiers.findFirstToken(TokenTypes.ANNOTATION);
|
---|
48 | if (annotation == null)
|
---|
49 | return false;
|
---|
50 | blockCommentBegin = annotation.findFirstToken(TokenTypes.BLOCK_COMMENT_BEGIN);
|
---|
51 | if (blockCommentBegin == null)
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | return JavadocUtils.isJavadocComment(blockCommentBegin);
|
---|
56 | //DetailNode javadocTree = new JavadocDetailNodeParser().parseJavadocAsDetailNode(blockCommentBegin).getTree();
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void visitToken(DetailAST ast)
|
---|
61 | {
|
---|
62 | DetailAST parent = ast.getParent();
|
---|
63 | if (parent == null || parent.getType() == TokenTypes.EOF) {
|
---|
64 | foundTopLevelClass = true;
|
---|
65 | if (!hasJavadoc(ast)) {
|
---|
66 | this.log(ast.getLineNo(), "no Javadoc for top level class or interface");
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|