mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
inline class to anonymous: generate final modifier according to setting for java 8 (IDEA-157802)
This commit is contained in:
+7
-2
@@ -17,11 +17,13 @@ package com.intellij.refactoring.inline;
|
||||
|
||||
import com.intellij.codeInsight.ChangeContextUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.patterns.ElementPattern;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
|
||||
import com.intellij.psi.search.ProjectScope;
|
||||
@@ -246,7 +248,8 @@ class InlineToAnonymousConstructorProcessor {
|
||||
}
|
||||
|
||||
private PsiVariable generateLocal(final String baseName, final PsiType type, final PsiExpression initializer) {
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myClass.getProject());
|
||||
final Project project = myClass.getProject();
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
|
||||
|
||||
String baseNameForIndex = baseName;
|
||||
int index = 0;
|
||||
@@ -262,7 +265,9 @@ class InlineToAnonymousConstructorProcessor {
|
||||
try {
|
||||
final PsiDeclarationStatement declaration = myElementFactory.createVariableDeclarationStatement(localName, type, initializer);
|
||||
PsiVariable variable = (PsiVariable)declaration.getDeclaredElements()[0];
|
||||
PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, true);
|
||||
if (!PsiUtil.isLanguageLevel8OrHigher(myNewExpression) || CodeStyleSettingsManager.getSettings(project).GENERATE_FINAL_LOCALS) {
|
||||
PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, true);
|
||||
}
|
||||
final PsiElement parent = myNewStatement.getParent();
|
||||
if (parent instanceof PsiCodeBlock) {
|
||||
variable = (PsiVariable)((PsiDeclarationStatement)parent.addBefore(declaration, myNewStatement)).getDeclaredElements()[0];
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
class InlineClassFinal {
|
||||
void sdf(String vp) {
|
||||
Object s = new M<caret>y(vp);
|
||||
}
|
||||
}
|
||||
|
||||
class My {
|
||||
private final String v;
|
||||
public My(String v) {
|
||||
this.v = v;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class InlineClassFinal {
|
||||
void sdf(String vp) {
|
||||
String v1 = vp;
|
||||
Object s = new Object() {
|
||||
private final String v = v1;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class My {
|
||||
private final String v;
|
||||
public My(String v) {
|
||||
this.v = v;
|
||||
}
|
||||
}
|
||||
+18
-1
@@ -2,6 +2,7 @@ package com.intellij.refactoring.inline;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.TargetElementUtil;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiCall;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiClassType;
|
||||
@@ -235,7 +236,18 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
|
||||
public void testAvailableInSupers() throws Exception {
|
||||
doTest(false, false);
|
||||
}
|
||||
|
||||
|
||||
public void testNoFinalForJava8() throws Exception {
|
||||
final LanguageLevel languageLevel = getLanguageLevel();
|
||||
try {
|
||||
setLanguageLevel(LanguageLevel.HIGHEST);
|
||||
doTest(true, false);
|
||||
}
|
||||
finally {
|
||||
setLanguageLevel(languageLevel);
|
||||
}
|
||||
}
|
||||
|
||||
public void testNoInlineAbstract() throws Exception {
|
||||
doTestNoInline("Abstract classes cannot be inlined");
|
||||
}
|
||||
@@ -469,4 +481,9 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
|
||||
assertTrue(superType != null);
|
||||
assertEquals(canBeInvokedOnReference, InlineToAnonymousClassHandler.canBeInvokedOnReference(callToInline, superType));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LanguageLevel getLanguageLevel() {
|
||||
return LanguageLevel.JDK_1_7;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user