KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > formatting > FormattingCheckerBase


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import com.pavelvlasov.jsel.impl.Token;
32 import org.hammurapi.HammurapiException;
33
34
35 /**
36  * Base class for FormattingCheckers implements invocation of
37  * dispatch methods by using reflection
38  *
39  * @author Pavel Vlasov
40  * @version $Revision: 1.1 $
41  */

42 public class FormattingCheckerBase implements FormattingChecker {
43   
44     private static final String JavaDoc CHECK_PREFIX = "check_";
45
46     private Map JavaDoc checkers=new HashMap JavaDoc();
47     
48     protected int indentationLevel=4;
49     
50     {
51         Class JavaDoc thisClass=this.getClass();
52         for (int i=0, mc=thisClass.getMethods().length; i<mc; i++) {
53             Method JavaDoc m=thisClass.getMethods()[i];
54             if (!m.getName().equals(CHECK_PREFIX)
55                     && Modifier.isPublic(m.getModifiers())
56                     && m.getName().startsWith(CHECK_PREFIX)
57                     && m.getParameterTypes().length==1
58                     && Token.class.isAssignableFrom(m.getParameterTypes()[0])
59                     && boolean.class.equals(m.getReturnType())) {
60                 checkers.put(m.getName().substring(CHECK_PREFIX.length()), m);
61             }
62         }
63     }
64     
65     public boolean check(Token aToken) throws HammurapiException {
66         Method JavaDoc m=(Method JavaDoc) checkers.get(aToken.getTypeName());
67         if (m==null) {
68             return false;
69         } else {
70             try {
71                 return ((Boolean JavaDoc) m.invoke(this, new Object JavaDoc[] {aToken})).booleanValue();
72             } catch (IllegalArgumentException JavaDoc e) {
73                 throw new HammurapiException(e);
74             } catch (IllegalAccessException JavaDoc e) {
75                 throw new HammurapiException(e);
76             } catch (InvocationTargetException JavaDoc e) {
77                 throw new HammurapiException(e);
78             }
79         }
80     }
81     
82     public void setIndentationLevel(int indentationLevel) {
83         this.indentationLevel = indentationLevel;
84     }
85 }
86
87
88
Popular Tags