use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class ExtractTempRefactoring method getEnclosingBodyNode.
private Block getEnclosingBodyNode() throws JavaModelException {
ASTNode node = getSelectedExpression().getAssociatedNode();
// expression must be in a method or initializer body
// make sure it is not in method or parameter annotation
StructuralPropertyDescriptor location = null;
while (node != null && !(node instanceof BodyDeclaration)) {
location = node.getLocationInParent();
node = node.getParent();
}
if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY) {
return (Block) node.getStructuralProperty(location);
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class ExtractConstantRefactoring method computeConstantDeclarationLocation.
private void computeConstantDeclarationLocation() throws JavaModelException {
if (isDeclarationLocationComputed())
return;
BodyDeclaration lastStaticDependency = null;
Iterator<BodyDeclaration> decls = getContainingTypeDeclarationNode().bodyDeclarations().iterator();
while (decls.hasNext()) {
BodyDeclaration decl = decls.next();
int modifiers;
if (decl instanceof FieldDeclaration)
modifiers = ((FieldDeclaration) decl).getModifiers();
else if (decl instanceof Initializer)
modifiers = ((Initializer) decl).getModifiers();
else {
continue;
/* this declaration is not a field declaration
or initializer, so the placement of the constant
declaration relative to it does not matter */
}
if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl))
lastStaticDependency = decl;
}
if (lastStaticDependency == null)
fInsertFirst = true;
else
fToInsertAfter = lastStaticDependency;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class NewAnnotationMemberProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fInvocationNode);
ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl = null;
if (typeDecl != null) {
newTypeDecl = typeDecl;
} else {
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration newAnnotationTypeDecl = (AnnotationTypeDeclaration) newTypeDecl;
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
AnnotationTypeMemberDeclaration newStub = getStub(rewrite, newAnnotationTypeDecl);
List<BodyDeclaration> members = newAnnotationTypeDecl.bodyDeclarations();
int insertIndex = members.size();
ListRewrite listRewriter = rewrite.getListRewrite(newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class NewVariableCorrectionProposal method doAddField.
private ASTRewrite doAddField(CompilationUnit astRoot) {
SimpleName node = fOriginalNode;
boolean isInDifferentCU = false;
ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding);
if (newTypeDecl == null) {
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
isInDifferentCU = true;
}
ImportRewrite imports = createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(node), imports);
if (newTypeDecl != null) {
AST ast = newTypeDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(node.getIdentifier()));
Type type = evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding);
FieldDeclaration newDecl = ast.newFieldDeclaration(fragment);
newDecl.setType(type);
newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl)));
if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) {
fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0));
}
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> decls = ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property);
int maxOffset = isInDifferentCU ? -1 : node.getStartPosition();
int insertIndex = findFieldInsertIndex(decls, newDecl, maxOffset);
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
listRewriter.insertAt(newDecl, insertIndex, null);
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, newDecl.modifiers(), fSenderBinding.isInterface());
addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
if (!isInDifferentCU) {
addLinkedPosition(rewrite.track(node), true, KEY_NAME);
}
addLinkedPosition(rewrite.track(fragment.getName()), false, KEY_NAME);
if (fragment.getInitializer() != null) {
addLinkedPosition(rewrite.track(fragment.getInitializer()), false, KEY_INITIALIZER);
}
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration 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;
}
Aggregations