Search in sources :

Example 21 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration 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 22 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.

the class AssignToVariableAssistProposal method doAddField.

private ASTRewrite doAddField() {
    boolean isParamToField = fNodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;
    ASTNode newTypeDecl = ASTResolving.findParentType(fNodeToAssign);
    if (newTypeDecl == null) {
        return null;
    }
    Expression expression = isParamToField ? ((SingleVariableDeclaration) fNodeToAssign).getName() : ((ExpressionStatement) fNodeToAssign).getExpression();
    AST ast = newTypeDecl.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
    BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(fNodeToAssign);
    Block body;
    if (bodyDecl instanceof MethodDeclaration) {
        body = ((MethodDeclaration) bodyDecl).getBody();
    } else if (bodyDecl instanceof Initializer) {
        body = ((Initializer) bodyDecl).getBody();
    } else {
        return null;
    }
    IJavaProject project = getCompilationUnit().getJavaProject();
    boolean isAnonymous = newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
    boolean isStatic = Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
    boolean isConstructorParam = isParamToField && fNodeToAssign.getParent() instanceof MethodDeclaration && ((MethodDeclaration) fNodeToAssign.getParent()).isConstructor();
    int modifiers = Modifier.PRIVATE;
    if (isStatic) {
        modifiers |= Modifier.STATIC;
    } else if (isConstructorParam) {
    //			String saveActionsKey= AbstractSaveParticipantPreferenceConfiguration.EDITOR_SAVE_PARTICIPANT_PREFIX + CleanUpPostSaveListener.POSTSAVELISTENER_ID;
    //			IScopeContext[] scopes= { InstanceScope.INSTANCE, new ProjectScope(project.getProject()) };
    //			boolean safeActionsEnabled= Platform.getPreferencesService().getBoolean(JavaPlugin.getPluginId(), saveActionsKey, false, scopes);
    }
    if (/*CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(
					CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, project))
			&&*/
    CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, project)) && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, project))) {
        int constructors = 0;
        if (newTypeDecl instanceof AbstractTypeDeclaration) {
            List<BodyDeclaration> bodyDeclarations = ((AbstractTypeDeclaration) newTypeDecl).bodyDeclarations();
            for (BodyDeclaration decl : bodyDeclarations) {
                if (decl instanceof MethodDeclaration && ((MethodDeclaration) decl).isConstructor()) {
                    constructors++;
                }
            }
        }
        if (constructors == 1) {
            modifiers |= Modifier.FINAL;
        }
    }
    VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression);
    String varName = newDeclFrag.getName().getIdentifier();
    Assignment assignment = ast.newAssignment();
    assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));
    boolean needsThis = StubUtility.useThisForFieldAccess(project);
    if (isParamToField) {
        needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
    }
    SimpleName accessName = ast.newSimpleName(varName);
    if (needsThis) {
        FieldAccess fieldAccess = ast.newFieldAccess();
        fieldAccess.setName(accessName);
        if (isStatic) {
            String typeName = ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
            fieldAccess.setExpression(ast.newSimpleName(typeName));
        } else {
            fieldAccess.setExpression(ast.newThisExpression());
        }
        assignment.setLeftHandSide(fieldAccess);
    } else {
        assignment.setLeftHandSide(accessName);
    }
    ASTNode selectionNode;
    if (isParamToField) {
        // assign parameter to field
        ExpressionStatement statement = ast.newExpressionStatement(assignment);
        int insertIdx = findAssignmentInsertIndex(body.statements());
        rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
        selectionNode = statement;
    } else {
        if (needsSemicolon(expression)) {
            rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
        } else {
            rewrite.replace(expression, assignment, null);
        }
        selectionNode = fNodeToAssign;
    }
    addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME);
    if (!isParamToField) {
        FieldDeclaration fieldDeclaration = (FieldDeclaration) newDeclFrag.getParent();
        addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
    }
    addLinkedPosition(rewrite.track(accessName), true, KEY_NAME);
    IVariableBinding variableBinding = newDeclFrag.resolveBinding();
    if (variableBinding != null) {
        SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(fNodeToAssign.getRoot(), variableBinding);
        for (int i = 0; i < linkedNodes.length; i++) {
            addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME);
        }
    }
    setEndPosition(rewrite.track(selectionNode));
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Assignment(org.eclipse.jdt.core.dom.Assignment) IJavaProject(org.eclipse.jdt.core.IJavaProject) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Initializer(org.eclipse.jdt.core.dom.Initializer) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 23 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration 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)

Example 24 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.

the class ASTNodeDeleteUtil method propagateFieldDeclarationNodeDeletions.

private static void propagateFieldDeclarationNodeDeletions(final List<ASTNode> removed, final CompilationUnitRewrite rewrite, final TextEditGroup group) {
    Set<ASTNode> removedNodes = getRemovedNodes(removed, rewrite);
    for (Iterator<ASTNode> iter = removedNodes.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        if (node instanceof VariableDeclarationFragment) {
            if (node.getParent() instanceof FieldDeclaration) {
                FieldDeclaration fd = (FieldDeclaration) node.getParent();
                if (!removed.contains(fd) && removedNodes.containsAll(fd.fragments()))
                    rewrite.getASTRewrite().remove(fd, group);
                rewrite.getImportRemover().registerRemovedNode(fd);
            }
        }
    }
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 25 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration 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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)41 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)24 ASTNode (org.eclipse.jdt.core.dom.ASTNode)23 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)17 AST (org.eclipse.jdt.core.dom.AST)15 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)13 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)12 Type (org.eclipse.jdt.core.dom.Type)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)10 Expression (org.eclipse.jdt.core.dom.Expression)8 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Block (org.eclipse.jdt.core.dom.Block)7 Initializer (org.eclipse.jdt.core.dom.Initializer)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)7 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)7 ArrayList (java.util.ArrayList)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6