View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.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.JavadocTokenTypes;
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   * Class for scraping class javadoc and all property setter javadocs from the
35   * given checkstyle module.
36   */
37  @FileStatefulCheck
38  public class ClassAndPropertiesSettersJavadocScraper extends AbstractJavadocCheck {
39  
40      /** Name of the module being scraped. */
41      private static String moduleName = "";
42  
43      /**
44       * Initialize the scraper. Clears static context and sets the module name.
45       *
46       * @param newModuleName the module name.
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              JavadocTokenTypes.JAVADOC,
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       * Checks if the given method is a method of the module being scraped. Traverses
87       * parent nodes until it finds the class definition and checks if the class name
88       * is the same as the module name. We want to avoid scraping javadocs from
89       * inner classes.
90       *
91       * @param methodDef the method definition.
92       * @return true if the method is a method of the given module, false otherwise.
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      * Get the parent node of the given type. Traverses up the tree until it finds
108      * the given type.
109      *
110      * @param ast the node to start traversing from.
111      * @param type the type of the parent node to find.
112      * @return the parent node of the given type, or null if not found.
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      * Get the property name from the setter method name. For example, getPropertyName("setFoo")
126      * returns "foo". This method removes the "set" prefix and decapitalizes the first letter
127      * of the property name.
128      *
129      * @param setterName the setter method name.
130      * @return the property name.
131      */
132     private static String getPropertyName(String setterName) {
133         return Introspector.decapitalize(setterName.substring("set".length()));
134     }
135 
136     /**
137      * Returns whether an AST represents a setter method.
138      *
139      * @param ast the AST to check with
140      * @return whether the AST represents a setter method
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 }