Issue #974: PMD violation ConfusingTernary (partial fix)

This commit is contained in:
Roman Ivanov 2015-10-07 06:08:39 -07:00
parent 423ed9e469
commit f4e5c1e2b2
8 changed files with 51 additions and 52 deletions

View File

@ -217,18 +217,7 @@ public class DeclarationOrderCheck extends Check {
private void processModifiers(DetailAST ast) {
final ScopeState state = scopeStates.peek();
if (ast.findFirstToken(TokenTypes.LITERAL_STATIC) != null) {
if (state.currentScopeState > STATE_STATIC_VARIABLE_DEF) {
if (!ignoreModifiers
|| state.currentScopeState > STATE_INSTANCE_VARIABLE_DEF) {
log(ast, MSG_STATIC);
}
}
else {
state.currentScopeState = STATE_STATIC_VARIABLE_DEF;
}
}
else {
if (ast.findFirstToken(TokenTypes.LITERAL_STATIC) == null) {
if (state.currentScopeState > STATE_INSTANCE_VARIABLE_DEF) {
log(ast, MSG_INSTANCE);
}
@ -237,6 +226,17 @@ public class DeclarationOrderCheck extends Check {
state.currentScopeState = STATE_INSTANCE_VARIABLE_DEF;
}
}
else {
if (state.currentScopeState > STATE_STATIC_VARIABLE_DEF) {
if (!ignoreModifiers
|| state.currentScopeState > STATE_INSTANCE_VARIABLE_DEF) {
log(ast, MSG_STATIC);
}
}
else {
state.currentScopeState = STATE_STATIC_VARIABLE_DEF;
}
}
final Scope access = ScopeUtils.getScopeFromMods(ast);
if (state.declarationAccess.compareTo(access) > 0) {

View File

@ -379,7 +379,20 @@ public class EqualsAvoidNullCheck extends Check {
boolean result = false;
final DetailAST previousSiblingAst = objCalledOn.getPreviousSibling();
final String name = objCalledOn.getText();
if (previousSiblingAst != null) {
if (previousSiblingAst == null) {
FieldFrame frame = currentFrame;
while (frame != null) {
final DetailAST field = frame.findField(name);
if (field != null
&& (frame.isClassOrEnumOrEnumConstDef()
|| checkLineNo(field, objCalledOn))) {
result = STRING.equals(getFieldType(field));
break;
}
frame = frame.getParent();
}
}
else {
if (previousSiblingAst.getType() == TokenTypes.LITERAL_THIS) {
final DetailAST field = getObjectFrame(currentFrame).findField(name);
result = STRING.equals(getFieldType(field));
@ -397,19 +410,6 @@ public class EqualsAvoidNullCheck extends Check {
}
}
}
else {
FieldFrame frame = currentFrame;
while (frame != null) {
final DetailAST field = frame.findField(name);
if (field != null
&& (frame.isClassOrEnumOrEnumConstDef()
|| checkLineNo(field, objCalledOn))) {
result = STRING.equals(getFieldType(field));
break;
}
frame = frame.getParent();
}
}
return result;
}

View File

@ -322,14 +322,14 @@ public final class ModifiedControlVariableCheck extends Check {
*/
private void leaveForDef(DetailAST ast) {
final DetailAST forInitAST = ast.findFirstToken(TokenTypes.FOR_INIT);
if (forInitAST != null) {
final Set<String> variablesManagedByForLoop = getVariablesManagedByForLoop(ast);
popCurrentVariables(variablesManagedByForLoop.size());
}
else {
if (forInitAST == null) {
// this is for-each loop, just pop variables
getCurrentVariables().pop();
}
else {
final Set<String> variablesManagedByForLoop = getVariablesManagedByForLoop(ast);
popCurrentVariables(variablesManagedByForLoop.size());
}
}
/**

View File

@ -93,12 +93,11 @@ public class MultipleStringLiteralsCheck extends Check {
* if unable to create Pattern object
*/
public final void setIgnoreStringsRegexp(String ignoreStringsRegexp) {
if (ignoreStringsRegexp != null
&& !ignoreStringsRegexp.isEmpty()) {
pattern = CommonUtils.createPattern(ignoreStringsRegexp);
if (ignoreStringsRegexp == null || ignoreStringsRegexp.isEmpty()) {
pattern = null;
}
else {
pattern = null;
pattern = CommonUtils.createPattern(ignoreStringsRegexp);
}
}

View File

@ -169,13 +169,13 @@ public final class OneStatementPerLineCheck extends Check {
*/
private static boolean isMultilineStatement(DetailAST ast) {
final boolean multiline;
if (ast.getPreviousSibling() != null) {
final DetailAST prevSibling = ast.getPreviousSibling();
multiline = prevSibling.getLineNo() != ast.getLineNo()
&& ast.getParent() != null;
if (ast.getPreviousSibling() == null) {
multiline = false;
}
else {
multiline = false;
final DetailAST prevSibling = ast.getPreviousSibling();
multiline = prevSibling.getLineNo() != ast.getLineNo()
&& ast.getParent() != null;
}
return multiline;
}

View File

@ -505,11 +505,11 @@ public class VariableDeclarationUsageDistanceCheck extends Check {
exprWithVariableUsage = blockWithVariableUsage.getFirstChild();
}
currentScopeAst = exprWithVariableUsage;
if (exprWithVariableUsage != null) {
variableUsageAst = exprWithVariableUsage;
if (exprWithVariableUsage == null) {
variableUsageAst = blockWithVariableUsage;
}
else {
variableUsageAst = blockWithVariableUsage;
variableUsageAst = exprWithVariableUsage;
}
}
// If variable usage exists in different scopes, then distance =

View File

@ -164,11 +164,11 @@ public final class ThrowsCountCheck extends Check {
private static String getAnnotationName(DetailAST annotation) {
final DetailAST dotAst = annotation.findFirstToken(TokenTypes.DOT);
String name;
if (dotAst != null) {
name = dotAst.findFirstToken(TokenTypes.IDENT).getText();
if (dotAst == null) {
name = annotation.findFirstToken(TokenTypes.IDENT).getText();
}
else {
name = annotation.findFirstToken(TokenTypes.IDENT).getText();
name = dotAst.findFirstToken(TokenTypes.IDENT).getText();
}
return name;
}

View File

@ -110,17 +110,17 @@ final class ImportControlLoader extends AbstractLoader {
final String pkg = attributes.getValue(PKG_ATTRIBUTE_NAME);
final boolean regex = attributes.getValue("regex") != null;
final Guard guard;
if (pkg != null) {
final boolean exactMatch =
attributes.getValue("exact-match") != null;
guard = new Guard(isAllow, isLocalOnly, pkg, exactMatch, regex);
}
else {
if (pkg == null) {
// handle class names which can be normal class names or regular
// expressions
final String clazz = safeGet(attributes, "class");
guard = new Guard(isAllow, isLocalOnly, clazz, regex);
}
else {
final boolean exactMatch =
attributes.getValue("exact-match") != null;
guard = new Guard(isAllow, isLocalOnly, pkg, exactMatch, regex);
}
final PkgControl pkgControl = stack.peek();
pkgControl.addGuard(guard);