void (@NotNull PsiMember constructorOrClass, @NotNull PsiClass targetClass, @NotNull UsageData usages, @NotNull ModPsiUpdater updater) { Project project = constructorOrClass.getProject(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiClass wrContainingClass = updater.getWritable( constructorOrClass instanceof PsiClass cls ? cls : Objects.requireNonNull(constructorOrClass.getContainingClass())); PsiReferenceExpression classReferenceExpression = factory.createReferenceExpression(targetClass); List<String> names = suggestNames(targetClass, Objects.requireNonNull(wrContainingClass.getName())); String factoryName = names.get(0); PsiReferenceExpression qualifiedMethodReference = (PsiReferenceExpression)factory.createExpressionFromText("A." + factoryName, null); PsiMethod constructor = ObjectUtils.tryCast(constructorOrClass, PsiMethod.class); PsiClass wrClass = updater.getWritable(targetClass); PsiMethod wrConstructor = updater.getWritable(constructor); List<PsiNewExpression> writableUsages = ContainerUtil.map(usages.newUsages, updater::getWritable); PsiMethod factoryMethod = createFactoryMethod(project, wrContainingClass, constructor, factoryName); factoryMethod = (PsiMethod)wrClass.add(factoryMethod); if (constructor == null) { wrConstructor = (PsiMethod)wrContainingClass.add(factory.createConstructor()); } PsiUtil.setModifierProperty(wrConstructor, getMinimalAccessLevel(constructorOrClass, usages.otherUsages), true); for (PsiNewExpression newExpression : writableUsages) { var factoryCall = (PsiMethodCallExpression)factory.createExpressionFromText(factoryName + "()", newExpression); factoryCall.getArgumentList().replace(Objects.requireNonNull(newExpression.getArgumentList())); PsiExpression newQualifier = newExpression.getQualifier(); PsiReferenceExpression factoryCallRef = factoryCall.getMethodExpression(); PsiElement resolvedFactoryMethod = factoryCallRef.resolve(); if (resolvedFactoryMethod != factoryMethod || newQualifier != null) { factoryCallRef = (PsiReferenceExpression)factoryCallRef.replace(qualifiedMethodReference); PsiElement qualifier = newQualifier == null ? classReferenceExpression : newQualifier; Objects.requireNonNull(factoryCallRef.getQualifierExpression()).replace(qualifier); } newExpression.replace(factoryCall); } updater.rename(factoryMethod, names); }