KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > indentation > HandlerFactory


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.checks.indentation;
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
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 /**
35  * Factory for handlers. Looks up constructor via reflection.
36  *
37  * @author jrichard
38  */

39 public class HandlerFactory
40 {
41     /** Logger for indentation check */
42     private static final Log LOG =
43         LogFactory.getLog("com.puppycrawl.tools.checkstyle.checks.indentation");
44
45     /**
46      * Registered handlers.
47      */

48     private final Map JavaDoc mTypeHandlers = new HashMap JavaDoc();
49
50     /**
51      * registers a handler
52      *
53      * @param aType type from TokenTypes
54      * @param aHandlerClass the handler to register
55      */

56     private void register(int aType, Class JavaDoc aHandlerClass)
57     {
58         try {
59             final Constructor JavaDoc ctor = aHandlerClass.getConstructor(new Class JavaDoc[] {
60                 IndentationCheck.class,
61                 DetailAST.class, // current AST
62
ExpressionHandler.class, // parent
63
});
64             mTypeHandlers.put(new Integer JavaDoc(aType), ctor);
65         }
66         ///CLOVER:OFF
67
catch (final NoSuchMethodException JavaDoc e) {
68             throw new RuntimeException JavaDoc("couldn't find ctor for "
69                                        + aHandlerClass);
70         }
71         catch (final SecurityException JavaDoc e) {
72             LOG.debug("couldn't find ctor for " + aHandlerClass, e);
73             throw new RuntimeException JavaDoc("couldn't find ctor for "
74                                        + aHandlerClass);
75         }
76         ///CLOVER:ON
77
}
78
79     /** Creates a HandlerFactory. */
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     /**
124      * Returns true if this type (form TokenTypes) is handled.
125      *
126      * @param aType type from TokenTypes
127      * @return true if handler is registered, false otherwise
128      */

129     public boolean isHandledType(int aType)
130     {
131         final Set JavaDoc typeSet = mTypeHandlers.keySet();
132         return typeSet.contains(new Integer JavaDoc(aType));
133     }
134
135     /**
136      * Gets list of registered handler types.
137      *
138      * @return int[] of TokenType types
139      */

140     public int[] getHandledTypes()
141     {
142         final Set JavaDoc typeSet = mTypeHandlers.keySet();
143         final int[] types = new int[typeSet.size()];
144         int index = 0;
145         for (final Iterator JavaDoc i = typeSet.iterator(); i.hasNext(); index++) {
146             types[index] = ((Integer JavaDoc) i.next()).intValue();
147         }
148
149         return types;
150     }
151
152     /**
153      * Get the handler for an AST.
154      *
155      * @param aIndentCheck the indentation check
156      * @param aAst ast to handle
157      * @param aParent the handler parent of this AST
158      *
159      * @return the ExpressionHandler for aAst
160      */

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 JavaDoc type = new Integer JavaDoc(aAst.getType());
175
176         ExpressionHandler expHandler = null;
177         try {
178             final Constructor JavaDoc handlerCtor =
179                 (Constructor JavaDoc) mTypeHandlers.get(type);
180             if (handlerCtor != null) {
181                 expHandler = (ExpressionHandler) handlerCtor.newInstance(
182                     new Object JavaDoc[] {
183                         aIndentCheck,
184                         aAst,
185                         aParent,
186                     }
187                 );
188             }
189         }
190         ///CLOVER:OFF
191
catch (final InstantiationException JavaDoc e) {
192             LOG.debug("couldn't instantiate constructor for " + aAst, e);
193             throw new RuntimeException JavaDoc("couldn't instantiate constructor for "
194                                        + aAst);
195         }
196         catch (final IllegalAccessException JavaDoc e) {
197             LOG.debug("couldn't access constructor for " + aAst, e);
198             throw new RuntimeException JavaDoc("couldn't access constructor for "
199                                        + aAst);
200         }
201         catch (final InvocationTargetException JavaDoc e) {
202             LOG.debug("couldn't instantiate constructor for " + aAst, e);
203             throw new RuntimeException JavaDoc("couldn't instantiate constructor for "
204                                        + aAst);
205         }
206         if (expHandler == null) {
207             throw new RuntimeException JavaDoc("no handler for type " + type);
208         }
209         ///CLOVER:ON
210
return expHandler;
211     }
212
213     /**
214      * Create new instance of handler for METHOD_CALL.
215      *
216      * @param aIndentCheck the indentation check
217      * @param aAst ast to handle
218      * @param aParent the handler parent of this AST
219      *
220      * @return new instance.
221      */

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     /** Clears cache of created handlers. */
237     void clearCreatedHandlers()
238     {
239         mCreatedHandlers.clear();
240     }
241
242     /** cache for created method call handlers */
243     private final Map JavaDoc mCreatedHandlers = new HashMap JavaDoc();
244 }
245
Popular Tags