KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.pavelvlasov.jsel.impl.JavaTokenTypes;
26 import com.pavelvlasov.jsel.impl.Token;
27
28 /**
29  * Implementation of FormattingChecker for formatting style with opening curly
30  * braces in the same line
31  *
32  * @author Jochen Skulj
33  * @version $Revision: 1.2 $
34  */

35 class FormattingCheckerSameLine extends FormattingCheckerBase {
36
37   /**
38    * constructor
39    */

40   public FormattingCheckerSameLine() {
41   }
42
43   // Dispatch methods
44
public boolean check_catch(Token aToken) {
45     return checkPrevRCurly(aToken) || checkNextLParen(aToken);
46   }
47
48   public boolean check_LITERAL_do(Token aToken) {
49     return checkNextLCurly(aToken);
50   }
51
52   public boolean check_LITERAL_else(Token aToken) {
53     boolean violation = checkPrevRCurly(aToken);
54     if (violation != true) {
55       // if the else-Token is followed by an if-token both tokens
56
// must be in the same line
57
Token next = nextNonWhitespace(aToken);
58       if (next.getType() == JavaTokenTypes.LITERAL_if) {
59         if (next.getLine() != aToken.getLine()) {
60           violation = true;
61         }
62       } else {
63         violation = checkNextLCurly(aToken);
64       }
65     }
66     return violation;
67   }
68
69   public boolean check_LITERAL_finally(Token aToken) {
70     return (checkPrevRCurly(aToken) || checkNextLCurly(aToken));
71   }
72
73   public boolean check_LITERAL_for(Token aToken) {
74     return checkNextLParen(aToken);
75   }
76
77   public boolean check_LITERAL_if(Token aToken) {
78     return checkNextLParen(aToken);
79   }
80
81   public boolean check_LITERAL_while(Token aToken) {
82     return checkWhile(aToken);
83   }
84
85   public boolean check_LITERAL_switch(Token aToken) {
86     return checkNextLParen(aToken);
87   }
88
89   public boolean check_LITERAL_synchronized(Token aToken) {
90     boolean violation = false;
91     Token next = nextNonWhitespace(aToken);
92     if (next.getType() == JavaTokenTypes.LPAREN) {
93       violation = checkNextLParen(aToken);
94     }
95     return violation;
96   }
97
98   public boolean check_LITERAL_try(Token aToken) {
99     return checkNextLCurly(aToken);
100   }
101
102   /**
103    * checks formatting for tokens that are followed by expression in parenthesis
104    *
105    * @param aToken
106    * currentToken
107    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
108    */

109   protected boolean checkNextLParen(Token aToken) {
110     // The token must be followed by a variously complex braced expression.
111
// The expression must start in the same line as the token
112
// This expression is followed by a LCURLY. The last token of the
113
// expression and the LCURLY must be in the same line.
114
Token expressionFirst = nextNonWhitespace(aToken);
115     boolean violation = false;
116     if (expressionFirst.getLine() != aToken.getLine()) {
117       violation = true;
118     }
119     Token expressionLast = skipExpressionTokens(expressionFirst);
120     Token lcurly = nextNonWhitespace(expressionLast);
121     if (lcurly.getType() == JavaTokenTypes.LCURLY) {
122       if (expressionLast.getLine() != lcurly.getLine()) {
123         violation = true;
124       }
125     } else {
126       violation = true;
127     }
128     return violation;
129   }
130
131   /**
132    * checks formatting for tokens that are follow opening curly brace (like do)
133    *
134    * @param aToken
135    * current Token
136    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
137    */

138   protected boolean checkNextLCurly(Token aToken) {
139     // The token must be followed by a LCURLY in the same line
140
boolean violation = false;
141     Token lcurly = nextNonWhitespace(aToken);
142     if (lcurly.getType() == JavaTokenTypes.LCURLY) {
143       if (lcurly.getLine() != aToken.getLine()) {
144         violation = true;
145       }
146     } else {
147       violation = true;
148     }
149     return violation;
150   }
151
152   /**
153    * checks formatting for tokens that are successors of a closing curly brace
154    * like else, catch or finally
155    *
156    * @param aToken
157    * current Token
158    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
159    */

160   protected boolean checkPrevRCurly(Token aToken) {
161     // the token must have e previous RCURLY in the same line
162
Token rcurly = previousNonWhitespace(aToken);
163     boolean violation = false;
164     if (rcurly.getType() == JavaTokenTypes.RCURLY) {
165       if (rcurly.getLine() != aToken.getLine()) {
166         violation = true;
167       }
168     } else {
169       violation = true;
170     }
171     return violation;
172   }
173
174   /**
175    * checks formatting for while-Tokens
176    *
177    * @param aToken
178    * current Token
179    * @return <code>true<code> if the token violates a rule; otherwise <code>false</code>
180    */

181   protected boolean checkWhile(Token aToken) {
182     // while can be used in two ways: as the beginning of a while-loop or
183
// the end of a do-loop. This is checked by inspecting previous tokens
184
boolean whileLoop = true;
185     Token previous = previousNonWhitespace(aToken);
186     if (previous.getType() == JavaTokenTypes.RCURLY) {
187       // get the matching LCURLY
188
int open = 1;
189       while (open > 0) {
190         previous = previousNonWhitespace(previous);
191         if (previous.getType() == JavaTokenTypes.LCURLY) {
192           --open;
193         }
194         if (previous.getType() == JavaTokenTypes.RCURLY) {
195           ++open;
196         }
197       }
198       // get the token previous to the matching LCURLY
199
previous = previousNonWhitespace(previous);
200       if (previous.getType() == JavaTokenTypes.LITERAL_do) {
201         whileLoop = false;
202       }
203     }
204     boolean violation = true;
205     if (whileLoop) {
206       // in a while-Loop the while is a token followed by a
207
// paranthesis
208
violation = checkNextLParen(aToken);
209     } else {
210       // in a do-Loop the while is a token that is a
211
// successor of a closing curly brace and that is
212
// followed by a expression in parenthesis and semicolon
213
violation = checkPrevRCurly(aToken);
214       if (!violation) {
215         Token expressionFirst = nextNonWhitespace(aToken);
216         if (expressionFirst.getLine() != aToken.getLine()) {
217           violation = true;
218         }
219         Token expressionLast = skipExpressionTokens(expressionFirst);
220         Token semicolon = nextNonWhitespace(expressionLast);
221         if (semicolon.getType() != JavaTokenTypes.SEMI
222             || semicolon.getLine() != aToken.getLine()) {
223           violation = true;
224         }
225       }
226
227     }
228     return violation;
229   }
230
231   /**
232    * skips all tokens of a expression in parenthesis
233    *
234    * @param currentToken
235    * first opening parenthesis of expression
236    * @return last closing parenthesis of expression
237    */

238   protected Token skipExpressionTokens(Token currentToken) {
239     int open = 0;
240     Token token = currentToken;
241     do {
242       if (token.getType() == JavaTokenTypes.LPAREN) {
243         ++open;
244       }
245       if (token.getType() == JavaTokenTypes.RPAREN) {
246         --open;
247       }
248
249       if (open > 0) {
250         token = nextNonWhitespace(token);
251       }
252     } while (token != null && open > 0);
253     return token;
254   }
255
256   /**
257    * skip all next tokens that are whitespaces
258    *
259    * @param aToken
260    * current token
261    * @return next non-whitespace token
262    */

263   protected Token nextNonWhitespace(Token aToken) {
264     Token current = (Token) aToken.getNextToken();
265     while (current.getType() == JavaTokenTypes.WS
266         || current.getType() == JavaTokenTypes.NEW_LINE) {
267       current = (Token) current.getNextToken();
268     }
269     return current;
270   }
271
272   /**
273    * skip all previous tokens that are whitespaces
274    *
275    * @param aToken
276    * current token
277    * @return next previous non-whitespace token
278    */

279   protected Token previousNonWhitespace(Token aToken) {
280     Token current = (Token) aToken.getPrevToken();
281     while (current.getType() == JavaTokenTypes.WS
282         || current.getType() == JavaTokenTypes.NEW_LINE) {
283       current = (Token) current.getPrevToken();
284     }
285     return current;
286   }
287 }
Popular Tags