View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.site;
21  
22  import java.beans.Introspector;
23  import java.util.Collections;
24  import java.util.LinkedHashMap;
25  import java.util.Map;
26  import java.util.regex.Pattern;
27  
28  import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
29  import com.puppycrawl.tools.checkstyle.api.DetailAST;
30  import com.puppycrawl.tools.checkstyle.api.DetailNode;
31  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
34  import com.puppycrawl.tools.checkstyle.utils.BlockCommentPosition;
35  
36  /**
37   * Class for scraping class javadoc and all property setter javadocs from the
38   * given checkstyle module.
39   */
40  @FileStatefulCheck
41  public class ClassAndPropertiesSettersJavadocScraper extends AbstractJavadocCheck {
42  
43      /**
44       * Map of scraped javadocs - name of property, javadoc detail node.
45       * The class javadoc is stored too, with the key being the module name.
46       */
47      private static final Map<String, DetailNode> JAVADOC_FOR_MODULE_OR_PROPERTY =
48              new LinkedHashMap<>();
49  
50      /** Name of the module being scraped. */
51      private static String moduleName = "";
52  
53      /**
54       * Initialize the scraper. Clears static context and sets the module name.
55       *
56       * @param newModuleName the module name.
57       */
58      public static void initialize(String newModuleName) {
59          JAVADOC_FOR_MODULE_OR_PROPERTY.clear();
60          moduleName = newModuleName;
61      }
62  
63      /**
64       * Get the module or property javadocs map.
65       *
66       * @return the javadocs.
67       */
68      public static Map<String, DetailNode> getJavadocsForModuleOrProperty() {
69          return Collections.unmodifiableMap(JAVADOC_FOR_MODULE_OR_PROPERTY);
70      }
71  
72      @Override
73      public int[] getDefaultJavadocTokens() {
74          return new int[] {
75              JavadocTokenTypes.JAVADOC,
76          };
77      }
78  
79      @Override
80      public void visitJavadocToken(DetailNode ast) {
81          final DetailAST blockCommentAst = getBlockCommentAst();
82          if (BlockCommentPosition.isOnMethod(blockCommentAst)) {
83              final DetailAST methodDef = getParentAst(blockCommentAst, TokenTypes.METHOD_DEF);
84              if (methodDef != null
85                      && isSetterMethod(methodDef)
86                      && isMethodOfScrapedModule(methodDef)) {
87                  final String methodName = methodDef.findFirstToken(TokenTypes.IDENT).getText();
88                  final String propertyName = getPropertyName(methodName);
89                  JAVADOC_FOR_MODULE_OR_PROPERTY.put(propertyName, ast);
90              }
91  
92          }
93          else if (BlockCommentPosition.isOnClass(blockCommentAst)) {
94              final DetailAST classDef = getParentAst(blockCommentAst, TokenTypes.CLASS_DEF);
95              if (classDef != null) {
96                  final String className = classDef.findFirstToken(TokenTypes.IDENT).getText();
97                  if (className.equals(moduleName)) {
98                      JAVADOC_FOR_MODULE_OR_PROPERTY.put(moduleName, ast);
99                  }
100             }
101         }
102     }
103 
104     /**
105      * Checks if the given method is a method of the module being scraped. Traverses
106      * parent nodes until it finds the class definition and checks if the class name
107      * is the same as the module name. We want to avoid scraping javadocs from
108      * inner classes.
109      *
110      * @param methodDef the method definition.
111      * @return true if the method is a method of the given module, false otherwise.
112      */
113     private static boolean isMethodOfScrapedModule(DetailAST methodDef) {
114         final DetailAST classDef = getParentAst(methodDef, TokenTypes.CLASS_DEF);
115 
116         boolean isMethodOfModule = false;
117         if (classDef != null) {
118             final String className = classDef.findFirstToken(TokenTypes.IDENT).getText();
119             isMethodOfModule = className.equals(moduleName);
120         }
121 
122         return isMethodOfModule;
123     }
124 
125     /**
126      * Get the parent node of the given type. Traverses up the tree until it finds
127      * the given type.
128      *
129      * @param ast the node to start traversing from.
130      * @param type the type of the parent node to find.
131      * @return the parent node of the given type, or null if not found.
132      */
133     private static DetailAST getParentAst(DetailAST ast, int type) {
134         DetailAST node = ast.getParent();
135 
136         while (node != null && node.getType() != type) {
137             node = node.getParent();
138         }
139 
140         return node;
141     }
142 
143     /**
144      * Get the property name from the setter method name. For example, getPropertyName("setFoo")
145      * returns "foo". This method removes the "set" prefix and decapitalizes the first letter
146      * of the property name.
147      *
148      * @param setterName the setter method name.
149      * @return the property name.
150      */
151     private static String getPropertyName(String setterName) {
152         return Introspector.decapitalize(setterName.substring("set".length()));
153     }
154 
155     /**
156      * Returns whether an AST represents a setter method.
157      *
158      * @param ast the AST to check with
159      * @return whether the AST represents a setter method
160      */
161     private static boolean isSetterMethod(DetailAST ast) {
162         boolean setterMethod = false;
163 
164         if (ast.getType() == TokenTypes.METHOD_DEF) {
165             final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
166             final String name = type.getNextSibling().getText();
167             final Pattern setterPattern = Pattern.compile("^set[A-Z].*");
168 
169             setterMethod = setterPattern.matcher(name).matches();
170         }
171         return setterMethod;
172     }
173 }