001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.Map;
024import java.util.Set;
025
026import com.google.common.collect.Maps;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
030
031/**
032 * Factory for handlers. Looks up constructor via reflection.
033 *
034 * @author jrichard
035 */
036public class HandlerFactory {
037    /**
038     * Registered handlers.
039     */
040    private final Map<Integer, Constructor<?>> typeHandlers =
041        Maps.newHashMap();
042
043    /** Cache for created method call handlers. */
044    private final Map<DetailAST, AbstractExpressionHandler> createdHandlers =
045        Maps.newHashMap();
046
047    /** Creates a HandlerFactory. */
048    public HandlerFactory() {
049        register(TokenTypes.CASE_GROUP, CaseHandler.class);
050        register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class);
051        register(TokenTypes.SLIST, SlistHandler.class);
052        register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class);
053        register(TokenTypes.LITERAL_ELSE, ElseHandler.class);
054        register(TokenTypes.LITERAL_IF, IfHandler.class);
055        register(TokenTypes.LITERAL_TRY, TryHandler.class);
056        register(TokenTypes.LITERAL_CATCH, CatchHandler.class);
057        register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class);
058        register(TokenTypes.LITERAL_DO, DoWhileHandler.class);
059        register(TokenTypes.LITERAL_WHILE, WhileHandler.class);
060        register(TokenTypes.LITERAL_FOR, ForHandler.class);
061        register(TokenTypes.METHOD_DEF, MethodDefHandler.class);
062        register(TokenTypes.CTOR_DEF, MethodDefHandler.class);
063        register(TokenTypes.CLASS_DEF, ClassDefHandler.class);
064        register(TokenTypes.ENUM_DEF, ClassDefHandler.class);
065        register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class);
066        register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class);
067        register(TokenTypes.IMPORT, ImportHandler.class);
068        register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class);
069        register(TokenTypes.METHOD_CALL, MethodCallHandler.class);
070        register(TokenTypes.CTOR_CALL, MethodCallHandler.class);
071        register(TokenTypes.LABELED_STAT, LabelHandler.class);
072        register(TokenTypes.STATIC_INIT, StaticInitHandler.class);
073        register(TokenTypes.INSTANCE_INIT, SlistHandler.class);
074        register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class);
075        register(TokenTypes.LITERAL_NEW, NewHandler.class);
076        register(TokenTypes.INDEX_OP, IndexHandler.class);
077        register(TokenTypes.LITERAL_SYNCHRONIZED, SynchronizedHandler.class);
078        register(TokenTypes.LAMBDA, LambdaHandler.class);
079    }
080
081    /**
082     * Registers a handler.
083     *
084     * @param type
085     *                type from TokenTypes
086     * @param handlerClass
087     *                the handler to register
088     */
089    private void register(int type, Class<?> handlerClass) {
090        final Constructor<?> ctor = CommonUtils.getConstructor(handlerClass,
091                IndentationCheck.class,
092                // current AST
093                DetailAST.class,
094                // parent
095                AbstractExpressionHandler.class
096        );
097        typeHandlers.put(type, ctor);
098    }
099
100    /**
101     * Returns true if this type (form TokenTypes) is handled.
102     *
103     * @param type type from TokenTypes
104     * @return true if handler is registered, false otherwise
105     */
106    public boolean isHandledType(int type) {
107        final Set<Integer> typeSet = typeHandlers.keySet();
108        return typeSet.contains(type);
109    }
110
111    /**
112     * Gets list of registered handler types.
113     *
114     * @return int[] of TokenType types
115     */
116    public int[] getHandledTypes() {
117        final Set<Integer> typeSet = typeHandlers.keySet();
118        final int[] types = new int[typeSet.size()];
119        int index = 0;
120        for (final Integer val : typeSet) {
121            types[index] = val;
122            index++;
123        }
124
125        return types;
126    }
127
128    /**
129     * Get the handler for an AST.
130     *
131     * @param indentCheck   the indentation check
132     * @param ast           ast to handle
133     * @param parent        the handler parent of this AST
134     *
135     * @return the ExpressionHandler for ast
136     */
137    public AbstractExpressionHandler getHandler(IndentationCheck indentCheck,
138        DetailAST ast, AbstractExpressionHandler parent) {
139        AbstractExpressionHandler resultHandler;
140        final AbstractExpressionHandler handler =
141            createdHandlers.get(ast);
142        if (handler != null) {
143            resultHandler = handler;
144        }
145        else if (ast.getType() == TokenTypes.METHOD_CALL) {
146            resultHandler = createMethodCallHandler(indentCheck, ast, parent);
147        }
148        else {
149            final Constructor<?> handlerCtor = typeHandlers.get(ast.getType());
150            resultHandler = (AbstractExpressionHandler) CommonUtils.invokeConstructor(
151                handlerCtor, indentCheck, ast, parent);
152        }
153        return resultHandler;
154    }
155
156    /**
157     * Create new instance of handler for METHOD_CALL.
158     *
159     * @param indentCheck   the indentation check
160     * @param ast           ast to handle
161     * @param parent        the handler parent of this AST
162     *
163     * @return new instance.
164     */
165    private AbstractExpressionHandler createMethodCallHandler(IndentationCheck indentCheck,
166        DetailAST ast, AbstractExpressionHandler parent) {
167        AbstractExpressionHandler theParent = parent;
168        DetailAST astNode = ast.getFirstChild();
169        while (astNode.getType() == TokenTypes.DOT) {
170            astNode = astNode.getFirstChild();
171        }
172        if (isHandledType(astNode.getType())) {
173            theParent = getHandler(indentCheck, astNode, theParent);
174            createdHandlers.put(astNode, theParent);
175        }
176        return new MethodCallHandler(indentCheck, ast, theParent);
177    }
178
179    /** Clears cache of created handlers. */
180    void clearCreatedHandlers() {
181        createdHandlers.clear();
182    }
183}