void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { PsiNewExpression newExpression = ObjectUtils.tryCast(element, PsiNewExpression.class); if (newExpression == null) return; IOStreamConstructorModel constructorModel = IOStreamConstructorModel.create(newExpression); if (constructorModel == null) return; PsiMethod containingMethod = PsiTreeUtil.getParentOfType(newExpression, PsiMethod.class); if (containingMethod == null) return; boolean isEffectivelyFinal = isEffectivelyFinal(newExpression, constructorModel.myArgument.getExpression()); String filesCallText = "java.nio.file." + constructorModel.myStreamType.myReplacement + "(" + constructorModel.myArgument.createReplacement() + ")"; PsiElement result = PsiReplacementUtil.replaceExpressionAndShorten(newExpression, filesCallText, new CommentTracker()); if (!isEffectivelyFinal) return; PsiMethodCallExpression filesMethodCall = ObjectUtils.tryCast(result, PsiMethodCallExpression.class); if (filesMethodCall == null) return; PsiMethodCallExpression toPathConversion = ObjectUtils.tryCast(getOnlyArgument(filesMethodCall), PsiMethodCallExpression.class); if (toPathConversion == null) return; PsiExpression[] occurrences = CodeInsightUtil.findExpressionOccurrences(containingMethod, toPathConversion); if (occurrences.length < 2) return; // maybe we can reuse already created file.toPath() / Path.of(...) variable List<PsiVariable> pathVars = Arrays.stream(occurrences).map(o -> findVariableAssignedTo(o)) .filter(var -> var != null && HighlightControlFlowUtil.isEffectivelyFinal(var, toPathConversion, null)) .toList(); if (!pathVars.isEmpty()) { PsiCodeBlock body = containingMethod.getBody(); if (body == null) return; ControlFlow flow = createControlFlow(body); if (flow == null) return; int conversionOffset = flow.getStartOffset(toPathConversion); if (conversionOffset == -1) return; Collection<PsiVariable> writtenVariables = ControlFlowUtil.getWrittenVariables(flow, 0, conversionOffset, true); PsiVariable pathVar = ContainerUtil.find(pathVars, var -> writtenVariables.contains(var)); if (pathVar != null) { String varName = pathVar.getName(); if (varName == null) return; PsiReplacementUtil.replaceExpressionAndShorten(toPathConversion, varName, new CommentTracker()); return; } } if (!myIsOnTheFly) return; Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) return; JavaRefactoringActionHandlerFactory factory = JavaRefactoringActionHandlerFactory.getInstance(); IntroduceVariableHandler handler = ObjectUtils.tryCast(factory.createIntroduceVariableHandler(), IntroduceVariableHandler.class); if (handler != null) handler.invoke(project, editor, toPathConversion); }