1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.site;
21
22 import java.beans.Introspector;
23 import java.util.regex.Pattern;
24
25 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.DetailNode;
28 import com.puppycrawl.tools.checkstyle.api.JavadocCommentsTokenTypes;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30 import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
31 import com.puppycrawl.tools.checkstyle.utils.BlockCommentPosition;
32 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
33
34
35
36
37
38 @FileStatefulCheck
39 public class ClassAndPropertiesSettersJavadocScraper extends AbstractJavadocCheck {
40
41
42 private static String moduleName = "";
43
44
45
46
47
48
49 public static void initialize(String newModuleName) {
50 JavadocScraperResultUtil.clearData();
51 moduleName = newModuleName;
52 }
53
54 @Override
55 public int[] getDefaultJavadocTokens() {
56 return new int[] {
57 JavadocCommentsTokenTypes.JAVADOC_CONTENT,
58 };
59 }
60
61 @Override
62 public void visitJavadocToken(DetailNode ast) {
63 final DetailAST blockCommentAst = getBlockCommentAst();
64 if (BlockCommentPosition.isOnMethod(blockCommentAst)) {
65 final DetailAST methodDef = getParentAst(blockCommentAst, TokenTypes.METHOD_DEF);
66 if (methodDef != null
67 && isSetterMethod(methodDef)
68 && isMethodOfScrapedModule(methodDef)) {
69 final String methodName = TokenUtil.getIdent(methodDef).getText();
70 final String propertyName = getPropertyName(methodName);
71 JavadocScraperResultUtil.putPropertyJavadocNode(propertyName, ast);
72 }
73
74 }
75 else if (BlockCommentPosition.isOnClass(blockCommentAst)) {
76 final DetailAST classDef = getParentAst(blockCommentAst, TokenTypes.CLASS_DEF);
77 if (classDef != null) {
78 final String className = TokenUtil.getIdent(classDef).getText();
79 if (className.equals(moduleName)) {
80 JavadocScraperResultUtil.setModuleJavadocNode(ast);
81 }
82 }
83 }
84 }
85
86
87
88
89
90
91
92
93
94
95 private static boolean isMethodOfScrapedModule(DetailAST methodDef) {
96 final DetailAST classDef = getParentAst(methodDef, TokenTypes.CLASS_DEF);
97
98 boolean isMethodOfModule = false;
99 if (classDef != null) {
100 final String className = TokenUtil.getIdent(classDef).getText();
101 isMethodOfModule = className.equals(moduleName);
102 }
103
104 return isMethodOfModule;
105 }
106
107
108
109
110
111
112
113
114
115 private static DetailAST getParentAst(DetailAST ast, int type) {
116 DetailAST node = ast.getParent();
117
118 while (node != null && node.getType() != type) {
119 node = node.getParent();
120 }
121
122 return node;
123 }
124
125
126
127
128
129
130
131
132
133 private static String getPropertyName(String setterName) {
134 return Introspector.decapitalize(setterName.substring("set".length()));
135 }
136
137
138
139
140
141
142
143 private static boolean isSetterMethod(DetailAST ast) {
144 boolean setterMethod = false;
145
146 if (ast.getType() == TokenTypes.METHOD_DEF) {
147 final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
148 final String name = type.getNextSibling().getText();
149 final Pattern setterPattern = Pattern.compile("^set[A-Z].*");
150
151 setterMethod = setterPattern.matcher(name).matches();
152 }
153 return setterMethod;
154 }
155 }