Skip to content

Commit

Permalink
Issue checkstyle#6320: added REMOVE_CONDITIONALS mutator for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Dec 30, 2018
1 parent ead0357 commit 7b90691
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 17 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,7 @@
<mutator>INVERT_NEGS</mutator>
<mutator>MATH</mutator>
<mutator>NEGATE_CONDITIONALS</mutator>
<mutator>REMOVE_CONDITIONALS</mutator>
<mutator>RETURN_VALS</mutator>
<mutator>TRUE_RETURNS</mutator>
<mutator>VOID_METHOD_CALLS</mutator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,8 @@ public void registerImport(DetailAST imp) {
final FullIdent ident = FullIdent.createFullIdent(
imp.getLastChild().getPreviousSibling());
final String fullName = ident.getText();
if (fullName.charAt(fullName.length() - 1) != '*') {
final int lastDot = fullName.lastIndexOf(DOT);
importedClassPackage.put(fullName.substring(lastDot + 1), fullName);
}
final int lastDot = fullName.lastIndexOf(DOT);
importedClassPackage.put(fullName.substring(lastDot + 1), fullName);
}

/**
Expand Down Expand Up @@ -443,17 +441,9 @@ public void checkCoupling() {
* @return true if we should count this class.
*/
private boolean isSignificant(String candidateClassName) {
boolean result = !excludedClasses.contains(candidateClassName)
&& !isFromExcludedPackage(candidateClassName);
if (result) {
for (Pattern pattern : excludeClassesRegexps) {
if (pattern.matcher(candidateClassName).matches()) {
result = false;
break;
}
}
}
return result;
return !excludedClasses.contains(candidateClassName)
&& !isFromExcludedPackage(candidateClassName)
&& !isExcludedClassRegexp(candidateClassName);
}

/**
Expand All @@ -477,6 +467,22 @@ private boolean isFromExcludedPackage(String candidateClassName) {
return isFromExcludedPackage;
}

/**
* Checks if given class should be ignored as it belongs to excluded class regexp.
* @param candidateClassName class to check.
* @return true if we should not count this class.
*/
private boolean isExcludedClassRegexp(String candidateClassName) {
boolean result = false;
for (Pattern pattern : excludeClassesRegexps) {
if (pattern.matcher(candidateClassName).matches()) {
result = true;
break;
}
}
return result;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ public void visitToken(DetailAST ast) {
* @return true if logical operator is part of constructor or method call
*/
private static boolean isPassedInParameter(DetailAST logicalOperator) {
return logicalOperator.getParent().getType() == TokenTypes.EXPR
&& logicalOperator.getParent().getParent().getType() == TokenTypes.ELIST;
return logicalOperator.getParent().getParent().getType() == TokenTypes.ELIST;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@ public void testWrongToken() {
}
}

@Test
public void testSmall() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(BooleanExpressionComplexityCheck.class);
checkConfig.addAttribute("max", "1");

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputBooleanExpressionComplexitySmall.java"), expected);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public void testCalculation() throws Exception {
"113:5: " + getCheckMessage(MSG_KEY, 48, 0),
"123:5: " + getCheckMessage(MSG_KEY, 1, 0),
"124:5: " + getCheckMessage(MSG_KEY, 1, 0),
"126:5: " + getCheckMessage(MSG_KEY, 64, 0),
"136:17: " + getCheckMessage(MSG_KEY, 3, 0),
"150:21: " + getCheckMessage(MSG_KEY, 3, 0),
};

verify(checkConfig, getPath("InputNPathComplexityDefault.java"), expected);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.puppycrawl.tools.checkstyle.checks.metrics.booleanexpressioncomplexity;

public class InputBooleanExpressionComplexitySmall {
public void method() {
java.awt.EventQueue.invokeLater(() -> {
try {
java.lang.reflect.Method stopDispatching = this.getClass()
.getDeclaredMethod("method", null);
stopDispatching.setAccessible(true);
stopDispatching.invoke(this, null);
}
catch (java.lang.reflect.InvocationTargetException | NoSuchMethodException
| IllegalAccessException ex) {
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,41 @@ public void InputNestedTernaryCheck() {
}
public boolean getSmth() { return true; };
public int apply(Object o) { return 0; }

@java.lang.SuppressWarnings({(false) ? "unchecked" :
("" == "") ? (false) ? (true) ? "" : "foo" : " " : "unused",
(false) ? "unchecked" : ("" == "") ? (false) ? (true) ? "" : "foo" : " " : "unused"})
public void seriously() {
}

public void inClass(int type, Short s, int color) {
switch (type) {
case 3:
new Object() {
public void anonymousMethod() {
{
switch (s) {
case 5:
switch (type) {
default:
}
}
}
}
};
default:
new Object() {
class SwitchClass {
{
switch (color) {
case 5:
switch (type) {
default:
}
}
}
}
};
}
}
}

0 comments on commit 7b90691

Please sign in to comment.