void (@NotNull PsiTryStatement statement) { PsiResourceList resourceList = statement.getResourceList(); if (resourceList == null) return; PsiCodeBlock tryBlock = statement.getTryBlock(); if (tryBlock == null) return; List<PsiStatement> terminatingStatements = getTerminatingStatements(ArrayUtil.getLastElement(tryBlock.getStatements())); for (PsiStatement last : terminatingStatements) { PsiExpressionStatement expressionStatement = tryCast(last, PsiExpressionStatement.class); if(expressionStatement == null) return; PsiMethodCallExpression call = tryCast(expressionStatement.getExpression(), PsiMethodCallExpression.class); if (!CLOSE.test(call)) return; PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); PsiReferenceExpression reference = tryCast(PsiUtil.skipParenthesizedExprDown(qualifier), PsiReferenceExpression.class); if(reference == null) return; PsiVariable variable = tryCast(reference.resolve(), PsiVariable.class); if(variable == null) return; boolean isReferenceToResourceVariable = StreamEx.of(resourceList.iterator()).anyMatch( element -> variable == element || element instanceof PsiResourceExpression && EquivalenceChecker.getCanonicalPsiEquivalence() .expressionsAreEquivalent(reference, ((PsiResourceExpression)element).getExpression())); if(!isReferenceToResourceVariable) return; holder.problem(last, JavaBundle.message("inspection.redundant.explicit.close")) .fix(new DeleteElementFix(last, CommonQuickFixBundle.message("fix.remove.redundant", "close()"))) .register(); } }