當前位置: 首頁>>代碼示例>>Java>>正文


Java ExpressionUtils.computeConstantExpression方法代碼示例

本文整理匯總了Java中com.siyeh.ig.psiutils.ExpressionUtils.computeConstantExpression方法的典型用法代碼示例。如果您正苦於以下問題:Java ExpressionUtils.computeConstantExpression方法的具體用法?Java ExpressionUtils.computeConstantExpression怎麽用?Java ExpressionUtils.computeConstantExpression使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.siyeh.ig.psiutils.ExpressionUtils的用法示例。


在下文中一共展示了ExpressionUtils.computeConstantExpression方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: looksLikeExpectedArgument

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private boolean looksLikeExpectedArgument(PsiExpression expression) {
  if (ExpressionUtils.computeConstantExpression(expression) != null || PsiType.NULL.equals(expression.getType())) {
    return true;
  }
  if (expression instanceof PsiReferenceExpression) {
    final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression;
    final PsiElement target = referenceExpression.resolve();
    if (target instanceof PsiEnumConstant) {
      return true;
    }
    if ((target instanceof PsiField)) {
      final PsiField field = (PsiField)target;
      if (field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL)) {
        return true;
      }
    }
  }
  return false;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:20,代碼來源:MisorderedAssertEqualsArgumentsInspectionBase.java

