mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
StreamToLoopInspection: fixed unqualified reference to stream() method handling
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -104,15 +104,17 @@ abstract class SourceOperation extends Operation {
|
||||
}
|
||||
|
||||
static class ForEachSource extends SourceOperation {
|
||||
private PsiExpression myQualifier;
|
||||
private @Nullable PsiExpression myQualifier;
|
||||
|
||||
ForEachSource(PsiExpression qualifier) {
|
||||
ForEachSource(@Nullable PsiExpression qualifier) {
|
||||
myQualifier = qualifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
void rename(String oldName, String newName, StreamToLoopReplacementContext context) {
|
||||
myQualifier = replaceVarReference(myQualifier, oldName, newName, context);
|
||||
if(myQualifier != null) {
|
||||
myQualifier = replaceVarReference(myQualifier, oldName, newName, context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -135,8 +137,9 @@ abstract class SourceOperation extends Operation {
|
||||
|
||||
@Override
|
||||
public String wrap(StreamVariable outVar, String code, StreamToLoopReplacementContext context) {
|
||||
return context.getLoopLabel() +
|
||||
"for(" + outVar.getDeclaration() + ": " + (myQualifier == null ? "this" : myQualifier.getText()) + ") {" + code + "}\n";
|
||||
PsiExpression iterationParameter = myQualifier == null ? ExpressionUtils
|
||||
.getQualifierOrThis(((PsiMethodCallExpression)context.createExpression("stream()")).getMethodExpression()) : myQualifier;
|
||||
return context.getLoopLabel() + "for(" + outVar.getDeclaration() + ": " + iterationParameter.getText() + ") {" + code + "}\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -355,7 +355,8 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
mySuffix = "Inner";
|
||||
}
|
||||
|
||||
public void registerReusedElement(PsiElement element) {
|
||||
public void registerReusedElement(@Nullable PsiElement element) {
|
||||
if(element == null) return;
|
||||
element.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitVariable(PsiVariable variable) {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Test extends ArrayList<String> {
|
||||
void test() {
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int sum = 0;
|
||||
for (String s : Test.this) {
|
||||
int length = s.length();
|
||||
sum += length;
|
||||
}
|
||||
System.out.println(sum);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Test extends ArrayList<String> {
|
||||
void test() {
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(stream().mapToInt(String::length).s<caret>um());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -904,6 +904,9 @@ public class ExpressionUtils {
|
||||
PsiClass memberClass = member.getContainingClass();
|
||||
if (memberClass != null) {
|
||||
PsiClass containingClass = ClassUtils.getContainingClass(ref);
|
||||
if (containingClass == null) {
|
||||
containingClass = PsiTreeUtil.getContextOfType(ref, PsiClass.class);
|
||||
}
|
||||
if (!InheritanceUtil.isInheritorOrSelf(containingClass, memberClass, true)) {
|
||||
containingClass = ClassUtils.getContainingClass(containingClass);
|
||||
while (containingClass != null && !InheritanceUtil.isInheritorOrSelf(containingClass, memberClass, true)) {
|
||||
|
||||
Reference in New Issue
Block a user