@Override
 public void visitBinaryExpression(PsiBinaryExpression expression) {
   final IElementType tokenType = expression.getOperationTokenType();
   if (JavaTokenType.OROR != tokenType) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   final PsiExpression rhs = expression.getROperand();
   final Pair<PsiReferenceExpression, PsiExpression> pair1 = getReferenceExpressionPair(lhs);
   final Pair<PsiReferenceExpression, PsiExpression> pair2 = getReferenceExpressionPair(rhs);
   if (pair1 == null || pair2 == null) {
     return;
   }
   final PsiExpression expression1 = pair1.getSecond();
   final PsiExpression expression2 = pair2.getSecond();
   if (expression1 == null || expression2 == null) {
     return;
   }
   final Project project = expression1.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiConstantEvaluationHelper constantEvaluationHelper =
       psiFacade.getConstantEvaluationHelper();
   final Object constant1 = constantEvaluationHelper.computeConstantExpression(expression1);
   final Object constant2 = constantEvaluationHelper.computeConstantExpression(expression2);
   if (constant1 == null || constant2 == null || constant1 == constant2) {
     return;
   }
   final PsiReferenceExpression referenceExpression1 = pair1.getFirst();
   final PsiReferenceExpression referenceExpression2 = pair2.getFirst();
   if (referenceExpression1.resolve() == referenceExpression2.resolve()) {
     registerError(expression);
   }
 }
 private boolean isTrivial(PsiMethod method) {
   final PsiCodeBlock body = method.getBody();
   if (body == null) {
     return true;
   }
   final PsiStatement[] statements = body.getStatements();
   if (statements.length == 0) {
     return true;
   }
   final Project project = method.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiConstantEvaluationHelper evaluationHelper = psiFacade.getConstantEvaluationHelper();
   for (PsiStatement statement : statements) {
     if (!(statement instanceof PsiIfStatement)) {
       return false;
     }
     final PsiIfStatement ifStatement = (PsiIfStatement) statement;
     final PsiExpression condition = ifStatement.getCondition();
     final Object result = evaluationHelper.computeConstantExpression(condition);
     if (result == null || !result.equals(Boolean.FALSE)) {
       return false;
     }
   }
   return true;
 }
