void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiClass aClass = ClassUtils.getContainingStaticClass(element); if (aClass == null) { return; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiReferenceExpression methodExpression)) { return; } final PsiElement grandParent = methodExpression.getParent(); if (!(grandParent instanceof PsiMethodCallExpression methodCallExpression)) { return; } final PsiExpressionList list = methodCallExpression.getArgumentList(); final PsiExpression[] expressions = list.getExpressions(); if (expressions.length == 0) return; CommentTracker commentTracker = new CommentTracker(); final String methodName = methodExpression.getReferenceName(); final boolean literalReplacement = "replace".equals(methodName); String regexpText = commentTracker.text(expressions[0]); String initializer = "java.util.regex.Pattern.compile(" + regexpText + (literalReplacement ? ", java.util.regex.Pattern.LITERAL" : "") + ")"; final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); List<String> names = new VariableNameGenerator(aClass, VariableKind.STATIC_FINAL_FIELD) .byExpression(factory.createExpressionFromText("__(" + regexpText + ")", null)) .byName("PATTERN", "REGEX", "REGEXP").generateAll(true); String name = names.get(0); String fieldText = "private static final java.util.regex.Pattern " + name + " = "+ initializer + ";"; final int expressionsLength = expressions.length; PsiField fieldTemplate = factory.createFieldFromText(fieldText, element); for (PsiField classField : aClass.getFields()) { if (classField.hasModifierProperty(PsiModifier.STATIC) && classField.hasModifierProperty(PsiModifier.FINAL) && fieldTemplate.getType().equals(classField.getType()) && EquivalenceChecker.getCanonicalPsiEquivalence() .expressionsAreEquivalent(fieldTemplate.getInitializer(), classField.getInitializer())) { name = classField.getName(); fieldTemplate = null; break; } } @NonNls final StringBuilder expressionText = new StringBuilder(name + "."); final PsiExpression qualifier = methodExpression.getQualifierExpression(); @NonNls final String qualifierText = (qualifier == null) ? "this" : commentTracker.text(qualifier); if ("split".equals(methodName)) { expressionText.append(methodName); expressionText.append('('); expressionText.append(qualifierText); for (int i = 1; i < expressionsLength; i++) { expressionText.append(',').append(commentTracker.text(expressions[i])); } expressionText.append(')'); } else { expressionText.append("matcher(").append(qualifierText).append(")."); if (literalReplacement) { expressionText.append("replaceAll"); } else { expressionText.append(methodName); } expressionText.append('('); boolean quote = false; if (literalReplacement) { quote = (expressionsLength > 1 && needsQuote(expressions[1])); if (quote) { expressionText.append("java.util.regex.Matcher.quoteReplacement("); } } if (expressionsLength > 1) { expressionText.append(commentTracker.text(expressions[1])); for (int i = 2; i < expressionsLength; i++) { expressionText.append(',').append(commentTracker.text(expressions[i])); } } if (literalReplacement && quote) { expressionText.append(')'); } expressionText.append(')'); } JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); if (fieldTemplate != null) { PsiField field = (PsiField)aClass.add(fieldTemplate); field = (PsiField)javaCodeStyleManager.shortenClassReferences(field); updater.rename(field, names); } javaCodeStyleManager.shortenClassReferences( commentTracker.replaceAndRestoreComments(methodCallExpression, expressionText.toString())); }