Implemented Other Left Curly check
This commit is contained in:
parent
e9faeb047f
commit
21c76786c6
|
|
@ -0,0 +1,86 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2002 Oliver Burn
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package com.puppycrawl.tools.checkstyle.checks;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.Utils;
|
||||
|
||||
public class OtherLeftCurlyCheck
|
||||
extends LeftCurlyCheck
|
||||
{
|
||||
/** @see Check */
|
||||
public int[] getDefaultTokens()
|
||||
{
|
||||
return new int[] {JavaTokenTypes.LITERAL_while,
|
||||
JavaTokenTypes.LITERAL_try,
|
||||
JavaTokenTypes.LITERAL_catch,
|
||||
JavaTokenTypes.LITERAL_finally,
|
||||
JavaTokenTypes.LITERAL_synchronized,
|
||||
JavaTokenTypes.LITERAL_switch,
|
||||
JavaTokenTypes.LITERAL_do,
|
||||
JavaTokenTypes.LITERAL_if,
|
||||
JavaTokenTypes.LITERAL_else,
|
||||
};
|
||||
// STATIC_INIT
|
||||
// "for"
|
||||
}
|
||||
|
||||
/** @see Check */
|
||||
public void visitToken(DetailAST aAST)
|
||||
{
|
||||
final DetailAST startToken = aAST;
|
||||
final DetailAST brace;
|
||||
|
||||
switch (aAST.getType()) {
|
||||
case JavaTokenTypes.LITERAL_while:
|
||||
case JavaTokenTypes.LITERAL_catch:
|
||||
case JavaTokenTypes.LITERAL_synchronized:
|
||||
brace = Utils.getLastSibling(aAST.getFirstChild());
|
||||
break;
|
||||
case JavaTokenTypes.LITERAL_try:
|
||||
case JavaTokenTypes.LITERAL_finally:
|
||||
case JavaTokenTypes.LITERAL_do:
|
||||
brace = (DetailAST) aAST.getFirstChild();
|
||||
break;
|
||||
case JavaTokenTypes.LITERAL_else:
|
||||
final DetailAST candidate = (DetailAST) aAST.getFirstChild();
|
||||
if (candidate.getType() == JavaTokenTypes.SLIST) {
|
||||
brace = candidate;
|
||||
}
|
||||
else {
|
||||
// silently ignore
|
||||
brace = null;
|
||||
}
|
||||
break;
|
||||
case JavaTokenTypes.LITERAL_switch:
|
||||
case JavaTokenTypes.LITERAL_if:
|
||||
brace = (DetailAST) aAST.getFirstChild().getNextSibling();
|
||||
break;
|
||||
default:
|
||||
brace = null;
|
||||
}
|
||||
|
||||
if (brace != null) {
|
||||
verifyBrace(brace, startToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.checks.OtherLeftCurlyCheck;
|
||||
|
||||
public class OtherLeftCurlyCheckTest
|
||||
extends BaseCheckTestCase
|
||||
{
|
||||
public OtherLeftCurlyCheckTest(String aName)
|
||||
{
|
||||
super(aName);
|
||||
}
|
||||
|
||||
public void testDefault()
|
||||
throws Exception
|
||||
{
|
||||
final CheckConfiguration checkConfig = new CheckConfiguration();
|
||||
checkConfig.setClassname(OtherLeftCurlyCheck.class.getName());
|
||||
final Checker c = createChecker(checkConfig);
|
||||
final String fname = getPath("InputLeftCurlyOther.java");
|
||||
final String[] expected = {
|
||||
"19:9: '{' should be on the previous line.",
|
||||
"21:13: '{' should be on the previous line.",
|
||||
"23:17: '{' should be on the previous line.",
|
||||
"30:17: '{' should be on the previous line.",
|
||||
"34:17: '{' should be on the previous line.",
|
||||
"42:13: '{' should be on the previous line.",
|
||||
"46:13: '{' should be on the previous line.",
|
||||
"52:9: '{' should be on the previous line.",
|
||||
"54:13: '{' should be on the previous line.",
|
||||
};
|
||||
verify(c, fname, expected);
|
||||
}
|
||||
|
||||
public void testNL()
|
||||
throws Exception
|
||||
{
|
||||
final CheckConfiguration checkConfig = new CheckConfiguration();
|
||||
checkConfig.setClassname(OtherLeftCurlyCheck.class.getName());
|
||||
checkConfig.addProperty("option", LeftCurlyOption.NL.toString());
|
||||
final Checker c = createChecker(checkConfig);
|
||||
final String fname = getPath("InputLeftCurlyOther.java");
|
||||
final String[] expected = {
|
||||
"26:33: '{' should be on a new line."
|
||||
};
|
||||
verify(c, fname, expected);
|
||||
}
|
||||
|
||||
public void testIgnore()
|
||||
throws Exception
|
||||
{
|
||||
final CheckConfiguration checkConfig = new CheckConfiguration();
|
||||
checkConfig.setClassname(OtherLeftCurlyCheck.class.getName());
|
||||
checkConfig.addProperty("option", LeftCurlyOption.IGNORE.toString());
|
||||
final Checker c = createChecker(checkConfig);
|
||||
final String fname = getPath("InputLeftCurlyOther.java");
|
||||
final String[] expected = {
|
||||
};
|
||||
verify(c, fname, expected);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue