mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
Java: remove enum functionality from constant evaluator
GitOrigin-RevId: edcb80e7ddeee3a45b83c98cf77f3c56fd850298
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e5ac6f48a0
commit
8827db8d65
@@ -64,7 +64,7 @@ public final class ConstantExpressionInspection extends AbstractBaseJavaLocalIns
|
||||
// inspection disabled for long expressions because of performance issues on
|
||||
// relatively common large string expressions.
|
||||
Object value = computeConstant(expression);
|
||||
if (value == null || value instanceof Enum<?>) return;
|
||||
if (value == null) return;
|
||||
if (value instanceof PsiField && !(value instanceof PsiEnumConstant)) return;
|
||||
if (value instanceof PsiElement e && expression instanceof PsiReferenceExpression ref && ref.isReferenceTo(e)) return;
|
||||
String valueText = getValueText(value);
|
||||
|
||||
@@ -537,25 +537,10 @@ final class ConstantExpressionVisitor extends JavaElementVisitor implements PsiC
|
||||
myResult = null;
|
||||
return;
|
||||
}
|
||||
qualifierExpression = ((PsiReferenceExpression) qualifierExpression).getQualifierExpression();
|
||||
qualifierExpression = qualifier.getQualifierExpression();
|
||||
}
|
||||
|
||||
PsiElement resolvedExpression = expression.resolve();
|
||||
if (resolvedExpression instanceof PsiEnumConstant) {
|
||||
String constant = ((PsiEnumConstant)resolvedExpression).getName();
|
||||
PsiReferenceExpression qualifier = (PsiReferenceExpression)expression.getQualifier();
|
||||
if (qualifier == null) return;
|
||||
PsiElement element = qualifier.resolve();
|
||||
if (!(element instanceof PsiClass)) return;
|
||||
String name = ClassUtil.getJVMClassName((PsiClass)element);
|
||||
try {
|
||||
Class aClass = Class.forName(name);
|
||||
//noinspection unchecked
|
||||
myResult = Enum.valueOf(aClass, constant);
|
||||
}
|
||||
catch (Throwable ignore) { }
|
||||
return;
|
||||
}
|
||||
if (resolvedExpression instanceof PsiVariable) {
|
||||
PsiVariable variable = (PsiVariable) resolvedExpression;
|
||||
// avoid cycles
|
||||
|
||||
@@ -1,21 +1,10 @@
|
||||
/*
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.psi;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
|
||||
/**
|
||||
@@ -23,22 +12,6 @@ import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
*/
|
||||
public class ConstantEvaluatorTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
|
||||
public enum MyEnum { Foo, Bar }
|
||||
|
||||
public void testEnum() {
|
||||
myFixture.addClass("package com.intellij.java.psi; public class ConstantEvaluatorTest { public enum MyEnum { Foo, Bar } }");
|
||||
myFixture.configureByText("Test.java", "import com.intellij.java.psi.ConstantEvaluatorTest; class Foo { ConstantEvaluatorTest.MyEnum e = ConstantEvaluatorTest.MyEnum.Foo; }");
|
||||
PsiClass psiClass = ((PsiJavaFile)getFile()).getClasses()[0];
|
||||
PsiField field = psiClass.findFieldByName("e", false);
|
||||
assertNotNull(field);
|
||||
PsiExpression initializer = field.getInitializer();
|
||||
assertNotNull(initializer);
|
||||
|
||||
Object result = JavaPsiFacade.getInstance(getProject()).getConstantEvaluationHelper().computeConstantExpression(initializer);
|
||||
|
||||
assertEquals(MyEnum.Foo, result);
|
||||
}
|
||||
|
||||
public void testPrefixExpressionEvaluation() {
|
||||
PsiJavaFile file = (PsiJavaFile)myFixture.configureByText("A.java", "class A {public static final int VALUE = ~0 >>> 1;}");
|
||||
PsiClass aClass = file.getClasses()[0];
|
||||
|
||||
Reference in New Issue
Block a user