Search in sources :

Example 11 with VariableDeclarationStatement

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

the class CallInliner method computeRealArguments.

private void computeRealArguments() {
    List<Expression> arguments = Invocations.getArguments(fInvocation);
    Set<Expression> canNotInline = crossCheckArguments(arguments);
    boolean needsVarargBoxing = needsVarargBoxing(arguments);
    int varargIndex = fSourceProvider.getVarargIndex();
    AST ast = fInvocation.getAST();
    Expression[] realArguments = new Expression[needsVarargBoxing ? varargIndex + 1 : arguments.size()];
    for (int i = 0; i < (needsVarargBoxing ? varargIndex : arguments.size()); i++) {
        Expression expression = arguments.get(i);
        ParameterData parameter = fSourceProvider.getParameterData(i);
        if (canInline(expression, parameter) && !canNotInline.contains(expression)) {
            realArguments[i] = expression;
        } else {
            String name = fInvocationScope.createName(parameter.getName(), true);
            realArguments[i] = ast.newSimpleName(name);
            VariableDeclarationStatement local = createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
            if (parameter.isFinal()) {
                local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
            }
            fLocals.add(local);
        }
    }
    if (needsVarargBoxing) {
        ParameterData parameter = fSourceProvider.getParameterData(varargIndex);
        String name = fInvocationScope.createName(parameter.getName(), true);
        realArguments[varargIndex] = ast.newSimpleName(name);
        Type type = fImportRewrite.addImport(parameter.getTypeBinding(), ast);
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        fragment.setName(ast.newSimpleName(name));
        ArrayInitializer initializer = ast.newArrayInitializer();
        for (int i = varargIndex; i < arguments.size(); i++) {
            initializer.expressions().add(fRewrite.createCopyTarget(arguments.get(i)));
        }
        fragment.setInitializer(initializer);
        VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
        decl.setType(type);
        fLocals.add(decl);
    }
    fContext.compilationUnit = fCUnit;
    fContext.arguments = realArguments;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 12 with VariableDeclarationStatement

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

the class CallInliner method createLocalDeclaration.

private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
    String typeName = fImportRewrite.addImport(type, context);
    VariableDeclarationStatement decl = (VariableDeclarationStatement) ASTNodeFactory.newStatement(fInvocation.getAST(), //$NON-NLS-1$ //$NON-NLS-2$
    typeName + " " + name + ";");
    ((VariableDeclarationFragment) decl.fragments().get(0)).setInitializer(initializer);
    return decl;
}
Also used : ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 13 with VariableDeclarationStatement

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

the class RemoveDeclarationCorrectionProposal method removeVariableReferences.

/**
	 * Remove the field or variable declaration including the initializer.
	 * @param rewrite the ast rewrite
	 * @param reference the reference
	 */
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
    ASTNode parent = reference.getParent();
    while (parent instanceof QualifiedName) {
        parent = parent.getParent();
    }
    if (parent instanceof FieldAccess) {
        parent = parent.getParent();
    }
    int nameParentType = parent.getNodeType();
    if (nameParentType == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) parent;
        Expression rightHand = assignment.getRightHandSide();
        ASTNode assignParent = assignment.getParent();
        if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
            removeVariableWithInitializer(rewrite, rightHand, assignParent);
        } else {
            rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
        }
    } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        rewrite.remove(parent, null);
    } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
        ASTNode varDecl = frag.getParent();
        List<VariableDeclarationFragment> fragments;
        if (varDecl instanceof VariableDeclarationExpression) {
            fragments = ((VariableDeclarationExpression) varDecl).fragments();
        } else if (varDecl instanceof FieldDeclaration) {
            fragments = ((FieldDeclaration) varDecl).fragments();
        } else {
            fragments = ((VariableDeclarationStatement) varDecl).fragments();
        }
        if (fragments.size() == 1) {
            rewrite.remove(varDecl, null);
        } else {
            // don't try to preserve
            rewrite.remove(frag, null);
        }
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 14 with VariableDeclarationStatement

