Search in sources :

Example 11 with ParenthesizedExpression

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

the class SourceProvider method createParenthesizedExpression.

private Expression createParenthesizedExpression(Expression newExpression, AST ast) {
    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
    parenthesized.setExpression(newExpression);
    return parenthesized;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression)

Example 12 with ParenthesizedExpression

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

the class AssociativeInfixExpressionFragment method replace.

public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) {
    ASTNode groupNode = getGroupRoot();
    List<Expression> allOperands = findGroupMembersInOrderFor(getGroupRoot());
    if (allOperands.size() == fOperands.size()) {
        if (replacement instanceof Name && groupNode.getParent() instanceof ParenthesizedExpression) {
            // replace including the parenthesized expression around it
            rewrite.replace(groupNode.getParent(), replacement, textEditGroup);
        } else {
            rewrite.replace(groupNode, replacement, textEditGroup);
        }
        return;
    }
    rewrite.replace(fOperands.get(0), replacement, textEditGroup);
    int first = allOperands.indexOf(fOperands.get(0));
    int after = first + fOperands.size();
    for (int i = first + 1; i < after; i++) {
        rewrite.remove(allOperands.get(i), textEditGroup);
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Name(org.eclipse.jdt.core.dom.Name)

Example 13 with ParenthesizedExpression

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

the class UnusedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean removeUnusedPrivateMethods, boolean removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields, boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables, boolean removeUnusedImports, boolean removeUnusedCast) {
    List<CompilationUnitRewriteOperation> result = new ArrayList<CompilationUnitRewriteOperation>();
    Hashtable<ASTNode, List<SimpleName>> variableDeclarations = new Hashtable<ASTNode, List<SimpleName>>();
    LinkedHashSet<CastExpression> unnecessaryCasts = new LinkedHashSet<CastExpression>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        int id = problem.getProblemId();
        if (removeUnusedImports && (id == IProblem.UnusedImport || id == IProblem.DuplicateImport || id == IProblem.ConflictingImport || id == IProblem.CannotImportPackage || id == IProblem.ImportNotFound)) {
            ImportDeclaration node = UnusedCodeFix.getImportDeclaration(problem, compilationUnit);
            if (node != null) {
                result.add(new RemoveImportOperation(node));
            }
        }
        if ((removeUnusedPrivateMethods && id == IProblem.UnusedPrivateMethod) || (removeUnusedPrivateConstructors && id == IProblem.UnusedPrivateConstructor) || (removeUnusedPrivateTypes && id == IProblem.UnusedPrivateType)) {
            SimpleName name = getUnusedName(compilationUnit, problem);
            if (name != null) {
                IBinding binding = name.resolveBinding();
                if (binding != null) {
                    result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
                }
            }
        }
        if ((removeUnusedLocalVariables && id == IProblem.LocalVariableIsNeverUsed) || (removeUnusedPrivateFields && id == IProblem.UnusedPrivateField)) {
            SimpleName name = getUnusedName(compilationUnit, problem);
            if (name != null) {
                IBinding binding = name.resolveBinding();
                if (binding instanceof IVariableBinding && !isFormalParameterInEnhancedForStatement(name) && (!((IVariableBinding) binding).isField() || isSideEffectFree(name, compilationUnit))) {
                    VariableDeclarationFragment parent = (VariableDeclarationFragment) ASTNodes.getParent(name, VariableDeclarationFragment.class);
                    if (parent != null) {
                        ASTNode varDecl = parent.getParent();
                        if (!variableDeclarations.containsKey(varDecl)) {
                            variableDeclarations.put(varDecl, new ArrayList<SimpleName>());
                        }
                        variableDeclarations.get(varDecl).add(name);
                    } else {
                        result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
                    }
                }
            }
        }
        if (removeUnusedCast && id == IProblem.UnnecessaryCast) {
            ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
            ASTNode curr = selectedNode;
            while (curr instanceof ParenthesizedExpression) {
                curr = ((ParenthesizedExpression) curr).getExpression();
            }
            if (curr instanceof CastExpression) {
                unnecessaryCasts.add((CastExpression) curr);
            }
        }
    }
    for (Iterator<ASTNode> iter = variableDeclarations.keySet().iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        List<SimpleName> names = variableDeclarations.get(node);
        result.add(new RemoveUnusedMemberOperation(names.toArray(new SimpleName[names.size()]), false));
    }
    if (unnecessaryCasts.size() > 0)
        result.add(new RemoveAllCastOperation(unnecessaryCasts));
    if (result.size() == 0)
        return null;
    return new UnusedCodeFix(FixMessages.UnusedCodeFix_change_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()]));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Hashtable(java.util.Hashtable) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 14 with ParenthesizedExpression

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

