001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.indentation;
021
022import java.lang.reflect.Constructor;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.Set;
026
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
030
031/**
032 * Factory for handlers. Looks up constructor via reflection.
033 *
034 */
035public class HandlerFactory {
036
037    /**
038     * Registered handlers.
039     */
040    private final Map<Integer, Constructor<?>> typeHandlers = new HashMap<>();
041
042    /** Cache for created method call handlers. */
043    private final Map<DetailAST, AbstractExpressionHandler> createdHandlers = new HashMap<>();
044
045    /**
046     * Creates a HandlerFactory.
047     *
048     * @noinspection OverlyCoupledMethod
049     * @noinspectionreason OverlyCoupledMethod - complex nature of indentation check
050     *      requires this coupling
051     */
052    public HandlerFactory() {
053        register(TokenTypes.CASE_GROUP, CaseHandler.class);
054        register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class);
055        register(TokenTypes.SLIST, SlistHandler.class);
056        register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class);
057        register(TokenTypes.LITERAL_ELSE, ElseHandler.class);
058        register(TokenTypes.LITERAL_IF, IfHandler.class);
059        register(TokenTypes.LITERAL_TRY, TryHandler.class);
060        register(TokenTypes.LITERAL_CATCH, CatchHandler.class);
061        register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class);
062        register(TokenTypes.LITERAL_DO, DoWhileHandler.class);
063        register(TokenTypes.LITERAL_WHILE, WhileHandler.class);
064        register(TokenTypes.LITERAL_FOR, ForHandler.class);
065        register(TokenTypes.METHOD_DEF, MethodDefHandler.class);
066        register(TokenTypes.CTOR_DEF, MethodDefHandler.class);
067        register(TokenTypes.CLASS_DEF, ClassDefHandler.class);
068        register(TokenTypes.ENUM_DEF, ClassDefHandler.class);
069        register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class);
070        register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class);
071        register(TokenTypes.IMPORT, ImportHandler.class);
072        register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class);
073        register(TokenTypes.ANNOTATION_ARRAY_INIT, AnnotationArrayInitHandler.class);
074        register(TokenTypes.METHOD_CALL, MethodCallHandler.class);
075        register(TokenTypes.CTOR_CALL, MethodCallHandler.class);
076        register(TokenTypes.SUPER_CTOR_CALL, MethodCallHandler.class);
077        register(TokenTypes.LABELED_STAT, LabelHandler.class);
078        register(TokenTypes.STATIC_INIT, StaticInitHandler.class);
079        register(TokenTypes.INSTANCE_INIT, SlistHandler.class);
080        register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class);
081        register(TokenTypes.LITERAL_NEW, NewHandler.class);
082        register(TokenTypes.INDEX_OP, IndexHandler.class);
083        register(TokenTypes.LITERAL_SYNCHRONIZED, SynchronizedHandler.class);
084        register(TokenTypes.LAMBDA, LambdaHandler.class);
085        register(TokenTypes.ANNOTATION_DEF, ClassDefHandler.class);
086        register(TokenTypes.ANNOTATION_FIELD_DEF, MethodDefHandler.class);
087        register(TokenTypes.SWITCH_RULE, SwitchRuleHandler.class);
088        register(TokenTypes.LITERAL_YIELD, YieldHandler.class);
089        register(TokenTypes.RECORD_DEF, ClassDefHandler.class);
090        register(TokenTypes.COMPACT_CTOR_DEF, MethodDefHandler.class);
091    }
092
093    /**
094     * Registers a handler.
095     *
096     * @param <T> type of the handler class object.
097     * @param type
098     *                type from TokenTypes
099     * @param handlerClass
100     *                the handler to register
101     */
102    private <T> void register(int type, Class<T> handlerClass) {
103        final Constructor<T> ctor = CommonUtil.getConstructor(handlerClass,
104                IndentationCheck.class,
105                // current AST
106                DetailAST.class,
107                // parent
108                AbstractExpressionHandler.class
109        );
110        typeHandlers.put(type, ctor);
111    }
112
113    /**
114     * Returns true if this type (form TokenTypes) is handled.
115     *
116     * @param type type from TokenTypes
117     * @return true if handler is registered, false otherwise
118     */
119    public boolean isHandledType(int type) {
120        final Set<Integer> typeSet = typeHandlers.keySet();
121        return typeSet.contains(type);
122    }
123
124    /**
125     * Gets list of registered handler types.
126     *
127     * @return int[] of TokenType types
128     */
129    public int[] getHandledTypes() {
130        final Set<Integer> typeSet = typeHandlers.keySet();
131        final int[] types = new int[typeSet.size()];
132        int index = 0;
133        for (final Integer val : typeSet) {
134            types[index] = val;
135            index++;
136        }
137
138        return types;
139    }
140
141    /**
142     * Get the handler for an AST.
143     *
144     * @param indentCheck   the indentation check
145     * @param ast           ast to handle
146     * @param parent        the handler parent of this AST
147     *
148     * @return the ExpressionHandler for ast
149     */
150    public AbstractExpressionHandler getHandler(IndentationCheck indentCheck,
151        DetailAST ast, AbstractExpressionHandler parent) {
152        final AbstractExpressionHandler resultHandler;
153        final AbstractExpressionHandler handler =
154            createdHandlers.get(ast);
155        if (handler != null) {
156            resultHandler = handler;
157        }
158        else if (ast.getType() == TokenTypes.METHOD_CALL) {
159            resultHandler = createMethodCallHandler(indentCheck, ast, parent);
160        }
161        else {
162            final Constructor<?> handlerCtor = typeHandlers.get(ast.getType());
163            resultHandler = (AbstractExpressionHandler) CommonUtil.invokeConstructor(
164                handlerCtor, indentCheck, ast, parent);
165        }
166        return resultHandler;
167    }
168
169    /**
170     * Create new instance of handler for METHOD_CALL.
171     *
172     * @param indentCheck   the indentation check
173     * @param ast           ast to handle
174     * @param parent        the handler parent of this AST
175     *
176     * @return new instance.
177     */
178    private AbstractExpressionHandler createMethodCallHandler(IndentationCheck indentCheck,
179        DetailAST ast, AbstractExpressionHandler parent) {
180        DetailAST astNode = ast.getFirstChild();
181        while (astNode.getType() == TokenTypes.DOT) {
182            astNode = astNode.getFirstChild();
183        }
184        AbstractExpressionHandler theParent = parent;
185        if (isHandledType(astNode.getType())) {
186            theParent = getHandler(indentCheck, astNode, theParent);
187            createdHandlers.put(astNode, theParent);
188        }
189        return new MethodCallHandler(indentCheck, ast, theParent);
190    }
191
192    /** Clears cache of created handlers. */
193    public void clearCreatedHandlers() {
194        createdHandlers.clear();
195    }
196
197}