Implemented brace checking for "if" and "while"

This commit is contained in:
Oliver Burn 2002-02-22 03:11:36 +00:00
parent 9943aefac7
commit f4860ef3b4
8 changed files with 31 additions and 26 deletions

View File

@ -160,6 +160,7 @@
staticPattern="^s[A-Z][a-zA-Z0-9]*$"
paramPattern="^a[A-Z][a-zA-Z0-9]*$"
lcurlyMethod="nl"
lcurlyOther="nlow"
lcurlyType="nl">
<fileset dir="src/checkstyle"
includes="**/*.java"

View File

@ -142,6 +142,7 @@
</tr>
</table>
<p>The check can be tailored for types, methods and others. What is included in others are: <span class="code">switch, while, if, </span>. It does not include array initializers, .</p>
<h3><a name="length">Long Lines</a></h3>
<p>Checks for lines that are longer than a specified length. The default is <span class="default">&quot;80&quot;</span>. This can be turned off for <code>import</code> statements.</p>

View File

@ -188,8 +188,7 @@ public class Checker
*/
private int checkPackageHtml(String[] aFiles)
{
if (!mConfig.isRequirePackageHtml())
{
if (!mConfig.isRequirePackageHtml()) {
return 0;
}
@ -199,14 +198,12 @@ public class Checker
{
final File file = new File(aFiles[i]);
final File packageDir = file.getParentFile();
if (!checkedPackages.contains(packageDir))
{
if (!checkedPackages.contains(packageDir)) {
final File packageDoc =
new File(packageDir, "package.html");
final String docFile = packageDoc.toString();
fireFileStarted(docFile);
if (!packageDoc.exists())
{
if (!packageDoc.exists()) {
final LineText error =
new LineText(0, "missing package documentation file.");
fireErrors(docFile, new LineText[]{error} );

View File

@ -481,8 +481,7 @@ public class Configuration
**/
public int getHeaderIgnoreLineNo()
{
if (mHeaderIgnoreLineNo.isEmpty())
{
if (mHeaderIgnoreLineNo.isEmpty()) {
return -1;
}
else
@ -764,14 +763,12 @@ public class Configuration
{
mHeaderIgnoreLineNo.clear();
if (aList == null)
{
if (aList == null) {
return;
}
final StringTokenizer tokens = new StringTokenizer(aList, ",");
while (tokens.hasMoreTokens())
{
while (tokens.hasMoreTokens()) {
final String ignoreLine = tokens.nextToken();
mHeaderIgnoreLineNo.add(new Integer(ignoreLine));
}

View File

@ -101,16 +101,13 @@ class MyModifierSet
/** @return the visibility scope of the modifiers. */
Scope getVisibilityScope()
{
if (containsPublic())
{
if (containsPublic()) {
return Scope.PUBLIC;
}
if (containsProtected())
{
else if (containsProtected()) {
return Scope.PROTECTED;
}
if (containsPrivate())
{
else if (containsPrivate()) {
return Scope.PRIVATE;
}
return Scope.PACKAGE;

View File

@ -315,8 +315,7 @@ class Verifier
**/
void verifyVariable(MyVariable aVar)
{
if (inMethodBlock())
{
if (inMethodBlock()) {
checkVariable(aVar,
mConfig.getLocalVarRegexp(),
mConfig.getLocalVarPat());
@ -1075,8 +1074,7 @@ class Verifier
/** checks that a file contains a valid header **/
private void checkHeader()
{
if (mConfig.getHeaderLines().length > mLines.length)
{
if (mConfig.getHeaderLines().length > mLines.length) {
log(1, "Missing a header - not enough lines in file.");
}
else
@ -1098,8 +1096,7 @@ class Verifier
createRE(headerLine).match(mLines[i]) :
headerLine.equals(mLines[i]);
if (!match)
{
if (!match) {
log(i + 1,
"Line does not match expected header line of '" +
mConfig.getHeaderLines()[i] + "'.");

View File

@ -538,6 +538,7 @@ statement[int[] aType, MyCommonAST[] aCurlies]
{
final MyModifierSet modSet = new MyModifierSet();
final int[] stmtType = new int[1];
final MyCommonAST[] stmtBraces = new MyCommonAST[2];
stmtType[0] = STMT_OTHER;
}
// A list of statements in curly braces -- start a new scope!
@ -561,13 +562,16 @@ statement[int[] aType, MyCommonAST[] aCurlies]
| IDENT c:COLON^ {#c.setType(LABELED_STAT);} statement[sIgnoreType, sIgnoreAST]
// If-else statement
| ii:"if"^ LPAREN! expression RPAREN! statement[stmtType, sIgnoreAST]
| ii:"if"^ LPAREN! expression RPAREN! statement[stmtType, stmtBraces]
{
aType[0] = STMT_IF;
ver.verifyWSAroundBegin(ii.getLine(), ii.getColumn(), ii.getText());
if (stmtType[0] != STMT_COMPOUND) {
ver.reportNeedBraces(ii);
}
else {
ver.verifyLCurlyOther(ii.getLine(), stmtBraces[0]);
}
}
(
// CONFLICT: the old "dangling-else" problem...
@ -604,12 +608,15 @@ statement[int[] aType, MyCommonAST[] aCurlies]
}
// While statement
| ww:"while"^ LPAREN! expression RPAREN! statement[stmtType, sIgnoreAST]
| ww:"while"^ LPAREN! expression RPAREN! statement[stmtType, stmtBraces]
{
ver.verifyWSAroundBegin(ww.getLine(), ww.getColumn(), ww.getText());
if (stmtType[0] != STMT_COMPOUND) {
ver.reportNeedBraces(ww);
}
else {
ver.verifyLCurlyOther(ww.getLine(), stmtBraces[0]);
}
}
// do-while statement

View File

@ -112,6 +112,8 @@ public class CheckerTest
filepath + ":39: 'try' is not followed by whitespace.",
filepath + ":41: 'catch' is not followed by whitespace.",
filepath + ":58: 'if' is not followed by whitespace.",
filepath + ":59: '{' should be on the previous line.",
filepath + ":75: '{' should be on the previous line.",
filepath + ":76: 'return' is not followed by whitespace.",
filepath + ":88: cast needs to be followed by whitespace.",
filepath + ":97: '?' is not preceeded with whitespace.",
@ -170,6 +172,8 @@ public class CheckerTest
filepath + ":39: 'try' is not followed by whitespace.",
filepath + ":41: 'catch' is not followed by whitespace.",
filepath + ":58: 'if' is not followed by whitespace.",
filepath + ":59: '{' should be on the previous line.",
filepath + ":75: '{' should be on the previous line.",
filepath + ":76: 'return' is not followed by whitespace.",
filepath + ":97: '?' is not preceeded with whitespace.",
filepath + ":97: '?' is not followed by whitespace.",
@ -206,6 +210,8 @@ public class CheckerTest
assertNotNull(c);
final String[] expected = {
filepath + ":13: type Javadoc comment is missing an @author tag.",
filepath + ":59: '{' should be on the previous line.",
filepath + ":75: '{' should be on the previous line.",
};
verify(c, filepath, expected);
}
@ -632,6 +638,8 @@ public class CheckerTest
final String filepath = getPath("InputLeftCurlyOther.java");
assertNotNull(c);
final String[] expected = {
filepath + ":19: '{' should be on the previous line.",
filepath + ":22: '{' should be on the previous line.",
filepath + ":33: '{' should be on the previous line.",
};
verify(c, filepath, expected);