@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); final PsiType type = expression.getType(); if (!(type instanceof PsiArrayType)) { return; } int size = 1; final PsiExpression[] dimensions = expression.getArrayDimensions(); for (final PsiExpression dimension : dimensions) { final Integer intValue = (Integer)ConstantExpressionUtil.computeCastTo( dimension, PsiType.INT); if (intValue != null) { size *= intValue.intValue(); } } if (size <= m_limit) { return; } if (outOfMemoryExceptionCaught(expression)) { return; } registerNewExpressionError(expression); }
@Override public void visitTypeCastExpression(PsiTypeCastExpression expression) { final PsiTypeElement castTypeElement = expression.getCastType(); PsiExpression operand = expression.getOperand(); Object opValue = getStoredValue(operand); if(castTypeElement == null || opValue == null) { myResult = null; return; } PsiType castType = castTypeElement.getType(); myResult = ConstantExpressionUtil.computeCastTo(opValue, castType); }
@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); } }
@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); } } }
private boolean isAllOnes(PsiExpression expression) { if (m_ignoreExpressionsContainingConstants && !(expression instanceof PsiLiteralExpression)) { return false; } final PsiType expressionType = expression.getType(); final Object value = ConstantExpressionUtil.computeCastTo(expression, expressionType); if (value == null) { return false; } if (value instanceof Integer && ((Integer)value).intValue() == 0xffffffff) { return true; } if (value instanceof Long && ((Long)value).longValue() == 0xffffffffffffffffL) { return true; } if (value instanceof Short && ((Short)value).shortValue() == (short)0xffff) { return true; } if (value instanceof Character && ((Character)value).charValue() == (char)0xffff) { return true; } return value instanceof Byte && ((Byte)value).byteValue() == (byte)0xff; }
public static boolean isZero(@Nullable PsiExpression expression) { if (expression == null) { return false; } final PsiType expressionType = expression.getType(); final Object value = ConstantExpressionUtil.computeCastTo(expression, expressionType); if (value == null) { return false; } if (value instanceof Double && ((Double)value).doubleValue() == 0.0) { return true; } if (value instanceof Float && ((Float)value).floatValue() == 0.0f) { return true; } if (value instanceof Integer && ((Integer)value).intValue() == 0) { return true; } if (value instanceof Long && ((Long)value).longValue() == 0L) { return true; } if (value instanceof Short && ((Short)value).shortValue() == 0) { return true; } if (value instanceof Character && ((Character)value).charValue() == 0) { return true; } return value instanceof Byte && ((Byte)value).byteValue() == 0; }
private static boolean isZero(PsiExpression expression) { final Object value = ConstantExpressionUtil.computeCastTo(expression, PsiType.DOUBLE); if (!(value instanceof Double)) { return false; } final double constantValue = ((Double)value).doubleValue(); return constantValue == 0.0 || constantValue == -0.0; }
private static boolean isMaxDouble(PsiExpression expression) { final Double value = (Double) ConstantExpressionUtil.computeCastTo( expression, PsiType.DOUBLE); //noinspection FloatingPointEquality return value != null && value.doubleValue() == Double.MAX_VALUE; }
private static boolean isMinFloat(PsiExpression expression) { final Float value = (Float) ConstantExpressionUtil.computeCastTo( expression, PsiType.FLOAT); //noinspection FloatingPointEquality return value != null && value.floatValue() == Float.MIN_VALUE; }
private static boolean isMaxFloat(PsiExpression expression) { final Float value = (Float) ConstantExpressionUtil.computeCastTo( expression, PsiType.FLOAT); //noinspection FloatingPointEquality return value != null && value.floatValue() == Float.MAX_VALUE; }
public static boolean isOne(@Nullable PsiExpression expression) { if (expression == null) { return false; } final PsiType expressionType = expression.getType(); final Object value = ConstantExpressionUtil.computeCastTo( expression, expressionType); if (value == null) { return false; } //noinspection FloatingPointEquality if (value instanceof Double && ((Double)value).doubleValue() == 1.0) { return true; } if (value instanceof Float && ((Float)value).floatValue() == 1.0f) { return true; } if (value instanceof Integer && ((Integer)value).intValue() == 1) { return true; } if (value instanceof Long && ((Long)value).longValue() == 1L) { return true; } if (value instanceof Short && ((Short)value).shortValue() == 1) { return true; } if (value instanceof Character && ((Character)value).charValue() == 1) { return true; } return value instanceof Byte && ((Byte)value).byteValue() == 1; }
public static boolean isZero(@Nullable PsiExpression expression) { if(expression == null) { return false; } final PsiType expressionType = expression.getType(); final Object value = ConstantExpressionUtil.computeCastTo(expression, expressionType); if(value == null) { return false; } if(value instanceof Double && ((Double) value).doubleValue() == 0.0) { return true; } if(value instanceof Float && ((Float) value).floatValue() == 0.0f) { return true; } if(value instanceof Integer && ((Integer) value).intValue() == 0) { return true; } if(value instanceof Long && ((Long) value).longValue() == 0L) { return true; } if(value instanceof Short && ((Short) value).shortValue() == 0) { return true; } if(value instanceof Character && ((Character) value).charValue() == 0) { return true; } return value instanceof Byte && ((Byte) value).byteValue() == 0; }
public static Object computeCastTo(PsiExpression expression, PsiType castTo, Set<PsiVariable> visitedVars) { Object value = JavaConstantExpressionEvaluator.computeConstantExpression(expression, visitedVars, false); if (value == null) return null; return ConstantExpressionUtil.computeCastTo(value, castTo); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression .getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!s_execMethodNames.contains(methodName)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "java.sql.Statement")) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] args = argumentList.getExpressions(); if (args.length == 0) { return; } final PsiExpression arg = args[0]; final PsiType type = arg.getType(); if (type == null) { return; } final String typeText = type.getCanonicalText(); if (!CommonClassNames.JAVA_LANG_STRING.equals(typeText)) { return; } final String stringValue = (String)ConstantExpressionUtil.computeCastTo(arg, type); if (stringValue != null) { return; } registerMethodCallError(expression); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"loadLibrary".equals(methodName)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "java.lang.System")) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] args = argumentList.getExpressions(); if (args.length == 0) { return; } final PsiExpression arg = args[0]; final PsiType type = arg.getType(); if (type == null) { return; } final String typeText = type.getCanonicalText(); if (!CommonClassNames.JAVA_LANG_STRING.equals(typeText)) { return; } final String stringValue = (String)ConstantExpressionUtil.computeCastTo(arg, type); if (stringValue != null) { return; } registerMethodCallError(expression); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!s_execMethodNames.contains(methodName)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "java.sql.Connection")) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] args = argumentList.getExpressions(); if (args.length == 0) { return; } final PsiExpression arg = args[0]; final PsiType type = arg.getType(); if (type == null) { return; } final String typeText = type.getCanonicalText(); if (!CommonClassNames.JAVA_LANG_STRING.equals(typeText)) { return; } final String stringValue = (String)ConstantExpressionUtil.computeCastTo(arg, type); if (stringValue != null) { return; } registerMethodCallError(expression); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"replaceAll".equals(methodName)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 2) { return; } final PsiExpression argument = arguments[0]; if (!PsiUtil.isConstantExpression(argument)) { return; } final PsiType argumentType = argument.getType(); if (argumentType == null) { return; } final String canonicalText = argumentType.getCanonicalText(); if (!CommonClassNames.JAVA_LANG_STRING.equals(canonicalText)) { return; } final String argValue = (String)ConstantExpressionUtil.computeCastTo(argument, argumentType); if (!".".equals(argValue)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass containingClass = method.getContainingClass(); if (containingClass == null) { return; } final String qualifiedName = containingClass.getQualifiedName(); if (!CommonClassNames.JAVA_LANG_STRING.equals(qualifiedName)) { return; } registerMethodCallError(expression); }
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; }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!constantMathCall.contains(methodName)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 0) { return; } final PsiExpression argument = arguments[0]; final Object argumentValue = ConstantExpressionUtil.computeCastTo(argument, PsiType.DOUBLE); if (!(argumentValue instanceof Double)) { return; } final double doubleValue = ((Double)argumentValue).doubleValue(); final String valueString = createValueString(methodName, doubleValue); if (valueString == null) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass referencedClass = method.getContainingClass(); if (referencedClass == null) { return; } final String className = referencedClass.getQualifiedName(); if (!"java.lang.Math".equals(className) && !"java.lang.StrictMath".equals(className)) { return; } registerMethodCallError(expression); }
private static boolean hasValue(PsiExpression expression, int testValue) { final Integer value = (Integer) ConstantExpressionUtil.computeCastTo( expression, PsiType.INT); return value != null && value.intValue() == testValue; }