IG: handle fluent api method chains (IDEA-157151)

This commit is contained in:
Bas Leijdekkers
2016-06-27 15:43:18 +02:00
parent 6db9c279d4
commit 36b167be6b
2 changed files with 50 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 Bas Leijdekkers
* Copyright 2008-2016 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -112,7 +112,7 @@ public abstract class ResourceInspection extends BaseInspection {
@Nullable
public static PsiVariable getVariable(@NotNull PsiExpression expression) {
final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression);
final PsiElement parent = getParent(expression);
if (parent instanceof PsiAssignmentExpression) {
final PsiAssignmentExpression assignment = (PsiAssignmentExpression)parent;
final PsiExpression lhs = assignment.getLExpression();
@@ -134,6 +134,30 @@ public abstract class ResourceInspection extends BaseInspection {
}
}
private static PsiElement getParent(PsiExpression expression) {
PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression);
if (parent == null) {
return null;
}
PsiElement grandParent = parent.getParent();
final PsiType type = expression.getType();
if (type == null) {
return null;
}
while (parent instanceof PsiReferenceExpression && grandParent instanceof PsiMethodCallExpression) {
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
if (!type.equals(methodCallExpression.getType())) {
return null;
}
parent = ParenthesesUtils.getParentSkipParentheses(grandParent);
if (parent == null) {
return null;
}
grandParent = parent.getParent();
}
return parent;
}
private boolean isSafelyClosed(@Nullable PsiVariable variable, PsiElement context) {
if (variable == null) {
return false;

View File

@@ -138,6 +138,19 @@ public class AutoCloseableResourceInspectionTest extends LightInspectionTestCase
"}");
}
public void testScanner() {
doTest("import java.util.Scanner;" +
"class A {" +
" void a() throws java.io.IOException {" +
" try (Scanner scanner = new Scanner(\"\").useDelimiter(\"\\\\A\")) {" +
" String sconf = scanner.next();" +
" System.out.println(sconf);" +
" }" +
" }" +
"}" +
"");
}
@Override
protected LocalInspectionTool getInspection() {
return new AutoCloseableResourceInspection();
@@ -147,10 +160,20 @@ public class AutoCloseableResourceInspectionTest extends LightInspectionTestCase
protected String[] getEnvironmentClasses() {
return new String[] {
"package java.util;" +
"public final class Formatter implements Closeable {" +
"public final class Formatter implements java.io.Closeable {" +
" public Formatter format(String format, Object ... args) {" +
" return this;" +
" }" +
"}",
"package java.util;" +
"public final class Scanner implements java.io.Closeable {" +
" public Scanner(String source) {}" +
" public Scanner useDelimiter(String pattern) {" +
" return this;" +
" }" +
" public String next() {" +
" return this;" +
" }" +
"}"
};
}