import org.eclipse.jdt.core.dom.BlockComment; //導入依賴的package包/類
@Override
public void modifyCompilationUnit(CompilationUnit astRoot,
IProgressMonitor monitor) throws JavaModelException, CoreException,
BadLocationException {
final ICompilationUnit adapter = (ICompilationUnit) astRoot
.getJavaElement().getAdapter(IOpenable.class);
modifiedDocument = false;
if (adapter != null) {
document = new Document(adapter.getSource());
List commentList = astRoot.getCommentList();
offset = 0;
for (Comment comment : commentList) {
comment.accept(new ASTVisitor() {
@Override
public boolean visit(BlockComment node) {
int startPosition = node.getStartPosition();
int endPosition = startPosition + node.getLength();
try {
int lineOfOffset = document
.getLineOfOffset(startPosition - offset);
startPosition = document
.getLineOffset(lineOfOffset);
int replaceLength = endPosition - startPosition
+ LINE_DELIMITER_LENGTH - offset;
if (startPosition > -1
&& document
.get()
.substring(
startPosition,
startPosition
+ replaceLength)
.contains(NON_JAVADOC_COMMENT)) {
int replaceStart = startPosition
- LINE_DELIMITER_LENGTH;
document.replace(replaceStart, replaceLength,
EMPTY_STRING);
offset = endPosition - startPosition
+ LINE_DELIMITER_LENGTH;
modifiedDocument = true;
}
} catch (BadLocationException e) {
e.printStackTrace();
}
return super.visit(node);
}
});
}
if (modifiedDocument) {
adapter.getBuffer().setContents(document.get());
adapter.save(monitor, true);
}
}
}