01: /*
02: * Hammurapi
03: * Automated Java code review system.
04: * Copyright (C) 2004 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.org
21: * e-Mail: support@hammurapi.biz
22: */
23: package org.hammurapi.inspectors.formatting;
24:
25: import java.lang.reflect.InvocationTargetException;
26: import java.lang.reflect.Method;
27: import java.lang.reflect.Modifier;
28: import java.util.HashMap;
29: import java.util.Map;
30:
31: import com.pavelvlasov.jsel.impl.Token;
32: import org.hammurapi.HammurapiException;
33:
34: /**
35: * Base class for FormattingCheckers implements invocation of
36: * dispatch methods by using reflection
37: *
38: * @author Pavel Vlasov
39: * @version $Revision: 1.1 $
40: */
41: public class FormattingCheckerBase implements FormattingChecker {
42:
43: private static final String CHECK_PREFIX = "check_";
44:
45: private Map checkers = new HashMap();
46:
47: protected int indentationLevel = 4;
48:
49: {
50: Class this Class = this .getClass();
51: for (int i = 0, mc = this Class.getMethods().length; i < mc; i++) {
52: Method m = this Class.getMethods()[i];
53: if (!m.getName().equals(CHECK_PREFIX)
54: && Modifier.isPublic(m.getModifiers())
55: && m.getName().startsWith(CHECK_PREFIX)
56: && m.getParameterTypes().length == 1
57: && Token.class.isAssignableFrom(m
58: .getParameterTypes()[0])
59: && boolean.class.equals(m.getReturnType())) {
60: checkers.put(m.getName().substring(
61: CHECK_PREFIX.length()), m);
62: }
63: }
64: }
65:
66: public boolean check(Token aToken) throws HammurapiException {
67: Method m = (Method) checkers.get(aToken.getTypeName());
68: if (m == null) {
69: return false;
70: } else {
71: try {
72: return ((Boolean) m.invoke(this ,
73: new Object[] { aToken })).booleanValue();
74: } catch (IllegalArgumentException e) {
75: throw new HammurapiException(e);
76: } catch (IllegalAccessException e) {
77: throw new HammurapiException(e);
78: } catch (InvocationTargetException e) {
79: throw new HammurapiException(e);
80: }
81: }
82: }
83:
84: public void setIndentationLevel(int indentationLevel) {
85: this.indentationLevel = indentationLevel;
86: }
87: }
|