@Override public void visitEmptyStatement(PsiEmptyStatement statement) { mVisitor.report("PsiEmptyStatement", statement.getText(), statement); super.visitStatement(statement); }
/** * Adds new statements before given anchor statement creating a new code block, if necessary * * @param anchor existing statement * @param newStatements the new statements which should be added before the existing one * @return last added physical statement */ public static PsiStatement addBefore(PsiStatement anchor, PsiStatement... newStatements) { if(newStatements.length == 0) { throw new IllegalArgumentException(); } PsiElement oldStatement = anchor; PsiElement parent = oldStatement.getParent(); while(parent instanceof PsiLabeledStatement) { oldStatement = parent; parent = oldStatement.getParent(); } if(newStatements.length == 1 && oldStatement instanceof PsiEmptyStatement) { return (PsiStatement) oldStatement.replace(newStatements[0]); } PsiElement result = null; if(parent instanceof PsiCodeBlock) { for(PsiStatement statement : newStatements) { result = parent.addBefore(statement, oldStatement); } } else { final PsiElementFactory factory = JavaPsiFacade.getElementFactory(anchor.getProject()); final PsiBlockStatement newBlockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", oldStatement); final PsiElement codeBlock = newBlockStatement.getCodeBlock(); for(PsiStatement newStatement : newStatements) { codeBlock.add(newStatement); } codeBlock.add(oldStatement); final PsiStatement[] statements = ((PsiBlockStatement) oldStatement.replace(newBlockStatement)).getCodeBlock().getStatements(); result = statements[statements.length - 2]; } return (PsiStatement) result; }