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.
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.
Try this regex to search replace either in eclipse / sed / your favorite editor that has regex support.
/\*\*(?s:(?!\*/).)*\*/
?s
treat input as single line\**
*/
*/
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
((?!\\)"([^"]|(?=\\)")*")|(/\*\*(?:(?!\*/).)*\*/)
/\*\*(?:(?!\*/).)*\*/
String s = "/**asd*/";
. It's a string, it's not part of a javadoc. However it got replaced.
((?<!\\)"([^"]|(?<=\\)")*")|(/\*\*(?s:(?!\*/).)*\*/)
matches ordinary strings! It killed all my strings.
^[^"]*/\*\*
to match the starts of javadoc comments without matching anything in a string.
Commented
Feb 2, 2012 at 22:50
\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!
The answer of Prashant Bhate didn't work for me in eclipse ide:
/\*\*(?s:(?!\*/).)*\*/
I had to use
/\*/*(?s:(?!\*/).)*\*/
instead.
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();
}
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
/\/\*\*\_.\{-}\*\/
only does the search. In command mode write :%s/\s\+\/\*\*\_.\{-}\*\///g
to replace all Javadocs, including leading whitespaces.