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.checks.indentation;
21  
22  import java.lang.reflect.Constructor;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import com.puppycrawl.tools.checkstyle.api.DetailAST;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  /**
32   * Factory for handlers. Looks up constructor via reflection.
33   *
34   */
35  public class HandlerFactory {
36  
37      /**
38       * Registered handlers.
39       */
40      private final Map<Integer, Constructor<?>> typeHandlers = new HashMap<>();
41  
42      /** Cache for created method call handlers. */
43      private final Map<DetailAST, AbstractExpressionHandler> createdHandlers = new HashMap<>();
44  
45      /**
46       * Creates a HandlerFactory.
47       *
48       * @noinspection OverlyCoupledMethod
49       * @noinspectionreason OverlyCoupledMethod - complex nature of indentation check
50       *      requires this coupling
51       */
52      public HandlerFactory() {
53          register(TokenTypes.CASE_GROUP, CaseHandler.class);
54          register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class);
55          register(TokenTypes.SLIST, SlistHandler.class);
56          register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class);
57          register(TokenTypes.LITERAL_ELSE, ElseHandler.class);
58          register(TokenTypes.LITERAL_IF, IfHandler.class);
59          register(TokenTypes.LITERAL_TRY, TryHandler.class);
60          register(TokenTypes.LITERAL_CATCH, CatchHandler.class);
61          register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class);
62          register(TokenTypes.LITERAL_DO, DoWhileHandler.class);
63          register(TokenTypes.LITERAL_WHILE, WhileHandler.class);
64          register(TokenTypes.LITERAL_FOR, ForHandler.class);
65          register(TokenTypes.METHOD_DEF, MethodDefHandler.class);
66          register(TokenTypes.CTOR_DEF, MethodDefHandler.class);
67          register(TokenTypes.CLASS_DEF, ClassDefHandler.class);
68          register(TokenTypes.ENUM_DEF, ClassDefHandler.class);
69          register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class);
70          register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class);
71          register(TokenTypes.IMPORT, ImportHandler.class);
72          register(TokenTypes.MODULE_IMPORT, ImportHandler.class);
73          register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class);
74          register(TokenTypes.ANNOTATION_ARRAY_INIT, AnnotationArrayInitHandler.class);
75          register(TokenTypes.METHOD_CALL, MethodCallHandler.class);
76          register(TokenTypes.CTOR_CALL, MethodCallHandler.class);
77          register(TokenTypes.SUPER_CTOR_CALL, MethodCallHandler.class);
78          register(TokenTypes.LABELED_STAT, LabelHandler.class);
79          register(TokenTypes.STATIC_INIT, StaticInitHandler.class);
80          register(TokenTypes.INSTANCE_INIT, SlistHandler.class);
81          register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class);
82          register(TokenTypes.LITERAL_NEW, NewHandler.class);
83          register(TokenTypes.INDEX_OP, IndexHandler.class);
84          register(TokenTypes.LITERAL_SYNCHRONIZED, SynchronizedHandler.class);
85          register(TokenTypes.LAMBDA, LambdaHandler.class);
86          register(TokenTypes.ANNOTATION_DEF, ClassDefHandler.class);
87          register(TokenTypes.ANNOTATION_FIELD_DEF, MethodDefHandler.class);
88          register(TokenTypes.SWITCH_RULE, SwitchRuleHandler.class);
89          register(TokenTypes.LITERAL_YIELD, YieldHandler.class);
90          register(TokenTypes.RECORD_DEF, ClassDefHandler.class);
91          register(TokenTypes.COMPACT_CTOR_DEF, MethodDefHandler.class);
92      }
93  
94      /**
95       * Registers a handler.
96       *
97       * @param <T> type of the handler class object.
98       * @param type
99       *                type from TokenTypes
100      * @param handlerClass
101      *                the handler to register
102      */
103     private <T> void register(int type, Class<T> handlerClass) {
104         final Constructor<T> ctor = CommonUtil.getConstructor(handlerClass,
105                 IndentationCheck.class,
106                 // current AST
107                 DetailAST.class,
108                 // parent
109                 AbstractExpressionHandler.class
110         );
111         typeHandlers.put(type, ctor);
112     }
113 
114     /**
115      * Returns true if this type (form TokenTypes) is handled.
116      *
117      * @param type type from TokenTypes
118      * @return true if handler is registered, false otherwise
119      */
120     public boolean isHandledType(int type) {
121         final Set<Integer> typeSet = typeHandlers.keySet();
122         return typeSet.contains(type);
123     }
124 
125     /**
126      * Gets list of registered handler types.
127      *
128      * @return int[] of TokenType types
129      */
130     public int[] getHandledTypes() {
131         final Set<Integer> typeSet = typeHandlers.keySet();
132         final int[] types = new int[typeSet.size()];
133         int index = 0;
134         for (final Integer val : typeSet) {
135             types[index] = val;
136             index++;
137         }
138 
139         return types;
140     }
141 
142     /**
143      * Get the handler for an AST.
144      *
145      * @param indentCheck   the indentation check
146      * @param ast           ast to handle
147      * @param parent        the handler parent of this AST
148      *
149      * @return the ExpressionHandler for ast
150      */
151     public AbstractExpressionHandler getHandler(IndentationCheck indentCheck,
152         DetailAST ast, AbstractExpressionHandler parent) {
153         final AbstractExpressionHandler resultHandler;
154         final AbstractExpressionHandler handler =
155             createdHandlers.get(ast);
156         if (handler != null) {
157             resultHandler = handler;
158         }
159         else if (ast.getType() == TokenTypes.METHOD_CALL) {
160             resultHandler = createMethodCallHandler(indentCheck, ast, parent);
161         }
162         else {
163             final Constructor<?> handlerCtor = typeHandlers.get(ast.getType());
164             resultHandler = (AbstractExpressionHandler) CommonUtil.invokeConstructor(
165                 handlerCtor, indentCheck, ast, parent);
166         }
167         return resultHandler;
168     }
169 
170     /**
171      * Create new instance of handler for METHOD_CALL.
172      *
173      * @param indentCheck   the indentation check
174      * @param ast           ast to handle
175      * @param parent        the handler parent of this AST
176      *
177      * @return new instance.
178      */
179     private AbstractExpressionHandler createMethodCallHandler(IndentationCheck indentCheck,
180         DetailAST ast, AbstractExpressionHandler parent) {
181         DetailAST astNode = ast.getFirstChild();
182         while (astNode.getType() == TokenTypes.DOT) {
183             astNode = astNode.getFirstChild();
184         }
185         AbstractExpressionHandler theParent = parent;
186         if (isHandledType(astNode.getType())) {
187             theParent = getHandler(indentCheck, astNode, theParent);
188             createdHandlers.put(astNode, theParent);
189         }
190         return new MethodCallHandler(indentCheck, ast, theParent);
191     }
192 
193     /** Clears cache of created handlers. */
194     public void clearCreatedHandlers() {
195         createdHandlers.clear();
196     }
197 
198 }