use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class TypeChangeCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
CompilationUnit newRoot = fAstRoot;
if (boundNode != null) {
// is same CU
declNode = boundNode;
} else {
newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast = declNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = createImportRewrite(newRoot);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
Type type = imports.addImport(fNewType, ast, context);
if (declNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declNode;
Type origReturnType = methodDecl.getReturnType2();
rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
// add javadoc tag
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
if (returnTag == null) {
returnTag = ast.newTagElement();
returnTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
returnTag.fragments().add(commentStart);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
}
}
} else if (declNode instanceof AnnotationTypeMemberDeclaration) {
AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
} else if (declNode instanceof VariableDeclarationFragment) {
ASTNode parent = declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
newField.setType(type);
AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
if (fieldDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newField, parent, null);
} else {
listRewrite.insertAfter(newField, parent, null);
}
} else {
rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
newStat.setType(type);
ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
if (varDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newStat, parent, null);
} else {
listRewrite.insertAfter(newStat, parent, null);
}
} else {
rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (declNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
// set up linked mode
//$NON-NLS-1$
final String KEY_TYPE = "type";
addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
if (fTypeProposals != null) {
for (int i = 0; i < fTypeProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, fTypeProposals[i]);
}
}
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class NewVariableCorrectionProposal method doAddParam.
private ASTRewrite doAddParam(CompilationUnit cu) {
AST ast = cu.getAST();
SimpleName node = fOriginalNode;
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(node);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = createImportRewrite((CompilationUnit) decl.getRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
SingleVariableDeclaration newDecl = ast.newSingleVariableDeclaration();
newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding()));
newDecl.setName(ast.newSimpleName(node.getIdentifier()));
ListRewrite listRewriter = rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY);
listRewriter.insertLast(newDecl, null);
addLinkedPosition(rewrite.track(node), true, KEY_NAME);
// add javadoc tag
Javadoc javadoc = methodDeclaration.getJavadoc();
if (javadoc != null) {
HashSet<String> leadingNames = new HashSet<String>();
for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
SingleVariableDeclaration curr = iter.next();
leadingNames.add(curr.getName().getIdentifier());
}
SimpleName newTagRef = ast.newSimpleName(node.getIdentifier());
TagElement newTagElement = ast.newTagElement();
newTagElement.setTagName(TagElement.TAG_PARAM);
newTagElement.fragments().add(newTagRef);
TextElement commentStart = ast.newTextElement();
newTagElement.fragments().add(commentStart);
addLinkedPosition(rewrite.track(newTagRef), false, KEY_NAME);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames);
}
addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
addLinkedPosition(rewrite.track(newDecl.getName()), false, KEY_NAME);
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class AbstractSerialVersionOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
final ASTRewrite rewrite = cuRewrite.getASTRewrite();
VariableDeclarationFragment fragment = null;
for (int i = 0; i < fNodes.length; i++) {
final ASTNode node = fNodes[i];
final AST ast = node.getAST();
fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(NAME_FIELD));
final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
if (!addInitializer(fragment, node))
continue;
if (fragment.getInitializer() != null) {
final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
if (node instanceof AbstractTypeDeclaration)
rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
else if (node instanceof AnonymousClassDeclaration)
rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
else if (node instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) node;
final ASTNode parent = type.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
if (anonymous != null)
rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
}
} else
Assert.isTrue(false);
addLinkedPositions(rewrite, fragment, positionGroups);
}
final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
if (comment != null && comment.length() > 0) {
final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
declaration.setJavadoc(doc);
}
}
if (fragment == null)
return;
positionGroups.setEndPosition(rewrite.track(fragment));
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocTagProposals.
public static void getMissingJavadocTagProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
node = ASTNodes.getNormalizedNode(node);
BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(node);
if (bodyDeclaration == null) {
return;
}
Javadoc javadoc = bodyDeclaration.getJavadoc();
if (javadoc == null) {
return;
}
String label;
StructuralPropertyDescriptor location = node.getLocationInParent();
if (location == SingleVariableDeclaration.NAME_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
if (node.getParent().getLocationInParent() != MethodDeclaration.PARAMETERS_PROPERTY) {
// paranoia checks
return;
}
} else if (location == TypeParameter.NAME_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
StructuralPropertyDescriptor parentLocation = node.getParent().getLocationInParent();
if (parentLocation != MethodDeclaration.TYPE_PARAMETERS_PROPERTY && parentLocation != TypeDeclaration.TYPE_PARAMETERS_PROPERTY) {
// paranoia checks
return;
}
} else if (location == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_returntag_description;
} else if (location == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_throwstag_description;
} else {
return;
}
ASTRewriteCorrectionProposal proposal = new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), bodyDeclaration, node, IProposalRelevance.ADD_MISSING_TAG);
proposals.add(proposal);
String label2 = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_allmissing_description;
ASTRewriteCorrectionProposal addAllMissing = new AddAllMissingJavadocTagsProposal(label2, context.getCompilationUnit(), bodyDeclaration, IProposalRelevance.ADD_ALL_MISSING_TAGS);
proposals.add(addAllMissing);
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ExtractMethodRefactoring method createNewMethod.
private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException {
MethodDeclaration result = createNewMethodDeclaration();
result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers()));
if (fGenerateJavadoc) {
AbstractTypeDeclaration enclosingType = (AbstractTypeDeclaration) ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class);
String string = CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
result.setJavadoc(javadoc);
}
}
return result;
}
Aggregations