Search in sources :

Example 16 with ChildListPropertyDescriptor

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;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 17 with ChildListPropertyDescriptor

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);
                }
            }
        }
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Iterator(java.util.Iterator) List(java.util.List) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Example 18 with ChildListPropertyDescriptor

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);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Example 19 with ChildListPropertyDescriptor

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Example 20 with ChildListPropertyDescriptor

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;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Aggregations

ChildListPropertyDescriptor (org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)26 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)17 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)14 AST (org.eclipse.jdt.core.dom.AST)10 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 Expression (org.eclipse.jdt.core.dom.Expression)7 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)6 Statement (org.eclipse.jdt.core.dom.Statement)6 Type (org.eclipse.jdt.core.dom.Type)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 Block (org.eclipse.jdt.core.dom.Block)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)5