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