From c40ea482f8c0be2181f1b3f17cfa129e093cef65 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 16 Oct 2015 14:50:24 +0200 Subject: [PATCH] IDEA-146476 (AutoCloseable inspection doesn't recognize Java 8 method references) --- .../AutoCloseableResourceInspectionBase.java | 31 ++++++++++++++----- .../AutoCloseableResourceInspectionTest.java | 18 +++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/resources/AutoCloseableResourceInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/resources/AutoCloseableResourceInspectionBase.java index 737b1581fd56..a26af33bb593 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/resources/AutoCloseableResourceInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/resources/AutoCloseableResourceInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -18,6 +18,7 @@ package com.siyeh.ig.resources; import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.*; +import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -59,9 +60,7 @@ public class AutoCloseableResourceInspectionBase extends BaseInspection { @NotNull @Override protected String buildErrorString(Object... infos) { - final PsiExpression expression = (PsiExpression)infos[0]; - final PsiType type = expression.getType(); - assert type != null; + final PsiType type = (PsiType)infos[0]; final String text = type.getPresentableText(); return InspectionGadgetsBundle.message("auto.closeable.resource.problem.descriptor", text); } @@ -108,7 +107,7 @@ public class AutoCloseableResourceInspectionBase extends BaseInspection { if (!isNotSafelyClosedResource(expression)) { return; } - registerNewExpressionError(expression, expression); + registerNewExpressionError(expression, expression.getType()); } @Override @@ -120,11 +119,29 @@ public class AutoCloseableResourceInspectionBase extends BaseInspection { if (!isNotSafelyClosedResource(expression)) { return; } - registerMethodCallError(expression, expression); + registerMethodCallError(expression, expression.getType()); + } + + @Override + public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) { + super.visitMethodReferenceExpression(expression); + if (!expression.isConstructor()) { + return; + } + final PsiType type = PsiMethodReferenceUtil.getQualifierType(expression); + if (!InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE)) { + return; + } + for (String ignoredType : ignoredTypes) { + if (InheritanceUtil.isInheritor(type, ignoredType)) { + return; + } + } + registerError(expression, type); } private boolean isNotSafelyClosedResource(PsiExpression expression) { - if (!TypeUtils.expressionHasTypeOrSubtype(expression, "java.lang.AutoCloseable")) { + if (!TypeUtils.expressionHasTypeOrSubtype(expression, CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE)) { return false; } if (TypeUtils.expressionHasTypeOrSubtype(expression, ignoredTypes)) { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/resources/AutoCloseableResourceInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/resources/AutoCloseableResourceInspectionTest.java index 5f3002a14c78..8b862b24a72b 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/resources/AutoCloseableResourceInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/resources/AutoCloseableResourceInspectionTest.java @@ -16,7 +16,9 @@ package com.siyeh.ig.resources; import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.testFramework.LightProjectDescriptor; import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.NotNull; /** * @author Bas Leijdekkers @@ -68,6 +70,22 @@ public class AutoCloseableResourceInspectionTest extends LightInspectionTestCase "}"); } + public void testMethodReference() { + doTest("import java.util.*;" + + "class X {" + + " void m(List list) {" + + " final Z f = /*'X.Y' used without 'try'-with-resources statement*/Y::new/**/;" + + " }" + + " class Y implements java.io.Closeable {" + + " Y(String s) {}" + + " public void close() throws java.io.IOException {}" + + " }" + + " interface Z {\n" + + " R apply(T t);" + + " }" + + "}"); + } + @Override protected LocalInspectionTool getInspection() { return new AutoCloseableResourceInspection();