extract method: do not declare variables after call with local class defined in extracted method (IDEA-46775)

This commit is contained in:
anna
2010-05-26 13:28:38 +04:00
parent 6806c16bfd
commit 5c6d20dc4e
3 changed files with 19 additions and 0 deletions
@@ -419,6 +419,13 @@ public class ExtractMethodProcessor implements MatchProvider {
if (!remainingReferences.isEmpty()) {
throw new PrepareFailedException("Cannot extract method because the selected code fragment defines local classes used outside of the fragment", remainingReferences.get(0));
}
if (classExtracted) {
for (PsiVariable variable : myControlFlowWrapper.getUsedVariables()) {
if (isDeclaredInside(variable) && !variable.equals(myOutputVariable) && PsiUtil.resolveClassInType(variable.getType()) == localClass) {
throw new PrepareFailedException("Cannot extract method because the selected code fragment defines variable of local class type used outside of the fragment", variable);
}
}
}
}
}
@@ -0,0 +1,8 @@
class Test {
void foo() {
<selection>class X {}
X x;
</selection>
x = null;
}
}
@@ -338,6 +338,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testLocalClassDefinedInMethodWhichIsUsedLater() throws Exception {
doPrepareErrorTest("Cannot extract method because the selected code fragment defines variable of local class type used outside of the fragment");
}
public void testForceBraces() throws Exception {
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
int old = settings.IF_BRACE_FORCE;