|
18 | 18 | import com.intellij.java.language.psi.*; |
19 | 19 | import com.intellij.java.language.psi.impl.source.resolve.DefaultParameterTypeInferencePolicy; |
20 | 20 | import com.intellij.java.language.psi.util.PsiUtil; |
| 21 | +import consulo.annotation.access.RequiredReadAction; |
21 | 22 | import consulo.annotation.access.RequiredWriteAction; |
22 | 23 | import consulo.annotation.component.ExtensionImpl; |
23 | 24 | import consulo.language.editor.inspection.LocalQuickFix; |
|
40 | 41 | */ |
41 | 42 | @ExtensionImpl |
42 | 43 | public class RedundantTypeArgsInspection extends GenericsInspectionToolBase { |
43 | | - private static final Logger LOG = Logger.getInstance(RedundantTypeArgsInspection.class); |
44 | | - |
45 | | - public RedundantTypeArgsInspection() { |
46 | | - myQuickFixAction = new MyQuickFixAction(); |
47 | | - } |
48 | | - |
49 | | - private final LocalQuickFix myQuickFixAction; |
50 | | - |
51 | | - @Override |
52 | | - public boolean isEnabledByDefault() { |
53 | | - return true; |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - @Nonnull |
58 | | - public LocalizeValue getGroupDisplayName() { |
59 | | - return InspectionLocalize.groupNamesVerboseOrRedundantCodeConstructs(); |
60 | | - } |
61 | | - |
62 | | - @Override |
63 | | - @Nonnull |
64 | | - public LocalizeValue getDisplayName() { |
65 | | - return InspectionLocalize.inspectionRedundantTypeDisplayName(); |
66 | | - } |
67 | | - |
68 | | - @Override |
69 | | - @Nonnull |
70 | | - public String getShortName() { |
71 | | - return "RedundantTypeArguments"; |
72 | | - } |
73 | | - |
74 | | - @Override |
75 | | - public ProblemDescriptor[] checkMethod(@Nonnull PsiMethod psiMethod, @Nonnull InspectionManager manager, boolean isOnTheFly, Object state) { |
76 | | - final PsiCodeBlock body = psiMethod.getBody(); |
77 | | - if (body != null) { |
78 | | - return getDescriptions(body, manager, isOnTheFly, state); |
79 | | - } |
80 | | - return null; |
81 | | - } |
82 | | - |
83 | | - @Override |
84 | | - public ProblemDescriptor[] getDescriptions(PsiElement place, final InspectionManager inspectionManager, boolean isOnTheFly, Object state) { |
85 | | - final List<ProblemDescriptor> problems = new ArrayList<>(); |
86 | | - place.accept(new JavaRecursiveElementWalkingVisitor() { |
87 | | - @Override |
88 | | - public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) { |
89 | | - final PsiType[] typeArguments = expression.getTypeArguments(); |
90 | | - if (typeArguments.length > 0) { |
91 | | - checkCallExpression(expression.getMethodExpression(), typeArguments, expression, inspectionManager, problems); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - @Override |
96 | | - public void visitNewExpression(@Nonnull PsiNewExpression expression) { |
97 | | - final PsiType[] typeArguments = expression.getTypeArguments(); |
98 | | - if (typeArguments.length > 0) { |
99 | | - final PsiJavaCodeReferenceElement classReference = expression.getClassReference(); |
100 | | - if (classReference != null) { |
101 | | - checkCallExpression(classReference, typeArguments, expression, inspectionManager, problems); |
102 | | - } |
103 | | - } |
104 | | - } |
| 44 | + private static final Logger LOG = Logger.getInstance(RedundantTypeArgsInspection.class); |
105 | 45 |
|
106 | | - private void checkCallExpression( |
107 | | - final PsiJavaCodeReferenceElement reference, |
108 | | - final PsiType[] typeArguments, |
109 | | - PsiCallExpression expression, |
110 | | - final InspectionManager inspectionManager, |
111 | | - final List<ProblemDescriptor> problems |
112 | | - ) { |
113 | | - PsiExpressionList argumentList = expression.getArgumentList(); |
114 | | - if (argumentList == null) return; |
115 | | - final JavaResolveResult resolveResult = reference.advancedResolve(false); |
116 | | - |
117 | | - final PsiElement element = resolveResult.getElement(); |
118 | | - if (element instanceof PsiMethod method && resolveResult.isValidResult()) { |
119 | | - final PsiTypeParameter[] typeParameters = method.getTypeParameters(); |
120 | | - if (typeParameters.length == typeArguments.length) { |
121 | | - final PsiParameter[] parameters = method.getParameterList().getParameters(); |
122 | | - PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(expression.getProject()).getResolveHelper(); |
123 | | - final PsiSubstitutor psiSubstitutor = resolveHelper.inferTypeArguments( |
124 | | - typeParameters, |
125 | | - parameters, |
126 | | - argumentList.getExpressions(), |
127 | | - PsiSubstitutor.EMPTY, |
128 | | - expression, |
129 | | - DefaultParameterTypeInferencePolicy.INSTANCE |
130 | | - ); |
131 | | - for (int i = 0, length = typeParameters.length; i < length; i++) { |
132 | | - PsiTypeParameter typeParameter = typeParameters[i]; |
133 | | - final PsiType inferredType = psiSubstitutor.getSubstitutionMap().get(typeParameter); |
134 | | - if (!typeArguments[i].equals(inferredType)) return; |
135 | | - if (PsiUtil.resolveClassInType(method.getReturnType()) == typeParameter |
136 | | - && PsiPrimitiveType.getUnboxedType(inferredType) != null) { |
137 | | - return; |
138 | | - } |
139 | | - } |
| 46 | + public RedundantTypeArgsInspection() { |
| 47 | + myQuickFixAction = new MyQuickFixAction(); |
| 48 | + } |
140 | 49 |
|
141 | | - final PsiCallExpression copy = (PsiCallExpression) expression.copy(); //see IDEADEV-8174 |
142 | | - try { |
143 | | - copy.getTypeArgumentList().delete(); |
144 | | - if (copy.resolveMethod() != element) return; |
145 | | - } catch (IncorrectOperationException e) { |
146 | | - LOG.error(e); |
147 | | - return; |
148 | | - } |
| 50 | + private final LocalQuickFix myQuickFixAction; |
149 | 51 |
|
150 | | - final ProblemDescriptor descriptor = inspectionManager.createProblemDescriptor( |
151 | | - expression.getTypeArgumentList(), |
152 | | - InspectionLocalize.inspectionRedundantTypeProblemDescriptor().get(), |
153 | | - myQuickFixAction, |
154 | | - ProblemHighlightType.LIKE_UNUSED_SYMBOL, |
155 | | - false |
156 | | - ); |
157 | | - problems.add(descriptor); |
158 | | - } |
159 | | - } |
160 | | - } |
| 52 | + @Override |
| 53 | + public boolean isEnabledByDefault() { |
| 54 | + return true; |
| 55 | + } |
161 | 56 |
|
162 | | - }); |
| 57 | + @Override |
| 58 | + @Nonnull |
| 59 | + public LocalizeValue getGroupDisplayName() { |
| 60 | + return InspectionLocalize.groupNamesVerboseOrRedundantCodeConstructs(); |
| 61 | + } |
163 | 62 |
|
164 | | - if (problems.isEmpty()) return null; |
165 | | - return problems.toArray(new ProblemDescriptor[problems.size()]); |
166 | | - } |
| 63 | + @Override |
| 64 | + @Nonnull |
| 65 | + public LocalizeValue getDisplayName() { |
| 66 | + return InspectionLocalize.inspectionRedundantTypeDisplayName(); |
| 67 | + } |
167 | 68 |
|
168 | | - private static class MyQuickFixAction implements LocalQuickFix { |
169 | 69 | @Override |
170 | 70 | @Nonnull |
171 | | - public LocalizeValue getName() { |
172 | | - return InspectionLocalize.inspectionRedundantTypeRemoveQuickfix(); |
| 71 | + public String getShortName() { |
| 72 | + return "RedundantTypeArguments"; |
173 | 73 | } |
174 | 74 |
|
175 | 75 | @Override |
176 | | - @RequiredWriteAction |
177 | | - public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) { |
178 | | - final PsiReferenceParameterList typeArgumentList = (PsiReferenceParameterList) descriptor.getPsiElement(); |
179 | | - try { |
180 | | - final PsiMethodCallExpression expr = (PsiMethodCallExpression) JavaPsiFacade.getInstance(project).getElementFactory() |
181 | | - .createExpressionFromText("foo()", null); |
182 | | - typeArgumentList.replace(expr.getTypeArgumentList()); |
183 | | - } catch (IncorrectOperationException e) { |
184 | | - LOG.error(e); |
185 | | - } |
| 76 | + public ProblemDescriptor[] checkMethod( |
| 77 | + @Nonnull PsiMethod psiMethod, |
| 78 | + @Nonnull InspectionManager manager, |
| 79 | + boolean isOnTheFly, |
| 80 | + Object state |
| 81 | + ) { |
| 82 | + PsiCodeBlock body = psiMethod.getBody(); |
| 83 | + if (body != null) { |
| 84 | + return getDescriptions(body, manager, isOnTheFly, state); |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public ProblemDescriptor[] getDescriptions( |
| 91 | + PsiElement place, |
| 92 | + final InspectionManager inspectionManager, |
| 93 | + boolean isOnTheFly, |
| 94 | + Object state |
| 95 | + ) { |
| 96 | + final List<ProblemDescriptor> problems = new ArrayList<>(); |
| 97 | + place.accept(new JavaRecursiveElementWalkingVisitor() { |
| 98 | + @Override |
| 99 | + @RequiredReadAction |
| 100 | + public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) { |
| 101 | + PsiType[] typeArguments = expression.getTypeArguments(); |
| 102 | + if (typeArguments.length > 0) { |
| 103 | + checkCallExpression(expression.getMethodExpression(), typeArguments, expression, inspectionManager, problems); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + @RequiredReadAction |
| 109 | + public void visitNewExpression(@Nonnull PsiNewExpression expression) { |
| 110 | + PsiType[] typeArguments = expression.getTypeArguments(); |
| 111 | + if (typeArguments.length > 0) { |
| 112 | + PsiJavaCodeReferenceElement classReference = expression.getClassReference(); |
| 113 | + if (classReference != null) { |
| 114 | + checkCallExpression(classReference, typeArguments, expression, inspectionManager, problems); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @RequiredReadAction |
| 120 | + private void checkCallExpression( |
| 121 | + PsiJavaCodeReferenceElement reference, |
| 122 | + PsiType[] typeArguments, |
| 123 | + PsiCallExpression expression, |
| 124 | + InspectionManager inspectionManager, |
| 125 | + List<ProblemDescriptor> problems |
| 126 | + ) { |
| 127 | + PsiExpressionList argumentList = expression.getArgumentList(); |
| 128 | + if (argumentList == null) { |
| 129 | + return; |
| 130 | + } |
| 131 | + JavaResolveResult resolveResult = reference.advancedResolve(false); |
| 132 | + |
| 133 | + if (resolveResult.getElement() instanceof PsiMethod method && resolveResult.isValidResult()) { |
| 134 | + PsiTypeParameter[] typeParameters = method.getTypeParameters(); |
| 135 | + if (typeParameters.length == typeArguments.length) { |
| 136 | + PsiParameter[] parameters = method.getParameterList().getParameters(); |
| 137 | + PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(expression.getProject()).getResolveHelper(); |
| 138 | + PsiSubstitutor psiSubstitutor = resolveHelper.inferTypeArguments( |
| 139 | + typeParameters, |
| 140 | + parameters, |
| 141 | + argumentList.getExpressions(), |
| 142 | + PsiSubstitutor.EMPTY, |
| 143 | + expression, |
| 144 | + DefaultParameterTypeInferencePolicy.INSTANCE |
| 145 | + ); |
| 146 | + for (int i = 0, length = typeParameters.length; i < length; i++) { |
| 147 | + PsiTypeParameter typeParameter = typeParameters[i]; |
| 148 | + PsiType inferredType = psiSubstitutor.getSubstitutionMap().get(typeParameter); |
| 149 | + if (!typeArguments[i].equals(inferredType)) { |
| 150 | + return; |
| 151 | + } |
| 152 | + if (PsiUtil.resolveClassInType(method.getReturnType()) == typeParameter |
| 153 | + && PsiPrimitiveType.getUnboxedType(inferredType) != null) { |
| 154 | + return; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + PsiCallExpression copy = (PsiCallExpression) expression.copy(); //see IDEADEV-8174 |
| 159 | + try { |
| 160 | + copy.getTypeArgumentList().delete(); |
| 161 | + if (copy.resolveMethod() != method) { |
| 162 | + return; |
| 163 | + } |
| 164 | + } |
| 165 | + catch (IncorrectOperationException e) { |
| 166 | + LOG.error(e); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + ProblemDescriptor descriptor = inspectionManager.newProblemDescriptor( |
| 171 | + InspectionLocalize.inspectionRedundantTypeProblemDescriptor() |
| 172 | + ) |
| 173 | + .range(expression.getTypeArgumentList()) |
| 174 | + .highlightType(ProblemHighlightType.LIKE_UNUSED_SYMBOL) |
| 175 | + .withOptionalFix(myQuickFixAction) |
| 176 | + .create(); |
| 177 | + problems.add(descriptor); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + if (problems.isEmpty()) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + return problems.toArray(new ProblemDescriptor[problems.size()]); |
| 187 | + } |
| 188 | + |
| 189 | + private static class MyQuickFixAction implements LocalQuickFix { |
| 190 | + @Override |
| 191 | + @Nonnull |
| 192 | + public LocalizeValue getName() { |
| 193 | + return InspectionLocalize.inspectionRedundantTypeRemoveQuickfix(); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + @RequiredWriteAction |
| 198 | + public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) { |
| 199 | + PsiReferenceParameterList typeArgumentList = (PsiReferenceParameterList) descriptor.getPsiElement(); |
| 200 | + try { |
| 201 | + PsiMethodCallExpression expr = (PsiMethodCallExpression) JavaPsiFacade.getInstance(project).getElementFactory() |
| 202 | + .createExpressionFromText("foo()", null); |
| 203 | + typeArgumentList.replace(expr.getTypeArgumentList()); |
| 204 | + } |
| 205 | + catch (IncorrectOperationException e) { |
| 206 | + LOG.error(e); |
| 207 | + } |
| 208 | + } |
186 | 209 | } |
187 | | - } |
188 | 210 | } |
0 commit comments