2727import com .intellij .java .language .psi .javadoc .PsiDocTag ;
2828import com .intellij .java .language .psi .util .PsiUtil ;
2929import com .intellij .java .language .util .VisibilityUtil ;
30+ import consulo .annotation .access .RequiredReadAction ;
31+ import consulo .annotation .access .RequiredWriteAction ;
3032import consulo .annotation .component .ExtensionImpl ;
3133import consulo .language .Language ;
3234import consulo .language .codeStyle .CodeStyleManager ;
@@ -53,15 +55,20 @@ public class JavaIntroduceParameterMethodUsagesProcessor implements IntroducePar
5355 Logger .getInstance (JavaIntroduceParameterMethodUsagesProcessor .class );
5456 private static final JavaLanguage myLanguage = Language .findInstance (JavaLanguage .class );
5557
58+ @ RequiredReadAction
5659 private static boolean isJavaUsage (UsageInfo usage ) {
5760 PsiElement e = usage .getElement ();
5861 return e != null && e .getLanguage ().is (myLanguage );
5962 }
6063
64+ @ Override
65+ @ RequiredReadAction
6166 public boolean isMethodUsage (UsageInfo usage ) {
6267 return RefactoringUtil .isMethodUsage (usage .getElement ()) && isJavaUsage (usage );
6368 }
6469
70+ @ Override
71+ @ RequiredWriteAction
6572 public boolean processChangeMethodUsage (
6673 IntroduceParameterData data ,
6774 UsageInfo usage ,
@@ -108,13 +115,9 @@ public boolean processChangeMethodUsage(
108115 PsiElement initializer =
109116 ExpressionConverter .getExpression (data .getParameterInitializer ().getExpression (), JavaLanguage .INSTANCE , data .getProject ());
110117 assert initializer instanceof PsiExpression ;
111- if (initializer instanceof PsiNewExpression ) {
112- if (!PsiDiamondTypeUtil .canChangeContextForDiamond (
113- (PsiNewExpression ) initializer ,
114- ((PsiNewExpression ) initializer ).getType ()
115- )) {
116- initializer = PsiDiamondTypeUtil .expandTopLevelDiamondsInside ((PsiNewExpression ) initializer );
117- }
118+ if (initializer instanceof PsiNewExpression newExpression
119+ && !PsiDiamondTypeUtil .canChangeContextForDiamond (newExpression , newExpression .getType ())) {
120+ initializer = PsiDiamondTypeUtil .expandTopLevelDiamondsInside (newExpression );
118121 }
119122 substituteTypeParametersInInitializer (initializer , callExpression , argList , methodToSearchFor );
120123 ChangeContextUtil .encodeContextInfo (initializer , true );
@@ -141,20 +144,23 @@ private static void substituteTypeParametersInInitializer(
141144 PsiMethod method
142145 ) {
143146 Project project = method .getProject ();
144- PsiSubstitutor psiSubstitutor = JavaPsiFacade .getInstance (project ).getResolveHelper ()
145- .inferTypeArguments (
146- method .getTypeParameters (),
147- method .getParameterList ().getParameters (),
148- argList .getExpressions (),
149- PsiSubstitutor .EMPTY ,
150- callExpression ,
151- DefaultParameterTypeInferencePolicy .INSTANCE
152- );
153- RefactoringUtil .replaceMovedMemberTypeParameters (initializer , PsiUtil .typeParametersIterable (method ), psiSubstitutor ,
147+ PsiSubstitutor psiSubstitutor = JavaPsiFacade .getInstance (project ).getResolveHelper ().inferTypeArguments (
148+ method .getTypeParameters (),
149+ method .getParameterList ().getParameters (),
150+ argList .getExpressions (),
151+ PsiSubstitutor .EMPTY ,
152+ callExpression ,
153+ DefaultParameterTypeInferencePolicy .INSTANCE
154+ );
155+ RefactoringUtil .replaceMovedMemberTypeParameters (
156+ initializer ,
157+ PsiUtil .typeParametersIterable (method ),
158+ psiSubstitutor ,
154159 JavaPsiFacade .getElementFactory (project )
155160 );
156161 }
157162
163+ @ RequiredWriteAction
158164 private static void removeParametersFromCall (@ Nonnull PsiExpressionList argList , IntList parametersToRemove ) {
159165 PsiExpression [] exprs = argList .getExpressions ();
160166
@@ -185,6 +191,7 @@ private static PsiExpression getLast(PsiExpression[] oldArgs) {
185191 }
186192
187193 @ Override
194+ @ RequiredReadAction
188195 public void findConflicts (IntroduceParameterData data , UsageInfo [] usages , MultiMap <PsiElement , LocalizeValue > conflicts ) {
189196 PsiMethod method = data .getMethodToReplaceIn ();
190197 int parametersCount = method .getParameterList ().getParametersCount ();
@@ -197,8 +204,8 @@ public void findConflicts(IntroduceParameterData data, UsageInfo[] usages, Multi
197204 PsiExpressionList argList = call .getArgumentList ();
198205 if (argList != null ) {
199206 int actualParamLength = argList .getExpressions ().length ;
200- if ((method .isVarArgs () && actualParamLength + 1 < parametersCount ) ||
201- (!method .isVarArgs () && actualParamLength < parametersCount )) {
207+ if ((method .isVarArgs () && actualParamLength + 1 < parametersCount )
208+ || (!method .isVarArgs () && actualParamLength < parametersCount )) {
202209 conflicts .putValue (
203210 call ,
204211 LocalizeValue .localizeTODO (
@@ -223,6 +230,7 @@ public void findConflicts(IntroduceParameterData data, UsageInfo[] usages, Multi
223230 }
224231
225232 @ Override
233+ @ RequiredWriteAction
226234 public boolean processChangeMethodSignature (
227235 IntroduceParameterData data ,
228236 UsageInfo usage ,
@@ -288,11 +296,12 @@ public static PsiParameter getAnchorParameter(PsiMethod methodToReplaceIn) {
288296 return anchorParameter ;
289297 }
290298
299+ @ Override
300+ @ RequiredWriteAction
291301 public boolean processAddDefaultConstructor (IntroduceParameterData data , UsageInfo usage , UsageInfo [] usages ) {
292- if (!(usage .getElement () instanceof PsiClass ) || !isJavaUsage (usage )) {
302+ if (!(usage .getElement () instanceof PsiClass aClass ) || !isJavaUsage (usage )) {
293303 return true ;
294304 }
295- PsiClass aClass = (PsiClass ) usage .getElement ();
296305 if (!(aClass instanceof PsiAnonymousClass )) {
297306 PsiElementFactory factory = JavaPsiFacade .getInstance (data .getProject ()).getElementFactory ();
298307 PsiMethod constructor = factory .createMethodFromText (aClass .getName () + "(){}" , aClass );
@@ -307,15 +316,13 @@ public boolean processAddDefaultConstructor(IntroduceParameterData data, UsageIn
307316 return false ;
308317 }
309318
310- public boolean processAddSuperCall (
311- IntroduceParameterData data ,
312- UsageInfo usage ,
313- UsageInfo [] usages
314- ) throws IncorrectOperationException {
315- if (!(usage .getElement () instanceof PsiMethod ) || !isJavaUsage (usage )) {
319+ @ Override
320+ @ RequiredWriteAction
321+ public boolean processAddSuperCall (IntroduceParameterData data , UsageInfo usage , UsageInfo [] usages )
322+ throws IncorrectOperationException {
323+ if (!(usage .getElement () instanceof PsiMethod constructor ) || !isJavaUsage (usage )) {
316324 return true ;
317325 }
318- PsiMethod constructor = (PsiMethod ) usage .getElement ();
319326
320327 if (!constructor .isConstructor ()) {
321328 return true ;
0 commit comments