public static boolean isAssociativeOperation(PsiPolyadicExpression expression) {
   final IElementType tokenType = expression.getOperationTokenType();
   final PsiType type = expression.getType();
   final PsiPrimitiveType primitiveType;
   if (type instanceof PsiClassType) {
     primitiveType = PsiPrimitiveType.getUnboxedType(type);
     if (primitiveType == null) {
       return false;
     }
   } else if (type instanceof PsiPrimitiveType) {
     primitiveType = (PsiPrimitiveType) type;
   } else {
     return false;
   }
   if (JavaTokenType.PLUS == tokenType || JavaTokenType.ASTERISK == tokenType) {
     return !PsiType.FLOAT.equals(primitiveType) && !PsiType.DOUBLE.equals(primitiveType);
   } else if (JavaTokenType.EQEQ == tokenType || JavaTokenType.NE == tokenType) {
     return PsiType.BOOLEAN.equals(primitiveType);
   } else if (JavaTokenType.AND == tokenType
       || JavaTokenType.OR == tokenType
       || JavaTokenType.XOR == tokenType) {
     return true;
   } else if (JavaTokenType.OROR == tokenType || JavaTokenType.ANDAND == tokenType) {
     return true;
   }
   return false;
 }
  private static void checkMethodCall(RefElement refWhat, final PsiElement element) {
    if (!(refWhat instanceof RefMethod)) return;
    final RefMethod refMethod = (RefMethod) refWhat;
    final PsiElement psiElement = refMethod.getElement();
    if (!(psiElement instanceof PsiMethod)) return;
    final PsiMethod psiMethod = (PsiMethod) psiElement;
    if (!PsiType.BOOLEAN.equals(psiMethod.getReturnType())) return;
    element.accept(
        new JavaRecursiveElementWalkingVisitor() {
          @Override
          public void visitMethodCallExpression(PsiMethodCallExpression call) {
            super.visitMethodCallExpression(call);
            final PsiReferenceExpression methodExpression = call.getMethodExpression();
            if (methodExpression.isReferenceTo(psiMethod)) {
              if (isInvertedMethodCall(methodExpression)) return;
              refMethod.putUserData(ALWAYS_INVERTED, Boolean.FALSE);
            }
          }

          @Override
          public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
            super.visitMethodReferenceExpression(expression);
            if (expression.isReferenceTo(psiElement)) {
              refMethod.putUserData(ALWAYS_INVERTED, Boolean.FALSE);
            }
          }
        });
  }
 @Override
 public void onInitialize(RefElement refElement) {
   if (refElement instanceof RefMethod) {
     final PsiElement element = refElement.getElement();
     if (!(element instanceof PsiMethod)) return;
     if (!PsiType.BOOLEAN.equals(((PsiMethod) element).getReturnType())) return;
     refElement.putUserData(ALWAYS_INVERTED, Boolean.TRUE); // initial mark boolean methods
   }
 }
 private static boolean isCompileTimeFlagReference(PsiElement element) {
   PsiElement resolved =
       element instanceof PsiReferenceExpression
           ? ((PsiReferenceExpression) element).resolve()
           : null;
   if (!(resolved instanceof PsiField)) return false;
   PsiField field = (PsiField) resolved;
   return field.hasModifierProperty(PsiModifier.FINAL)
       && field.hasModifierProperty(PsiModifier.STATIC)
       && PsiType.BOOLEAN.equals(field.getType());
 }
 private static PsiExpression getExpressionToSimplify(final Editor editor, final PsiFile file) {
   int offset = editor.getCaretModel().getOffset();
   PsiElement element = file.findElementAt(offset);
   if (element == null) return null;
   PsiExpression expression = PsiTreeUtil.getParentOfType(element, PsiExpression.class);
   PsiElement parent = expression;
   while (parent instanceof PsiExpression
       && (PsiType.BOOLEAN.equals(((PsiExpression) parent).getType())
           || parent instanceof PsiConditionalExpression)) {
     expression = (PsiExpression) parent;
     parent = parent.getParent();
   }
   return expression;
 }
