mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
extract method: fix name conflicts at method call site
This commit is contained in:
+4
-2
@@ -891,8 +891,10 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
|
||||
try {
|
||||
run.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitAnonymousClass(final PsiAnonymousClass aClass) {
|
||||
}
|
||||
public void visitClass(final PsiClass aClass) {}
|
||||
|
||||
@Override
|
||||
public void visitForeachStatement(PsiForeachStatement statement) {}
|
||||
|
||||
@Override public void visitVariable(PsiVariable variable) {
|
||||
if (name1.equals(variable.getName())) {
|
||||
|
||||
+8
-3
@@ -760,8 +760,8 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
CodeStyleManager.getInstance(myProject).reformat(ifStatement);
|
||||
}
|
||||
else if (myNotNullConditionalCheck) {
|
||||
final String varName = myOutputVariable.getName();
|
||||
declareVariableAtMethodCallLocation(varName, myReturnType instanceof PsiPrimitiveType ? ((PsiPrimitiveType)myReturnType).getBoxedType(myCodeFragmentMember) : myReturnType);
|
||||
String varName = myOutputVariable.getName();
|
||||
varName = declareVariableAtMethodCallLocation(varName, myReturnType instanceof PsiPrimitiveType ? ((PsiPrimitiveType)myReturnType).getBoxedType(myCodeFragmentMember) : myReturnType);
|
||||
addToMethodCallLocation(myElementFactory.createStatementFromText("if (" + varName + " != null) return " + varName + ";", null));
|
||||
}
|
||||
else if (myGenerateConditionalExit) {
|
||||
@@ -903,12 +903,17 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
declareVariableAtMethodCallLocation(name, myOutputVariable.getType());
|
||||
}
|
||||
|
||||
private void declareVariableAtMethodCallLocation(String name, PsiType type) {
|
||||
private String declareVariableAtMethodCallLocation(String name, PsiType type) {
|
||||
if (myControlFlowWrapper.getOutputVariables(false).length == 0) {
|
||||
PsiElement lastStatement = myEnclosingBlockStatement != null ? myEnclosingBlockStatement : myElements[myElements.length - 1];
|
||||
name = JavaCodeStyleManager.getInstance(myProject).suggestUniqueVariableName(name, lastStatement, true);
|
||||
}
|
||||
PsiDeclarationStatement statement = myElementFactory.createVariableDeclarationStatement(name, type, myMethodCall);
|
||||
statement = (PsiDeclarationStatement)addToMethodCallLocation(statement);
|
||||
PsiVariable var = (PsiVariable)statement.getDeclaredElements()[0];
|
||||
myMethodCall = (PsiMethodCallExpression)var.getInitializer();
|
||||
var.getModifierList().replace(myOutputVariable.getModifierList());
|
||||
return name;
|
||||
}
|
||||
|
||||
private void adjustFinalParameters(final PsiMethod method) throws IncorrectOperationException {
|
||||
|
||||
@@ -12,8 +12,8 @@ class Main {
|
||||
public static Result doIt(String name) {
|
||||
Result result;
|
||||
|
||||
Result result = newMethod(name);
|
||||
if (result != null) return result;
|
||||
Result result1 = newMethod(name);
|
||||
if (result1 != null) return result1;
|
||||
|
||||
result = new Result("Name is " + name);
|
||||
return result;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
class X {
|
||||
@NotNull
|
||||
public X fun1(int x) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public X fun2(boolean b) {
|
||||
|
||||
<selection>if (b) {
|
||||
int x = 1;
|
||||
return fun1(x);
|
||||
}</selection>
|
||||
|
||||
int x = 0;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void foo(int i, int j) {
|
||||
bar(i, j);
|
||||
}
|
||||
|
||||
private void bar(int i, int j) {
|
||||
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class X {
|
||||
@NotNull
|
||||
public X fun1(int x) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public X fun2(boolean b) {
|
||||
|
||||
X x1 = newMethod(b);
|
||||
if (x1 != null) return x1;
|
||||
|
||||
int x = 0;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private X newMethod(boolean b) {
|
||||
if (b) {
|
||||
int x = 1;
|
||||
return fun1(x);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
void foo(int i, int j) {
|
||||
bar(i, j);
|
||||
}
|
||||
|
||||
private void bar(int i, int j) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNotNullCheckNameConflicts() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testContinueInside() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user