mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
SourceOperation: avoid copying final field to bound variable
GitOrigin-RevId: fd750ded28fac7a89e41c22610298fe9a23cc25b
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1a0362acf4
commit
c5617ef120
@@ -300,7 +300,7 @@ abstract class SourceOperation extends Operation {
|
||||
@Override
|
||||
String wrap(StreamVariable outVar, String code, StreamToLoopReplacementContext context) {
|
||||
String bound = myBound.getText();
|
||||
if(!ExpressionUtils.isSafelyRecomputableExpression(context.createExpression(bound))) {
|
||||
if(needBound(context, bound)) {
|
||||
bound = context.declare("bound", outVar.getType().getCanonicalText(), bound);
|
||||
}
|
||||
String loopVar = outVar.getName();
|
||||
@@ -316,6 +316,18 @@ abstract class SourceOperation extends Operation {
|
||||
reassign +
|
||||
code + "}\n";
|
||||
}
|
||||
|
||||
private static boolean needBound(StreamToLoopReplacementContext context, String bound) {
|
||||
PsiExpression expression = PsiUtil.skipParenthesizedExprDown(context.createExpression(bound));
|
||||
while (expression instanceof PsiReferenceExpression) {
|
||||
PsiElement ref = ((PsiReferenceExpression)expression).resolve();
|
||||
if (!(ref instanceof PsiVariable) || !((PsiVariable)ref).hasModifierProperty(PsiModifier.FINAL)) {
|
||||
break;
|
||||
}
|
||||
expression = ((PsiReferenceExpression)expression).getQualifierExpression();
|
||||
}
|
||||
return !ExpressionUtils.isSafelyRecomputableExpression(expression);
|
||||
}
|
||||
}
|
||||
|
||||
static class ArraySliceSource extends SourceOperation {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Test {
|
||||
private static final String ARRAY_ELEMENT_SEPARATOR = ", ", ARRAY_START = "[", ARRAY_END = "]";
|
||||
|
||||
public static String nullSafeToString(byte[] array) {
|
||||
StringJoiner joiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
String s = String.valueOf(array[i]);
|
||||
joiner.add(s);
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Test {
|
||||
private static final String ARRAY_ELEMENT_SEPARATOR = ", ", ARRAY_START = "[", ARRAY_END = "]";
|
||||
|
||||
public static String nullSafeToString(byte[] array) {
|
||||
return IntStream.range(0, array.length).mapToObj(i -> String.valueOf(array[i]))
|
||||
.collec<caret>t(Collectors.joining(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user