示例2: collapseConstant

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private static String collapseConstant(@NonNls String expressionText,
                                       PsiElement context)
  throws IncorrectOperationException {
  final Project project = context.getProject();
  final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
  final PsiElementFactory factory = psiFacade.getElementFactory();
  final PsiExpression fromOffsetExpression =
    factory.createExpressionFromText(expressionText, context);
  final Object fromOffsetConstant =
    ExpressionUtils.computeConstantExpression(
      fromOffsetExpression);
  if (fromOffsetConstant != null) {
    return fromOffsetConstant.toString();
  }
  else {
    return expressionText;
  }
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:19,代碼來源:ManualArrayCopyInspection.java

示例3: collapseConstant

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private static String collapseConstant(String expressionText,
                                       PsiElement context)
  throws IncorrectOperationException {
  final Project project = context.getProject();
  final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
  final PsiElementFactory factory = psiFacade.getElementFactory();
  final PsiExpression fromOffsetExpression =
    factory.createExpressionFromText(expressionText, context);
  final Object fromOffsetConstant =
    ExpressionUtils.computeConstantExpression(
      fromOffsetExpression);
  if (fromOffsetConstant != null) {
    return fromOffsetConstant.toString();
  }
  else {
    return expressionText;
  }
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:19,代碼來源:ManualArrayToCollectionCopyInspection.java

示例4: isParameterCheckNotNeeded

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private static boolean isParameterCheckNotNeeded(PsiCodeBlock body, PsiParameter parameter) {
  final PsiStatement[] statements = body.getStatements();
  if (statements.length == 0) {
    return true; // incomplete code
  }
  if (statements.length != 1) {
    return false;
  }
  final PsiStatement statement = statements[0];
  if (!(statement instanceof PsiReturnStatement)) {
    return true; // incomplete code
  }
  final PsiReturnStatement returnStatement = (PsiReturnStatement)statement;
  final PsiExpression returnValue = returnStatement.getReturnValue();
  final Object constant = ExpressionUtils.computeConstantExpression(returnValue);
  if (Boolean.FALSE.equals(constant)) {
    return true; // incomplete code
  }
  if (isEqualsBuilderReflectionEquals(returnValue)) {
    return true;
  }
  if (isIdentityEquals(returnValue, parameter)) {
    return true;
  }
  return false;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:27,代碼來源:EqualsWhichDoesntCheckParameterClassInspection.java

示例5: processIntention

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
@Override
protected void processIntention(@NotNull final PsiElement element) throws IncorrectOperationException {
  final PsiExpression expression = (PsiExpression)element;
  final Number value = (Number)ExpressionUtils.computeConstantExpression(expression);
  if (value == null) return;
  final PsiType type = expression.getType();
  final boolean negated = ExpressionUtils.isNegative(expression);

  final String resultString = convertValue(value, type, negated);
  if (resultString == null) return;

  if (negated) {
    PsiReplacementUtil.replaceExpression((PsiExpression)expression.getParent(), resultString);
  }
  else {
    PsiReplacementUtil.replaceExpression(expression, resultString);
  }
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:19,代碼來源:ConvertNumberIntentionBase.java

示例6: isParameterCheckNotNeeded

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private static boolean isParameterCheckNotNeeded(PsiCodeBlock body) {
  final PsiStatement[] statements = body.getStatements();
  if (statements.length == 0) {
    return true;
  }
  if (statements.length != 1) {
    return false;
  }
  final PsiStatement statement = statements[0];
  if (!(statement instanceof PsiReturnStatement)) {
    return false;
  }
  final PsiReturnStatement returnStatement = (PsiReturnStatement)statement;
  final PsiExpression returnValue = returnStatement.getReturnValue();
  final Object constant = ExpressionUtils.computeConstantExpression(returnValue);
  return Boolean.FALSE.equals(constant);
}
 
開發者ID:lshain-android-source,項目名稱:tools-idea,代碼行數:18,代碼來源:EqualsWhichDoesntCheckParameterClassInspection.java

示例7: buildConcatenationText

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private static StringBuilder buildConcatenationText(PsiPolyadicExpression polyadicExpression, StringBuilder out) {
  for (PsiElement element : polyadicExpression.getChildren()) {
    if (element instanceof PsiExpression) {
      final PsiExpression expression = (PsiExpression)element;
      final Object value = ExpressionUtils.computeConstantExpression(expression);
      if (value == null) {
        out.append('?');
      }
      else {
        out.append(value.toString());
      }
    }
    else if (element instanceof PsiWhiteSpace && element.getText().contains("\n") &&
             (out.length() == 0 || out.charAt(out.length() - 1) != '\n')) {
      out.append('\n');
    }
  }
  return out;
}
 
開發者ID:consulo,項目名稱:consulo-java,代碼行數:20,代碼來源:CopyConcatenatedStringToClipboardIntention.java

示例8: buildFix

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
@Nullable
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
  final PsiExpression expression = (PsiExpression)infos[0];
  final Object value = ExpressionUtils.computeConstantExpression(expression);
  if (!(value instanceof  Integer)) {
    return null;
  }
  final int roundingMode = ((Integer)value).intValue();
  if (roundingMode < 0 || roundingMode > 7) {
    return null;
  }
  return new BigDecimalLegacyMethodFix();
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:15,代碼來源:BigDecimalLegacyMethodInspection.java

示例9: isCallToRegexMethod

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private boolean isCallToRegexMethod(PsiMethodCallExpression expression) {
  final PsiReferenceExpression methodExpression = expression.getMethodExpression();
  final String name = methodExpression.getReferenceName();
  if (!regexMethodNames.contains(name)) {
    return false;
  }
  final PsiExpressionList argumentList = expression.getArgumentList();
  final PsiExpression[] arguments = argumentList.getExpressions();
  if (arguments.length == 0) {
    return false;
  }
  final Object value = ExpressionUtils.computeConstantExpression(arguments[0]);
  if (!(value instanceof String)) {
    return false;
  }
  final String regex = (String)value;
  if (ignoreForSplitOptimization && "split".equals(name) && isOptimizedPattern(regex)) {
    return false;
  }
  final PsiMethod method = expression.resolveMethod();
  if (method == null) {
    return false;
  }
  final PsiClass containingClass = method.getContainingClass();
  if (containingClass == null) {
    return false;
  }
  final String className = containingClass.getQualifiedName();
  if (!CommonClassNames.JAVA_LANG_STRING.equals(className)) {
    return false;
  }
  if (Extensions.getRootArea().hasExtensionPoint(AllowedApiFilterExtension.EP_NAME.getName())) {
    //todo[nik] remove this condition when the extension point will be registered in java-analysis-impl module
    return AllowedApiFilterExtension.isClassAllowed("java.util.regex.Pattern", expression);
  }
  return true;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:38,代碼來源:DynamicRegexReplaceableByCompiledPatternInspectionBase.java

示例10: isNegativeArgument

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private static boolean isNegativeArgument(
  @NotNull PsiExpression argument) {
  final Object constant =
    ExpressionUtils.computeConstantExpression(argument);
  if (!(constant instanceof Integer)) {
    return false;
  }
  final Integer integer = (Integer)constant;
  return integer.intValue() < 0;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:11,代碼來源:SuspiciousSystemArraycopyInspection.java

示例11: visitMethodCallExpression

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
  super.visitMethodCallExpression(expression);
  final PsiReferenceExpression methodExpression = expression.getMethodExpression();
  @NonNls final String methodName = methodExpression.getReferenceName();
  if (methodName == null) {
    return;
  }
  final boolean resultSet;
  if (methodName.startsWith("get") || methodName.startsWith("update")) {
    resultSet = true;
  } else if (methodName.startsWith("set")) {
    resultSet = false;
  } else {
    return;
  }
  final PsiExpressionList argumentList = expression.getArgumentList();
  final PsiExpression[] arguments = argumentList.getExpressions();
  if (arguments.length == 0) {
    return;
  }
  final PsiExpression argument = arguments[0];
  final Object val = ExpressionUtils.computeConstantExpression(argument);
  if (!(val instanceof Integer) || ((Integer)val).intValue() != 0) {
    return;
  }
  final PsiExpression qualifier = methodExpression.getQualifierExpression();
  if (resultSet) {
    if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) {
      registerError(argument, Boolean.TRUE);
    }
  } else if (arguments.length > 1) {
    if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.PreparedStatement")) {
      registerError(argument, Boolean.FALSE);
    }
  }
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:38,代碼來源:ResultSetIndexZeroInspection.java

示例12: doFix

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
  final PsiElement element = descriptor.getPsiElement();
  if (!(element instanceof PsiBinaryExpression)) {
    return;
  }
  final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)element;
  final PsiElement parent = binaryExpression.getParent();
  if (!(parent instanceof PsiExpressionList)) {
    return;
  }
  final PsiExpressionList expressionList = (PsiExpressionList)parent;
  final PsiExpression lhs = binaryExpression.getLOperand();
  final PsiExpression rhs = binaryExpression.getROperand();
  if (rhs == null) {
    return;
  }
  final PsiExpression[] expressions = expressionList.getExpressions();
  final int parameter = expressions.length - 1;
  expressionList.add(rhs);
  final Object constant =
    ExpressionUtils.computeConstantExpression(lhs);
  if (constant instanceof String) {
    final PsiExpression newExpression = addParameter(lhs, parameter);
    if (newExpression == null) {
      expressionList.addAfter(lhs, binaryExpression);
    }
    else {
      expressionList.addAfter(newExpression, binaryExpression);
    }
  }
  else {
    expressionList.addAfter(lhs, binaryExpression);
  }
  binaryExpression.delete();
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:37,代碼來源:StringConcatenationInMessageFormatCallInspection.java

示例13: getReplacementIsEmptyCall

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
@Nullable
private String getReplacementIsEmptyCall(PsiExpression lhs, PsiExpression rhs, boolean flipped, IElementType tokenType) {
  final PsiMethodCallExpression callExpression = (PsiMethodCallExpression)lhs;
  final String isEmptyCall = getIsEmptyCall(callExpression);
  if (isEmptyCall == null) {
    return null;
  }
  final Object object = ExpressionUtils.computeConstantExpression(rhs);
  if (!(object instanceof Integer)) {
    return null;
  }
  final Integer integer = (Integer)object;
  final int constant = integer.intValue();
  if (constant != 0) {
    return null;
  }
  if (JavaTokenType.EQEQ.equals(tokenType)) {
    return isEmptyCall;
  }
  if (ignoreNegations) {
    return null;
  }
  if (JavaTokenType.NE.equals(tokenType)) {
    return '!' + isEmptyCall;
  }
  else if (flipped) {
    if (JavaTokenType.LT.equals(tokenType)) {
      return '!' + isEmptyCall;
    }
  }
  else if (JavaTokenType.GT.equals(tokenType)) {
    return '!' + isEmptyCall;
  }
  return null;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:36,代碼來源:SizeReplaceableByIsEmptyInspectionBase.java

示例14: satisfiedBy

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
public boolean satisfiedBy(PsiElement element) {
  if (!(element instanceof PsiPolyadicExpression)) {
    return false;
  }
  if (element instanceof PsiLiteralExpression || element instanceof PsiClassObjectAccessExpression) {
    return false;
  }
  final PsiPolyadicExpression expression = (PsiPolyadicExpression)element;
  final PsiType expressionType = expression.getType();
  if (expressionType == null || expressionType.equalsToText(JAVA_LANG_STRING)) {
    // intention disabled for string concatenations because of performance issues on
    // relatively common large string expressions.
    return false;
  }
  final PsiExpression[] operands = expression.getOperands();
  for (PsiExpression operand : operands) {
    if (operand == null) {
      return false;
    }
    final PsiType type = operand.getType();
    if (type == null || type.equalsToText(JAVA_LANG_STRING)) {
      return false;
    }
  }
  if (!PsiUtil.isConstantExpression(expression)) {
    return false;
  }
  try {
    final Object value = ExpressionUtils.computeConstantExpression(expression, true);
    if (value == null) {
      return false;
    }
  }
  catch (ConstantEvaluationOverflowException ignore) {
    return false;
  }
  final PsiElement parent = element.getParent();
  return !(parent instanceof PsiExpression) || !PsiUtil.isConstantExpression((PsiExpression)parent);
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:40,代碼來源:ConstantExpressionPredicate.java

示例15: checkConstantValueVariableUse

import com.siyeh.ig.psiutils.ExpressionUtils; //導入方法依賴的package包/類
private boolean checkConstantValueVariableUse(
  @Nullable PsiExpression expression,
  @NotNull PsiExpression constantExpression,
  @NotNull PsiElement body) {
  final PsiType constantType = constantExpression.getType();
  if (PsiType.DOUBLE.equals(constantType)) {
    final Object result = ExpressionUtils.computeConstantExpression(
      constantExpression, false);
    if (Double.valueOf(0.0).equals(result) ||
        Double.valueOf(-0.0).equals(result)) {
      return false;
    }
  }
  expression = ParenthesesUtils.stripParentheses(expression);
  if (!(expression instanceof PsiReferenceExpression)) {
    return false;
  }
  final PsiReferenceExpression referenceExpression =
    (PsiReferenceExpression)expression;
  final PsiElement target = referenceExpression.resolve();
  if (!(target instanceof PsiVariable)) {
    return false;
  }
  if (target instanceof PsiField) {
    return false;
  }
  final PsiVariable variable = (PsiVariable)target;
  final VariableReadVisitor visitor = new VariableReadVisitor(variable);
  body.accept(visitor);
  if (!visitor.isRead()) {
    return false;
  }
  registerError(visitor.getReference(), constantExpression);
  return true;
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:36,代碼來源:ConstantValueVariableUseInspection.java


注:本文中的com.siyeh.ig.psiutils.ExpressionUtils.computeConstantExpression方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。