OptionalToIfInspection: preserve comments

GitOrigin-RevId: 7e97d9997d6f0eaca40e1e9cd720dc27e7ca687a
This commit is contained in:
Artemiy Sartakov
2019-11-12 17:31:02 +07:00
committed by intellij-monorepo-bot
parent f58531c341
commit 7e0b374d13
4 changed files with 68 additions and 3 deletions

View File

@@ -12,6 +12,7 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import com.siyeh.ig.psiutils.MethodCallUtils;
import one.util.streamex.StreamEx;
@@ -185,13 +186,19 @@ public class OptionalToIfInspection extends AbstractBaseJavaLocalInspectionTool
String code = generateCode(context, operations);
if (code == null) return;
code = context.addInitializer(code);
PsiStatement firstStatement = chainStatement;
PsiStatement[] statements = addStatements(factory, chainStatement, code);
if (statements.length > 0) firstStatement = statements[0];
List<Instruction> instructions = createInstructions(statements);
if (instructions != null) {
code = Simplifier.simplify(instructions);
Arrays.stream(statements).forEach(PsiStatement::delete);
addStatements(factory, chainStatement, code);
statements = addStatements(factory, chainStatement, code);
firstStatement = statements.length > 0 ? statements[0] : chainStatement;
}
CommentTracker tracker = new CommentTracker();
tracker.grabComments(chainStatement);
tracker.insertCommentsBefore(firstStatement);
chainStatement.delete();
}
}

View File

@@ -0,0 +1,33 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
import java.util.*;
class Test {
private LicenseManager ourInstance = null;
LicenseManager setInstance(LicenseManager instance) {
LicenseManager old = this.ourInstance;
this.ourInstance = instance;
return old;
}
private static interface LicenseManager {
}
private static class IdeaLicenseManager implements LicenseManager {
}
public LicenseManager getInstance() {
final LicenseManager instance = ourInstance;
/*1*/
/*2*/
/*3*/
/*4*/
LicenseManager result = null;
if (instance != null) result = instance;
if (result == null) result = setInstance(new IdeaLicenseManager(/*3*/));
return result;
}
}

View File

@@ -0,0 +1,26 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
import java.util.*;
class Test {
private LicenseManager ourInstance = null;
LicenseManager setInstance(LicenseManager instance) {
LicenseManager old = this.ourInstance;
this.ourInstance = instance;
return old;
}
private static interface LicenseManager {
}
private static class IdeaLicenseManager implements LicenseManager {
}
public LicenseManager getInstance() {
final LicenseManager instance = ourInstance;
return/*1*/ Optional.ofNul<caret>lable(instance/*2*/).orElseGet(() -> setInstance(new IdeaLicenseManager(/*3*/))) /*4*/;
}
}

View File

@@ -13,8 +13,7 @@ import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTes
/**
* @see OptionalToIfInspection
*/
public class
OptionalToIfInspectionTest extends LightQuickFixParameterizedTestCase {
public class OptionalToIfInspectionTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override