Search in sources :

Example 36 with FieldDeclaration

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

the class PromoteTempToFieldRefactoring method createNewFieldDeclaration.

private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) {
    AST ast = getAST();
    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    SimpleName variableName = ast.newSimpleName(fFieldName);
    fragment.setName(variableName);
    addLinkedName(rewrite, variableName, false);
    List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite);
    fragment.extraDimensions().addAll(extraDimensions);
    if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()) {
        Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer());
        fragment.setInitializer(initializer);
    }
    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment);
    VariableDeclarationStatement vds = getTempDeclarationStatement();
    Type type = (Type) rewrite.createCopyTarget(vds.getType());
    fieldDeclaration.setType(type);
    fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
    return fieldDeclaration;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Dimension(org.eclipse.jdt.core.dom.Dimension) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 37 with FieldDeclaration

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

the class PromoteTempToFieldRefactoring method checkClashesWithExistingFields.

private RefactoringStatus checkClashesWithExistingFields() {
    FieldDeclaration[] existingFields = getFieldDeclarations();
    for (int i = 0; i < existingFields.length; i++) {
        FieldDeclaration declaration = existingFields[i];
        VariableDeclarationFragment[] fragments = (VariableDeclarationFragment[]) declaration.fragments().toArray(new VariableDeclarationFragment[declaration.fragments().size()]);
        for (int j = 0; j < fragments.length; j++) {
            VariableDeclarationFragment fragment = fragments[j];
            if (fFieldName.equals(fragment.getName().getIdentifier())) {
                //cannot conflict with more than 1 name
                RefactoringStatusContext context = JavaStatusContext.create(fCu, fragment);
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict_with_field, context);
            }
        }
    }
    return null;
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 38 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 39 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 40 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)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)48 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)28 ASTNode (org.eclipse.jdt.core.dom.ASTNode)26 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)18 AST (org.eclipse.jdt.core.dom.AST)15 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)15 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)13 Type (org.eclipse.jdt.core.dom.Type)13 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)9 Expression (org.eclipse.jdt.core.dom.Expression)8 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)8 Block (org.eclipse.jdt.core.dom.Block)7 Initializer (org.eclipse.jdt.core.dom.Initializer)7 Javadoc (org.eclipse.jdt.core.dom.Javadoc)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)7 ArrayList (java.util.ArrayList)6