Class ConfusingTernaryRule

  • All Implemented Interfaces:
    JavaParserVisitor, ImmutableLanguage, PropertySource, Rule

    public class ConfusingTernaryRule
    extends AbstractJavaRule
    if (x != y) { diff(); } else { same(); } and
    (!x ? diff() : same());

    XPath can handle the easy cases, e.g.:

        //IfStatement[
          Statement[2]
          and Expression[
            EqualityExpression[@Image="!="] or
            UnaryExpressionNotPlusMinus[@Image="!"]]]
     

    But "&&" and "||" are difficult, since we need a match for all children instead of just one. This can be done by using a double-negative, e.g.:

        not(*[not(matchme)])
     

    Still, XPath is unable to handle arbitrarily nested cases, since it lacks recursion, e.g.:

     if (((x != !y)) || !(x)) {
         diff();
     } else {
         same();
     }