001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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     */
050    public HandlerFactory() {
051        register(TokenTypes.CASE_GROUP, CaseHandler.class);
052        register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class);
053        register(TokenTypes.SLIST, SlistHandler.class);
054        register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class);
055        register(TokenTypes.LITERAL_ELSE, ElseHandler.class);
056        register(TokenTypes.LITERAL_IF, IfHandler.class);
057        register(TokenTypes.LITERAL_TRY, TryHandler.class);
058        register(TokenTypes.LITERAL_CATCH, CatchHandler.class);
059        register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class);
060        register(TokenTypes.LITERAL_DO, DoWhileHandler.class);
061        register(TokenTypes.LITERAL_WHILE, WhileHandler.class);
062        register(TokenTypes.LITERAL_FOR, ForHandler.class);
063        register(TokenTypes.METHOD_DEF, MethodDefHandler.class);
064        register(TokenTypes.CTOR_DEF, MethodDefHandler.class);
065        register(TokenTypes.CLASS_DEF, ClassDefHandler.class);
066        register(TokenTypes.ENUM_DEF, ClassDefHandler.class);
067        register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class);
068        register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class);
069        register(TokenTypes.IMPORT, ImportHandler.class);
070        register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class);
071        register(TokenTypes.ANNOTATION_ARRAY_INIT, AnnotationArrayInitHandler.class);
072        register(TokenTypes.METHOD_CALL, MethodCallHandler.class);
073        register(TokenTypes.CTOR_CALL, MethodCallHandler.class);
074        register(TokenTypes.SUPER_CTOR_CALL, MethodCallHandler.class);
075        register(TokenTypes.LABELED_STAT, LabelHandler.class);
076        register(TokenTypes.STATIC_INIT, StaticInitHandler.class);
077        register(TokenTypes.INSTANCE_INIT, SlistHandler.class);
078        register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class);
079        register(TokenTypes.LITERAL_NEW, NewHandler.class);
080        register(TokenTypes.INDEX_OP, IndexHandler.class);
081        register(TokenTypes.LITERAL_SYNCHRONIZED, SynchronizedHandler.class);
082        register(TokenTypes.LAMBDA, LambdaHandler.class);
083        register(TokenTypes.ANNOTATION_DEF, ClassDefHandler.class);
084        register(TokenTypes.ANNOTATION_FIELD_DEF, MethodDefHandler.class);
085        register(TokenTypes.SWITCH_RULE, SwitchRuleHandler.class);
086        register(TokenTypes.LITERAL_YIELD, YieldHandler.class);
087        register(TokenTypes.RECORD_DEF, ClassDefHandler.class);
088        register(TokenTypes.COMPACT_CTOR_DEF, MethodDefHandler.class);
089    }
090
091    /**
092     * Registers a handler.
093     *
094     * @param <T> type of the handler class object.
095     * @param type
096     *                type from TokenTypes
097     * @param handlerClass
098     *                the handler to register
099     */
100    private <T> void register(int type, Class<T> handlerClass) {
101        final Constructor<T> ctor = CommonUtil.getConstructor(handlerClass,
102                IndentationCheck.class,
103                // current AST
104                DetailAST.class,
105                // parent
106                AbstractExpressionHandler.class
107        );
108        typeHandlers.put(type, ctor);
109    }
110
111    /**
112     * Returns true if this type (form TokenTypes) is handled.
113     *
114     * @param type type from TokenTypes
115     * @return true if handler is registered, false otherwise
116     */
117    public boolean isHandledType(int type) {
118        final Set<Integer> typeSet = typeHandlers.keySet();
119        return typeSet.contains(type);
120    }
121
122    /**
123     * Gets list of registered handler types.
124     *
125     * @return int[] of TokenType types
126     */
127    public int[] getHandledTypes() {
128        final Set<Integer> typeSet = typeHandlers.keySet();
129        final int[] types = new int[typeSet.size()];
130        int index = 0;
131        for (final Integer val : typeSet) {
132            types[index] = val;
133            index++;
134        }
135
136        return types;
137    }
138
139    /**
140     * Get the handler for an AST.
141     *
142     * @param indentCheck   the indentation check
143     * @param ast           ast to handle
144     * @param parent        the handler parent of this AST
145     *
146     * @return the ExpressionHandler for ast
147     */
148    public AbstractExpressionHandler getHandler(IndentationCheck indentCheck,
149        DetailAST ast, AbstractExpressionHandler parent) {
150        final AbstractExpressionHandler resultHandler;
151        final AbstractExpressionHandler handler =
152            createdHandlers.get(ast);
153        if (handler != null) {
154            resultHandler = handler;
155        }
156        else if (ast.getType() == TokenTypes.METHOD_CALL) {
157            resultHandler = createMethodCallHandler(indentCheck, ast, parent);
158        }
159        else {
160            final Constructor<?> handlerCtor = typeHandlers.get(ast.getType());
161            resultHandler = (AbstractExpressionHandler) CommonUtil.invokeConstructor(
162                handlerCtor, indentCheck, ast, parent);
163        }
164        return resultHandler;
165    }
166
167    /**
168     * Create new instance of handler for METHOD_CALL.
169     *
170     * @param indentCheck   the indentation check
171     * @param ast           ast to handle
172     * @param parent        the handler parent of this AST
173     *
174     * @return new instance.
175     */
176    private AbstractExpressionHandler createMethodCallHandler(IndentationCheck indentCheck,
177        DetailAST ast, AbstractExpressionHandler parent) {
178        DetailAST astNode = ast.getFirstChild();
179        while (astNode.getType() == TokenTypes.DOT) {
180            astNode = astNode.getFirstChild();
181        }
182        AbstractExpressionHandler theParent = parent;
183        if (isHandledType(astNode.getType())) {
184            theParent = getHandler(indentCheck, astNode, theParent);
185            createdHandlers.put(astNode, theParent);
186        }
187        return new MethodCallHandler(indentCheck, ast, theParent);
188    }
189
190    /** Clears cache of created handlers. */
191    public void clearCreatedHandlers() {
192        createdHandlers.clear();
193    }
194
195}