mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method: null checks should not miss final modifiers ( IDEA-55515 )
This commit is contained in:
+11
-11
@@ -636,11 +636,7 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
if (myNullConditionalCheck) {
|
||||
final String varName = myOutputVariable.getName();
|
||||
if (isDeclaredInside(myOutputVariable)) {
|
||||
PsiDeclarationStatement statement = (PsiDeclarationStatement)myElementFactory
|
||||
.createStatementFromText(myOutputVariable.getType().getCanonicalText() + " " + varName + "=x;", null);
|
||||
statement = (PsiDeclarationStatement)addToMethodCallLocation(statement);
|
||||
myMethodCall =
|
||||
(PsiMethodCallExpression)((PsiLocalVariable)statement.getDeclaredElements()[0]).getInitializer().replace(myMethodCall);
|
||||
declareVariableAtMethodCallLocation(varName);
|
||||
}
|
||||
else {
|
||||
PsiExpressionStatement assignmentExpression =
|
||||
@@ -676,12 +672,7 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
myMethodCall = (PsiMethodCallExpression)assignment.getRExpression().replace(myMethodCall);
|
||||
}
|
||||
else {
|
||||
PsiDeclarationStatement statement =
|
||||
myElementFactory.createVariableDeclarationStatement(name, myOutputVariable.getType(), myMethodCall);
|
||||
statement = (PsiDeclarationStatement)addToMethodCallLocation(statement);
|
||||
PsiVariable var = (PsiVariable)statement.getDeclaredElements()[0];
|
||||
myMethodCall = (PsiMethodCallExpression)var.getInitializer();
|
||||
var.getModifierList().replace(myOutputVariable.getModifierList());
|
||||
declareVariableAtMethodCallLocation(name);
|
||||
}
|
||||
}
|
||||
else if (myHasReturnStatementOutput) {
|
||||
@@ -737,6 +728,15 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
|
||||
}
|
||||
|
||||
private void declareVariableAtMethodCallLocation(String name) {
|
||||
PsiDeclarationStatement statement =
|
||||
myElementFactory.createVariableDeclarationStatement(name, myOutputVariable.getType(), myMethodCall);
|
||||
statement = (PsiDeclarationStatement)addToMethodCallLocation(statement);
|
||||
PsiVariable var = (PsiVariable)statement.getDeclaredElements()[0];
|
||||
myMethodCall = (PsiMethodCallExpression)var.getInitializer();
|
||||
var.getModifierList().replace(myOutputVariable.getModifierList());
|
||||
}
|
||||
|
||||
private void adjustFinalParameters(final PsiMethod method) throws IncorrectOperationException {
|
||||
final IncorrectOperationException[] exc = new IncorrectOperationException[1];
|
||||
exc[0] = null;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
<selection>final String str = "";
|
||||
if (str == "") {
|
||||
return;
|
||||
}</selection>
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
System.out.println(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
final String str = newMethod();
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
System.out.println(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String newMethod() {
|
||||
final String str = "";
|
||||
if (str == "") {
|
||||
return;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
@@ -400,6 +400,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNullableCheckDontMissFinal() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSimpleArrayAccess() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user