Exemplo n.º 6
0
 private boolean canBeReused(@NotNull DfaValue dfaValue) {
   if (dfaValue instanceof DfaBoxedValue) {
     DfaValue valueToWrap = ((DfaBoxedValue) dfaValue).getWrappedValue();
     if (valueToWrap instanceof DfaConstValue) {
       return cacheable((DfaConstValue) valueToWrap);
     }
     if (valueToWrap instanceof DfaVariableValue) {
       if (PsiType.BOOLEAN.equals(((DfaVariableValue) valueToWrap).getVariableType())) return true;
       for (DfaValue value : getEquivalentValues(valueToWrap)) {
         if (value instanceof DfaConstValue && cacheable((DfaConstValue) value)) return true;
       }
     }
     return false;
   }
   return true;
 }
Exemplo n.º 7
0
 private static boolean isBoolean(@Nullable PsiType propertyType) {
   return PsiType.BOOLEAN.equals(propertyType)
       || propertyType != null
           && CommonClassNames.JAVA_LANG_BOOLEAN.equals(propertyType.getCanonicalText());
 }
 public static boolean areParenthesesNeeded(
     PsiExpression expression,
     PsiExpression parentExpression,
     boolean ignoreClarifyingParentheses) {
   if (parentExpression instanceof PsiParenthesizedExpression
       || parentExpression instanceof PsiArrayInitializerExpression) {
     return false;
   }
   final int parentPrecedence = getPrecedence(parentExpression);
   final int childPrecedence = getPrecedence(expression);
   if (parentPrecedence > childPrecedence) {
     if (ignoreClarifyingParentheses) {
       if (expression instanceof PsiPolyadicExpression) {
         if (parentExpression instanceof PsiPolyadicExpression
             || parentExpression instanceof PsiConditionalExpression
             || parentExpression instanceof PsiInstanceOfExpression) {
           return true;
         }
       } else if (expression instanceof PsiInstanceOfExpression) {
         return true;
       }
     }
     return false;
   }
   if (parentExpression instanceof PsiPolyadicExpression
       && expression instanceof PsiPolyadicExpression) {
     final PsiPolyadicExpression parentPolyadicExpression =
         (PsiPolyadicExpression) parentExpression;
     final PsiType parentType = parentPolyadicExpression.getType();
     if (parentType == null) {
       return true;
     }
     final PsiPolyadicExpression childPolyadicExpression = (PsiPolyadicExpression) expression;
     final PsiType childType = childPolyadicExpression.getType();
     if (!parentType.equals(childType)) {
       return true;
     }
     if (childType.equalsToText(CommonClassNames.JAVA_LANG_STRING)
         && !PsiTreeUtil.isAncestor(
             parentPolyadicExpression.getOperands()[0], childPolyadicExpression, true)) {
       final PsiExpression[] operands = childPolyadicExpression.getOperands();
       for (PsiExpression operand : operands) {
         if (!childType.equals(operand.getType())) {
           return true;
         }
       }
     } else if (childType.equals(PsiType.BOOLEAN)) {
       final PsiExpression[] operands = childPolyadicExpression.getOperands();
       for (PsiExpression operand : operands) {
         if (!PsiType.BOOLEAN.equals(operand.getType())) {
           return true;
         }
       }
     }
     final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType();
     final IElementType childOperator = childPolyadicExpression.getOperationTokenType();
     if (ignoreClarifyingParentheses) {
       if (!childOperator.equals(parentOperator)) {
         return true;
       }
     }
     final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands();
     if (!PsiTreeUtil.isAncestor(parentOperands[0], expression, false)) {
       if (!isAssociativeOperation(parentPolyadicExpression)
           || JavaTokenType.DIV == childOperator
           || JavaTokenType.PERC == childOperator) {
         return true;
       }
     }
   } else if (parentExpression instanceof PsiConditionalExpression
       && expression instanceof PsiConditionalExpression) {
     final PsiConditionalExpression conditionalExpression =
         (PsiConditionalExpression) parentExpression;
     final PsiExpression condition = conditionalExpression.getCondition();
     return PsiTreeUtil.isAncestor(condition, expression, true);
   }
   return parentPrecedence < childPrecedence;
 }