/
SneakyThrowsExceptionHandler.java
68 lines (56 loc) · 2.77 KB
/
SneakyThrowsExceptionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package de.plushnikov.intellij.plugin.handler;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.CustomExceptionHandler;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiTreeUtil;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
public class SneakyThrowsExceptionHandler extends CustomExceptionHandler {
private static final String ANNOTATION_FQN = SneakyThrows.class.getName();
private static final String JAVA_LANG_THROWABLE = "java.lang.Throwable";
@Override
public boolean isHandled(@Nullable PsiElement element, @NotNull PsiClassType exceptionType, PsiElement topElement) {
final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
return psiMethod != null && isExceptionHandled(psiMethod, exceptionType);
}
public static boolean isExceptionHandled(@NotNull PsiModifierListOwner psiModifierListOwner, PsiClassType exceptionClassType) {
final PsiAnnotation psiAnnotation = AnnotationUtil.findAnnotation(psiModifierListOwner, ANNOTATION_FQN);
if (psiAnnotation == null) {
return false;
}
final Collection<PsiType> sneakedExceptionTypes = PsiAnnotationUtil.getAnnotationValues(psiAnnotation, PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME, PsiType.class);
//Default SneakyThrows handles all exceptions
if (sneakedExceptionTypes.isEmpty() || sneakedExceptionTypes.iterator().next().equalsToText(JAVA_LANG_THROWABLE)) {
return true;
}
return isExceptionHandled(exceptionClassType, sneakedExceptionTypes);
}
private static boolean isExceptionHandled(@NotNull PsiClassType exceptionClassType, @NotNull Collection<PsiType> sneakedExceptionTypes) {
for (PsiType sneakedExceptionType : sneakedExceptionTypes) {
if (sneakedExceptionType.equalsToText(JAVA_LANG_THROWABLE) || sneakedExceptionType.equals(exceptionClassType)) {
return true;
}
}
final PsiClass unhandledExceptionClass = exceptionClassType.resolve();
if (null != unhandledExceptionClass) {
for (PsiType sneakedExceptionType : sneakedExceptionTypes) {
if (sneakedExceptionType instanceof PsiClassType) {
final PsiClass sneakedExceptionClass = ((PsiClassType) sneakedExceptionType).resolve();
if (null != sneakedExceptionClass && unhandledExceptionClass.isInheritor(sneakedExceptionClass, true)) {
return true;
}
}
}
}
return false;
}
}