OptionalToIfInspection: misc fixes

1. generate if with curly braces when it contains single declaration inside
2. restore else branch after generating code for flatMap
3. do not reassign source variable for nested source operations

GitOrigin-RevId: 061fbcc8d8dda586dcecb9a448752709a43370da
This commit is contained in:
Artemiy Sartakov
2020-04-21 08:55:39 +00:00
committed by intellij-monorepo-bot
parent 05017ec31f
commit f0c78aea15
6 changed files with 37 additions and 4 deletions
@@ -143,7 +143,7 @@ interface Instruction {
@Override
public String generate() {
if (myInstructions.size() == 1 && !hasElseBranch()) {
if (myInstructions.size() == 1 && !hasElseBranch() && !(myInstructions.get(0) instanceof Declaration)) {
return "if(" + myCondition.getText() + ")" + myInstructions.get(0).generate();
}
String thenBranch = "if(" + myCondition.getText() + "){\n" +
@@ -204,8 +204,11 @@ abstract class IntermediateOperation implements Operation {
@NotNull ChainVariable outVar,
@NotNull String code,
@NotNull OptionalToIfContext context) {
String elseBranch = context.getElseBranch();
List<OperationRecord> records = StreamEx.of(myRecords).map(r -> replaceFnVariable(r, inVar, context)).collect(Collectors.toList());
return OptionalToIfInspection.wrapCode(context, records, code);
String wrapped = OptionalToIfInspection.wrapCode(context, records, code);
context.setElseBranch(elseBranch);
return wrapped;
}
@NotNull
@@ -57,6 +57,10 @@ class OptionalToIfContext extends ChainContext {
myElseBranch = elseBranch;
}
String getElseBranch() {
return myElseBranch;
}
@NotNull
String generateNotNullCondition(@NotNull String arg, @NotNull String code) {
if (myElseBranch == null) {
@@ -67,7 +67,7 @@ abstract class SourceOperation implements Operation {
@NotNull ChainVariable outVar,
@NotNull String code,
@NotNull OptionalToIfContext context) {
if (SourceOperation.getSourceName(myArg) != null) {
if (SourceOperation.getSourceName(myArg) != null || myArg.getText().equals(outVar.getName())) {
return "if(" + outVar.getName() + "==null)throw new java.lang.NullPointerException();" +
code;
}
@@ -111,7 +111,7 @@ abstract class SourceOperation implements Operation {
@NotNull ChainVariable outVar,
@NotNull String code,
@NotNull OptionalToIfContext context) {
if (SourceOperation.getSourceName(myArg) != null) {
if (SourceOperation.getSourceName(myArg) != null || myArg.getText().equals(outVar.getName())) {
return context.generateNotNullCondition(outVar.getName(), code);
}
return outVar.getDeclaration(myArg.getText()) +
@@ -50,6 +50,20 @@ class Test {
}
}
void nestedFlatMap(String var0) {
boolean b = false;
if (var0 != null) {
String var2 = var0.toLowerCase();
b = true;
}
}
String flatMapWithOrInside() {
Object o1 = null;
String empty = null;
throw new NoSuchElementException("No value present");
}
<T> T id(T t) {
return t;
}
@@ -32,6 +32,18 @@ class Test {
String out = Optional.ofNullable(in).flatMap(s1 -> Optional.of(p)).orElse("bar");
}
void nestedFlatMap(String var0) {
boolean b = Optional.ofNullable(var0)
.flatMap(var1 ->
Optional.of(var1).map(s -> s.toLowerCase())
.flatMap(var2 -> Optional.ofNullable(var2)))
.isPresent();
}
String flatMapWithOrInside() {
return Optional.<String>empty().flatMap(s1 -> Optional.empty().or(() -> Optional.empty())).get();
}
<T> T id(T t) {
return t;
}