ModCommand (@NotNull ActionContext actionContext) { PsiElement element = actionContext.findLeaf(); List<ExpressionToConstantComparison> comparisons = comparisons(element).toList(); if (comparisons.size() < 2) return ModCommand.nop(); PsiClass containingClass = ClassUtils.getContainingStaticClass(element); if (containingClass == null) return ModCommand.nop(); List<ExpressionToConstantReplacementContext> copies = myProcessDuplicates == ThreeState.NO ? List.of() : findCopies(comparisons, containingClass); if (myProcessDuplicates == ThreeState.UNSURE && !copies.isEmpty()) { return new ModChooseAction(JavaBundle.message("intention.extract.set.from.comparison.chain.popup.title"), List.of(new ExtractSetFromComparisonChainAction(false), new ExtractSetFromComparisonChainAction(true))); } LinkedHashSet<String> suggestions = getSuggestions(comparisons); return ModCommand.psiUpdate(containingClass, (cls, updater) -> { Project project = cls.getProject(); ExpressionToConstantReplacementContext context = new ExpressionToConstantReplacementContext(comparisons).getWritable(updater); List<ExpressionToConstantReplacementContext> writableCopies = ContainerUtil.map(copies, copy -> copy.getWritable(updater)); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(project); String name = manager.suggestUniqueVariableName(suggestions.iterator().next(), cls, false); String fieldInitializer = context.myInitializer; PsiType elementType = context.myType; if (elementType == null) return; String pattern = getInitializer(elementType, cls); String initializer = MessageFormat.format(pattern, fieldInitializer, elementType.getCanonicalText()); String modifiers = cls.isInterface() ? "" : "private static final "; String type = CommonClassNames.JAVA_UTIL_SET + (PsiUtil.isLanguageLevel5OrHigher(cls) ? "<" + elementType.getCanonicalText() + ">" : ""); PsiField field = factory.createFieldFromText(modifiers + type + " " + name + "=" + initializer + ";", cls); field = (PsiField)cls.add(field); RemoveRedundantTypeArgumentsUtil.removeRedundantTypeArguments(field); CodeStyleManager.getInstance(project).reformat(manager.shortenClassReferences(field)); context.replace(field); for (ExpressionToConstantReplacementContext copy : writableCopies) { copy.replace(field); } updater.rename(field, List.copyOf(suggestions)); }); }