use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project flux by eclipse.

the class VariableDeclarationRewrite method rewriteModifiers.

public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();
    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();
    ListRewrite blockRewrite = null;
    ASTNode parentStatement = declarationNode.getParent();
    if (parentStatement instanceof SwitchStatement) {
        blockRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
    } else if (parentStatement instanceof Block) {
        blockRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
    } else {
        // should not happen. VariableDeclaration's can not be in a control statement body
        Assert.isTrue(false);
    }
    VariableDeclarationFragment lastFragment = iter.next();
    ASTNode lastStatement = declarationNode;
    boolean modifiersModified = false;
    if (fragmentsToChange.contains(lastFragment)) {
        ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
        modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
        modifiersModified = true;
    }
    ListRewrite fragmentsRewrite = null;
    while (iter.hasNext()) {
        VariableDeclarationFragment currentFragment = iter.next();
        if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) {
            VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement((VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment));
            newStatement.setType((Type) rewrite.createCopyTarget(declarationNode.getType()));
            ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
            if (fragmentsToChange.contains(currentFragment)) {
                modifierRewrite.copyAllAnnotations(declarationNode, group);
                int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
                modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
            } else {
                modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified);
            }
            blockRewrite.insertAfter(newStatement, lastStatement, group);
            fragmentsRewrite = rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
            lastStatement = newStatement;
        } else if (fragmentsRewrite != null) {
            ASTNode fragment0 = rewrite.createMoveTarget(currentFragment);
            fragmentsRewrite.insertLast(fragment0, group);
        }
        lastFragment = currentFragment;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 15 with VariableDeclarationStatement

use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project flux by eclipse.

the class QuickAssistProcessor method getConvertToStringBufferProposal.

