``$categoryDoc`` is already an (Java) object of type Document so I don't see why you need to call ``$xwiki.getDocument()``. Any particular reason?