public void testExprNewArrayPrimitive2Rvalue() throws Exception {
   doTestFieldType(
       "myField",
       "Expr",
       PsiType.BOOLEAN.createArrayType().createArrayType(),
       PsiType.INT.createArrayType().createArrayType());
 }
 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;
 }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
0
 @SuppressWarnings({"HardCodedStringLiteral"})
 public static String getPrimitiveSignature(String typeName) {
   if (PsiType.BOOLEAN.getCanonicalText().equals(typeName)) {
     return "Z";
   } else if (PsiType.BYTE.getCanonicalText().equals(typeName)) {
     return "B";
   } else if (PsiType.CHAR.getCanonicalText().equals(typeName)) {
     return "C";
   } else if (PsiType.SHORT.getCanonicalText().equals(typeName)) {
     return "S";
   } else if (PsiType.INT.getCanonicalText().equals(typeName)) {
     return "I";
   } else if (PsiType.LONG.getCanonicalText().equals(typeName)) {
     return "J";
   } else if (PsiType.FLOAT.getCanonicalText().equals(typeName)) {
     return "F";
   } else if (PsiType.DOUBLE.getCanonicalText().equals(typeName)) {
     return "D";
   } else if (PsiType.VOID.getCanonicalText().equals(typeName)) {
     return "V";
   }
   return null;
 }
    @Override
    public void visitConditionalExpression(GrConditionalExpression exp) {
      super.visitConditionalExpression(exp);
      final GrExpression condition = exp.getCondition();
      final PsiType type = condition.getType();
      if (type == null || !(PsiType.BOOLEAN.isAssignableFrom(type))) {
        return;
      }

      if (ErrorUtil.containsError(exp)) return;

      final GrExpression thenExpression = exp.getThenBranch();
      if (thenExpression == null) {
        return;
      }
      final GrExpression elseExpression = exp.getElseBranch();
      if (elseExpression == null) {
        return;
      }
      if ((isFalse(thenExpression) && isTrue(elseExpression))
          || (isTrue(thenExpression) && isFalse(elseExpression))) {
        registerError(exp);
      }
    }
Ejemplo n.º 10
0
 public static Value createValue(VirtualMachineProxyImpl vm, String expectedType, boolean value) {
   if (PsiType.BOOLEAN.getPresentableText().equals(expectedType)) {
     return vm.mirrorOf(value);
   }
   return null;
 }
  @Override
  public void handleInsert(InsertionContext context) {
    PsiVariable variable = getObject();

    Document document = context.getDocument();
    document.replaceString(context.getStartOffset(), context.getTailOffset(), variable.getName());
    context.commitDocument();

    if (variable instanceof PsiField) {
      if (willBeImported()) {
        RangeMarker toDelete =
            JavaCompletionUtil.insertTemporary(context.getTailOffset(), document, " ");
        context.commitDocument();
        final PsiReferenceExpression ref =
            PsiTreeUtil.findElementOfClassAtOffset(
                context.getFile(), context.getStartOffset(), PsiReferenceExpression.class, false);
        if (ref != null) {
          ref.bindToElementViaStaticImport(((PsiField) variable).getContainingClass());
          PostprocessReformattingAspect.getInstance(ref.getProject()).doPostponedFormatting();
        }
        if (toDelete.isValid()) {
          document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
        }
        context.commitDocument();
      } else if (shouldQualify((PsiField) variable, context)) {
        qualifyFieldReference(context, (PsiField) variable);
      }
    }

    PsiReferenceExpression ref =
        PsiTreeUtil.findElementOfClassAtOffset(
            context.getFile(), context.getTailOffset() - 1, PsiReferenceExpression.class, false);
    if (ref != null) {
      JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(ref);
    }

    ref =
        PsiTreeUtil.findElementOfClassAtOffset(
            context.getFile(), context.getTailOffset() - 1, PsiReferenceExpression.class, false);
    makeVariableFinalIfNeeded(context, ref);

    final char completionChar = context.getCompletionChar();
    if (completionChar == '=') {
      context.setAddCompletionChar(false);
      TailType.EQ.processTail(context.getEditor(), context.getTailOffset());
    } else if (completionChar == ','
        && getAttribute(LookupItem.TAIL_TYPE_ATTR) != TailType.UNKNOWN) {
      context.setAddCompletionChar(false);
      TailType.COMMA.processTail(context.getEditor(), context.getTailOffset());
      AutoPopupController.getInstance(context.getProject())
          .autoPopupParameterInfo(context.getEditor(), null);
    } else if (completionChar == ':') {
      context.setAddCompletionChar(false);
      TailType.COND_EXPR_COLON.processTail(context.getEditor(), context.getTailOffset());
    } else if (completionChar == '.') {
      AutoPopupController.getInstance(context.getProject())
          .autoPopupMemberLookup(context.getEditor(), null);
    } else if (completionChar == '!' && PsiType.BOOLEAN.isAssignableFrom(variable.getType())) {
      context.setAddCompletionChar(false);
      if (ref != null) {
        FeatureUsageTracker.getInstance()
            .triggerFeatureUsed(CodeCompletionFeatures.EXCLAMATION_FINISH);
        document.insertString(ref.getTextRange().getStartOffset(), "!");
      }
    }
  }
