1919import com .intellij .java .language .psi .*;
2020import com .intellij .java .language .psi .util .PsiUtil ;
2121import com .intellij .java .language .util .VisibilityUtil ;
22+ import consulo .annotation .access .RequiredReadAction ;
23+ import consulo .annotation .access .RequiredWriteAction ;
2224import consulo .language .codeStyle .CodeStyleManager ;
2325import consulo .language .editor .refactoring .BaseRefactoringProcessor ;
2426import consulo .language .editor .refactoring .localize .RefactoringLocalize ;
4042import consulo .util .collection .MultiMap ;
4143import consulo .util .lang .ref .SimpleReference ;
4244import jakarta .annotation .Nonnull ;
43- import org .jetbrains .annotations .NonNls ;
4445
45- import java .util .ArrayList ;
46- import java .util .Collection ;
47- import java .util .HashSet ;
48- import java .util .List ;
46+ import java .util .*;
4947
5048/**
5149 * @author dsl
@@ -65,7 +63,7 @@ public ReplaceConstructorWithFactoryProcessor(
6563 PsiMethod originalConstructor ,
6664 PsiClass originalClass ,
6765 PsiClass targetClass ,
68- @ NonNls String factoryName
66+ String factoryName
6967 ) {
7068 super (project );
7169 myOriginalClass = originalClass ;
@@ -79,15 +77,16 @@ public ReplaceConstructorWithFactoryProcessor(
7977 }
8078
8179 private boolean isInner (PsiClass originalClass ) {
82- final boolean result = PsiUtil .isInnerClass (originalClass );
80+ boolean result = PsiUtil .isInnerClass (originalClass );
8381 if (result ) {
8482 LOG .assertTrue (PsiTreeUtil .isAncestor (myTargetClass , originalClass , false ));
8583 }
8684 return result ;
8785 }
8886
8987 @ Nonnull
90- protected UsageViewDescriptor createUsageViewDescriptor (UsageInfo [] usages ) {
88+ @ Override
89+ protected UsageViewDescriptor createUsageViewDescriptor (@ Nonnull UsageInfo [] usages ) {
9190 if (myConstructor != null ) {
9291 return new ReplaceConstructorWithFactoryViewDescriptor (myConstructor );
9392 }
@@ -99,11 +98,13 @@ protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) {
9998 private List <PsiElement > myNonNewConstructorUsages ;
10099
101100 @ Nonnull
101+ @ Override
102+ @ RequiredReadAction
102103 protected UsageInfo [] findUsages () {
103104 GlobalSearchScope projectScope = GlobalSearchScope .projectScope (myProject );
104105
105- ArrayList <UsageInfo > usages = new ArrayList <UsageInfo >();
106- myNonNewConstructorUsages = new ArrayList <PsiElement >();
106+ ArrayList <UsageInfo > usages = new ArrayList <>();
107+ myNonNewConstructorUsages = new ArrayList <>();
107108
108109 for (PsiReference reference : ReferencesSearch .search (
109110 myConstructor == null ? myOriginalClass : myConstructor ,
@@ -112,13 +113,13 @@ protected UsageInfo[] findUsages() {
112113 )) {
113114 PsiElement element = reference .getElement ();
114115
115- if (element .getParent () instanceof PsiNewExpression ) {
116- usages .add (new UsageInfo (element . getParent () ));
116+ if (element .getParent () instanceof PsiNewExpression newExpr ) {
117+ usages .add (new UsageInfo (newExpr ));
117118 }
118119 else if ("super" .equals (element .getText ()) || "this" .equals (element .getText ())) {
119120 myNonNewConstructorUsages .add (element );
120121 }
121- else if (element instanceof PsiMethod && (( PsiMethod ) element ) .isConstructor ()) {
122+ else if (element instanceof PsiMethod method && method .isConstructor ()) {
122123 myNonNewConstructorUsages .add (element );
123124 }
124125 else if (element instanceof PsiClass ) {
@@ -146,9 +147,9 @@ else if (element instanceof PsiClass) {
146147 protected boolean preprocessUsages (@ Nonnull SimpleReference <UsageInfo []> refUsages ) {
147148 UsageInfo [] usages = refUsages .get ();
148149
149- MultiMap <PsiElement , String > conflicts = new MultiMap <PsiElement , String >();
150- final PsiResolveHelper helper = JavaPsiFacade .getInstance (myProject ).getResolveHelper ();
151- final PsiClass constructorContainingClass = getConstructorContainingClass ();
150+ MultiMap <PsiElement , String > conflicts = new MultiMap <>();
151+ PsiResolveHelper helper = JavaPsiFacade .getInstance (myProject ).getResolveHelper ();
152+ PsiClass constructorContainingClass = getConstructorContainingClass ();
152153 if (!helper .isAccessible (constructorContainingClass , myTargetClass , null )) {
153154 LocalizeValue message = RefactoringLocalize .class0IsNotAccessibleFromTarget1 (
154155 RefactoringUIUtil .getDescription (constructorContainingClass , true ),
@@ -157,10 +158,10 @@ protected boolean preprocessUsages(@Nonnull SimpleReference<UsageInfo[]> refUsag
157158 conflicts .putValue (constructorContainingClass , message .get ());
158159 }
159160
160- HashSet <PsiElement > reportedContainers = new HashSet <PsiElement >();
161- final String targetClassDescription = RefactoringUIUtil .getDescription (myTargetClass , true );
161+ Set <PsiElement > reportedContainers = new HashSet <>();
162+ String targetClassDescription = RefactoringUIUtil .getDescription (myTargetClass , true );
162163 for (UsageInfo usage : usages ) {
163- final PsiElement container = ConflictsUtil .getContainer (usage .getElement ());
164+ PsiElement container = ConflictsUtil .getContainer (usage .getElement ());
164165 if (!reportedContainers .contains (container )) {
165166 reportedContainers .add (container );
166167 if (!helper .isAccessible (myTargetClass , usage .getElement (), null )) {
@@ -175,9 +176,9 @@ protected boolean preprocessUsages(@Nonnull SimpleReference<UsageInfo[]> refUsag
175176
176177 if (myIsInner ) {
177178 for (UsageInfo usage : usages ) {
178- final PsiField field = PsiTreeUtil .getParentOfType (usage .getElement (), PsiField .class );
179+ PsiField field = PsiTreeUtil .getParentOfType (usage .getElement (), PsiField .class );
179180 if (field != null ) {
180- final PsiClass containingClass = field .getContainingClass ();
181+ PsiClass containingClass = field .getContainingClass ();
181182
182183 if (PsiTreeUtil .isAncestor (containingClass , myTargetClass , true )) {
183184 LocalizeValue message = RefactoringLocalize .constructorBeingRefactoredIsUsedInInitializerOf0 (
@@ -203,8 +204,9 @@ private PsiClass getConstructorContainingClass() {
203204 }
204205 }
205206
206- protected void performRefactoring (UsageInfo [] usages ) {
207-
207+ @ Override
208+ @ RequiredWriteAction
209+ protected void performRefactoring (@ Nonnull UsageInfo [] usages ) {
208210 try {
209211 PsiReferenceExpression classReferenceExpression =
210212 myFactory .createReferenceExpression (myTargetClass );
@@ -263,16 +265,17 @@ protected void performRefactoring(UsageInfo[] usages) {
263265 }
264266 }
265267
268+ @ RequiredReadAction
266269 private PsiMethod createFactoryMethod () throws IncorrectOperationException {
267- final PsiClass containingClass = getConstructorContainingClass ();
270+ PsiClass containingClass = getConstructorContainingClass ();
268271 PsiClassType type = myFactory .createType (containingClass , PsiSubstitutor .EMPTY );
269- final PsiMethod factoryMethod = myFactory .createMethod (myFactoryName , type );
272+ PsiMethod factoryMethod = myFactory .createMethod (myFactoryName , type );
270273 if (myConstructor != null ) {
271274 factoryMethod .getParameterList ().replace (myConstructor .getParameterList ());
272275 factoryMethod .getThrowsList ().replace (myConstructor .getThrowsList ());
273276 }
274277
275- Collection <String > names = new HashSet <String >();
278+ Collection <String > names = new HashSet <>();
276279 for (PsiTypeParameter typeParameter : PsiUtil .typeParametersIterable (myConstructor != null ? myConstructor : containingClass )) {
277280 if (!names .contains (typeParameter .getName ())) { //Otherwise type parameter is hidden in the constructor
278281 names .add (typeParameter .getName ());
@@ -285,7 +288,7 @@ private PsiMethod createFactoryMethod() throws IncorrectOperationException {
285288 PsiNewExpression newExpression = (PsiNewExpression )returnStatement .getReturnValue ();
286289 PsiJavaCodeReferenceElement classRef = myFactory .createReferenceElementByType (type );
287290 newExpression .getClassReference ().replace (classRef );
288- final PsiExpressionList argumentList = newExpression .getArgumentList ();
291+ PsiExpressionList argumentList = newExpression .getArgumentList ();
289292
290293 PsiParameter [] params = factoryMethod .getParameterList ().getParameters ();
291294
@@ -306,7 +309,7 @@ private PsiMethod createFactoryMethod() throws IncorrectOperationException {
306309
307310 @ PsiModifier .ModifierConstant
308311 private String getDefaultFactoryVisibility () {
309- final PsiModifierList modifierList ;
312+ PsiModifierList modifierList ;
310313 if (myConstructor != null ) {
311314 modifierList = myConstructor .getModifierList ();
312315 }
@@ -316,7 +319,9 @@ private String getDefaultFactoryVisibility() {
316319 return VisibilityUtil .getVisibilityModifier (modifierList );
317320 }
318321
319-
322+ @ Nonnull
323+ @ Override
324+ @ RequiredReadAction
320325 protected String getCommandName () {
321326 if (myConstructor != null ) {
322327 return RefactoringLocalize .replaceConstructor0WithAFactoryMethod (DescriptiveNameUtil .getDescriptiveName (myConstructor )).get ();
0 commit comments