ensure evaluation don't downcast to long (IDEA-167984)

This commit is contained in:
Anna.Kozlova
2017-02-10 14:57:36 +01:00
parent 677046e3bc
commit dfe0c02166
2 changed files with 15 additions and 3 deletions

View File

@@ -483,9 +483,12 @@ class ConstantExpressionVisitor extends JavaElementVisitor implements PsiConstan
else if (tokenType == JavaTokenType.TILDE) {
if (operandValue instanceof Character) operandValue = Integer.valueOf(((Character)operandValue).charValue());
if (isIntegral(operandValue)) {
value = operandValue instanceof Long
? Long.valueOf(~((Number)operandValue).longValue())
: Integer.valueOf(~((Number)operandValue).intValue());
if (operandValue instanceof Long) {
value = Long.valueOf(~((Number)operandValue).longValue());
}
else {
value = Integer.valueOf(~((Number)operandValue).intValue());
}
}
}
else if (tokenType == JavaTokenType.EXCL) {

View File

@@ -37,4 +37,13 @@ public class ConstantEvaluatorTest extends LightCodeInsightFixtureTestCase {
assertEquals(MyEnum.Foo, result);
}
public void testPrefixExpressionEvaluation() throws Exception {
PsiJavaFile file = (PsiJavaFile)myFixture.configureByText("A.java", "class A {public static final int VALUE = ~0 >>> 1;}");
PsiClass aClass = file.getClasses()[0];
PsiField vField = aClass.getFields()[0];
Object result = JavaPsiFacade.getInstance(getProject()).getConstantEvaluationHelper().computeConstantExpression(vField.getInitializer());
assertEquals(2147483647, result);
}
}