问题
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.
回答1:
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
- negative lookahead for
- 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
((?!\\)"([^"]|(?=\\)")*")|(/\*\*(?:(?!\*/).)*\*/)
Debuggex Demo
/\*\*(?:(?!\*/).)*\*/
Debuggex Demo
回答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();
}
回答3:
The answer of Prashant Bhate didn't work for me in eclipse ide:
/\*\*(?s:(?!\*/).)*\*/
I had to use
/\*/*(?s:(?!\*/).)*\*/
instead.
回答4:
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
来源:https://stackoverflow.com/questions/9078528/tool-to-remove-javadoc-comments