Loops have to have only one breal/continue. Issue #46

This commit is contained in:
Ilja Dubinin 2015-08-12 23:46:48 +01:00 committed by Roman Ivanov
parent 6a0bad784f
commit 7770f2a2c2
5 changed files with 96 additions and 42 deletions

View File

@ -43,20 +43,17 @@ public final class ScopeUtils {
*/
public static Scope getScopeFromMods(DetailAST aMods) {
Scope retVal = Scope.PACKAGE; // default scope
for (AST token = aMods.getFirstChild();
token != null;
token = token.getNextSibling()) {
for (AST token = aMods.getFirstChild(); token != null
&& retVal == Scope.PACKAGE;
token = token.getNextSibling()) {
if ("public".equals(token.getText())) {
retVal = Scope.PUBLIC;
break;
}
else if ("protected".equals(token.getText())) {
retVal = Scope.PROTECTED;
break;
}
else if ("private".equals(token.getText())) {
retVal = Scope.PRIVATE;
break;
}
}
return retVal;
@ -105,20 +102,19 @@ public final class ScopeUtils {
// Loop up looking for a containing interface block
for (DetailAST token = aAST.getParent();
token != null;
token != null && !retVal;
token = token.getParent()) {
final int type = token.getType();
if (type == TokenTypes.CLASS_DEF
|| type == TokenTypes.ENUM_DEF
|| type == TokenTypes.ANNOTATION_DEF) {
break; // in a class, enum or annotation
}
else if (type == TokenTypes.LITERAL_NEW) {
break; // inner implementation
}
else if (type == TokenTypes.INTERFACE_DEF) {
if (type == TokenTypes.INTERFACE_DEF) {
retVal = true;
break;
}
else if (type == TokenTypes.CLASS_DEF
|| type == TokenTypes.ENUM_DEF
|| type == TokenTypes.ANNOTATION_DEF
|| type == TokenTypes.LITERAL_NEW) {
break; // in a class, enum or annotation
}
}
@ -137,21 +133,19 @@ public final class ScopeUtils {
// Loop up looking for a containing interface block
for (DetailAST token = aAST.getParent();
token != null;
token != null && !retVal;
token = token.getParent()) {
final int type = token.getType();
if (type == TokenTypes.CLASS_DEF
if (type == TokenTypes.ANNOTATION_DEF) {
retVal = true;
}
else if (type == TokenTypes.CLASS_DEF
|| type == TokenTypes.ENUM_DEF
|| type == TokenTypes.INTERFACE_DEF) {
|| type == TokenTypes.INTERFACE_DEF
|| type == TokenTypes.LITERAL_NEW) {
break; // in a class, enum or interface
}
else if (type == TokenTypes.LITERAL_NEW) {
break; // inner implementation
}
else if (type == TokenTypes.ANNOTATION_DEF) {
retVal = true;
break;
}
}
return retVal;
@ -181,20 +175,17 @@ public final class ScopeUtils {
// Loop up looking for a containing interface block
for (DetailAST token = aAST.getParent();
token != null;
token != null && !retVal;
token = token.getParent()) {
final int type = token.getType();
if (type == TokenTypes.INTERFACE_DEF
|| type == TokenTypes.ANNOTATION_DEF
|| type == TokenTypes.CLASS_DEF) {
break; // in an interface, annotation or class
}
else if (type == TokenTypes.LITERAL_NEW) {
break; // inner implementation, enums can't be inner classes
}
else if (type == TokenTypes.ENUM_DEF) {
if (type == TokenTypes.ENUM_DEF) {
retVal = true;
break;
}
else if (type == TokenTypes.INTERFACE_DEF
|| type == TokenTypes.ANNOTATION_DEF
|| type == TokenTypes.CLASS_DEF
|| type == TokenTypes.LITERAL_NEW) {
break; // in an interface, annotation or class
}
}

View File

@ -671,10 +671,7 @@ public class CustomImportOrderCheck extends Check {
int result = 0;
final String[] import1Tokens = import1.split("\\.");
final String[] import2Tokens = import2.split("\\.");
for (int i = 0; i < import1Tokens.length; i++) {
if (i == import2Tokens.length) {
break;
}
for (int i = 0; i < import1Tokens.length && i != import2Tokens.length; i++) {
final String import1Token = import1Tokens[i];
final String import2Token = import2Tokens[i];
result = import1Token.compareTo(import2Token);

View File

@ -51,6 +51,20 @@ public class ScopeUtilsTest {
Assert.assertFalse(ScopeUtils.inEnumBlock(ast2));
}
@Test
public void testInEnumBlockWithEnum() throws ReflectiveOperationException {
DetailAST ast0 = new DetailAST();
ast0.setType(TokenTypes.OBJBLOCK);
DetailAST ast1 = new DetailAST();
ast1.setType(TokenTypes.ENUM_DEF);
ast0.addChild(ast1);
DetailAST ast2 = new DetailAST();
ast2.setType(TokenTypes.MODIFIERS);
ast1.addChild(ast2);
Assert.assertTrue(ScopeUtils.inEnumBlock(ast2));
}
@Test
public void testInEnumBlockInInterface() throws ReflectiveOperationException {
DetailAST ast = new DetailAST();

View File

@ -0,0 +1,38 @@
package com.puppycrawl.tools.checkstyle.checks.sizes;
public class MethodCountCheckInput3 {
/**
* Dummy inner class to check that the inner-classes methods are not counted for the outer class.
*/
/**
* Dummy method doing nothing
*/
void doNothing50() {
}
/**
* Dummy method doing nothing
*/
void doNothing51() {
}
/**
* Dummy method doing nothing
*/
void doNothing52() {
}
/**
* Dummy method doing nothing
*/
void doNothing53() {
}
/**
* Dummy method doing nothing
*/
void doNothing54() {
}
}

View File

@ -102,4 +102,18 @@ public class MethodCountCheckTest extends BaseCheckTestSupport {
verify(checkConfig,
getSrcPath("checks/sizes/MethodCountCheckInput2.java"), expected);
}
@Test
public void testWithPackageModifier() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(MethodCountCheck.class);
checkConfig.addAttribute("maxPrivate", "0");
checkConfig.addAttribute("maxTotal", "2");
final String[] expected = {
"3: " + getCheckMessage(MSG_MANY_METHODS, 5, 2),
};
verify(checkConfig,
getSrcPath("checks/sizes/MethodCountCheckInput3.java"), expected);
}
}