Need help on PsiDocComment

Any idea as to how to create a new PsiDocComment object and replace the exsiting PsiDocComment object with the new one.

Any help would be appreciated
Ruchir Awadhawal

Comment actions Permalink

Ruchir Awadhawal wrote:

Any idea as to how to create a new PsiDocComment object and replace the
exsiting PsiDocComment object with the new one.

Well, I am afraid there is no direct way at the moment, however the
following should work:

1. Create bogus method with your doc comment using

final PsiMethod bogusMethod = 
   factory.createMethodFromText("/**\n * comment text here*/\nvoid a();",

2. Use bogusMethod.getDocComment() to obtain a PsiDocComment
3. Add it/replace with it wherever you want

Hope this helps,
Dmitry Lomov
Software Developer
JetBrains Inc.
"Develop with pleasure!"

Comment actions Permalink

I am the writer of the to-string plugin and it has a feature to add/replace javadoc on the toString method.

Here is some code from my plugin. You can download the source from


<![CDATA[    /**
     * Add or replaces the javadoc comment to the given method.
     * @param factory element factory.
     * @param method  the method the javadoc should be added/set to.
     * @param javadoc the javadoc comment.
     * @param replace true if any existing javadoc should be replaced. false will not replace any existing javadoc and thus leave the javadoc untouched.
     * @return  the added/replace javadoc comment, null if the was an existing javadoc and it should not be replaced.
     * @throws IncorrectOperationException is thrown if error adding/replacing the javadoc comment.
     * @since 2.14
    public PsiComment addOrReplaceJavadoc(PsiElementFactory factory, PsiMethod method, String javadoc, boolean replace) throws IncorrectOperationException {
        PsiComment comment = factory.createCommentFromText(javadoc, null);

        // does a method already exists?
        if (method.getDocComment() != null) {
            if (replace) {
                // javadoc already exists, so replace
                return comment;
            } else {
                // do not relace existing javadoc
                return null;
        } else {
            // add new javadoc
            method.addBefore(comment, method.getFirstChild());
            return comment;

<![CDATA[        // add javadoc (either existing javadoc or from velocity template if specified in top using /* insert my javadoc here **/
        String javaDoc = config.getMethodBodyJavaDocOnly();
        if (params.get("existingJavaDoc") != null) {
            PsiDocComment existingJavaDoc = (PsiDocComment) params.get("existingJavaDoc");
            javaDoc = existingJavaDoc.getText();
        // add javadoc if needed
        if (javaDoc != null) {
            if (log.isDebugEnabled()) log.debug("Adding javadoc: " + javaDoc);
            PsiMethod toStringMethod = psi.findMethodByName(clazz, "toString"); // must find again to be able to add javadoc (IDEA does not add if using method parameter)
            if (psi.addOrReplaceJavadoc(elementFactory, toStringMethod, javaDoc, false) != null);
                codeStyleManager.reformat(toStringMethod); // reformat to also format javadoc comments


Please sign in to leave a comment.