private static LinkedCorrectionProposal getConvertToStringBufferProposal(IInvocationContext context, AST ast, InfixExpression oldInfixExpression) {
    String bufferOrBuilderName;
    ICompilationUnit cu = context.getCompilationUnit();
    if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
        //$NON-NLS-1$
        bufferOrBuilderName = "StringBuilder";
    } else {
        //$NON-NLS-1$
        bufferOrBuilderName = "StringBuffer";
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    SimpleName existingBuffer = getEnclosingAppendBuffer(oldInfixExpression);
    String mechanismName = BasicElementLabels.getJavaElementName(existingBuffer == null ? bufferOrBuilderName : existingBuffer.getIdentifier());
    String label = Messages.format(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, mechanismName);
    //Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CONVERT_TO_STRING_BUFFER);
    proposal.setCommandId(CONVERT_TO_STRING_BUFFER_ID);
    Statement insertAfter;
    String bufferName;
    //$NON-NLS-1$
    String groupID = "nameId";
    ListRewrite listRewrite;
    Statement enclosingStatement = ASTResolving.findParentStatement(oldInfixExpression);
    if (existingBuffer != null) {
        if (ASTNodes.isControlStatementBody(enclosingStatement.getLocationInParent())) {
            Block newBlock = ast.newBlock();
            listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
            insertAfter = null;
            rewrite.replace(enclosingStatement, newBlock, null);
        } else {
            listRewrite = rewrite.getListRewrite(enclosingStatement.getParent(), (ChildListPropertyDescriptor) enclosingStatement.getLocationInParent());
            insertAfter = enclosingStatement;
        }
        bufferName = existingBuffer.getIdentifier();
    } else {
        // create buffer
        VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
        // check if name is already in use and provide alternative
        List<String> fExcludedVariableNames = Arrays.asList(ASTResolving.getUsedVariableNames(oldInfixExpression));
        SimpleType bufferType = ast.newSimpleType(ast.newName(bufferOrBuilderName));
        ClassInstanceCreation newBufferExpression = ast.newClassInstanceCreation();
        //StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), bufferOrBuilderName, 0, fExcludedVariableNames, true);
        String[] newBufferNames = new String[] {};
        bufferName = newBufferNames[0];
        SimpleName bufferNameDeclaration = ast.newSimpleName(bufferName);
        frag.setName(bufferNameDeclaration);
        proposal.addLinkedPosition(rewrite.track(bufferNameDeclaration), true, groupID);
        for (int i = 0; i < newBufferNames.length; i++) {
            proposal.addLinkedPositionProposal(groupID, newBufferNames[i], null);
        }
        newBufferExpression.setType(bufferType);
        frag.setInitializer(newBufferExpression);
        VariableDeclarationStatement bufferDeclaration = ast.newVariableDeclarationStatement(frag);
        bufferDeclaration.setType(ast.newSimpleType(ast.newName(bufferOrBuilderName)));
        insertAfter = bufferDeclaration;
        Statement statement = ASTResolving.findParentStatement(oldInfixExpression);
        if (ASTNodes.isControlStatementBody(statement.getLocationInParent())) {
            Block newBlock = ast.newBlock();
            listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
            listRewrite.insertFirst(bufferDeclaration, null);
            listRewrite.insertLast(rewrite.createMoveTarget(statement), null);
            rewrite.replace(statement, newBlock, null);
        } else {
            listRewrite = rewrite.getListRewrite(statement.getParent(), (ChildListPropertyDescriptor) statement.getLocationInParent());
            listRewrite.insertBefore(bufferDeclaration, statement, null);
        }
    }
    List<Expression> operands = new ArrayList<Expression>();
    collectInfixPlusOperands(oldInfixExpression, operands);
    Statement lastAppend = insertAfter;
    for (Iterator<Expression> iter = operands.iterator(); iter.hasNext(); ) {
        Expression operand = iter.next();
        MethodInvocation appendIncovationExpression = ast.newMethodInvocation();
        //$NON-NLS-1$
        appendIncovationExpression.setName(ast.newSimpleName("append"));
        SimpleName bufferNameReference = ast.newSimpleName(bufferName);
        // If there was an existing name, don't offer to rename it
        if (existingBuffer == null) {
            proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
        }
        appendIncovationExpression.setExpression(bufferNameReference);
        appendIncovationExpression.arguments().add(rewrite.createCopyTarget(operand));
        ExpressionStatement appendExpressionStatement = ast.newExpressionStatement(appendIncovationExpression);
        if (lastAppend == null) {
            listRewrite.insertFirst(appendExpressionStatement, null);
        } else {
            listRewrite.insertAfter(appendExpressionStatement, lastAppend, null);
        }
        lastAppend = appendExpressionStatement;
    }
    if (existingBuffer != null) {
        proposal.setEndPosition(rewrite.track(lastAppend));
        if (insertAfter != null) {
            rewrite.remove(enclosingStatement, null);
        }
    } else {
        // replace old expression with toString
        MethodInvocation bufferToString = ast.newMethodInvocation();
        //$NON-NLS-1$
        bufferToString.setName(ast.newSimpleName("toString"));
        SimpleName bufferNameReference = ast.newSimpleName(bufferName);
        bufferToString.setExpression(bufferNameReference);
        proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
        rewrite.replace(oldInfixExpression, bufferToString, null);
        proposal.setEndPosition(rewrite.track(bufferToString));
    }
    return proposal;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Aggregations

VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)38 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)26 ASTNode (org.eclipse.jdt.core.dom.ASTNode)24 AST (org.eclipse.jdt.core.dom.AST)18 Expression (org.eclipse.jdt.core.dom.Expression)17 Type (org.eclipse.jdt.core.dom.Type)14 Block (org.eclipse.jdt.core.dom.Block)13 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)10 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)10 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)9 Statement (org.eclipse.jdt.core.dom.Statement)9 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)9 ArrayList (java.util.ArrayList)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)8 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)8 Assignment (org.eclipse.jdt.core.dom.Assignment)7 CastExpression (org.eclipse.jdt.core.dom.CastExpression)7