本文整理汇总了Java中com.intellij.psi.util.PsiUtil.isConstantExpression方法的典型用法代码示例。如果您正苦于以下问题:Java PsiUtil.isConstantExpression方法的具体用法?Java PsiUtil.isConstantExpression怎么用?Java PsiUtil.isConstantExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.psi.util.PsiUtil
的用法示例。
在下文中一共展示了PsiUtil.isConstantExpression方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitPolyadicExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
super.visitPolyadicExpression(expression);
if (!ExpressionUtils.hasStringType(expression)) {
return;
}
final PsiExpression[] operands = expression.getOperands();
for (PsiExpression operand : operands) {
operand = ParenthesesUtils.stripParentheses(operand);
if (operand == null) {
return;
}
if (!ExpressionUtils.isEmptyStringLiteral(operand)) {
continue;
}
if (PsiUtil.isConstantExpression(expression)) {
return;
}
registerError(operand, operand);
}
}
示例2: visitAssertStatement
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitAssertStatement(PsiAssertStatement statement) {
super.visitAssertStatement(statement);
final PsiExpression assertCondition =
statement.getAssertCondition();
final PsiExpression expression =
ParenthesesUtils.stripParentheses(assertCondition);
if (expression == null) {
return;
}
if (BoolUtils.isFalse(expression)) {
return;
}
if (!PsiUtil.isConstantExpression(expression)) {
return;
}
registerError(expression);
}
示例3: checkConstantExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Nullable
static HighlightInfo checkConstantExpression(PsiExpression expression) {
final PsiElement parent = expression.getParent();
if (PsiUtil.isAnnotationMethod(parent) || parent instanceof PsiNameValuePair || parent instanceof PsiArrayInitializerMemberValue) {
if (!PsiUtil.isConstantExpression(expression)) {
String description = JavaErrorMessages.message("annotation.non.constant.attribute.value");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
}
}
return null;
}
示例4: checkCondition
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private boolean checkCondition(@Nullable PsiExpression condition,
@Nullable PsiStatement body) {
if (body == null) {
return false;
}
if (!(condition instanceof PsiPolyadicExpression)) {
return false;
}
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)condition;
final IElementType tokenType = polyadicExpression.getOperationTokenType();
if (JavaTokenType.ANDAND == tokenType) {
for (PsiExpression operand : polyadicExpression.getOperands()) {
if (checkCondition(operand, body)) {
return true;
}
}
return false;
}
if (JavaTokenType.EQEQ != tokenType) {
return false;
}
final PsiExpression[] operands = polyadicExpression.getOperands();
if (operands.length != 2) {
return false;
}
final PsiExpression lhs = operands[0];
final PsiExpression rhs = operands[1];
if (PsiUtil.isConstantExpression(lhs)) {
return checkConstantValueVariableUse(rhs, lhs, body);
}
else if (PsiUtil.isConstantExpression(rhs)) {
return checkConstantValueVariableUse(lhs, rhs, body);
}
return false;
}
示例5: visitMethodCallExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
@NonNls final String methodName = methodExpression.getReferenceName();
if (!"exec".equals(methodName)) {
return;
}
final PsiMethod method = expression.resolveMethod();
if (method == null) {
return;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return;
}
final String className = aClass.getQualifiedName();
if (!"java.lang.Runtime".equals(className)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression argument = arguments[0];
final PsiType type = argument.getType();
if (type == null || !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
return;
}
if (PsiUtil.isConstantExpression(argument)) {
return;
}
registerMethodCallError(expression);
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:36,代码来源:RuntimeExecWithNonConstantStringInspection.java
示例6: satisfiedBy
import com.intellij.psi.util.PsiUtil; //导入方法依赖的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);
}
示例7: isEqualsConstant
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static boolean isEqualsConstant(PsiExpression expression, PsiVariable variable) {
if (!(expression instanceof PsiMethodCallExpression)) {
return false;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expression;
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
if (!HardcodedMethodConstants.EQUALS.equals(methodName) && !HardcodedMethodConstants.EQUALS_IGNORE_CASE.equals(methodName)) {
return false;
}
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (!(qualifier instanceof PsiReferenceExpression)) {
return false;
}
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifier;
final PsiElement target = referenceExpression.resolve();
if (!variable.equals(target)) {
return false;
}
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length != 1) {
return false;
}
final PsiExpression argument = arguments[0];
return PsiUtil.isConstantExpression(argument);
}
示例8: checkExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private void checkExpression(PsiExpression expression) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiParenthesizedExpression) {
return;
}
if (ignoreConstantConversions) {
PsiExpression rootExpression = expression;
while (rootExpression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)rootExpression;
rootExpression = parenthesizedExpression.getExpression();
}
if (rootExpression instanceof PsiLiteralExpression || PsiUtil.isConstantExpression(rootExpression)) {
return;
}
}
final PsiType expressionType = expression.getType();
if (expressionType == null || !ClassUtils.isPrimitiveNumericType(expressionType)) {
return;
}
if (PsiType.CHAR.equals(expressionType) && (ignoreCharConversions || isArgumentOfStringIndexOf(parent))) {
return;
}
final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, true);
if (!ClassUtils.isPrimitiveNumericType(expectedType)) {
return;
}
if (expressionType.equals(expectedType)) {
return;
}
if (ignoreWideningConversions && hasLowerPrecision(expressionType, expectedType)) {
return;
}
if (ignoreCharConversions && PsiType.CHAR.equals(expectedType)) {
return;
}
registerError(expression, expression, expressionType, expectedType);
}
示例9: visitMethodCallExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final String referenceName = methodExpression.getReferenceName();
if (!logMethodNameList.contains(referenceName)) {
return;
}
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier == null) {
return;
}
if (!TypeUtils.expressionHasTypeOrSubtype(qualifier, loggerClassName)) {
return;
}
if (isSurroundedByLogGuard(expression, referenceName)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
if (!flagAllUnguarded) {
boolean constant = true;
for (PsiExpression argument : arguments) {
if (!PsiUtil.isConstantExpression(argument)) {
constant = false;
break;
}
}
if (constant) {
return;
}
}
registerMethodCallError(expression);
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:38,代码来源:LogStatementGuardedByLogConditionInspectionBase.java
示例10: canBeCaseLabel
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static boolean canBeCaseLabel(PsiExpression expression, LanguageLevel languageLevel) {
if (expression == null) {
return false;
}
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_5) && expression instanceof PsiReferenceExpression) {
final PsiElement referent = ((PsiReference)expression).resolve();
if (referent instanceof PsiEnumConstant) {
return true;
}
}
final PsiType type = expression.getType();
return (PsiType.INT.equals(type) || PsiType.SHORT.equals(type) || PsiType.BYTE.equals(type) || PsiType.CHAR.equals(type)) &&
PsiUtil.isConstantExpression(expression);
}
示例11: visitBinaryExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitBinaryExpression(
@NotNull PsiBinaryExpression expression) {
super.visitBinaryExpression(expression);
final PsiJavaToken sign = expression.getOperationSign();
final IElementType tokenType = sign.getTokenType();
if (!tokenType.equals(JavaTokenType.LTLT) &&
!tokenType.equals(JavaTokenType.GTGT) &&
!tokenType.equals(JavaTokenType.GTGTGT)) {
return;
}
final PsiType expressionType = expression.getType();
if (expressionType == null) {
return;
}
final PsiExpression rhs = expression.getROperand();
if (rhs == null) {
return;
}
if (!PsiUtil.isConstantExpression(rhs)) {
return;
}
final Integer valueObject =
(Integer)ConstantExpressionUtil.computeCastTo(rhs,
PsiType.INT);
if (valueObject == null) {
return;
}
final int value = valueObject.intValue();
if (expressionType.equals(PsiType.LONG)) {
if (value < 0 || value > 63) {
registerError(sign, valueObject, Boolean.TRUE);
}
}
if (expressionType.equals(PsiType.INT)) {
if (value < 0 || value > 31) {
registerError(sign, valueObject, Boolean.FALSE);
}
}
}
示例12: visitMethodCallExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression xpathArgument = arguments[0];
if (!ExpressionUtils.hasStringType(xpathArgument)) {
return;
}
if (!PsiUtil.isConstantExpression(xpathArgument)) {
return;
}
final PsiType type = xpathArgument.getType();
if (type == null) {
return;
}
final String value = (String)ConstantExpressionUtil.computeCastTo(xpathArgument, type);
if (value == null) {
return;
}
if (!callTakesXPathExpression(expression)) {
return;
}
final XPathFactory xpathFactory = XPathFactory.newInstance();
final XPath xpath = xpathFactory.newXPath();
//noinspection UnusedCatchParameter,ProhibitedExceptionCaught
try {
xpath.compile(value);
}
catch (XPathExpressionException ignore) {
registerError(xpathArgument);
}
}
示例13: visitBinaryExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public void visitBinaryExpression(
@NotNull PsiBinaryExpression expression) {
super.visitBinaryExpression(expression);
final PsiExpression rhs = expression.getROperand();
if (!ComparisonUtils.isEqualityComparison(expression)) {
return;
}
final PsiType expressionType = expression.getType();
if (expressionType == null) {
return;
}
final PsiExpression strippedRhs =
ParenthesesUtils.stripParentheses(rhs);
if (strippedRhs == null) {
return;
}
final PsiExpression lhs = expression.getLOperand();
final PsiExpression strippedLhs =
ParenthesesUtils.stripParentheses(lhs);
if (strippedLhs == null) {
return;
}
if (isConstantMask(strippedLhs) &&
PsiUtil.isConstantExpression(strippedRhs)) {
if (isIncompatibleMask((PsiBinaryExpression)strippedLhs,
strippedRhs)) {
registerError(expression, expression);
}
}
else if (isConstantMask(strippedRhs) &&
PsiUtil.isConstantExpression(strippedLhs)) {
if (isIncompatibleMask((PsiBinaryExpression)strippedRhs,
strippedLhs)) {
registerError(expression, expression);
}
}
}
示例14: isIncompatibleMask
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static boolean isIncompatibleMask(
PsiBinaryExpression maskExpression,
PsiExpression constantExpression) {
final IElementType tokenType =
maskExpression.getOperationTokenType();
final Object constantValue =
ConstantExpressionUtil.computeCastTo(constantExpression,
PsiType.LONG);
if (constantValue == null) {
return false;
}
final long constantLongValue = ((Long)constantValue).longValue();
final PsiExpression maskRhs = maskExpression.getROperand();
final PsiExpression maskLhs = maskExpression.getLOperand();
final long constantMaskValue;
if (PsiUtil.isConstantExpression(maskRhs)) {
final Object rhsValue =
ConstantExpressionUtil.computeCastTo(maskRhs,
PsiType.LONG);
if (rhsValue == null) {
return false; // Might indeed be the case with "null" literal
// whoes constant value evaluates to null. Check out (a|null) case.
}
constantMaskValue = ((Long)rhsValue).longValue();
}
else {
final Object lhsValue =
ConstantExpressionUtil.computeCastTo(maskLhs,
PsiType.LONG);
if (lhsValue == null) {
return false;
}
constantMaskValue = ((Long)lhsValue).longValue();
}
if (tokenType.equals(JavaTokenType.OR)) {
if ((constantMaskValue | constantLongValue) != constantLongValue) {
return true;
}
}
if (tokenType.equals(JavaTokenType.AND)) {
if ((constantMaskValue | constantLongValue) != constantMaskValue) {
return true;
}
}
return false;
}
示例15: buildAppendExpression
import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Nullable
public static StringBuilder buildAppendExpression(@Nullable PsiExpression concatenation, boolean useStringValueOf, @NonNls StringBuilder out) {
if (concatenation == null) return null;
final PsiType type = concatenation.getType();
if (concatenation instanceof PsiPolyadicExpression && type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)concatenation;
final PsiExpression[] operands = polyadicExpression.getOperands();
boolean isConstant = true;
boolean isString = false;
final StringBuilder builder = new StringBuilder();
for (PsiExpression operand : operands) {
if (isConstant && PsiUtil.isConstantExpression(operand)) {
if (builder.length() != 0) {
builder.append('+');
}
final PsiType operandType = operand.getType();
if (operandType != null && operandType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
isString = true;
}
builder.append(operand.getText());
}
else {
isConstant = false;
if (builder.length() != 0) {
append(builder, useStringValueOf && !isString, out);
builder.setLength(0);
}
buildAppendExpression(operand, useStringValueOf, out);
}
}
if (builder.length() != 0) {
append(builder, false, out);
}
}
else if (concatenation instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)concatenation;
final PsiExpression expression = parenthesizedExpression.getExpression();
if (expression != null) {
return buildAppendExpression(expression, useStringValueOf, out);
}
}
else {
append(concatenation.getText(), useStringValueOf && (type == null || !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)), out);
}
return out;
}