下面列出了怎么用org.eclipse.jdt.core.dom.Comment的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Parse the routeFile given while building the instance and fill the model.
* @param model The EIP Model to fill with parsed elements from routeFile.
* @throws InvalidArgumentException if given file is not a valid Spring integration file
*/
public void parseAndFillModel(EIPModel model) throws Exception {
// Read source content.
routeSource = parseRouteClass();
// Parse and get the compilation unit.
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(routeSource.toCharArray());
parser.setResolveBindings(false);
routeCU = (CompilationUnit) parser.createAST(null);
// Visit and build a comment map before parsing.
for (Object comment : routeCU.getCommentList()) {
((Comment) comment).accept(this);
}
// Initialize and build a route.
route = EipFactory.eINSTANCE.createRoute();
model.getOwnedRoutes().add(route);
//
routeCU.accept(this);
}
@Override
public void apply(int kind, TokenManager tokenManager, ASTNode astRoot) {
if ((kind & CodeFormatter.F_INCLUDE_COMMENTS) != 0) {
ASTVisitor visitor = new Vistor(tokenManager);
for (Comment comment : getComments(astRoot)) {
comment.accept(visitor);
}
}
}
@SuppressWarnings("unchecked")
private List<Comment> getComments(ASTNode astRoot) {
if (astRoot.getRoot() instanceof CompilationUnit) {
CompilationUnit compilationUnit = (CompilationUnit) astRoot.getRoot();
return compilationUnit.getCommentList();
}
return Collections.emptyList();
}
/**
* Adds copyright header to the compilation unit
*
* @param compilationUnit
* compilation unit affected
* @return compilation unit change
*/
public CompilationUnitChange addCopyrightsHeader(final CompilationUnit compilationUnit) {
final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
change = new CompilationUnitChange(ADD_COPYRIGHT, unit);
rewriter = ASTRewrite.create(compilationUnit.getAST());
final ListRewrite listRewrite = rewriter.getListRewrite(compilationUnit.getPackage(),
PackageDeclaration.ANNOTATIONS_PROPERTY);
final Comment placeHolder = (Comment) rewriter.createStringPlaceholder(getCopyrightText() + NEW_LINE_SEPARATOR,
ASTNode.BLOCK_COMMENT);
listRewrite.insertFirst(placeHolder, null);
rewriteCompilationUnit(unit, getNewUnitSource(unit, null));
return change;
}
/**
* Replaces copyright header to the compilation unit
*
* @param compilationUnit
* compilation unit affected
* @return compilation unit change
*/
public CompilationUnitChange replaceCopyrightsHeader(final CompilationUnit compilationUnit) {
final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
change = new CompilationUnitChange(OVERRIDE_COPYRIGHT, unit);
rewriter = ASTRewrite.create(compilationUnit.getAST());
final List<Comment> comments = getCommentList(compilationUnit);
Comment copyrightComment = null;
if (!comments.isEmpty()) {
copyrightComment = comments.get(0);
}
rewriteCompilationUnit(unit, getNewUnitSource(unit, copyrightComment));
return change;
}
/**
* Checks whether {@link CompilationUnit} has copyright header
*
* @param compilationUnit
* checked compilation unit
* @return true if {@link CompilationUnit} has copyright header
*/
public boolean hasCopyrightsComment(final CompilationUnit compilationUnit) {
final List<Comment> comments = getCommentList(compilationUnit);
boolean hasCopyrights = false;
if (!comments.isEmpty()) {
final PackageDeclaration packageNode = compilationUnit.getPackage();
final boolean commentBeforePackage = comments.get(0).getStartPosition() <= packageNode.getStartPosition();
final boolean hasJavaDoc = packageNode.getJavadoc() != null;
hasCopyrights = commentBeforePackage || hasJavaDoc;
}
return hasCopyrights;
}
/**
* Gets compilation unit's source
*
* @param unit
* affected compilation unit
* @param comment
* comment to be replaced; set null if comment is not present
* @return new compilation unit's source
*/
private String getNewUnitSource(final ICompilationUnit unit, final Comment comment) {
String source = null;
try {
source = unit.getSource();
if (comment != null) {
final int endOfComment = comment.getLength() + comment.getStartPosition();
source = source.replace(source.substring(0, endOfComment), getCopyrightText());
}
} catch (final JavaModelException e) {
ConsoleUtils.printError(e.getMessage());
}
return source;
}
/**
* Finds the comment that contains the specified position
*
* @param comments
* @param offset
* @return
*/
public ASTNode containingComment(List<Comment> comments, int offset) {
for (Comment comment : comments) {
ASTNode result = NodeFinder.perform(comment, offset, 0);
if (result != null) {
return result;
}
}
return null;
}
private String commentContent(final Comment comment) {
int _startPosition = comment.getStartPosition();
int _startPosition_1 = comment.getStartPosition();
int _length = comment.getLength();
int _plus = (_startPosition_1 + _length);
return this.javaSources.substring(_startPosition, _plus);
}
private final CommentItem extractCommentItem(final int index) {
if (commentsVisited[index]) {
return null;
} else {
final Comment comment = (Comment) comments.get(index);
if (comment.isDocComment()) {
/* Interpret DocComment as Line or Block */
comment.delete();
}
final int start = comment.getStartPosition();
final int end = start + comment.getLength();
final String value = this.src.substring(start, end);
return new CommentItem(comment, value);
}
}
private final int getNodeFirstLeadingCommentIndex(final ASTNode node) {
if (node instanceof PackageDeclaration) {
if (commentsVisited.length > 0) {
final Comment comment = (Comment) comments.get(0);
if (comment.getStartPosition() + comment.getLength() <= ((PackageDeclaration) node).getName()
.getStartPosition()) {
return 0;
}
}
return -1;
} else {
return cu.firstLeadingCommentIndex(node);
}
}
/** Extract comment content from source. */
private String getCommentContent(Comment comment) {
int start = comment.getStartPosition();
int end = start + comment.getLength();
String content = routeSource.substring(start, end);
if (content.startsWith("//")) {
content = content.substring(2).trim();
}
return content;
}
private List<CommentObject> processComments(IFile iFile, IDocument iDocument,
AbstractTypeDeclaration typeDeclaration, List<Comment> comments) {
List<CommentObject> commentList = new ArrayList<CommentObject>();
int typeDeclarationStartPosition = typeDeclaration.getStartPosition();
int typeDeclarationEndPosition = typeDeclarationStartPosition + typeDeclaration.getLength();
for(Comment comment : comments) {
int commentStartPosition = comment.getStartPosition();
int commentEndPosition = commentStartPosition + comment.getLength();
int commentStartLine = 0;
int commentEndLine = 0;
String text = null;
try {
commentStartLine = iDocument.getLineOfOffset(commentStartPosition);
commentEndLine = iDocument.getLineOfOffset(commentEndPosition);
text = iDocument.get(commentStartPosition, comment.getLength());
} catch (BadLocationException e) {
e.printStackTrace();
}
CommentType type = null;
if(comment.isLineComment()) {
type = CommentType.LINE;
}
else if(comment.isBlockComment()) {
type = CommentType.BLOCK;
}
else if(comment.isDocComment()) {
type = CommentType.JAVADOC;
}
CommentObject commentObject = new CommentObject(text, type, commentStartLine, commentEndLine);
commentObject.setComment(comment);
String fileExtension = iFile.getFileExtension() != null ? "." + iFile.getFileExtension() : "";
if(typeDeclarationStartPosition <= commentStartPosition && typeDeclarationEndPosition >= commentEndPosition) {
commentList.add(commentObject);
}
else if(iFile.getName().equals(typeDeclaration.getName().getIdentifier() + fileExtension)) {
commentList.add(commentObject);
}
}
return commentList;
}
private boolean notAssigned(final Comment comment) {
boolean _contains = this.assignedComments.contains(comment);
return (!_contains);
}
@Override
public boolean visit(final TypeDeclaration it) {
boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(it);
if (_isDummyType) {
this.visitAll(it.bodyDeclarations(), this.nl());
return false;
}
boolean _isNotSupportedInnerType = this._aSTFlattenerUtils.isNotSupportedInnerType(it);
if (_isNotSupportedInnerType) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("/* FIXME Non-static inner classes are not supported.*/");
this.appendToBuffer(_builder.toString());
this.addProblem(it, "Non-static inner classes are not supported.");
}
Javadoc _javadoc = it.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
it.getJavadoc().accept(this);
}
this.appendModifiers(it, it.modifiers());
boolean _isInterface = it.isInterface();
if (_isInterface) {
this.appendToBuffer("interface ");
} else {
boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
if (_isPackageVisibility) {
this.appendToBuffer("package ");
}
this.appendToBuffer("class ");
}
it.getName().accept(this);
boolean _isEmpty = it.typeParameters().isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
this.appendTypeParameters(it.typeParameters());
}
this.appendSpaceToBuffer();
Type _superclassType = it.getSuperclassType();
boolean _tripleNotEquals_1 = (_superclassType != null);
if (_tripleNotEquals_1) {
this.appendToBuffer("extends ");
it.getSuperclassType().accept(this);
this.appendSpaceToBuffer();
}
boolean _isEmpty_1 = it.superInterfaceTypes().isEmpty();
boolean _not_1 = (!_isEmpty_1);
if (_not_1) {
boolean _isInterface_1 = it.isInterface();
if (_isInterface_1) {
this.appendToBuffer("extends ");
} else {
this.appendToBuffer("implements ");
}
this.visitAllSeparatedByComma(it.superInterfaceTypes());
}
this.appendToBuffer("{");
this.increaseIndent();
BodyDeclaration prev = null;
List _bodyDeclarations = it.bodyDeclarations();
for (final BodyDeclaration body : ((Iterable<BodyDeclaration>) _bodyDeclarations)) {
{
if ((prev instanceof EnumConstantDeclaration)) {
if ((body instanceof EnumConstantDeclaration)) {
this.appendToBuffer(", ");
} else {
this.appendToBuffer("; ");
}
}
this.appendLineWrapToBuffer();
body.accept(this);
prev = body;
}
}
ASTNode _root = it.getRoot();
if ((_root instanceof CompilationUnit)) {
ASTNode _root_1 = it.getRoot();
final CompilationUnit cu = ((CompilationUnit) _root_1);
final Consumer<Comment> _function = (Comment it_1) -> {
it_1.accept(this);
this.assignedComments.add(it_1);
};
this.unAssignedComments(cu).forEach(_function);
}
this.decreaseIndent();
this.appendLineWrapToBuffer();
this.appendToBuffer("}");
return false;
}
private Iterable<Comment> unAssignedComments(final CompilationUnit cu) {
final Function1<Comment, Boolean> _function = (Comment c) -> {
return Boolean.valueOf(((!(c.isDocComment() && (c.getParent() != null))) && this.notAssigned(c)));
};
return IterableExtensions.<Comment>filter(cu.getCommentList(), _function);
}
protected CommentItem(final Comment node, final String value) {
this.node = node;
this.value = value;
}
@Override
public final boolean visit(final Comment node) {
return false;
}
public void setComment(Comment comment) {
this.comment = ASTInformationGenerator.generateASTInformation(comment);
}
public Comment getComment() {
return (Comment)comment.recoverASTNode();
}
private ClassObject processEnumDeclaration(IFile iFile, IDocument document, EnumDeclaration enumDeclaration, List<Comment> comments) {
final ClassObject classObject = new ClassObject();
classObject.setEnum(true);
classObject.setIFile(iFile);
classObject.addComments(processComments(iFile, document, enumDeclaration, comments));
classObject.setName(enumDeclaration.resolveBinding().getQualifiedName());
classObject.setAbstractTypeDeclaration(enumDeclaration);
int modifiers = enumDeclaration.getModifiers();
if((modifiers & Modifier.ABSTRACT) != 0)
classObject.setAbstract(true);
if((modifiers & Modifier.PUBLIC) != 0)
classObject.setAccess(Access.PUBLIC);
else if((modifiers & Modifier.PROTECTED) != 0)
classObject.setAccess(Access.PROTECTED);
else if((modifiers & Modifier.PRIVATE) != 0)
classObject.setAccess(Access.PRIVATE);
else
classObject.setAccess(Access.NONE);
if((modifiers & Modifier.STATIC) != 0)
classObject.setStatic(true);
List<Type> superInterfaceTypes = enumDeclaration.superInterfaceTypes();
for(Type interfaceType : superInterfaceTypes) {
ITypeBinding binding = interfaceType.resolveBinding();
String qualifiedName = binding.getQualifiedName();
TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName);
classObject.addInterface(typeObject);
}
List<EnumConstantDeclaration> enumConstantDeclarations = enumDeclaration.enumConstants();
for(EnumConstantDeclaration enumConstantDeclaration : enumConstantDeclarations) {
EnumConstantDeclarationObject enumConstantDeclarationObject = new EnumConstantDeclarationObject(enumConstantDeclaration.getName().getIdentifier());
enumConstantDeclarationObject.setEnumName(classObject.getName());
enumConstantDeclarationObject.setEnumConstantDeclaration(enumConstantDeclaration);
List<Expression> arguments = enumConstantDeclaration.arguments();
for(Expression argument : arguments) {
AbstractExpression abstractExpression = new AbstractExpression(argument);
enumConstantDeclarationObject.addArgument(abstractExpression);
}
classObject.addEnumConstantDeclaration(enumConstantDeclarationObject);
}
List<BodyDeclaration> bodyDeclarations = enumDeclaration.bodyDeclarations();
for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
if(bodyDeclaration instanceof MethodDeclaration) {
processMethodDeclaration(classObject, (MethodDeclaration)bodyDeclaration);
}
else if(bodyDeclaration instanceof FieldDeclaration) {
processFieldDeclaration(classObject, (FieldDeclaration)bodyDeclaration);
}
}
return classObject;
}
/**
* Returns list of {@link Comment}
*
* @param compilationUnit
* compilation unit to be analyzed
* @return lists of comments
*/
@SuppressWarnings("unchecked")
private List<Comment> getCommentList(final CompilationUnit compilationUnit) {
return compilationUnit.getCommentList();
}