Ejemplo n.º 12
0
 private static boolean isBoolean(@Nullable PsiType propertyType) {
   return PsiType.BOOLEAN.equals(propertyType)
       || propertyType != null
           && CommonClassNames.JAVA_LANG_BOOLEAN.equals(propertyType.getCanonicalText());
 }
  @Override
  public Result acceptChar(char c, final int prefixLength, final Lookup lookup) {
    if (!lookup.getPsiFile().getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
      return null;
    }

    LookupElement item = lookup.getCurrentItem();
    if (item == null) return null;

    final Object o = item.getObject();
    if (c == '!') {
      if (o instanceof PsiVariable) {
        if (PsiType.BOOLEAN.isAssignableFrom(((PsiVariable) o).getType()))
          return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
      }
      if (o instanceof PsiMethod) {
        final PsiType type = ((PsiMethod) o).getReturnType();
        if (type != null && PsiType.BOOLEAN.isAssignableFrom(type))
          return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
      }
      if (o instanceof PsiKeyword && ((PsiKeyword) o).textMatches(PsiKeyword.INSTANCEOF)) {
        return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
      }

      return null;
    }
    if (c == '.' && isWithinLiteral(lookup)) return Result.ADD_TO_PREFIX;

    if (c == ':') {
      PsiFile file = lookup.getPsiFile();
      PsiDocumentManager.getInstance(file.getProject())
          .commitDocument(lookup.getEditor().getDocument());
      PsiElement leaf = file.findElementAt(lookup.getEditor().getCaretModel().getOffset() - 1);
      if (PsiUtil.getLanguageLevel(file).isAtLeast(LanguageLevel.JDK_1_8)) {
        PsiStatement statement = PsiTreeUtil.getParentOfType(leaf, PsiStatement.class);
        if (statement == null
            || statement.getTextRange().getStartOffset()
                != leaf.getTextRange().getStartOffset()) { // not typing a statement label
          return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
      }
      if (PsiTreeUtil.getParentOfType(leaf, PsiSwitchLabelStatement.class) != null
          || PsiTreeUtil.getParentOfType(leaf, PsiConditionalExpression.class) != null) {
        return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
      }
      return Result.HIDE_LOOKUP;
    }

    if (c == '[' || c == ']' || c == ')' || c == '>')
      return CharFilter.Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '<' && o instanceof PsiClass) return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '(') {
      if (o instanceof PsiClass) {
        if (PsiJavaPatterns.psiElement()
            .afterLeaf(PsiKeyword.NEW)
            .accepts(lookup.getPsiElement())) {
          return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        return Result.HIDE_LOOKUP;
      }
      if (o instanceof PsiType) {
        return Result.HIDE_LOOKUP;
      }
    }
    if ((c == ',' || c == '=') && o instanceof PsiVariable) {
      int lookupStart = lookup.getLookupStart();
      String name = ((PsiVariable) o).getName();
      if (lookupStart >= 0 && name != null && name.equals(lookup.itemPattern(item))) {
        return Result.HIDE_LOOKUP;
      }
    }

    if (c == '#'
        && PsiTreeUtil.getParentOfType(lookup.getPsiElement(), PsiDocComment.class) != null) {
      if (o instanceof PsiClass) {
        return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
      }
    }
    if (c == '(' && PsiKeyword.RETURN.equals(item.getLookupString())) {
      return Result.HIDE_LOOKUP;
    }
    return null;
  }
 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;
 }