From 5763ba435b4b180dbf33e4861105209ad3e6effd Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 3 Dec 2014 15:23:53 +0100 Subject: [PATCH] IPP: fix convert catch to throws inside a lambda --- .../ConvertCatchToThrowsIntention.java | 17 ++++++++++++++--- .../ConvertCatchToThrowsPredicate.java | 6 +++--- .../ipp/exceptions/catchToThrows/Lambda.java | 19 +++++++++++++++++++ .../catchToThrows/Lambda_after.java | 16 ++++++++++++++++ .../exceptions/ConvertCatchToThrowsTest.java | 3 ++- 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda_after.java diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java index 9fadbda240a4..df340db6e5da 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2012 Bas Leijdekkers + * Copyright 2007-2014 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package com.siyeh.ipp.exceptions; +import com.intellij.codeInsight.FileModificationService; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; @@ -36,8 +37,18 @@ public class ConvertCatchToThrowsIntention extends Intention { @Override protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { final PsiCatchSection catchSection = (PsiCatchSection)element.getParent(); - final PsiMethod method = PsiTreeUtil.getParentOfType(catchSection, PsiMethod.class); - if (method == null) { + final NavigatablePsiElement owner = PsiTreeUtil.getParentOfType(catchSection, PsiMethod.class, PsiLambdaExpression.class); + final PsiMethod method; + if (owner instanceof PsiMethod) { + method = (PsiMethod)owner; + } + else if (owner instanceof PsiLambdaExpression) { + method = LambdaUtil.getFunctionalInterfaceMethod(owner); + if (method == null || !FileModificationService.getInstance().preparePsiElementsForWrite(method)) { + return; + } + } + else { return; } // todo warn if method implements or overrides some base method diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsPredicate.java index dd7e88a49121..7e15549e10a1 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2013 Bas Leijdekkers + * Copyright 2007-2014 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ class ConvertCatchToThrowsPredicate implements PsiElementPredicate { if (element instanceof PsiCodeBlock) { return false; } - final PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class, true, PsiClass.class); - return method != null; + final PsiElement owner = PsiTreeUtil.getParentOfType(parent, PsiMethod.class, PsiClass.class, PsiLambdaExpression.class); + return owner instanceof PsiMethod || owner instanceof PsiLambdaExpression; } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda.java new file mode 100644 index 000000000000..da64d3ef583f --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda.java @@ -0,0 +1,19 @@ +class Lambda { + + void bar() throws E {} + + void foo() { + U u = () -> { + try { + + } catch (E e) { + e.printStackTrace(); + } + }; + } + + interface U { + void f(); + } + class E extends Exception {} +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda_after.java new file mode 100644 index 000000000000..c82292f3f141 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/Lambda_after.java @@ -0,0 +1,16 @@ +class Lambda { + + void bar() throws E {} + + void foo() { + U u = () -> { + + + }; + } + + interface U { + void f() throws E; + } + class E extends Exception {} +} diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java index 195a8bfb533c..4d4814fca6bd 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -25,6 +25,7 @@ public class ConvertCatchToThrowsTest extends IPPTestCase { public void testArmWithPluralCatches() { doTest(); } public void testArmWithSingleCatch() { doTest(); } public void testExistingThrows() { doTest(); } + public void testLambda() { doTest(); } @Override protected String getIntentionName() {