示例#3
0
  @NotNull
  private static PsiExpression psiToClsExpression(
      final PsiExpression expr, @Nullable final ClsElementImpl parent) {
    if (expr instanceof PsiLiteralExpression) {
      return new ClsLiteralExpressionImpl(
          parent, expr.getText(), expr.getType(), ((PsiLiteralExpression) expr).getValue());
    } else if (expr instanceof PsiPrefixExpression) {
      final PsiPrefixExpression prefixExpr = (PsiPrefixExpression) expr;
      final ClsJavaTokenImpl operation =
          new ClsJavaTokenImpl(
              null, prefixExpr.getOperationTokenType(), prefixExpr.getOperationSign().getText());
      final ClsLiteralExpressionImpl literal =
          (ClsLiteralExpressionImpl) psiToClsExpression(prefixExpr.getOperand(), null);
      return new ClsPrefixExpressionImpl(parent, operation, literal);
    } else if (expr instanceof PsiClassObjectAccessExpression) {
      final String canonicalClassText =
          ((PsiClassObjectAccessExpression) expr).getOperand().getType().getCanonicalText();
      return new ClsClassObjectAccessExpressionImpl(parent, canonicalClassText);
    } else if (expr instanceof PsiReferenceExpression) {
      return new ClsReferenceExpressionImpl(parent, (PsiReferenceExpression) expr);
    } else if (expr instanceof PsiBinaryExpression) {
      final PsiBinaryExpression binaryExpr = (PsiBinaryExpression) expr;
      final PsiExpression lOperand = psiToClsExpression(binaryExpr.getLOperand(), null);
      final ClsJavaTokenImpl operation =
          new ClsJavaTokenImpl(
              null, binaryExpr.getOperationTokenType(), binaryExpr.getOperationSign().getText());
      final PsiExpression rOperand = psiToClsExpression(binaryExpr.getROperand(), null);
      if (lOperand instanceof ClsLiteralExpressionImpl) {
        return new ClsBinaryExpressionImpl(
            parent,
            (ClsLiteralExpressionImpl) lOperand,
            operation,
            (ClsLiteralExpressionImpl) rOperand);
      } else if (lOperand instanceof ClsPrefixExpressionImpl) {
        return new ClsBinaryExpressionImpl(
            parent,
            (ClsPrefixExpressionImpl) lOperand,
            operation,
            (ClsLiteralExpressionImpl) rOperand);
      }
    } else {
      final PsiConstantEvaluationHelper evaluator =
          JavaPsiFacade.getInstance(expr.getProject()).getConstantEvaluationHelper();
      final Object value = evaluator.computeConstantExpression(expr);
      if (value != null) {
        return new ClsLiteralExpressionImpl(parent, expr.getText(), expr.getType(), value);
      }
    }

    LOG.error("Unable to compute expression value: " + expr);
    return null;
  }
  private static String resolveEpName(PsiField psiField) {
    final PsiExpression initializer = psiField.getInitializer();

    PsiExpressionList expressionList = null;
    if (initializer instanceof PsiMethodCallExpression) {
      expressionList = ((PsiMethodCallExpression) initializer).getArgumentList();
    } else if (initializer instanceof PsiNewExpression) {
      expressionList = ((PsiNewExpression) initializer).getArgumentList();
    }
    if (expressionList == null) return null;

    final PsiExpression[] expressions = expressionList.getExpressions();
    if (expressions.length != 1) return null;

    final PsiExpression epNameExpression = expressions[0];
    final PsiConstantEvaluationHelper helper =
        JavaPsiFacade.getInstance(psiField.getProject()).getConstantEvaluationHelper();
    final Object o = helper.computeConstantExpression(epNameExpression);
    return o instanceof String ? (String) o : null;
  }
 @Nullable
 public static String getTestDataBasePath(PsiClass psiClass) {
   final PsiAnnotation annotation =
       AnnotationUtil.findAnnotationInHierarchy(
           psiClass, Collections.singleton(TEST_DATA_PATH_ANNOTATION_QUALIFIED_NAME));
   if (annotation != null) {
     final PsiAnnotationMemberValue value =
         annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
     if (value instanceof PsiExpression) {
       final Project project = value.getProject();
       final PsiConstantEvaluationHelper evaluationHelper =
           JavaPsiFacade.getInstance(project).getConstantEvaluationHelper();
       final Object constantValue = evaluationHelper.computeConstantExpression(value, false);
       if (constantValue instanceof String) {
         String path = (String) constantValue;
         if (path.contains(CONTENT_ROOT_VARIABLE)) {
           final ProjectFileIndex fileIndex =
               ProjectRootManager.getInstance(project).getFileIndex();
           final VirtualFile file = psiClass.getContainingFile().getVirtualFile();
           if (file == null) {
             return null;
           }
           final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
           if (contentRoot == null) return null;
           path = path.replace(CONTENT_ROOT_VARIABLE, contentRoot.getPath());
         }
         if (path.contains(PROJECT_ROOT_VARIABLE)) {
           final VirtualFile baseDir = project.getBaseDir();
           if (baseDir == null) {
             return null;
           }
           path = path.replace(PROJECT_ROOT_VARIABLE, baseDir.getPath());
         }
         return path;
       }
     }
   }
   return null;
 }
  @Override
  public JComponent getPreviewComponent(@NotNull PsiElement element) {
    final PsiNewExpression psiNewExpression =
        PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);

    if (psiNewExpression != null) {
      final PsiJavaCodeReferenceElement referenceElement =
          PsiTreeUtil.getChildOfType(psiNewExpression, PsiJavaCodeReferenceElement.class);

      if (referenceElement != null) {
        final PsiReference reference = referenceElement.getReference();

        if (reference != null) {
          final PsiElement psiElement = reference.resolve();

          if (psiElement instanceof PsiClass
              && "java.awt.Color".equals(((PsiClass) psiElement).getQualifiedName())) {
            final PsiExpressionList argumentList = psiNewExpression.getArgumentList();

            if (argumentList != null) {
              final PsiExpression[] expressions = argumentList.getExpressions();
              int[] values = ArrayUtil.newIntArray(expressions.length);
              float[] values2 = new float[expressions.length];
              int i = 0;
              int j = 0;

              final PsiConstantEvaluationHelper helper =
                  JavaPsiFacade.getInstance(element.getProject()).getConstantEvaluationHelper();
              for (final PsiExpression each : expressions) {
                final Object o = helper.computeConstantExpression(each);
                if (o instanceof Integer) {
                  values[i] = ((Integer) o).intValue();
                  if (expressions.length != 1) {
                    values[i] = values[i] > 255 ? 255 : values[i] < 0 ? 0 : values[i];
                  }

                  i++;
                } else if (o instanceof Float) {
                  values2[j] = ((Float) o).floatValue();
                  values2[j] = values2[j] > 1 ? 1 : values2[j] < 0 ? 0 : values2[j];
                  j++;
                }
              }

              Color c = null;
              if (i == expressions.length) {
                if (i == 1 && values[0] > 255) {
                  c = new Color(values[0]);
                } else {
                  switch (values.length) {
                    case 1:
                      c = new Color(values[0]);
                      break;
                    case 3:
                      c = new Color(values[0], values[1], values[2]);
                      break;
                    case 4:
                      c = new Color(values[0], values[1], values[2], values[3]);
                      break;
                    default:
                      break;
                  }
                }
              } else if (j == expressions.length) {
                switch (values2.length) {
                  case 3:
                    c = new Color(values2[0], values2[1], values2[2]);
                    break;
                  case 4:
                    c = new Color(values2[0], values2[1], values2[2], values2[3]);
                    break;
                  default:
                    break;
                }
              }

              if (c != null) {
                return new ColorPreviewComponent(c);
              }
            }
          }
        }
      }
    }

    if (ColorChooserIntentionAction.isInsideDecodeOrGetColorMethod(element)) {
      final String color = StringUtil.unquoteString(element.getText());
      try {
        return new ColorPreviewComponent(Color.decode(color));
      } catch (NumberFormatException ignore) {
      }
    }

    if (PlatformPatterns.psiElement(PsiIdentifier.class)
        .withParent(PlatformPatterns.psiElement(PsiReferenceExpression.class))
        .accepts(element)) {
      final PsiReference reference = element.getParent().getReference();

      if (reference != null) {
        final PsiElement psiElement = reference.resolve();

        if (psiElement instanceof PsiField) {
          if ("java.awt.Color"
              .equals(((PsiField) psiElement).getContainingClass().getQualifiedName())) {
            final String colorName =
                ((PsiField) psiElement).getName().toLowerCase().replace("_", "");
            final String hex = ColorSampleLookupValue.getHexCodeForColorName(colorName);
            return new ColorPreviewComponent(Color.decode("0x" + hex.substring(1)));
          }
        }
      }
    }

    if (PlatformPatterns.psiElement()
        .withParent(PlatformPatterns.psiElement(PsiLiteralExpression.class))
        .accepts(element)) {
      final PsiLiteralExpression psiLiteralExpression = (PsiLiteralExpression) element.getParent();
      if (psiLiteralExpression != null) {
        return ImagePreviewComponent.getPreviewComponent(psiLiteralExpression);
      }
    }

    return null;
  }
 private boolean alwaysTrue(GroovyPsiElement condition) {
   return Boolean.TRUE.equals(myConstantEvaluator.computeConstantExpression(condition));
 }