12

Is there any tool / Eclipse plugin that can remove all JavaDoc comments in a file?

The normal (non-JavaDoc) comments should be intact after running the tool.

2
  • 2
    are you talking about javadocs? Commented Jan 31, 2012 at 11:14
  • @DenisTulskiy yes javadocs.. my bad
    – Pacerier
    Commented Jan 31, 2012 at 12:45

4 Answers 4

38

Try this regex to search replace either in eclipse / sed / your favorite editor that has regex support.

/\*\*(?s:(?!\*/).)*\*/
  • ?s treat input as single line
  • a starting string \**
  • zero or more
    • negative lookahead for */
    • space or non space char
  • a trailing string */

Edit

To tackle cases where strings containing javadoc, use this regex

((?<!\\)"([^"]|(?<=\\)")*")|(/\*\*(?s:(?!\*/).)*\*/)

and replace it with first capture group

/1

Then if you say this shouldn't match

///** */

or more complex cases

I suggest, using a parser tool like antlr to parse using java grammar for tokens like comments and strings and remove elements that you don't want


Sometime I find my own regex answers cryptic !!

So here is some visual feedback

((?!\\)"([^"]|(?=\\)")*")|(/\*\*(?:(?!\*/).)*\*/)

Regular expression visualization

Debuggex Demo


/\*\*(?:(?!\*/).)*\*/

Regular expression visualization

Debuggex Demo

6
  • 1
    This doesn't work. It erroneously replaces sequences in strings. For example, I have a String s = "/**asd*/";. It's a string, it's not part of a javadoc. However it got replaced.
    – Pacerier
    Commented Jan 31, 2012 at 12:48
  • 1
    ((?<!\\)"([^"]|(?<=\\)")*")|(/\*\*(?s:(?!\*/).)*\*/) matches ordinary strings! It killed all my strings.
    – Pacerier
    Commented Feb 2, 2012 at 21:44
  • You should be able to use something like ^[^"]*/\*\* to match the starts of javadoc comments without matching anything in a string. Commented Feb 2, 2012 at 22:50
  • @Pacerier yes, it does match strings but as you are replace it with capture group 1 , it(string) will get replaced with itself ! Commented Feb 3, 2012 at 2:51
  • 1
    @PrashantBhate Oh for the eclipse editor it should have been \1 instead of /1. I'll take this as answer until someone else has a solution which doesn't fall in cases like these ///**test*/, thanks!
    – Pacerier
    Commented Feb 3, 2012 at 4:15
3

The answer of Prashant Bhate didn't work for me in eclipse ide:

/\*\*(?s:(?!\*/).)*\*/

I had to use

/\*/*(?s:(?!\*/).)*\*/

instead.

1
  • This matches normal comments
    – caduceus
    Commented Nov 16, 2022 at 8:41
2

I made a open source library for this purpose , its called CommentRemover you can remove single line and multiple line Java Comments.

It supports remove or NOT remove TODO's.
Also it supports JavaScript , HTML , CSS , Properties , JSP and XML Comments too.

Little code snippet how to use it(There is 2 type usage):

First way InternalPath

 public static void main(String[] args) throws CommentRemoverException {

 // root dir is: /Users/user/Projects/MyProject
 // example for startInternalPath

 CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder()
        .removeJava(true) // Remove Java file Comments....
        .removeJavaScript(true) // Remove JavaScript file Comments....
        .removeJSP(true) // etc.. goes like that
        .removeTodos(false) //  Do Not Touch Todos (leave them alone)
        .removeSingleLines(true) // Remove single line type comments
        .removeMultiLines(true) // Remove multiple type comments
        .startInternalPath("src.main.app") // Starts from {rootDir}/src/main/app , leave it empty string when you want to start from root dir
        .setExcludePackages(new String[]{"src.main.java.app.pattern"}) // Refers to {rootDir}/src/main/java/app/pattern and skips this directory
        .build();

 CommentProcessor commentProcessor = new CommentProcessor(commentRemover);
                  commentProcessor.start();        
  }

Second way ExternalPath

 public static void main(String[] args) throws CommentRemoverException {

 // example for externalInternalPath

 CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder()
        .removeJava(true) // Remove Java file Comments....
        .removeJavaScript(true) // Remove JavaScript file Comments....
        .removeJSP(true) // etc..
        .removeTodos(true) // Remove todos
        .removeSingleLines(false) // Do not remove single line type comments
        .removeMultiLines(true) // Remove multiple type comments
        .startExternalPath("/Users/user/Projects/MyOtherProject")// Give it full path for external directories
        .setExcludePackages(new String[]{"src.main.java.model"}) // Refers to /Users/user/Projects/MyOtherProject/src/main/java/model and skips this directory.
        .build();

 CommentProcessor commentProcessor = new CommentProcessor(commentRemover);
                  commentProcessor.start();        
  }
1
  • Thanks so much, great tool! Commented May 5, 2017 at 14:38
0

The following works for me in vim:

/\/\*\*\_.\{-}\*\/

The \_.\{-} pattern matches any character (.), including the newline (\_), as few as possible (\{-}).

This matches against multi-line Javadoc comments such as:

/**
 * My Javadoc comment here
 */

But will not match against comments such as:

// My non-Javadoc comment
1
  • String /\/\*\*\_.\{-}\*\/only does the search. In command mode write :%s/\s\+\/\*\*\_.\{-}\*\///g to replace all Javadocs, including leading whitespaces.
    – Mauricio
    Commented Jul 11, 2021 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.