1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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      /** Creates a HandlerFactory. */
46      public HandlerFactory() {
47          register(TokenTypes.CASE_GROUP, CaseHandler.class);
48          register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class);
49          register(TokenTypes.SLIST, SlistHandler.class);
50          register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class);
51          register(TokenTypes.LITERAL_ELSE, ElseHandler.class);
52          register(TokenTypes.LITERAL_IF, IfHandler.class);
53          register(TokenTypes.LITERAL_TRY, TryHandler.class);
54          register(TokenTypes.LITERAL_CATCH, CatchHandler.class);
55          register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class);
56          register(TokenTypes.LITERAL_DO, DoWhileHandler.class);
57          register(TokenTypes.LITERAL_WHILE, WhileHandler.class);
58          register(TokenTypes.LITERAL_FOR, ForHandler.class);
59          register(TokenTypes.METHOD_DEF, MethodDefHandler.class);
60          register(TokenTypes.CTOR_DEF, MethodDefHandler.class);
61          register(TokenTypes.CLASS_DEF, ClassDefHandler.class);
62          register(TokenTypes.ENUM_DEF, ClassDefHandler.class);
63          register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class);
64          register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class);
65          register(TokenTypes.IMPORT, ImportHandler.class);
66          register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class);
67          register(TokenTypes.METHOD_CALL, MethodCallHandler.class);
68          register(TokenTypes.CTOR_CALL, MethodCallHandler.class);
69          register(TokenTypes.SUPER_CTOR_CALL, MethodCallHandler.class);
70          register(TokenTypes.LABELED_STAT, LabelHandler.class);
71          register(TokenTypes.STATIC_INIT, StaticInitHandler.class);
72          register(TokenTypes.INSTANCE_INIT, SlistHandler.class);
73          register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class);
74          register(TokenTypes.LITERAL_NEW, NewHandler.class);
75          register(TokenTypes.INDEX_OP, IndexHandler.class);
76          register(TokenTypes.LITERAL_SYNCHRONIZED, SynchronizedHandler.class);
77          register(TokenTypes.LAMBDA, LambdaHandler.class);
78          register(TokenTypes.ANNOTATION_DEF, ClassDefHandler.class);
79          register(TokenTypes.ANNOTATION_FIELD_DEF, MethodDefHandler.class);
80      }
81  
82      /**
83       * Registers a handler.
84       *
85       * @param type
86       *                type from TokenTypes
87       * @param handlerClass
88       *                the handler to register
89       * @param <T> type of the handler class object.
90       */
91      private <T> void register(int type, Class<T> handlerClass) {
92          final Constructor<T> ctor = CommonUtil.getConstructor(handlerClass,
93                  IndentationCheck.class,
94                  // current AST
95                  DetailAST.class,
96                  // parent
97                  AbstractExpressionHandler.class
98          );
99          typeHandlers.put(type, ctor);
100     }
101 
102     /**
103      * Returns true if this type (form TokenTypes) is handled.
104      *
105      * @param type type from TokenTypes
106      * @return true if handler is registered, false otherwise
107      */
108     public boolean isHandledType(int type) {
109         final Set<Integer> typeSet = typeHandlers.keySet();
110         return typeSet.contains(type);
111     }
112 
113     /**
114      * Gets list of registered handler types.
115      *
116      * @return int[] of TokenType types
117      */
118     public int[] getHandledTypes() {
119         final Set<Integer> typeSet = typeHandlers.keySet();
120         final int[] types = new int[typeSet.size()];
121         int index = 0;
122         for (final Integer val : typeSet) {
123             types[index] = val;
124             index++;
125         }
126 
127         return types;
128     }
129 
130     /**
131      * Get the handler for an AST.
132      *
133      * @param indentCheck   the indentation check
134      * @param ast           ast to handle
135      * @param parent        the handler parent of this AST
136      *
137      * @return the ExpressionHandler for ast
138      */
139     public AbstractExpressionHandler getHandler(IndentationCheck indentCheck,
140         DetailAST ast, AbstractExpressionHandler parent) {
141         final AbstractExpressionHandler resultHandler;
142         final AbstractExpressionHandler handler =
143             createdHandlers.get(ast);
144         if (handler != null) {
145             resultHandler = handler;
146         }
147         else if (ast.getType() == TokenTypes.METHOD_CALL) {
148             resultHandler = createMethodCallHandler(indentCheck, ast, parent);
149         }
150         else {
151             final Constructor<?> handlerCtor = typeHandlers.get(ast.getType());
152             resultHandler = (AbstractExpressionHandler) CommonUtil.invokeConstructor(
153                 handlerCtor, indentCheck, ast, parent);
154         }
155         return resultHandler;
156     }
157 
158     /**
159      * Create new instance of handler for METHOD_CALL.
160      *
161      * @param indentCheck   the indentation check
162      * @param ast           ast to handle
163      * @param parent        the handler parent of this AST
164      *
165      * @return new instance.
166      */
167     private AbstractExpressionHandler createMethodCallHandler(IndentationCheck indentCheck,
168         DetailAST ast, AbstractExpressionHandler parent) {
169         DetailAST astNode = ast.getFirstChild();
170         while (astNode.getType() == TokenTypes.DOT) {
171             astNode = astNode.getFirstChild();
172         }
173         AbstractExpressionHandler theParent = parent;
174         if (isHandledType(astNode.getType())) {
175             theParent = getHandler(indentCheck, astNode, theParent);
176             createdHandlers.put(astNode, theParent);
177         }
178         return new MethodCallHandler(indentCheck, ast, theParent);
179     }
180 
181     /** Clears cache of created handlers. */
182     public void clearCreatedHandlers() {
183         createdHandlers.clear();
184     }
185 
186 }