PsiElement (@NotNull PsiJavaModule module, @NotNull PsiStatement moduleStatement) { PsiElement anchor = SyntaxTraverser.psiTraverser().children(module).filter(moduleStatement.getClass()).last(); if (anchor == null) { anchor = SyntaxTraverser.psiTraverser().children(module).filter(e -> isJavaToken(e, JavaTokenType.LBRACE)).first(); } if (anchor == null) { throw new IllegalStateException("No anchor in " + Arrays.toString(module.getChildren())); } return module.addAfter(moduleStatement, anchor); }