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.

Pacerier
  • 86,231
  • 106
  • 366
  • 634

4 Answers4

36

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

Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
  • 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 Jan 31 '12 at 12:48
  • 1
    `((?<!\\)"([^"]|(?<=\\)")*")|(/\*\*(?s:(?!\*/).)*\*/)` matches ordinary strings! It killed all my strings. – Pacerier Feb 02 '12 at 21:44
  • You should be able to use something like `^[^"]*/\*\*` to match the starts of javadoc comments without matching anything in a string. – Tom Anderson Feb 02 '12 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 ! – Prashant Bhate Feb 03 '12 at 02: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 Feb 03 '12 at 04:15
  • @PrashantBhate sed doesnt support lookahead nor non-capturing groups so that regex wont compile. – Alan Berezin Apr 28 '16 at 04:52
3

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

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

I had to use

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

instead.

isumi
  • 69
  • 3
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();        
  }
Ertuğrul Çetin
  • 5,131
  • 5
  • 37
  • 76
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
jchilders
  • 351
  • 1
  • 5
  • 10
  • String `/\/\*\*\_.\{-}\*\/`only does the search. In command mode write `:%s/\s\+\/\*\*\_.\{-}\*\///g` to replace all Javadocs, including leading whitespaces. – Mauricio Jul 11 '21 at 05:41