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