mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
[lombok] IDEA-303185 Review: Reuse existing functionality
GitOrigin-RevId: 57cb8fa2712107131fb9673a0eb61acd9a6abf0f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
a5dd7dfe6d
commit
dd2dd53669
@@ -1,6 +1,7 @@
|
|||||||
package de.plushnikov.intellij.plugin.handler;
|
package de.plushnikov.intellij.plugin.handler;
|
||||||
|
|
||||||
import com.intellij.codeInsight.CustomExceptionHandler;
|
import com.intellij.codeInsight.CustomExceptionHandler;
|
||||||
|
import com.intellij.codeInsight.ExceptionUtil;
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
import com.intellij.util.JavaPsiConstructorUtil;
|
import com.intellij.util.JavaPsiConstructorUtil;
|
||||||
@@ -13,7 +14,6 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SneakyThrowsExceptionHandler extends CustomExceptionHandler {
|
public class SneakyThrowsExceptionHandler extends CustomExceptionHandler {
|
||||||
@@ -22,12 +22,6 @@ public class SneakyThrowsExceptionHandler extends CustomExceptionHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isHandled(@Nullable PsiElement element, @NotNull PsiClassType exceptionType, PsiElement topElement) {
|
public boolean isHandled(@Nullable PsiElement element, @NotNull PsiClassType exceptionType, PsiElement topElement) {
|
||||||
final PsiCodeBlock containingCodeBlock = PsiTreeUtil.getParentOfType(element, PsiCodeBlock.class, false);
|
|
||||||
if (isCodeBlockWithExceptionInConstructorCall(containingCodeBlock, Collections.singleton(exceptionType))) {
|
|
||||||
// call to a sibling or super constructor is excluded from the @SneakyThrows treatment
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class, PsiTryStatement.class, PsiMethod.class);
|
PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class, PsiTryStatement.class, PsiMethod.class);
|
||||||
if (parent instanceof PsiLambdaExpression) {
|
if (parent instanceof PsiLambdaExpression) {
|
||||||
// lambda it's another scope, @SneakyThrows annotation can't neglect exceptions in lambda only on method, constructor
|
// lambda it's another scope, @SneakyThrows annotation can't neglect exceptions in lambda only on method, constructor
|
||||||
@@ -44,42 +38,30 @@ public class SneakyThrowsExceptionHandler extends CustomExceptionHandler {
|
|||||||
}
|
}
|
||||||
if (!(topElement instanceof PsiCodeBlock)) {
|
if (!(topElement instanceof PsiCodeBlock)) {
|
||||||
final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
||||||
return psiMethod != null && isExceptionHandled(psiMethod, exceptionType);
|
if (psiMethod != null) {
|
||||||
|
if (isConstructorMethodWithExceptionInSiblingConstructorCall(psiMethod, exceptionType)) {
|
||||||
|
// call to a sibling or super constructor is excluded from the @SneakyThrows treatment
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return isExceptionHandled(psiMethod, exceptionType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isCodeBlockWithExceptionInConstructorCall(@Nullable PsiCodeBlock codeBlock,
|
private static boolean isConstructorMethodWithExceptionInSiblingConstructorCall(@NotNull PsiMethod containingMethod,
|
||||||
@NotNull Collection<PsiClassType> exceptionTypes) {
|
@NotNull PsiClassType exceptionTypes) {
|
||||||
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(codeBlock, PsiMethod.class);
|
final PsiMethodCallExpression thisOrSuperCallInConstructor = JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(containingMethod);
|
||||||
if (null != containingMethod) {
|
|
||||||
final PsiMethodCallExpression thisOrSuperCallInConstructor =
|
|
||||||
JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(containingMethod);
|
|
||||||
if (null != thisOrSuperCallInConstructor) {
|
if (null != thisOrSuperCallInConstructor) {
|
||||||
return throwsExceptionsTypes(thisOrSuperCallInConstructor, exceptionTypes);
|
return throwsExceptionsTypes(thisOrSuperCallInConstructor, Collections.singleton(exceptionTypes));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean throwsExceptionsTypes(@NotNull PsiMethodCallExpression thisOrSuperCallInConstructor,
|
static boolean throwsExceptionsTypes(@NotNull PsiMethodCallExpression thisOrSuperCallInConstructor,
|
||||||
@NotNull Collection<PsiClassType> exceptionTypes) {
|
@NotNull Collection<PsiClassType> exceptionTypes) {
|
||||||
ExceptionTypesCollector visitor = new ExceptionTypesCollector();
|
final List<PsiClassType> thrownExceptions = ExceptionUtil.getThrownExceptions(thisOrSuperCallInConstructor);
|
||||||
thisOrSuperCallInConstructor.accept(visitor);
|
return ContainerUtil.intersects(thrownExceptions, exceptionTypes);
|
||||||
return ContainerUtil.intersects(visitor.exceptionTypes, exceptionTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ExceptionTypesCollector extends JavaRecursiveElementWalkingVisitor {
|
|
||||||
private final Collection<PsiClassType> exceptionTypes = new HashSet<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
|
|
||||||
PsiMethod method = expression.resolveMethod();
|
|
||||||
if (method != null) {
|
|
||||||
Collections.addAll(exceptionTypes, method.getThrowsList().getReferencedTypes());
|
|
||||||
}
|
|
||||||
super.visitMethodCallExpression(expression);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isHandledByTryCatch(@NotNull PsiClassType exceptionType, PsiTryStatement topElement) {
|
private static boolean isHandledByTryCatch(@NotNull PsiClassType exceptionType, PsiTryStatement topElement) {
|
||||||
|
|||||||
Reference in New Issue
Block a user