InlineConstantFieldHandler: avoid compiled field initializer

Fixes EA-213600 - assert: ChangeUtil.copyToElement

GitOrigin-RevId: f29bf467ef4cbe6fc99aff24b09994f03f5a8b2e
This commit is contained in:
Tagir Valeev
2019-09-30 10:32:59 +00:00
committed by intellij-monorepo-bot
parent 12fc2fd09e
commit 77a646a2ea
4 changed files with 76 additions and 1 deletions
@@ -135,7 +135,12 @@ public class InlineConstantFieldHandler extends JavaInlineActionHandler {
@Nullable
public static PsiExpression getInitializer(PsiField field) {
if (field.hasInitializer()) {
return field.getInitializer();
PsiExpression initializer = field.getInitializer();
if (initializer instanceof PsiCompiledElement) {
// Could be a literal initializer: we still can inline it, though passing compiled element downstream may cause exceptions
initializer = JavaPsiFacade.getElementFactory(field.getProject()).createExpressionFromText(initializer.getText(), field);
}
return initializer;
}
if (field.hasModifierProperty(PsiModifier.FINAL)) {
@@ -0,0 +1,5 @@
class Test {
void foo() {
int x = Integer.<caret>MIN_VALUE;
}
}
@@ -0,0 +1,5 @@
class Test {
void foo() {
int x = -2147483648;
}
}
@@ -0,0 +1,60 @@
/*
* 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.
*/
package com.intellij.java.refactoring.inline;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.java.refactoring.LightRefactoringTestCase;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.refactoring.inline.InlineConstantFieldProcessor;
import com.intellij.testFramework.IdeaTestUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class InlineConstantFieldNoSourcesTest extends LightRefactoringTestCase {
@NotNull
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath();
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();
}
public void testCompiledConstantNoSource() {
doTest();
}
private void doTest() {
String name = getTestName(false);
@NonNls String fileName = "/refactoring/inlineConstantField/" + name + ".java";
configureByFile(fileName);
PsiElement element = TargetElementUtil
.findTargetElement(getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED);
final PsiReference ref = getFile().findReferenceAt(getEditor().getCaretModel().getOffset());
PsiReferenceExpression refExpr = ref instanceof PsiReferenceExpression ? (PsiReferenceExpression)ref : null;
assertTrue(element instanceof PsiField);
PsiField field = (PsiField)element.getNavigationElement();
new InlineConstantFieldProcessor(field, getProject(), refExpr, true).run();
checkResultByFile(fileName + ".after");
}
}