mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
anonym -> inner: ensure encoded (IDEA-201012)
This commit is contained in:
+7
-5
@@ -37,6 +37,7 @@ import com.intellij.refactoring.HelpID;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.RefactoringChangeUtil;
|
||||
import com.intellij.refactoring.util.classMembers.ElementNeedsThis;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
@@ -59,7 +60,7 @@ public class AnonymousToInnerHandler implements RefactoringActionHandler {
|
||||
private PsiClass myTargetClass;
|
||||
protected String myNewClassName;
|
||||
|
||||
private VariableInfo[] myVariableInfos;
|
||||
protected VariableInfo[] myVariableInfos;
|
||||
protected boolean myMakeStatic;
|
||||
private final Set<PsiTypeParameter> myTypeParametersToCreate = new LinkedHashSet<>();
|
||||
|
||||
@@ -176,13 +177,14 @@ public class AnonymousToInnerHandler implements RefactoringActionHandler {
|
||||
|
||||
private void doRefactoring() throws IncorrectOperationException {
|
||||
calculateTypeParametersToCreate();
|
||||
PsiClass aClass = createClass(myNewClassName);
|
||||
myTargetClass.add(aClass);
|
||||
|
||||
ChangeContextUtil.encodeContextInfo(myAnonClass, false);
|
||||
PsiClass innerClass = (PsiClass)myTargetClass.add(createClass(myNewClassName));
|
||||
ChangeContextUtil.decodeContextInfo(innerClass, myTargetClass, RefactoringChangeUtil.createThisExpression(myTargetClass.getManager(), myTargetClass));
|
||||
|
||||
PsiNewExpression newExpr = (PsiNewExpression) myAnonClass.getParent();
|
||||
@NonNls StringBuilder buf = new StringBuilder();
|
||||
buf.append("new ");
|
||||
buf.append(aClass.getName());
|
||||
buf.append(innerClass.getName());
|
||||
if (!myTypeParametersToCreate.isEmpty()) {
|
||||
buf.append("<");
|
||||
int idx = 0;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
class ConvertAnonymousToInner {
|
||||
|
||||
boolean myFast = true;
|
||||
|
||||
void convert(boolean fast) {
|
||||
new Object() {<caret>
|
||||
public String toString() {
|
||||
if (myFast != fast) {
|
||||
myFast = fast;
|
||||
System.out.println(fast);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
class ConvertAnonymousToInner {
|
||||
|
||||
boolean myFast = true;
|
||||
|
||||
void convert(boolean fast) {
|
||||
new MyObject(fast);
|
||||
}
|
||||
|
||||
private class MyObject {
|
||||
private final boolean myFast;
|
||||
|
||||
public MyObject(boolean fast) {
|
||||
myFast = fast;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if (ConvertAnonymousToInner.this.myFast != myFast) {
|
||||
ConvertAnonymousToInner.this.myFast = myFast;
|
||||
System.out.println(myFast);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ package com.intellij.java.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.refactoring.anonymousToInner.AnonymousToInnerHandler;
|
||||
import com.intellij.refactoring.anonymousToInner.VariableInfo;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -60,6 +61,24 @@ public class AnonymousToInnerTest extends LightCodeInsightTestCase {
|
||||
assertFalse(handler.needsThis());
|
||||
checkResultByFile(TEST_ROOT + getTestName(true) + "_after.java");
|
||||
}
|
||||
|
||||
public void testNameConflict() {
|
||||
configureByFile(TEST_ROOT + getTestName(true) + ".java");
|
||||
AnonymousToInnerHandler handler = new AnonymousToInnerHandler(){
|
||||
@Override
|
||||
protected boolean showRefactoringDialog() {
|
||||
myNewClassName = "MyObject";
|
||||
myMakeStatic = !needsThis();
|
||||
VariableInfo info = myVariableInfos[0];
|
||||
info.fieldName = "myFast";
|
||||
info.parameterName = "fast";
|
||||
myVariableInfos = new VariableInfo[] {info};
|
||||
return true;
|
||||
}
|
||||
};
|
||||
handler.invoke(getProject(), myEditor, myFile, null);
|
||||
checkResultByFile(TEST_ROOT + getTestName(true) + "_after.java");
|
||||
}
|
||||
|
||||
|
||||
private void doTest(final String newClassName, final boolean makeStatic) {
|
||||
|
||||
Reference in New Issue
Block a user