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  
33  
34  
35  
36  
37  @FileStatefulCheck
38  public class ClassAndPropertiesSettersJavadocScraper extends AbstractJavadocCheck {
39  
40      
41      private static String moduleName = "";
42  
43      
44  
45  
46  
47  
48      public static void initialize(String newModuleName) {
49          JavadocScraperResultUtil.clearData();
50          moduleName = newModuleName;
51      }
52  
53      @Override
54      public int[] getDefaultJavadocTokens() {
55          return new int[] {
56              JavadocCommentsTokenTypes.JAVADOC_CONTENT,
57          };
58      }
59  
60      @Override
61      public void visitJavadocToken(DetailNode ast) {
62          final DetailAST blockCommentAst = getBlockCommentAst();
63          if (BlockCommentPosition.isOnMethod(blockCommentAst)) {
64              final DetailAST methodDef = getParentAst(blockCommentAst, TokenTypes.METHOD_DEF);
65              if (methodDef != null
66                      && isSetterMethod(methodDef)
67                      && isMethodOfScrapedModule(methodDef)) {
68                  final String methodName = methodDef.findFirstToken(TokenTypes.IDENT).getText();
69                  final String propertyName = getPropertyName(methodName);
70                  JavadocScraperResultUtil.putPropertyJavadocNode(propertyName, ast);
71              }
72  
73          }
74          else if (BlockCommentPosition.isOnClass(blockCommentAst)) {
75              final DetailAST classDef = getParentAst(blockCommentAst, TokenTypes.CLASS_DEF);
76              if (classDef != null) {
77                  final String className = classDef.findFirstToken(TokenTypes.IDENT).getText();
78                  if (className.equals(moduleName)) {
79                      JavadocScraperResultUtil.setModuleJavadocNode(ast);
80                  }
81              }
82          }
83      }
84  
85      
86  
87  
88  
89  
90  
91  
92  
93  
94      private static boolean isMethodOfScrapedModule(DetailAST methodDef) {
95          final DetailAST classDef = getParentAst(methodDef, TokenTypes.CLASS_DEF);
96  
97          boolean isMethodOfModule = false;
98          if (classDef != null) {
99              final String className = classDef.findFirstToken(TokenTypes.IDENT).getText();
100             isMethodOfModule = className.equals(moduleName);
101         }
102 
103         return isMethodOfModule;
104     }
105 
106     
107 
108 
109 
110 
111 
112 
113 
114     private static DetailAST getParentAst(DetailAST ast, int type) {
115         DetailAST node = ast.getParent();
116 
117         while (node != null && node.getType() != type) {
118             node = node.getParent();
119         }
120 
121         return node;
122     }
123 
124     
125 
126 
127 
128 
129 
130 
131 
132     private static String getPropertyName(String setterName) {
133         return Introspector.decapitalize(setterName.substring("set".length()));
134     }
135 
136     
137 
138 
139 
140 
141 
142     private static boolean isSetterMethod(DetailAST ast) {
143         boolean setterMethod = false;
144 
145         if (ast.getType() == TokenTypes.METHOD_DEF) {
146             final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
147             final String name = type.getNextSibling().getText();
148             final Pattern setterPattern = Pattern.compile("^set[A-Z].*");
149 
150             setterMethod = setterPattern.matcher(name).matches();
151         }
152         return setterMethod;
153     }
154 }