Issue #2451: removed excess hierarchy from MethodParamPadCheck

This commit is contained in:
rnveach 2015-11-10 13:16:50 -05:00 committed by Roman Ivanov
parent d2495fd0cf
commit bc646907e9
2 changed files with 32 additions and 10 deletions

View File

@ -19,11 +19,14 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace;
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.ArrayUtils;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
@ -65,7 +68,7 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
*/
public class MethodParamPadCheck
extends AbstractOptionCheck<PadOption> {
extends Check {
/**
* A key is pointing to the warning message text in "messages.properties"
@ -91,12 +94,8 @@ public class MethodParamPadCheck
*/
private boolean allowLineBreaks;
/**
* Sets the pad option to nospace.
*/
public MethodParamPadCheck() {
super(PadOption.NOSPACE, PadOption.class);
}
/** The policy to enforce. */
private PadOption option = PadOption.NOSPACE;
@Override
public int[] getDefaultTokens() {
@ -141,11 +140,11 @@ public class MethodParamPadCheck
}
else {
final int before = parenAST.getColumnNo() - 1;
if (getAbstractOption() == PadOption.NOSPACE
if (option == PadOption.NOSPACE
&& Character.isWhitespace(line.charAt(before))) {
log(parenAST, WS_PRECEDED, parenAST.getText());
}
else if (getAbstractOption() == PadOption.SPACE
else if (option == PadOption.SPACE
&& !Character.isWhitespace(line.charAt(before))) {
log(parenAST, WS_NOT_PRECEDED, parenAST.getText());
}
@ -160,4 +159,18 @@ public class MethodParamPadCheck
public void setAllowLineBreaks(boolean allowLineBreaks) {
this.allowLineBreaks = allowLineBreaks;
}
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
*/
public void setOption(String optionStr) {
try {
option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
}
}
}

View File

@ -33,6 +33,7 @@ import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public class MethodParamPadCheckTest
@ -145,4 +146,12 @@ public class MethodParamPadCheckTest
};
assertArrayEquals(expected, actual);
}
@Test(expected = CheckstyleException.class)
public void testInvalidOption() throws Exception {
checkConfig.addAttribute("option", "invalid_option");
final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputMethodParamPad.java"), expected);
}
}