use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class UnresolvedElementsSubProcessor method addMissingCastParentsProposal.
private static void addMissingCastParentsProposal(ICompilationUnit cu, MethodInvocation invocationNode, Collection<ICommandAccess> proposals) {
Expression sender = invocationNode.getExpression();
if (sender instanceof ThisExpression) {
return;
}
ITypeBinding senderBinding = sender.resolveTypeBinding();
if (senderBinding == null || Modifier.isFinal(senderBinding.getModifiers())) {
return;
}
if (sender instanceof Name && ((Name) sender).resolveBinding() instanceof ITypeBinding) {
// static access
return;
}
ASTNode parent = invocationNode.getParent();
while (parent instanceof Expression && parent.getNodeType() != ASTNode.CAST_EXPRESSION) {
parent = parent.getParent();
}
boolean hasCastProposal = false;
if (parent instanceof CastExpression) {
// (TestCase) x.getName() -> ((TestCase) x).getName
hasCastProposal = useExistingParentCastProposal(cu, (CastExpression) parent, sender, invocationNode.getName(), getArgumentTypes(invocationNode.arguments()), proposals);
}
if (!hasCastProposal) {
// x.getName() -> ((TestCase) x).getName
Expression target = sender;
while (target instanceof ParenthesizedExpression) {
target = ((ParenthesizedExpression) target).getExpression();
}
String label;
if (target.getNodeType() != ASTNode.CAST_EXPRESSION) {
String targetName = null;
if (target.getLength() <= 18) {
targetName = ASTNodes.asString(target);
}
if (targetName == null) {
label = CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast_description;
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
}
} else {
String targetName = null;
if (target.getLength() <= 18) {
targetName = ASTNodes.asString(((CastExpression) target).getExpression());
}
if (targetName == null) {
label = CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast_description;
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
}
}
proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class VarargsWarningsSubProcessor method addRemoveSafeVarargsProposals.
public static void addRemoveSafeVarargsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
if (!(coveringNode instanceof MethodDeclaration))
return;
MethodDeclaration methodDeclaration = (MethodDeclaration) coveringNode;
MarkerAnnotation annotation = null;
List<? extends ASTNode> modifiers = methodDeclaration.modifiers();
for (Iterator<? extends ASTNode> iterator = modifiers.iterator(); iterator.hasNext(); ) {
ASTNode node = iterator.next();
if (node instanceof MarkerAnnotation) {
annotation = (MarkerAnnotation) node;
if ("SafeVarargs".equals(annotation.resolveAnnotationBinding().getName())) {
//$NON-NLS-1$
break;
}
}
}
if (annotation == null)
return;
ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
rewrite.remove(annotation, null);
String label = CorrectionMessages.VarargsWarningsSubProcessor_remove_safevarargs_label;
//JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_SAFEVARARGS, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class AddTypeParameterProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
if (boundNode != null) {
// is same CU
declNode = boundNode;
createImportRewrite(fAstRoot);
} else {
CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
createImportRewrite(newRoot);
}
AST ast = declNode.getAST();
TypeParameter newTypeParam = ast.newTypeParameter();
newTypeParam.setName(ast.newSimpleName(fTypeParamName));
if (fBounds != null && fBounds.length > 0) {
List<Type> typeBounds = newTypeParam.typeBounds();
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
for (int i = 0; i < fBounds.length; i++) {
Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext);
typeBounds.add(newBound);
}
}
ASTRewrite rewrite = ASTRewrite.create(ast);
ListRewrite listRewrite;
Javadoc javadoc;
List<TypeParameter> otherTypeParams;
if (declNode instanceof TypeDeclaration) {
TypeDeclaration declaration = (TypeDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
} else {
MethodDeclaration declaration = (MethodDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
}
listRewrite.insertLast(newTypeParam, null);
if (javadoc != null && otherTypeParams != null) {
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
String name = '<' + fTypeParamName + '>';
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_PARAM);
TextElement text = ast.newTextElement();
text.setText(name);
newTag.fragments().add(text);
JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
}
return rewrite;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class JavadocContentAccess2 method handleInheritDoc.
/**
* Handle {@inheritDoc}.
*
* @param node
* the node
* @return <code>true</code> iff the node was an {@inheritDoc} node and has been handled
*/
private boolean handleInheritDoc(TagElement node) {
if (!TagElement.TAG_INHERITDOC.equals(node.getTagName()))
return false;
try {
if (fMethod == null)
return false;
TagElement blockTag = (TagElement) node.getParent();
String blockTagName = blockTag.getTagName();
if (blockTagName == null) {
CharSequence inherited = fJavadocLookup.getInheritedMainDescription(fMethod);
return handleInherited(inherited);
} else if (TagElement.TAG_PARAM.equals(blockTagName)) {
List<? extends ASTNode> fragments = blockTag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof SimpleName) {
String name = ((SimpleName) first).getIdentifier();
String[] parameterNames = fMethod.getParameterNames();
for (int i = 0; i < parameterNames.length; i++) {
if (name.equals(parameterNames[i])) {
CharSequence inherited = fJavadocLookup.getInheritedParamDescription(fMethod, i);
return handleInherited(inherited);
}
}
}
}
} else if (TagElement.TAG_RETURN.equals(blockTagName)) {
CharSequence inherited = fJavadocLookup.getInheritedReturnDescription(fMethod);
return handleInherited(inherited);
} else if (TagElement.TAG_THROWS.equals(blockTagName) || TagElement.TAG_EXCEPTION.equals(blockTagName)) {
List<? extends ASTNode> fragments = blockTag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof Name) {
String name = ASTNodes.getSimpleNameIdentifier((Name) first);
CharSequence inherited = fJavadocLookup.getInheritedExceptionDescription(fMethod, name);
return handleInherited(inherited);
}
}
}
} catch (JavaModelException e) {
LOG.error(e.getMessage(), e);
}
return false;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class JavadocContentAccess2 method toHTML.
private String toHTML() {
fBuf = new StringBuffer();
fLiteralContent = 0;
// After first loop, non-null entries in the following two lists are missing and need to be inherited:
List<String> parameterNames = initParameterNames();
List<String> exceptionNames = initExceptionNames();
TagElement deprecatedTag = null;
TagElement start = null;
List<TagElement> parameters = new ArrayList<TagElement>();
TagElement returnTag = null;
List<TagElement> exceptions = new ArrayList<TagElement>();
List<TagElement> versions = new ArrayList<TagElement>();
List<TagElement> authors = new ArrayList<TagElement>();
List<TagElement> sees = new ArrayList<TagElement>();
List<TagElement> since = new ArrayList<TagElement>();
List<TagElement> rest = new ArrayList<TagElement>();
List<TagElement> tags = fJavadoc.tags();
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
TagElement tag = iter.next();
String tagName = tag.getTagName();
if (tagName == null) {
start = tag;
} else if (TagElement.TAG_PARAM.equals(tagName)) {
parameters.add(tag);
List<? extends ASTNode> fragments = tag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof SimpleName) {
String name = ((SimpleName) first).getIdentifier();
int paramIndex = parameterNames.indexOf(name);
if (paramIndex != -1) {
parameterNames.set(paramIndex, null);
}
}
}
} else if (TagElement.TAG_RETURN.equals(tagName)) {
if (returnTag == null)
// the Javadoc tool only shows the first return tag
returnTag = tag;
} else if (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName)) {
exceptions.add(tag);
List<? extends ASTNode> fragments = tag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof Name) {
String name = ASTNodes.getSimpleNameIdentifier((Name) first);
int exceptionIndex = exceptionNames.indexOf(name);
if (exceptionIndex != -1) {
exceptionNames.set(exceptionIndex, null);
}
}
}
} else if (TagElement.TAG_SINCE.equals(tagName)) {
since.add(tag);
} else if (TagElement.TAG_VERSION.equals(tagName)) {
versions.add(tag);
} else if (TagElement.TAG_AUTHOR.equals(tagName)) {
authors.add(tag);
} else if (TagElement.TAG_SEE.equals(tagName)) {
sees.add(tag);
} else if (TagElement.TAG_DEPRECATED.equals(tagName)) {
if (deprecatedTag == null)
// the Javadoc tool only shows the first deprecated tag
deprecatedTag = tag;
} else {
rest.add(tag);
}
}
//TODO: @Documented annotations before header
if (deprecatedTag != null)
handleDeprecatedTag(deprecatedTag);
if (start != null)
handleContentElements(start.fragments());
else if (fMethod != null) {
CharSequence inherited = fJavadocLookup.getInheritedMainDescription(fMethod);
// The Javadoc tool adds "Description copied from class: ..." (only for the main description).
// We don't bother doing that.
handleInherited(inherited);
}
CharSequence[] parameterDescriptions = new CharSequence[parameterNames.size()];
boolean hasInheritedParameters = inheritParameterDescriptions(parameterNames, parameterDescriptions);
boolean hasParameters = parameters.size() > 0 || hasInheritedParameters;
CharSequence returnDescription = null;
if (returnTag == null && needsReturnTag())
returnDescription = fJavadocLookup.getInheritedReturnDescription(fMethod);
boolean hasReturnTag = returnTag != null || returnDescription != null;
CharSequence[] exceptionDescriptions = new CharSequence[exceptionNames.size()];
boolean hasInheritedExceptions = inheritExceptionDescriptions(exceptionNames, exceptionDescriptions);
boolean hasExceptions = exceptions.size() > 0 || hasInheritedExceptions;
if (hasParameters || hasReturnTag || hasExceptions || versions.size() > 0 || authors.size() > 0 || since.size() > 0 || sees.size() > 0 || rest.size() > 0 || (fBuf.length() > 0 && (parameterDescriptions.length > 0 || exceptionDescriptions.length > 0))) {
handleSuperMethodReferences();
fBuf.append(BLOCK_TAG_START);
handleParameterTags(parameters, parameterNames, parameterDescriptions);
handleReturnTag(returnTag, returnDescription);
handleExceptionTags(exceptions, exceptionNames, exceptionDescriptions);
handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_since_section, since);
handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_version_section, versions);
handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_author_section, authors);
handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_see_section, sees);
handleBlockTags(rest);
fBuf.append(BLOCK_TAG_END);
} else if (fBuf.length() > 0) {
handleSuperMethodReferences();
}
String result = fBuf.toString();
fBuf = null;
return result;
}
Aggregations