void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element.getParent().getParent(); final PsiStatement statement = PsiTreeUtil.getParentOfType(methodCallExpression, PsiStatement.class); if (statement == null) { return; } final List<PsiStatement> logStatements = new ArrayList<>(); logStatements.add(statement); final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final String referenceName = methodExpression.getReferenceName(); if (referenceName == null) { return; } PsiStatement previousStatement = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class); while (isSameLogMethodCall(previousStatement, referenceName)) { logStatements.add(0, previousStatement); previousStatement = PsiTreeUtil.getPrevSiblingOfType(previousStatement, PsiStatement.class); } PsiStatement nextStatement = PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class); while (isSameLogMethodCall(nextStatement, referenceName)) { logStatements.add(nextStatement); nextStatement = PsiTreeUtil.getNextSiblingOfType(nextStatement, PsiStatement.class); } final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null) { return; } final int index = logMethodNameList.indexOf(referenceName); final String conditionMethodText = logConditionMethodNameList.get(index); @NonNls final String ifStatementText = "if (" + qualifier.getText() + '.' + conditionMethodText + ") {}"; final PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(ifStatementText, statement); final PsiBlockStatement blockStatement = (PsiBlockStatement)ifStatement.getThenBranch(); if (blockStatement == null) { return; } final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); for (PsiStatement logStatement : logStatements) { codeBlock.add(logStatement); } final PsiStatement firstStatement = logStatements.get(0); final PsiElement parent = firstStatement.getParent(); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); if (parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() != null) { final PsiBlockStatement newBlockStatement = (PsiBlockStatement)factory.createStatementFromText("{}", statement); newBlockStatement.getCodeBlock().add(ifStatement); final PsiElement result = firstStatement.replace(newBlockStatement); codeStyleManager.shortenClassReferences(result); return; } final PsiElement result = parent.addBefore(ifStatement, firstStatement); codeStyleManager.shortenClassReferences(result); for (PsiStatement logStatement : logStatements) { logStatement.delete(); } }