[cdr] Improved catch analysis relaxed for general exceptions

This commit is contained in:
Roman Shevchenko
2011-05-18 19:46:04 +04:00
parent ba746d1c12
commit fc32246f0c
3 changed files with 28 additions and 22 deletions

View File

@@ -466,11 +466,14 @@ public class ExceptionUtil {
return errorClass != null && InheritanceUtil.isInheritorOrSelf(aClass, errorClass, true);
}
public static boolean isUncheckedExceptionOrSuperclass(@NotNull PsiClassType type) {
String canonicalText = type.getCanonicalText();
return "java.lang.Throwable".equals(canonicalText) ||
"java.lang.Exception".equals(canonicalText) ||
isUncheckedException(type);
public static boolean isUncheckedExceptionOrSuperclass(@NotNull final PsiClassType type) {
return isGeneralExceptionType(type) || isUncheckedException(type);
}
public static boolean isGeneralExceptionType(@NotNull final PsiType type) {
final String canonicalText = type.getCanonicalText();
return CommonClassNames.JAVA_LANG_THROWABLE.equals(canonicalText) ||
CommonClassNames.JAVA_LANG_EXCEPTION.equals(canonicalText);
}
public static boolean isHandled(PsiClassType exceptionType, PsiElement throwPlace) {

View File

@@ -981,7 +981,8 @@ public class HighlightUtil {
@Nullable
static Collection<HighlightInfo> checkWithImprovedCatchAnalysis(final PsiParameter parameter, Collection<PsiClassType> thrownTypes) {
static Collection<HighlightInfo> checkWithImprovedCatchAnalysis(final PsiParameter parameter,
final Collection<PsiClassType> thrownInTryStatement) {
final PsiElement scope = parameter.getDeclarationScope();
if (!(scope instanceof PsiCatchSection)) return null;
@@ -990,7 +991,7 @@ public class HighlightUtil {
final int idx = ArrayUtil.find(allCatchSections, catchSection);
if (idx <= 0) return null;
thrownTypes = Sets.newHashSet(thrownTypes);
final Collection<PsiClassType> thrownTypes = Sets.newHashSet(thrownInTryStatement);
thrownTypes.add(PsiType.getJavaLangError(parameter.getManager(), parameter.getResolveScope()));
thrownTypes.add(PsiType.getJavaLangRuntimeException(parameter.getManager(), parameter.getResolveScope()));
final Collection<HighlightInfo> result = Lists.newArrayList();
@@ -998,8 +999,10 @@ public class HighlightUtil {
final List<PsiTypeElement> parameterTypeElements = PsiUtil.getParameterTypeElements(parameter);
final boolean isMultiCatch = parameterTypeElements.size() > 1;
for (PsiTypeElement catchTypeElement : parameterTypeElements) {
// collect exceptions which are caught by this type
final PsiType catchType = catchTypeElement.getType();
if (ExceptionUtil.isGeneralExceptionType(catchType)) continue;
// collect exceptions which are caught by this type
Collection<PsiClassType> caught = ContainerUtil.findAll(thrownTypes, new Condition<PsiClassType>() {
@Override public boolean value(PsiClassType type) { return catchType.isAssignableFrom(type); }
});

View File

@@ -104,7 +104,7 @@ class C {
void m15() {
try { f(); }
catch (RuntimeException e) { }
<warning descr="Unreachable section: exception 'java.lang.RuntimeException' has already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m16() {
@@ -116,34 +116,34 @@ class C {
void m17() {
try { h(); }
catch (RuntimeException e) { }
<warning descr="Unreachable section: exception 'java.lang.RuntimeException' has already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m18() {
try { f(); }
catch (RuntimeException e) { }
catch (<error descr="Exception 'C.E' is never thrown in the corresponding try block">E e</error>) { }
<warning descr="Unreachable section: exception 'java.lang.RuntimeException' has already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m19() {
try { g(); }
catch (RuntimeException e) { }
catch (E e) { }
<warning descr="Unreachable section: exceptions 'C.E, java.lang.RuntimeException' have already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m20() {
try { h(); }
catch (RuntimeException e) { }
catch (<error descr="Exception 'C.E' is never thrown in the corresponding try block">E e</error>) { }
<warning descr="Unreachable section: exception 'java.lang.RuntimeException' has already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m21() {
try { f(); }
catch (RuntimeException e) { }
<warning descr="Unreachable section: exception 'java.lang.RuntimeException' has already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m22() {
@@ -155,14 +155,14 @@ class C {
void m23() {
try { h(); }
catch (RuntimeException e) { }
<warning descr="Unreachable section: exception 'java.lang.RuntimeException' has already been caught">catch (Exception e) { }</warning>
catch (Exception e) { }
}
void m24() {
try { f(); }
catch (RuntimeException e) { }
catch (Error e) { }
<warning descr="Unreachable section: exceptions 'java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m25() {
@@ -176,7 +176,7 @@ class C {
try { h(); }
catch (RuntimeException e) { }
catch (Error e) { }
<warning descr="Unreachable section: exceptions 'java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m27() {
@@ -184,7 +184,7 @@ class C {
catch (RuntimeException e) { }
catch (Error e) { }
catch (<error descr="Exception 'C.E' is never thrown in the corresponding try block">E e</error>) { }
<warning descr="Unreachable section: exceptions 'java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m28() {
@@ -192,7 +192,7 @@ class C {
catch (RuntimeException e) { }
catch (Error e) { }
catch (E e) { }
<warning descr="Unreachable section: exceptions 'C.E, java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m29() {
@@ -200,14 +200,14 @@ class C {
catch (RuntimeException e) { }
catch (Error e) { }
catch (<error descr="Exception 'C.E' is never thrown in the corresponding try block">E e</error>) { }
<warning descr="Unreachable section: exceptions 'java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m30() {
try { f(); }
catch (RuntimeException e) { }
catch (Error e) { }
<warning descr="Unreachable section: exceptions 'java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m31() {
@@ -221,7 +221,7 @@ class C {
try { h(); }
catch (RuntimeException e) { }
catch (Error e) { }
<warning descr="Unreachable section: exceptions 'java.lang.RuntimeException, java.lang.Error' have already been caught">catch (Throwable t) { }</warning>
catch (Throwable t) { }
}
void m33() {