1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import java.lang.reflect.Constructor;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26
27 import com.puppycrawl.tools.checkstyle.api.DetailAST;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31
32
33
34
35 public class HandlerFactory {
36
37
38
39
40 private final Map<Integer, Constructor<?>> typeHandlers = new HashMap<>();
41
42
43 private final Map<DetailAST, AbstractExpressionHandler> createdHandlers = new HashMap<>();
44
45
46
47
48
49
50
51
52 public HandlerFactory() {
53 register(TokenTypes.CASE_GROUP, CaseHandler.class);
54 register(TokenTypes.LITERAL_SWITCH, SwitchHandler.class);
55 register(TokenTypes.SLIST, SlistHandler.class);
56 register(TokenTypes.PACKAGE_DEF, PackageDefHandler.class);
57 register(TokenTypes.LITERAL_ELSE, ElseHandler.class);
58 register(TokenTypes.LITERAL_IF, IfHandler.class);
59 register(TokenTypes.LITERAL_TRY, TryHandler.class);
60 register(TokenTypes.LITERAL_CATCH, CatchHandler.class);
61 register(TokenTypes.LITERAL_FINALLY, FinallyHandler.class);
62 register(TokenTypes.LITERAL_DO, DoWhileHandler.class);
63 register(TokenTypes.LITERAL_WHILE, WhileHandler.class);
64 register(TokenTypes.LITERAL_FOR, ForHandler.class);
65 register(TokenTypes.METHOD_DEF, MethodDefHandler.class);
66 register(TokenTypes.CTOR_DEF, MethodDefHandler.class);
67 register(TokenTypes.CLASS_DEF, ClassDefHandler.class);
68 register(TokenTypes.ENUM_DEF, ClassDefHandler.class);
69 register(TokenTypes.OBJBLOCK, ObjectBlockHandler.class);
70 register(TokenTypes.INTERFACE_DEF, ClassDefHandler.class);
71 register(TokenTypes.IMPORT, ImportHandler.class);
72 register(TokenTypes.ARRAY_INIT, ArrayInitHandler.class);
73 register(TokenTypes.ANNOTATION_ARRAY_INIT, AnnotationArrayInitHandler.class);
74 register(TokenTypes.METHOD_CALL, MethodCallHandler.class);
75 register(TokenTypes.CTOR_CALL, MethodCallHandler.class);
76 register(TokenTypes.SUPER_CTOR_CALL, MethodCallHandler.class);
77 register(TokenTypes.LABELED_STAT, LabelHandler.class);
78 register(TokenTypes.STATIC_INIT, StaticInitHandler.class);
79 register(TokenTypes.INSTANCE_INIT, SlistHandler.class);
80 register(TokenTypes.VARIABLE_DEF, MemberDefHandler.class);
81 register(TokenTypes.LITERAL_NEW, NewHandler.class);
82 register(TokenTypes.INDEX_OP, IndexHandler.class);
83 register(TokenTypes.LITERAL_SYNCHRONIZED, SynchronizedHandler.class);
84 register(TokenTypes.LAMBDA, LambdaHandler.class);
85 register(TokenTypes.ANNOTATION_DEF, ClassDefHandler.class);
86 register(TokenTypes.ANNOTATION_FIELD_DEF, MethodDefHandler.class);
87 register(TokenTypes.SWITCH_RULE, SwitchRuleHandler.class);
88 register(TokenTypes.LITERAL_YIELD, YieldHandler.class);
89 register(TokenTypes.RECORD_DEF, ClassDefHandler.class);
90 register(TokenTypes.COMPACT_CTOR_DEF, MethodDefHandler.class);
91 }
92
93
94
95
96
97
98
99
100
101
102 private <T> void register(int type, Class<T> handlerClass) {
103 final Constructor<T> ctor = CommonUtil.getConstructor(handlerClass,
104 IndentationCheck.class,
105
106 DetailAST.class,
107
108 AbstractExpressionHandler.class
109 );
110 typeHandlers.put(type, ctor);
111 }
112
113
114
115
116
117
118
119 public boolean isHandledType(int type) {
120 final Set<Integer> typeSet = typeHandlers.keySet();
121 return typeSet.contains(type);
122 }
123
124
125
126
127
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
143
144
145
146
147
148
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
171
172
173
174
175
176
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
193 public void clearCreatedHandlers() {
194 createdHandlers.clear();
195 }
196
197 }