the class UnusedCodeFix method createRemoveUnusedCastFix.

public static UnusedCodeFix createRemoveUnusedCastFix(CompilationUnit compilationUnit, IProblemLocation problem) {
    if (problem.getProblemId() != IProblem.UnnecessaryCast)
        return null;
    ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
    ASTNode curr = selectedNode;
    while (curr instanceof ParenthesizedExpression) {
        curr = ((ParenthesizedExpression) curr).getExpression();
    }
    if (!(curr instanceof CastExpression))
        return null;
    return new UnusedCodeFix(FixMessages.UnusedCodeFix_RemoveCast_description, compilationUnit, new CompilationUnitRewriteOperation[] { new RemoveCastOperation((CastExpression) curr) });
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 15 with ParenthesizedExpression

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

the class CallInliner method replaceCall.

private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGroup textEditGroup) {
    // Inline empty body
    if (blocks.length == 0 && fTargetNode != null) {
        if (fNeedsStatement) {
            fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
        } else {
            fRewrite.remove(fTargetNode, textEditGroup);
        }
    } else {
        ASTNode node = null;
        for (int i = 0; i < blocks.length - 1; i++) {
            node = fRewrite.createStringPlaceholder(blocks[i], ASTNode.RETURN_STATEMENT);
            fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
        }
        String block = blocks[blocks.length - 1];
        // returned expression must be evaluated.
        if (fContext.callMode == ASTNode.EXPRESSION_STATEMENT && fSourceProvider.hasReturnValue()) {
            if (fSourceProvider.mustEvaluateReturnedExpression()) {
                if (fSourceProvider.returnValueNeedsLocalVariable()) {
                    IMethodBinding invocation = Invocations.resolveBinding(fInvocation);
                    node = createLocalDeclaration(invocation.getReturnType(), fInvocationScope.createName(fSourceProvider.getMethodName(), true), (Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                } else {
                    node = fRewrite.getAST().newExpressionStatement((Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                }
            } else {
                node = null;
            }
        } else if (fTargetNode instanceof Expression) {
            node = fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION);
            // fixes bug #24941
            if (needsExplicitCast(status)) {
                AST ast = node.getAST();
                CastExpression castExpression = ast.newCastExpression();
                Type returnType = fImportRewrite.addImport(fSourceProvider.getReturnType(), ast);
                castExpression.setType(returnType);
                if (NecessaryParenthesesChecker.needsParentheses(fSourceProvider.getReturnExpressions().get(0), castExpression, CastExpression.EXPRESSION_PROPERTY)) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
                castExpression.setExpression((Expression) node);
                node = castExpression;
                if (NecessaryParenthesesChecker.needsParentheses(castExpression, fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
            } else if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                ParenthesizedExpression pExp = fTargetNode.getAST().newParenthesizedExpression();
                pExp.setExpression((Expression) node);
                node = pExp;
            }
        } else {
            node = fRewrite.createStringPlaceholder(block, ASTNode.RETURN_STATEMENT);
        }
        // Now replace the target node with the source node
        if (node != null) {
            if (fTargetNode == null) {
                fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
            } else {
                fRewrite.replace(fTargetNode, node, textEditGroup);
            }
        } else {
            if (fTargetNode != null) {
                fRewrite.remove(fTargetNode, textEditGroup);
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Aggregations

ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 Expression (org.eclipse.jdt.core.dom.Expression)18 AST (org.eclipse.jdt.core.dom.AST)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)13 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)12 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7 Assignment (org.eclipse.jdt.core.dom.Assignment)6 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)4 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3