improve DetailExceptionsIntention to insert intermediate detailed exception

This commit is contained in:
Dmitry Batkovich
2017-11-29 12:40:49 +03:00
parent 62627a7cf3
commit 8b45d6ed10
4 changed files with 64 additions and 10 deletions
@@ -55,26 +55,37 @@ public class DetailExceptionsIntention extends Intention {
newTryStatement.append(tryBlockText);
ExceptionUtils.calculateExceptionsThrown(tryBlock, exceptionsThrown);
final Comparator<PsiType> comparator = new HierarchicalTypeComparator();
final List<PsiType> exceptionsAlreadyEmitted = new ArrayList<>();
final PsiCatchSection[] catchSections = tryStatement.getCatchSections();
for (PsiCatchSection catchSection : catchSections) {
final PsiParameter parameter = catchSection.getParameter();
final PsiCodeBlock block = catchSection.getCatchBlock();
if (parameter != null && block != null) {
final PsiType caughtType = parameter.getType();
final List<PsiType> exceptionsToExpand = new ArrayList<>(10);
for (Object aExceptionsThrown : exceptionsThrown) {
final PsiType thrownType = (PsiType)aExceptionsThrown;
if (caughtType.isAssignableFrom(thrownType)) {
exceptionsToExpand.add(thrownType);
List<PsiClassType> exceptionsToExpand = new ArrayList<>(exceptionsThrown.size());
for (PsiClassType aExceptionsThrown : exceptionsThrown) {
if (caughtType.isAssignableFrom(aExceptionsThrown)) {
exceptionsToExpand.add(aExceptionsThrown);
}
}
exceptionsToExpand.removeAll(exceptionsAlreadyEmitted);
Collections.sort(exceptionsToExpand, comparator);
for (PsiType thrownType : exceptionsToExpand) {
exceptionsThrown.removeAll(exceptionsToExpand);
PsiClassType commonSuperType = null;
PsiClass commonSuper = ObscureThrownExceptionsIntention.findCommonSuperClass(exceptionsToExpand.toArray(PsiClassType.EMPTY_ARRAY));
if (commonSuper != null) {
commonSuperType = JavaPsiFacade.getElementFactory(commonSuper.getProject()).createType(commonSuper);
if (commonSuperType.equals(caughtType)) {
commonSuperType = null;
}
}
if (commonSuperType != null) {
exceptionsToExpand = Collections.singletonList(commonSuperType);
} else {
Collections.sort(exceptionsToExpand, comparator);
}
for (PsiClassType thrownType : exceptionsToExpand) {
newTryStatement.append("catch(").append(thrownType.getCanonicalText()).append(' ').append(parameter.getName()).append(')');
newTryStatement.append(commentTracker.markUnchanged(block).getText());
exceptionsAlreadyEmitted.add(thrownType);
}
}
}
@@ -0,0 +1,21 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
public class Test {
void m() {
try {
createModuleNode(null);
} catch (Ex<caret>ception e) {
e.printStackTrace();
}
}
protected Object createModuleNode(Object module) throws B1, B2, B3, B4 {
return null;
}
class A extends Exception {}
class B1 extends A {}
class B2 extends A {}
class B3 extends A {}
class B4 extends A {}
}
@@ -0,0 +1,21 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
public class Test {
void m() {
try {
createModuleNode(null);
} catch (A e) {
e.printStackTrace();
}
}
protected Object createModuleNode(Object module) throws B1, B2, B3, B4 {
return null;
}
class A extends Exception {}
class B1 extends A {}
class B2 extends A {}
class B3 extends A {}
class B4 extends A {}
}
@@ -16,6 +16,7 @@ public class DetailExceptionsIntentionTest extends IPPTestCase {
public void testTryWithResources() { doTest(); }
public void testPolyadicParentheses() { doTest(); }
public void testCaretAtParameter() { doTest(); }
public void testCommonSuperType() { doTest(); }
@Override
protected String getIntentionName() {