use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor 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.ChildListPropertyDescriptor in project che by eclipse.
the class TightSourceRangeComputer method addTightSourceNode.
/**
* Add the given node to the set of "tight" nodes.
*
* @param reference a node
* @since 3.2
*/
public void addTightSourceNode(ASTNode reference) {
fTightSourceRangeNodes.add(reference);
List<StructuralPropertyDescriptor> properties = reference.structuralPropertiesForType();
for (Iterator<StructuralPropertyDescriptor> iterator = properties.iterator(); iterator.hasNext(); ) {
StructuralPropertyDescriptor descriptor = iterator.next();
if (descriptor.isChildProperty()) {
ASTNode child = (ASTNode) reference.getStructuralProperty(descriptor);
if (child != null && isExtending(child, reference)) {
addTightSourceNode(child);
}
} else if (descriptor.isChildListProperty()) {
List<? extends ASTNode> children = ASTNodes.getChildListProperty(reference, (ChildListPropertyDescriptor) descriptor);
for (Iterator<? extends ASTNode> iterator2 = children.iterator(); iterator2.hasNext(); ) {
ASTNode child = iterator2.next();
if (isExtending(child, reference)) {
addTightSourceNode(child);
}
}
}
}
}
use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project che by eclipse.
the class PromoteTempToFieldRefactoring method addFieldDeclaration.
private void addFieldDeclaration(ASTRewrite rewrite) {
FieldDeclaration[] fields = getFieldDeclarations();
ASTNode parent = getMethodDeclaration().getParent();
ChildListPropertyDescriptor descriptor = ASTNodes.getBodyDeclarationsProperty(parent);
int insertIndex;
if (fields.length == 0)
insertIndex = 0;
else
insertIndex = ASTNodes.getBodyDeclarations(parent).indexOf(fields[fields.length - 1]) + 1;
final FieldDeclaration declaration = createNewFieldDeclaration(rewrite);
rewrite.getListRewrite(parent, descriptor).insertAt(declaration, insertIndex, null);
}
use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project che by eclipse.
the class AbstractMethodCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl = null;
boolean isInDifferentCU;
if (typeDecl != null) {
isInDifferentCU = false;
newTypeDecl = typeDecl;
} else {
isInDifferentCU = true;
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl != null) {
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
MethodDeclaration newStub = getStub(rewrite, newTypeDecl);
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(newTypeDecl);
int insertIndex;
if (isConstructor()) {
insertIndex = findConstructorInsertIndex(members);
} else if (!isInDifferentCU) {
insertIndex = findMethodInsertIndex(members, fNode.getStartPosition());
} else {
insertIndex = members.size();
}
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.ChildListPropertyDescriptor in project che by eclipse.
the class AssignToVariableAssistProposal method addFieldDeclaration.
private VariableDeclarationFragment addFieldDeclaration(ASTRewrite rewrite, ASTNode newTypeDecl, int modifiers, Expression expression) {
if (fExistingFragment != null) {
return fExistingFragment;
}
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> decls = ASTNodes.getBodyDeclarations(newTypeDecl);
AST ast = newTypeDecl.getAST();
String[] varNames = suggestFieldNames(fTypeBinding, expression, modifiers);
for (int i = 0; i < varNames.length; i++) {
addLinkedPositionProposal(KEY_NAME, varNames[i], null);
}
String varName = varNames[0];
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
newDeclFrag.setName(ast.newSimpleName(varName));
FieldDeclaration newDecl = ast.newFieldDeclaration(newDeclFrag);
Type type = evaluateType(ast);
newDecl.setType(type);
newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers));
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, newDecl.modifiers(), false);
int insertIndex = findFieldInsertIndex(decls, fNodeToAssign.getStartPosition());
rewrite.getListRewrite(newTypeDecl, property).insertAt(newDecl, insertIndex, null);
return newDeclFrag;
}
Aggregations