1 package com.puppycrawl.tools.checkstyle.checks.indentation; 20 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import com.puppycrawl.tools.checkstyle.api.DetailAST; 29 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 39 public class HandlerFactory 40 { 41 42 private static final Log LOG = 43 LogFactory.getLog("com.puppycrawl.tools.checkstyle.checks.indentation"); 44 45 48 private final Map mTypeHandlers = new HashMap (); 49 50 56 private void register(int aType, Class aHandlerClass) 57 { 58 try { 59 final Constructor ctor = aHandlerClass.getConstructor(new Class [] { 60 IndentationCheck.class, 61 DetailAST.class, ExpressionHandler.class, }); 64 mTypeHandlers.put(new Integer (aType), ctor); 65 } 66 catch (final NoSuchMethodException e) { 68 throw new RuntimeException ("couldn't find ctor for " 69 + aHandlerClass); 70 } 71 catch (final SecurityException e) { 72 LOG.debug("couldn't find ctor for " + aHandlerClass, e); 73 throw new RuntimeException ("couldn't find ctor for " 74 + aHandlerClass); 75 } 76 } 78 79 80 public HandlerFactory() 81 { 82 register(TokenTypes.CASE_GROUP, CaseHandler.class); 83 register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class); 84 register(TokenTypes.SLIST, SlistHandler.class); 85 register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class); 86 register(TokenTypes.LITERAL_ELSE, ElseHandler.class); 87 register(TokenTypes.LITERAL_IF, IfHandler.class); 88 register(TokenTypes.LITERAL_TRY, TryHandler.class); 89 register(TokenTypes.LITERAL_CATCH, CatchHandler.class); 90 register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class); 91 register(TokenTypes.LITERAL_DO, DoWhileHandler.class); 92 register(TokenTypes.LITERAL_WHILE, WhileHandler.class); 93 register(TokenTypes.LITERAL_FOR, ForHandler.class); 94 register(TokenTypes.METHOD_DEF, MethodDefHandler.class); 95 register(TokenTypes.CTOR_DEF, MethodDefHandler.class); 96 register(TokenTypes.CLASS_DEF, ClassDefHandler.class); 97 register(TokenTypes.ENUM_DEF, ClassDefHandler.class); 98 register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class); 99 register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class); 100 register(TokenTypes.IMPORT, ImportHandler.class); 101 register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class); 102 register(TokenTypes.METHOD_CALL, MethodCallHandler.class); 103 register(TokenTypes.CTOR_CALL, MethodCallHandler.class); 104 register(TokenTypes.LABELED_STAT, LabelHandler.class); 105 register(TokenTypes.STATIC_INIT, StaticInitHandler.class); 106 register(TokenTypes.INSTANCE_INIT, SlistHandler.class); 107 register(TokenTypes.ASSIGN, AssignHandler.class); 108 register(TokenTypes.PLUS_ASSIGN, AssignHandler.class); 109 register(TokenTypes.MINUS_ASSIGN, AssignHandler.class); 110 register(TokenTypes.STAR_ASSIGN, AssignHandler.class); 111 register(TokenTypes.DIV_ASSIGN, AssignHandler.class); 112 register(TokenTypes.MOD_ASSIGN, AssignHandler.class); 113 register(TokenTypes.SR_ASSIGN, AssignHandler.class); 114 register(TokenTypes.BSR_ASSIGN, AssignHandler.class); 115 register(TokenTypes.SL_ASSIGN, AssignHandler.class); 116 register(TokenTypes.BAND_ASSIGN, AssignHandler.class); 117 register(TokenTypes.BXOR_ASSIGN, AssignHandler.class); 118 register(TokenTypes.BOR_ASSIGN, AssignHandler.class); 119 register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class); 120 register(TokenTypes.LITERAL_NEW, NewHandler.class); 121 } 122 123 129 public boolean isHandledType(int aType) 130 { 131 final Set typeSet = mTypeHandlers.keySet(); 132 return typeSet.contains(new Integer (aType)); 133 } 134 135 140 public int[] getHandledTypes() 141 { 142 final Set typeSet = mTypeHandlers.keySet(); 143 final int[] types = new int[typeSet.size()]; 144 int index = 0; 145 for (final Iterator i = typeSet.iterator(); i.hasNext(); index++) { 146 types[index] = ((Integer ) i.next()).intValue(); 147 } 148 149 return types; 150 } 151 152 161 public ExpressionHandler getHandler(IndentationCheck aIndentCheck, 162 DetailAST aAst, ExpressionHandler aParent) 163 { 164 final ExpressionHandler handler = 165 (ExpressionHandler) mCreatedHandlers.get(aAst); 166 if (handler != null) { 167 return handler; 168 } 169 170 if (aAst.getType() == TokenTypes.METHOD_CALL) { 171 return createMethodCallHandler(aIndentCheck, aAst, aParent); 172 } 173 174 final Integer type = new Integer (aAst.getType()); 175 176 ExpressionHandler expHandler = null; 177 try { 178 final Constructor handlerCtor = 179 (Constructor ) mTypeHandlers.get(type); 180 if (handlerCtor != null) { 181 expHandler = (ExpressionHandler) handlerCtor.newInstance( 182 new Object [] { 183 aIndentCheck, 184 aAst, 185 aParent, 186 } 187 ); 188 } 189 } 190 catch (final InstantiationException e) { 192 LOG.debug("couldn't instantiate constructor for " + aAst, e); 193 throw new RuntimeException ("couldn't instantiate constructor for " 194 + aAst); 195 } 196 catch (final IllegalAccessException e) { 197 LOG.debug("couldn't access constructor for " + aAst, e); 198 throw new RuntimeException ("couldn't access constructor for " 199 + aAst); 200 } 201 catch (final InvocationTargetException e) { 202 LOG.debug("couldn't instantiate constructor for " + aAst, e); 203 throw new RuntimeException ("couldn't instantiate constructor for " 204 + aAst); 205 } 206 if (expHandler == null) { 207 throw new RuntimeException ("no handler for type " + type); 208 } 209 return expHandler; 211 } 212 213 222 ExpressionHandler createMethodCallHandler(IndentationCheck aIndentCheck, 223 DetailAST aAst, ExpressionHandler aParent) 224 { 225 DetailAST ast = (DetailAST) aAst.getFirstChild(); 226 while ((ast != null) && (ast.getType() == TokenTypes.DOT)) { 227 ast = (DetailAST) ast.getFirstChild(); 228 } 229 if ((ast != null) && isHandledType(ast.getType())) { 230 aParent = getHandler(aIndentCheck, ast, aParent); 231 mCreatedHandlers.put(ast, aParent); 232 } 233 return new MethodCallHandler(aIndentCheck, aAst, aParent); 234 } 235 236 237 void clearCreatedHandlers() 238 { 239 mCreatedHandlers.clear(); 240 } 241 242 243 private final Map mCreatedHandlers = new HashMap (); 244 } 245 | Popular Tags |