下面列出了怎么用org.eclipse.jdt.core.dom.TagElement的API类实例代码及写法,或者点击链接到github查看源代码。
private void insertAllMissingTypeTags(ASTRewrite rewriter, TypeDeclaration typeDecl) {
AST ast= typeDecl.getAST();
Javadoc javadoc= typeDecl.getJavadoc();
ListRewrite tagsRewriter= rewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
List<TypeParameter> typeParams= typeDecl.typeParameters();
for (int i= typeParams.size() - 1; i >= 0; i--) {
TypeParameter decl= typeParams.get(i);
String name= '<' + decl.getName().getIdentifier() + '>';
if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) {
TagElement newTag= ast.newTagElement();
newTag.setTagName(TagElement.TAG_PARAM);
TextElement text= ast.newTextElement();
text.setText(name);
newTag.fragments().add(text);
insertTabStop(rewriter, newTag.fragments(), "typeParam" + i); //$NON-NLS-1$
insertTag(tagsRewriter, newTag, getPreviousTypeParamNames(typeParams, decl));
}
}
}
/**
* {@inheritDoc}
*/
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo,
final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
public boolean visit(MethodDeclaration node) {
Javadoc doc = node.getJavadoc();
if (doc == null) {
doc = node.getAST().newJavadoc();
node.setJavadoc(doc);
}
TagElement newTag = node.getAST().newTagElement();
newTag.setTagName("TODO Added by MethodLimit Sample quickfix");
doc.tags().add(0, newTag);
return true;
}
};
}
protected boolean handleDocRoot(TagElement node) {
if (!TagElement.TAG_DOCROOT.equals(node.getTagName()))
return false;
URI uri = EcoreUtil.getURI(context);
if (uri.isPlatformResource()) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
IPath fullPath = file.getFullPath();
IProject project = file.getProject();
if (project.exists() && project.isOpen()) {
for (IContainer f : sourceFolderProvider.getSourceFolders(project)) {
if (f.getFullPath().isPrefixOf(fullPath)) {
IPath location = f.getLocation();
if (location != null) {
buffer.append(location.toFile().toURI().toASCIIString());
return true;
}
}
}
}
}
return true;
}
protected void handleBlockTags(String title, List<TagElement> tags) {
if (tags.isEmpty())
return;
handleBlockTagTitle(title);
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
buffer.append(BlOCK_TAG_ENTRY_START);
if (TagElement.TAG_SEE.equals(tag.getTagName())) {
handleSeeTag(tag);
} else {
@SuppressWarnings("unchecked")
List<ASTNode> fragments = tag.fragments();
handleContentElements(fragments);
}
buffer.append(BlOCK_TAG_ENTRY_END);
}
}
protected void handleExceptionTags(List<TagElement> tags, Map<String, URI> exceptionNamesToURI) {
if (tags.size() == 0 && containsOnlyNull(exceptionNamesToURI.values()))
return;
handleBlockTagTitle("Throws:");
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
buffer.append(BlOCK_TAG_ENTRY_START);
handleThrowsTag(tag);
buffer.append(BlOCK_TAG_ENTRY_END);
}
for (int i = 0; i < exceptionNamesToURI.size(); i++) {
String name = Lists.newArrayList(exceptionNamesToURI.keySet()).get(i);
if (name != null && exceptionNamesToURI.get(name) != null) {
buffer.append(BlOCK_TAG_ENTRY_START);
buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, exceptionNamesToURI.get(name),
name));
buffer.append(BlOCK_TAG_ENTRY_END);
}
}
}
protected void handleParameters(EObject object, List<TagElement> parameters, List<String> parameterNames) {
if (parameters.size() == 0 && containsOnlyNull(parameterNames))
return;
handleBlockTagTitle("Parameters:");
for (Iterator<TagElement> iter = parameters.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
buffer.append(BlOCK_TAG_ENTRY_START);
handleParamTag(tag);
buffer.append(BlOCK_TAG_ENTRY_END);
}
for (int i = 0; i < parameterNames.size(); i++) {
String name = parameterNames.get(i);
if (name != null) {
buffer.append(BlOCK_TAG_ENTRY_START);
buffer.append(PARAM_NAME_START);
buffer.append(name);
buffer.append(PARAM_NAME_END);
buffer.append(BlOCK_TAG_ENTRY_END);
}
}
}
private TagElement getDelegateJavadocTag(BodyDeclaration declaration) throws JavaModelException {
Assert.isNotNull(declaration);
String msg= RefactoringCoreMessages.DelegateCreator_use_member_instead;
int firstParam= msg.indexOf("{0}"); //$NON-NLS-1$
Assert.isTrue(firstParam != -1);
List<ASTNode> fragments= new ArrayList<>();
TextElement text= getAst().newTextElement();
text.setText(msg.substring(0, firstParam).trim());
fragments.add(text);
fragments.add(createJavadocMemberReferenceTag(declaration, getAst()));
text= getAst().newTextElement();
text.setText(msg.substring(firstParam + 3).trim());
fragments.add(text);
final TagElement tag= getAst().newTagElement();
tag.setTagName(TagElement.TAG_DEPRECATED);
tag.fragments().addAll(fragments);
return tag;
}
private void handleTag(String tag, String tagContent) {
tagContent= tagContent.trim();
if (TagElement.TAG_PARAM.equals(tag)) {
fParameters.add(tagContent);
} else if (TagElement.TAG_RETURN.equals(tag)) {
fReturn= tagContent;
} else if (TagElement.TAG_EXCEPTION.equals(tag)) {
fExceptions.add(tagContent);
} else if (TagElement.TAG_THROWS.equals(tag)) {
fExceptions.add(tagContent);
} else if (TagElement.TAG_AUTHOR.equals(tag)) {
fAuthors.add(substituteQualification(tagContent));
} else if (TagElement.TAG_SEE.equals(tag)) {
fSees.add(substituteQualification(tagContent));
} else if (TagElement.TAG_SINCE.equals(tag)) {
fSince.add(substituteQualification(tagContent));
} else if (tagContent != null) {
fRest.add(new Pair(tag, tagContent));
}
}
CharSequence getMainDescription() {
if (fMainDescription == null) {
fMainDescription = new StringBuffer();
fBuf = fMainDescription;
fLiteralContent = 0;
List<TagElement> tags = fJavadoc.tags();
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
String tagName = tag.getTagName();
if (tagName == null) {
handleContentElements(tag.fragments());
break;
}
}
fBuf = null;
}
return fMainDescription.length() > 0 ? fMainDescription : null;
}
CharSequence getReturnDescription() {
if (fReturnDescription == null) {
fReturnDescription = new StringBuffer();
fBuf = fReturnDescription;
fLiteralContent = 0;
List<TagElement> tags = fJavadoc.tags();
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
String tagName = tag.getTagName();
if (TagElement.TAG_RETURN.equals(tagName)) {
handleContentElements(tag.fragments());
break;
}
}
fBuf = null;
}
return fReturnDescription.length() > 0 ? fReturnDescription : null;
}
private void handleBlockTags(String title, List<TagElement> tags) {
if (tags.isEmpty()) {
return;
}
handleBlockTagTitle(title);
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
fBuf.append(BLOCK_TAG_START);
fBuf.append(BlOCK_TAG_ENTRY_START);
if (TagElement.TAG_SEE.equals(tag.getTagName())) {
handleSeeTag(tag);
} else {
handleContentElements(tag.fragments());
}
fBuf.append(BlOCK_TAG_ENTRY_END);
fBuf.append(BLOCK_TAG_END);
}
fBuf.append(BlOCK_TAG_ENTRY_END);
}
private void handleReturnTag(TagElement tag, CharSequence returnDescription) {
if (tag == null && returnDescription == null) {
return;
}
handleBlockTagTitle(JavaDoc2HTMLTextReader_returns_section);
fBuf.append(BLOCK_TAG_START);
fBuf.append(BlOCK_TAG_ENTRY_START);
if (tag != null) {
handleContentElements(tag.fragments());
} else {
fBuf.append(returnDescription);
}
fBuf.append(BlOCK_TAG_ENTRY_END);
fBuf.append(BLOCK_TAG_END);
fBuf.append(BlOCK_TAG_ENTRY_END);
}
public static TagElement findTag(Javadoc javadoc, String name, String arg) {
List<TagElement> tags= javadoc.tags();
int nTags= tags.size();
for (int i= 0; i < nTags; i++) {
TagElement curr= tags.get(i);
if (name.equals(curr.getTagName())) {
if (arg != null) {
String argument= getArgument(curr);
if (arg.equals(argument)) {
return curr;
}
} else {
return curr;
}
}
}
return null;
}
private static String getArgument(TagElement curr) {
List<? extends ASTNode> fragments= curr.fragments();
if (!fragments.isEmpty()) {
Object first= fragments.get(0);
if (first instanceof Name) {
return ASTNodes.getSimpleNameIdentifier((Name) first);
} else if (first instanceof TextElement && TagElement.TAG_PARAM.equals(curr.getTagName())) {
String text= ((TextElement) first).getText();
if ("<".equals(text) && fragments.size() >= 3) { //$NON-NLS-1$
Object second= fragments.get(1);
Object third= fragments.get(2);
if (second instanceof Name && third instanceof TextElement && ">".equals(((TextElement) third).getText())) { //$NON-NLS-1$
return '<' + ASTNodes.getSimpleNameIdentifier((Name) second) + '>';
}
} else if (text.startsWith(String.valueOf('<')) && text.endsWith(String.valueOf('>')) && text.length() > 2) {
return text.substring(1, text.length() - 1);
}
}
}
return null;
}
public static void getRemoveJavadocTagProposals(IInvocationContext context, IProblemLocationCore problem,
Collection<ChangeCorrectionProposal> proposals) {
ASTNode node= problem.getCoveringNode(context.getASTRoot());
while (node != null && !(node instanceof TagElement)) {
node= node.getParent();
}
if (node == null) {
return;
}
ASTRewrite rewrite= ASTRewrite.create(node.getAST());
rewrite.remove(node, null);
String label= CorrectionMessages.JavadocTagsSubProcessor_removetag_description;
proposals.add(new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite,
IProposalRelevance.REMOVE_TAG));
}
private UMLJavadoc generateJavadoc(BodyDeclaration bodyDeclaration) {
UMLJavadoc doc = null;
Javadoc javaDoc = bodyDeclaration.getJavadoc();
if(javaDoc != null) {
doc = new UMLJavadoc();
List<TagElement> tags = javaDoc.tags();
for(TagElement tag : tags) {
UMLTagElement tagElement = new UMLTagElement(tag.getTagName());
List fragments = tag.fragments();
for(Object docElement : fragments) {
tagElement.addFragment(docElement.toString());
}
doc.addTag(tagElement);
}
}
return doc;
}
private TagElement getDelegateJavadocTag(BodyDeclaration declaration) throws JavaModelException {
Assert.isNotNull(declaration);
String msg= RefactoringCoreMessages.DelegateCreator_use_member_instead;
int firstParam= msg.indexOf("{0}"); //$NON-NLS-1$
Assert.isTrue(firstParam != -1);
List<ASTNode> fragments= new ArrayList<ASTNode>();
TextElement text= getAst().newTextElement();
text.setText(msg.substring(0, firstParam).trim());
fragments.add(text);
fragments.add(createJavadocMemberReferenceTag(declaration, getAst()));
text= getAst().newTextElement();
text.setText(msg.substring(firstParam + 3).trim());
fragments.add(text);
final TagElement tag= getAst().newTagElement();
tag.setTagName(TagElement.TAG_DEPRECATED);
tag.fragments().addAll(fragments);
return tag;
}
@Override
public boolean visit(TagElement node) {
String tagName= node.getTagName();
List<? extends ASTNode> list= node.fragments();
int idx= 0;
if (tagName != null && !list.isEmpty()) {
Object first= list.get(0);
if (first instanceof Name) {
if ("@throws".equals(tagName) || "@exception".equals(tagName)) { //$NON-NLS-1$//$NON-NLS-2$
typeRefFound((Name) first);
} else if ("@see".equals(tagName) || "@link".equals(tagName) || "@linkplain".equals(tagName)) { //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Name name= (Name) first;
possibleTypeRefFound(name);
}
idx++;
}
}
for (int i= idx; i < list.size(); i++) {
doVisitNode(list.get(i));
}
return false;
}
private void handleExceptionTags(List<TagElement> tags, List<String> exceptionNames, CharSequence[] exceptionDescriptions) {
if (tags.size() == 0 && containsOnlyNull(exceptionNames))
return;
handleBlockTagTitle(JavaDocMessages.JavaDoc2HTMLTextReader_throws_section);
for (Iterator<TagElement> iter= tags.iterator(); iter.hasNext(); ) {
TagElement tag= iter.next();
fBuf.append(BlOCK_TAG_ENTRY_START);
handleThrowsTag(tag);
fBuf.append(BlOCK_TAG_ENTRY_END);
}
for (int i= 0; i < exceptionDescriptions.length; i++) {
CharSequence description= exceptionDescriptions[i];
String name= exceptionNames.get(i);
if (name != null) {
fBuf.append(BlOCK_TAG_ENTRY_START);
handleLink(Collections.singletonList(fJavadoc.getAST().newSimpleName(name)));
if (description != null) {
fBuf.append(JavaElementLabels.CONCAT_STRING);
fBuf.append(description);
}
fBuf.append(BlOCK_TAG_ENTRY_END);
}
}
}
private void handleTag(String tag, String tagContent) {
tagContent= tagContent.trim();
if (TagElement.TAG_PARAM.equals(tag))
fParameters.add(tagContent);
else if (TagElement.TAG_RETURN.equals(tag))
fReturn= tagContent;
else if (TagElement.TAG_EXCEPTION.equals(tag))
fExceptions.add(tagContent);
else if (TagElement.TAG_THROWS.equals(tag))
fExceptions.add(tagContent);
else if (TagElement.TAG_AUTHOR.equals(tag))
fAuthors.add(substituteQualification(tagContent));
else if (TagElement.TAG_SEE.equals(tag))
fSees.add(substituteQualification(tagContent));
else if (TagElement.TAG_SINCE.equals(tag))
fSince.add(substituteQualification(tagContent));
else if (tagContent != null)
fRest.add(new Pair(tag, tagContent));
}
CharSequence getMainDescription() {
if (fMainDescription == null) {
fMainDescription= new StringBuffer();
fBuf= fMainDescription;
fLiteralContent= 0;
List<TagElement> tags= fJavadoc.tags();
for (Iterator<TagElement> iter= tags.iterator(); iter.hasNext(); ) {
TagElement tag= iter.next();
String tagName= tag.getTagName();
if (tagName == null) {
handleContentElements(tag.fragments());
break;
}
}
fBuf= null;
}
return fMainDescription.length() > 0 ? fMainDescription : null;
}
CharSequence getReturnDescription() {
if (fReturnDescription == null) {
fReturnDescription= new StringBuffer();
fBuf= fReturnDescription;
fLiteralContent= 0;
List<TagElement> tags= fJavadoc.tags();
for (Iterator<TagElement> iter= tags.iterator(); iter.hasNext(); ) {
TagElement tag= iter.next();
String tagName= tag.getTagName();
if (TagElement.TAG_RETURN.equals(tagName)) {
handleContentElements(tag.fragments());
break;
}
}
fBuf= null;
}
return fReturnDescription.length() > 0 ? fReturnDescription : null;
}
private void handleBlockTags(String title, List<TagElement> tags) {
if (tags.isEmpty())
return;
handleBlockTagTitle(title);
for (Iterator<TagElement> iter= tags.iterator(); iter.hasNext(); ) {
TagElement tag= iter.next();
fBuf.append(BlOCK_TAG_ENTRY_START);
if (TagElement.TAG_SEE.equals(tag.getTagName())) {
handleSeeTag(tag);
} else {
handleContentElements(tag.fragments());
}
fBuf.append(BlOCK_TAG_ENTRY_END);
}
}
@Override
public boolean visit(TagElement node) {
if (isSquashRequired(node, this.declaration)) {
int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false);
Token token = this.commentTokenManager.get(startIndex);
token.clearLineBreaksBefore();
token.putLineBreaksBefore(
this.declaration instanceof TypeDeclaration && this.firstTagElement && this.hasText ? 2 : 1);
this.firstTagElement = false;
}
return true;
}
private String extractFullText(TagElement tagElement) {
StringBuilder builder = new StringBuilder();
for (Object fragment : tagElement.fragments()) {
if (fragment instanceof TextElement) {
TextElement textElement = (TextElement) fragment;
builder.append(textElement.getText() + " ");
}
}
return builder.toString().trim();
}
protected boolean handleInheritDoc(TagElement node) {
if (!TagElement.TAG_INHERITDOC.equals(node.getTagName()))
return false;
// Not handled explicit for Xbase for now
handleText(node.toString());
return true;
}
protected void handleBlockTags(List<TagElement> tags) {
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext();) {
TagElement tag = iter.next();
handleBlockTagTitle(tag.getTagName());
buffer.append(BlOCK_TAG_ENTRY_START);
@SuppressWarnings("unchecked")
List<ASTNode> fragments = tag.fragments();
handleContentElements(fragments);
buffer.append(BlOCK_TAG_ENTRY_END);
}
}
protected void handleThrowsTag(TagElement tag) {
@SuppressWarnings("unchecked")
List<? extends ASTNode> fragments = tag.fragments();
int size = fragments.size();
if (size > 0) {
handleLink(fragments.subList(0, 1));
if (size > 1) {
buffer.append(JavaElementLabels.CONCAT_STRING);
handleContentElements(fragments.subList(1, size));
}
}
}
public static TagElement findThrowsTag(Javadoc javadoc, String arg) {
List<TagElement> tags= javadoc.tags();
int nTags= tags.size();
for (int i= 0; i < nTags; i++) {
TagElement curr= tags.get(i);
String currName= curr.getTagName();
if (TagElement.TAG_THROWS.equals(currName) || TagElement.TAG_EXCEPTION.equals(currName)) {
String argument= getArgument(curr);
if (arg.equals(argument)) {
return curr;
}
}
}
return null;
}
public static void getRemoveJavadocTagProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode node= problem.getCoveringNode(context.getASTRoot());
while (node != null && !(node instanceof TagElement)) {
node= node.getParent();
}
if (node == null) {
return;
}
ASTRewrite rewrite= ASTRewrite.create(node.getAST());
rewrite.remove(node, null);
String label= CorrectionMessages.JavadocTagsSubProcessor_removetag_description;
Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_TAG, image));
}