redundant throws: find throws clauses inside functional expressions (IDEA-135181)

This commit is contained in:
Anna Kozlova
2015-01-14 13:25:05 +01:00
parent 73239255ec
commit a1b44a53ac
4 changed files with 74 additions and 0 deletions
@@ -20,6 +20,7 @@
*/
package com.intellij.codeInspection.reference;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
@@ -30,6 +31,9 @@ import com.intellij.util.VisibilityUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
public class RefJavaUtilImpl extends RefJavaUtil{
@Override
@@ -114,6 +118,29 @@ public class RefJavaUtilImpl extends RefJavaUtil{
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(aClass);
if (interfaceMethod != null) {
refFrom.addReference(refFrom.getRefManager().getReference(interfaceMethod), interfaceMethod, psiFrom, false, true, null);
PsiElement body = null;
PsiElement topElement = null;
if (expression instanceof PsiLambdaExpression) {
body = ((PsiLambdaExpression)expression).getBody();
topElement = expression;
}
else {
final PsiElement resolve = ((PsiMethodReferenceExpression)expression).resolve();
if (resolve instanceof PsiMethod) {
body = ((PsiMethod)resolve).getBody();
topElement = resolve;
}
}
final Collection<PsiClassType> exceptionTypes = body != null ? ExceptionUtil.collectUnhandledExceptions(body, topElement, false)
: Collections.<PsiClassType>emptyList();
RefElement refResolved = refFrom.getRefManager().getReference(interfaceMethod);
if (refResolved instanceof RefMethodImpl) {
for (final PsiClassType exceptionType : exceptionTypes) {
((RefMethodImpl)refResolved).updateThrowsList(exceptionType);
}
}
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Foo.java</file>
<line>21</line>
<description>ObjectStreamException</description>
</problem>
</problems>
@@ -0,0 +1,23 @@
import java.io.*;
class ExceptionTest {
MyFunction method() {
return () -> {
throw new EOFException();
};
}
MyFunction method1() {
return this::e;
}
private void e() throws FileNotFoundException {
throw new FileNotFoundException();
}
@FunctionalInterface
private interface MyFunction {
void call() throws FileNotFoundException, EOFException, ObjectStreamException;
}
}
@@ -17,6 +17,10 @@ package com.intellij.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.unneededThrows.RedundantThrows;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.InspectionTestCase;
public class RedundantThrowTest extends InspectionTestCase {
@@ -60,4 +64,15 @@ public class RedundantThrowTest extends InspectionTestCase {
public void testSelfCall() throws Exception {
doTest();
}
public void testThrownClausesInFunctionalExpressions() throws Exception {
doTest();
}
@Override
protected Sdk getTestProjectSdk() {
Sdk sdk = IdeaTestUtil.getMockJdk17();
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8);
return sdk;
}
}