Refactor to improve the AbstractOptionCheck code.

This commit is contained in:
Oliver Burn 2002-11-06 00:40:26 +00:00
parent 903f780d58
commit 267bdf5417
6 changed files with 40 additions and 20 deletions

View File

@ -30,8 +30,13 @@ public abstract class AbstractOptionCheck
extends Check
{
/** the policy to enforce */
protected AbstractOption mOption = null;
private AbstractOption mOption;
public AbstractOptionCheck(AbstractOption aDefault)
{
mOption = aDefault;
}
/**
* Set the option to enforce.
* @param aOption string to decode option from

View File

@ -28,6 +28,11 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*/
public class EmptyBlockCheck extends AbstractOptionCheck
{
public EmptyBlockCheck()
{
super(BlockOption.STMT);
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
@ -52,7 +57,7 @@ public class EmptyBlockCheck extends AbstractOptionCheck
{
final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);
if (slistAST != null) {
if (mOption == BlockOption.STMT) {
if (getAbstractOption() == BlockOption.STMT) {
if (slistAST.getChildCount() <= 1) {
log(slistAST.getLineNo(),
slistAST.getColumnNo(),
@ -60,7 +65,7 @@ public class EmptyBlockCheck extends AbstractOptionCheck
aAST.getText());
}
}
else if (mOption == BlockOption.TEXT) {
else if (getAbstractOption() == BlockOption.TEXT) {
if (!hasText(slistAST)) {
log(slistAST.getLineNo(),
slistAST.getColumnNo(),
@ -71,6 +76,10 @@ public class EmptyBlockCheck extends AbstractOptionCheck
}
}
/**
* @param slistAST a <code>DetailAST</code> value
* @return whether the SLIST token contains any text.
*/
private boolean hasText(final DetailAST slistAST)
{
boolean retVal = false;

View File

@ -27,8 +27,7 @@ public abstract class LeftCurlyCheck
*/
public LeftCurlyCheck()
{
super();
mOption = LeftCurlyOption.EOL;
super(LeftCurlyOption.EOL);
}
/** @see the hack above */
@ -62,13 +61,13 @@ public abstract class LeftCurlyCheck
{
// ignore
}
else if (mOption == LeftCurlyOption.NL) {
else if (getAbstractOption() == LeftCurlyOption.NL) {
if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) {
log(aBrace.getLineNo(), aBrace.getColumnNo(),
"line.new", "{");
}
}
else if (mOption == LeftCurlyOption.EOL) {
else if (getAbstractOption() == LeftCurlyOption.EOL) {
if (Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)
&& ((prevLineLen + 2) <= mMaxLineLength))
{
@ -76,7 +75,7 @@ public abstract class LeftCurlyCheck
"line.previous", "{");
}
}
else if (mOption == LeftCurlyOption.NLOW) {
else if (getAbstractOption() == LeftCurlyOption.NLOW) {
if (aStartToken.getLineNo() == aBrace.getLineNo()) {
// all ok as on the same line
}

View File

@ -37,8 +37,7 @@ public class ParenPadCheck
*/
public ParenPadCheck()
{
super();
mOption = PadOption.NOSPACE;
super(PadOption.NOSPACE);
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
@ -73,12 +72,12 @@ public class ParenPadCheck
final String line = getLines()[aAST.getLineNo() - 1];
final int after = aAST.getColumnNo() + 1;
if (after < line.length()) {
if ((PadOption.NOSPACE == mOption)
if ((PadOption.NOSPACE == getAbstractOption())
&& (Character.isWhitespace(line.charAt(after))))
{
log(aAST.getLineNo(), after, "ws.followed", "(");
}
else if ((PadOption.SPACE == mOption)
else if ((PadOption.SPACE == getAbstractOption())
&& !Character.isWhitespace(line.charAt(after))
&& (line.charAt(after) != ')'))
{
@ -96,13 +95,13 @@ public class ParenPadCheck
final String line = getLines()[aAST.getLineNo() - 1];
final int before = aAST.getColumnNo() - 1;
if (before >= 0) {
if ((PadOption.NOSPACE == mOption)
if ((PadOption.NOSPACE == getAbstractOption())
&& Character.isWhitespace(line.charAt(before))
&& !Utils.whitespaceBefore(before, line))
{
log(aAST.getLineNo(), before, "ws.preceeded", ")");
}
else if ((PadOption.SPACE == mOption)
else if ((PadOption.SPACE == getAbstractOption())
&& !Character.isWhitespace(line.charAt(before))
&& (line.charAt(before) != '('))
{

View File

@ -37,8 +37,7 @@ public class RightCurlyCheck
*/
public RightCurlyCheck()
{
super();
mOption = RightCurlyOption.SAME;
super(RightCurlyOption.SAME);
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
@ -72,13 +71,13 @@ public class RightCurlyCheck
// If have both tokens, perform the check
if ((rcurly != null) && (nextToken != null)) {
if ((mOption == RightCurlyOption.SAME)
if ((getAbstractOption() == RightCurlyOption.SAME)
&& (rcurly.getLineNo() != nextToken.getLineNo()))
{
log(rcurly.getLineNo(), rcurly.getColumnNo(),
"line.same", "}");
}
else if ((mOption == RightCurlyOption.ALONE)
else if ((getAbstractOption() == RightCurlyOption.ALONE)
&& (rcurly.getLineNo() == nextToken.getLineNo()))
{
log(rcurly.getLineNo(), rcurly.getColumnNo(),

View File

@ -19,7 +19,16 @@ public class EmptyBlockCheckTest
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSemantic.java");
final String[] expected = {
};
"52:65: Must have at least one statement.",
"54:41: Must have at least one statement.",
"71:38: Must have at least one statement.",
"72:52: Must have at least one statement.",
"73:45: Must have at least one statement.",
"75:13: Must have at least one statement.",
"77:17: Must have at least one statement.",
"79:13: Must have at least one statement.",
"82:17: Must have at least one statement.",
};
verify(c, fname, expected);
}
@ -40,7 +49,7 @@ public class EmptyBlockCheckTest
};
verify(c, fname, expected);
}
public void testStatement()
throws Exception
{