|
25 | 25 | import consulo.language.psi.PsiElement; |
26 | 26 | import consulo.localize.LocalizeValue; |
27 | 27 | import jakarta.annotation.Nonnull; |
28 | | -import org.jetbrains.annotations.Nls; |
29 | 28 |
|
30 | 29 | import javax.swing.*; |
31 | 30 |
|
32 | 31 | @ExtensionImpl |
33 | 32 | public class NewExceptionWithoutArgumentsInspection extends BaseInspection { |
| 33 | + @SuppressWarnings("PublicField") |
| 34 | + public boolean ignoreWithoutParameters = false; |
34 | 35 |
|
35 | | - @SuppressWarnings("PublicField") |
36 | | - public boolean ignoreWithoutParameters = false; |
37 | | - |
38 | | - @Nls |
39 | | - @Nonnull |
40 | | - @Override |
41 | | - public String getDisplayName() { |
42 | | - return InspectionGadgetsLocalize.newExceptionWithoutArgumentsDisplayName().get(); |
43 | | - } |
44 | | - |
45 | | - @Nonnull |
46 | | - @Override |
47 | | - protected String buildErrorString(Object... infos) { |
48 | | - return InspectionGadgetsLocalize.newExceptionWithoutArgumentsProblemDescriptor().get(); |
49 | | - } |
50 | | - |
51 | | - @Override |
52 | | - public JComponent createOptionsPanel() { |
53 | | - LocalizeValue message = InspectionGadgetsLocalize.newExceptionWithoutArgumentsIgnoreOption(); |
54 | | - return new SingleCheckboxOptionsPanel(message.get(), this, "ignoreWithoutParameters"); |
55 | | - } |
| 36 | + @Nonnull |
| 37 | + @Override |
| 38 | + public LocalizeValue getDisplayName() { |
| 39 | + return InspectionGadgetsLocalize.newExceptionWithoutArgumentsDisplayName(); |
| 40 | + } |
56 | 41 |
|
57 | | - @Override |
58 | | - public BaseInspectionVisitor buildVisitor() { |
59 | | - return new NewExceptionWithoutArgumentsVisitor(); |
60 | | - } |
| 42 | + @Nonnull |
| 43 | + @Override |
| 44 | + protected String buildErrorString(Object... infos) { |
| 45 | + return InspectionGadgetsLocalize.newExceptionWithoutArgumentsProblemDescriptor().get(); |
| 46 | + } |
61 | 47 |
|
62 | | - private class NewExceptionWithoutArgumentsVisitor extends BaseInspectionVisitor { |
| 48 | + @Override |
| 49 | + public JComponent createOptionsPanel() { |
| 50 | + LocalizeValue message = InspectionGadgetsLocalize.newExceptionWithoutArgumentsIgnoreOption(); |
| 51 | + return new SingleCheckboxOptionsPanel(message.get(), this, "ignoreWithoutParameters"); |
| 52 | + } |
63 | 53 |
|
64 | 54 | @Override |
65 | | - public void visitNewExpression(PsiNewExpression expression) { |
66 | | - super.visitNewExpression(expression); |
67 | | - final PsiExpressionList argumentList = expression.getArgumentList(); |
68 | | - if (argumentList == null) { |
69 | | - return; |
70 | | - } |
71 | | - final PsiExpression[] expressions = argumentList.getExpressions(); |
72 | | - if (expressions.length != 0) { |
73 | | - return; |
74 | | - } |
75 | | - final PsiJavaCodeReferenceElement classReference = expression.getClassReference(); |
76 | | - if (classReference == null) { |
77 | | - return; |
78 | | - } |
79 | | - final PsiElement target = classReference.resolve(); |
80 | | - if (!(target instanceof PsiClass)) { |
81 | | - return; |
82 | | - } |
83 | | - final PsiClass aClass = (PsiClass)target; |
84 | | - if (!InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_LANG_EXCEPTION)) { |
85 | | - return; |
86 | | - } |
87 | | - if (ignoreWithoutParameters) { |
88 | | - if (!hasAccessibleConstructorWithParameters(aClass, expression)) return; |
89 | | - } |
90 | | - registerNewExpressionError(expression); |
| 55 | + public BaseInspectionVisitor buildVisitor() { |
| 56 | + return new NewExceptionWithoutArgumentsVisitor(); |
91 | 57 | } |
92 | 58 |
|
93 | | - private boolean hasAccessibleConstructorWithParameters(PsiClass aClass, PsiElement context) { |
94 | | - final PsiMethod[] constructors = aClass.getConstructors(); |
95 | | - for (PsiMethod constructor : constructors) { |
96 | | - final PsiParameterList parameterList = constructor.getParameterList(); |
97 | | - final int count = parameterList.getParametersCount(); |
98 | | - if (count <= 0) { |
99 | | - continue; |
| 59 | + private class NewExceptionWithoutArgumentsVisitor extends BaseInspectionVisitor { |
| 60 | + |
| 61 | + @Override |
| 62 | + public void visitNewExpression(PsiNewExpression expression) { |
| 63 | + super.visitNewExpression(expression); |
| 64 | + final PsiExpressionList argumentList = expression.getArgumentList(); |
| 65 | + if (argumentList == null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + final PsiExpression[] expressions = argumentList.getExpressions(); |
| 69 | + if (expressions.length != 0) { |
| 70 | + return; |
| 71 | + } |
| 72 | + final PsiJavaCodeReferenceElement classReference = expression.getClassReference(); |
| 73 | + if (classReference == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + final PsiElement target = classReference.resolve(); |
| 77 | + if (!(target instanceof PsiClass)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + final PsiClass aClass = (PsiClass) target; |
| 81 | + if (!InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_LANG_EXCEPTION)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + if (ignoreWithoutParameters) { |
| 85 | + if (!hasAccessibleConstructorWithParameters(aClass, expression)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + registerNewExpressionError(expression); |
100 | 90 | } |
101 | | - final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(context.getProject()).getResolveHelper(); |
102 | | - if (resolveHelper.isAccessible(constructor, context, aClass)) { |
103 | | - return true; |
| 91 | + |
| 92 | + private boolean hasAccessibleConstructorWithParameters(PsiClass aClass, PsiElement context) { |
| 93 | + final PsiMethod[] constructors = aClass.getConstructors(); |
| 94 | + for (PsiMethod constructor : constructors) { |
| 95 | + final PsiParameterList parameterList = constructor.getParameterList(); |
| 96 | + final int count = parameterList.getParametersCount(); |
| 97 | + if (count <= 0) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(context.getProject()).getResolveHelper(); |
| 101 | + if (resolveHelper.isAccessible(constructor, context, aClass)) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + } |
| 105 | + return false; |
104 | 106 | } |
105 | | - } |
106 | | - return false; |
107 | 107 | } |
108 | | - } |
109 | 108 | } |
0 commit comments