Search in sources :

Example 46 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.

the class BigNumberRefactoring method getCompareToNode.

private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final MethodInvocation mi = b.invoke(b.copy(node.getExpression()), "compareTo", b.copy(arg0(node)));
    return b.infixExpr(mi, isPositive ? EQUALS : NOT_EQUALS, b.int0(0));
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 47 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.

the class EnumSetRatherThanHashSetRefactoring method replace.

/**
 * Refactoring is not correct if argument for HashSet constructor is a Collection, but other
 * than EnumSet. <br>
 * In case of empty collection <code>EnumSet.copyOf</code> will throw an
 * <code>IllegalArgumentException</code>, <br>
 * and HashSet(Collection) will not. <br>
 * <br>
 * Other constructors can be replaced with <code>EnumSet.noneOf(Class)</code> method. <br>
 * <br>
 *
 * @see {@link java.util.EnumSet#copyOf(Collection)}
 * @see {@link java.util.EnumSet#copyOf(EnumSet)}
 * @see {@link java.util.EnumSet#noneOf(Class)} <br>
 * @param cic
 *            - class instance creation node to be replaced
 * @param type
 *            - type argument of the declaration
 */
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
    if (types == null || types.length < 1) {
        return VISIT_SUBTREE;
    }
    Type type = types[0];
    ASTBuilder b = ctx.getASTBuilder();
    List<Expression> arguments = arguments(cic);
    final MethodInvocation invocation;
    if (!arguments.isEmpty() && instanceOf(arguments.get(0), "java.util.Collection")) {
        Expression typeArg = arguments.get(0);
        if (!instanceOf(typeArg, "java.util.EnumSet")) {
            return VISIT_SUBTREE;
        }
        invocation = b.invoke(b.name("java", "util", "EnumSet"), "copyOf", b.copy(typeArg));
    } else {
        TypeLiteral newTypeLiteral = ctx.getAST().newTypeLiteral();
        newTypeLiteral.setType(b.copy(type));
        invocation = b.invoke(b.name("java", "util", "EnumSet"), "noneOf", newTypeLiteral);
    }
    ctx.getRefactorings().replace(cic, invocation);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 48 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.

the class CFGBuilder method addVariableAccess.

/**
 * @return whether the current variable access can throw an exception.
 */
@SuppressWarnings("unchecked")
private boolean addVariableAccess(CFGBasicBlock basicBlock, Expression node, int flags, ThrowerBlocks throwers) {
    if (node == null) {
        return false;
    }
    switch(node.getNodeType()) {
        case ARRAY_ACCESS:
            ArrayAccess aa = (ArrayAccess) node;
            addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
            addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
            throwers.addThrow(aa, newException(node, "java.lang.ArrayIndexOutOfBoundsException"));
            return true;
        case ARRAY_CREATION:
            ArrayCreation ac = (ArrayCreation) node;
            boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
            boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
            return acMightThrow1 || acMightThrow2;
        case ARRAY_INITIALIZER:
            ArrayInitializer ai = (ArrayInitializer) node;
            return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
        case ASSIGNMENT:
            Assignment a = (Assignment) node;
            boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), WRITE, throwers);
            boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), READ, throwers);
            return aMightThrow1 || aMightThrow2;
        case BOOLEAN_LITERAL:
        case CHARACTER_LITERAL:
        case NULL_LITERAL:
        case NUMBER_LITERAL:
        case STRING_LITERAL:
        case TYPE_LITERAL:
            // nothing to do
            return false;
        case CAST_EXPRESSION:
            CastExpression cae = (CastExpression) node;
            return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
        case CLASS_INSTANCE_CREATION:
            ClassInstanceCreation cic = (ClassInstanceCreation) node;
            addVariableAccess(basicBlock, cic.getExpression(), flags, throwers);
            addVariableAccesses(basicBlock, cic.arguments(), flags, throwers);
            IMethodBinding cicBinding = cic.resolveConstructorBinding();
            if (cicBinding != null) {
                ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
                throwers.addThrow(cic, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case CONDITIONAL_EXPRESSION:
            ConditionalExpression coe = (ConditionalExpression) node;
            boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
            boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
            boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
            return mightThrow1 || mightThrow2 || mightThrow3;
        case FIELD_ACCESS:
            FieldAccess fa = (FieldAccess) node;
            boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
            basicBlock.addVariableAccess(new VariableAccess(fa, flags));
            if (is(flags, READ)) {
                throwers.addThrow(fa, newException(node, "java.lang.NullPointerException"));
                mightThrow = true;
            }
            return mightThrow;
        case INFIX_EXPRESSION:
            InfixExpression ie = (InfixExpression) node;
            boolean ieMightThrow1 = addVariableAccess(basicBlock, ie.getLeftOperand(), flags, throwers);
            boolean ieMightThrow2 = addVariableAccess(basicBlock, ie.getRightOperand(), flags, throwers);
            return ieMightThrow1 || ieMightThrow2;
        case INSTANCEOF_EXPRESSION:
            InstanceofExpression ioe = (InstanceofExpression) node;
            return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
        case METHOD_INVOCATION:
            MethodInvocation mi = (MethodInvocation) node;
            addVariableAccess(basicBlock, mi.getExpression(), flags, throwers);
            addVariableAccesses(basicBlock, mi.arguments(), flags, throwers);
            IMethodBinding methodBinding = mi.resolveMethodBinding();
            if (methodBinding != null) {
                ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
                throwers.addThrow(mi, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case SIMPLE_NAME:
            SimpleName sn = (SimpleName) node;
            basicBlock.addVariableAccess(new VariableAccess(sn, flags));
            if (is(flags, READ)) {
                throwers.addThrow(sn, newException(node, "java.lang.NullPointerException"));
                return true;
            }
            return false;
        case QUALIFIED_NAME:
            QualifiedName qn = (QualifiedName) node;
            basicBlock.addVariableAccess(new VariableAccess(qn, flags));
            throwers.addThrow(qn, newException(node, "java.lang.NullPointerException"));
            return true;
        case PARENTHESIZED_EXPRESSION:
            ParenthesizedExpression pe = (ParenthesizedExpression) node;
            return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
        case POSTFIX_EXPRESSION:
            PostfixExpression poe = (PostfixExpression) node;
            return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
        case PREFIX_EXPRESSION:
            PrefixExpression pre = (PrefixExpression) node;
            return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
        case SUPER_FIELD_ACCESS:
            SuperFieldAccess sfa = (SuperFieldAccess) node;
            boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
            boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
            return sfaMightThrow1 || sfaMightThrow2;
        case SUPER_METHOD_INVOCATION:
            SuperMethodInvocation smi = (SuperMethodInvocation) node;
            addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
            addVariableAccess(basicBlock, smi.getName(), flags, throwers);
            IMethodBinding sMethodBinding = smi.resolveMethodBinding();
            if (sMethodBinding != null) {
                ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
                throwers.addThrow(smi, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case THIS_EXPRESSION:
            ThisExpression te = (ThisExpression) node;
            // TODO JNR remember use of "this" here
            return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
        case VARIABLE_DECLARATION_EXPRESSION:
            return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
        default:
            throw new NotImplementedException(node);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) NotImplementedException(org.autorefactor.util.NotImplementedException) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableAccess(org.autorefactor.cfg.VariableAccess) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 49 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.

the class ASTBuilder method invoke.

/**
 * Builds a new {@link MethodInvocation} instance.
 *
 * @param expression the method invocation expression
 * @param methodName the name of the invoked method
 * @param arguments the arguments for the method invocation
 * @return a new method invocation
 */
public MethodInvocation invoke(String expression, String methodName, Expression... arguments) {
    final MethodInvocation mi = ast.newMethodInvocation();
    mi.setExpression(ast.newSimpleName(expression));
    mi.setName(ast.newSimpleName(methodName));
    addAll(arguments(mi), arguments);
    return mi;
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 50 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.

the class ForLoopHelper method iterateOverContainer.

/**
 * Returns the {@link ForLoopContent} if this for loop iterates over a container.
 *
 * @param node the for statement
 * @return the {@link ForLoopContent} if this for loop iterates over a container, null otherwise
 */
public static ForLoopContent iterateOverContainer(ForStatement node) {
    final List<Expression> initializers = initializers(node);
    final Expression condition = node.getExpression();
    final List<Expression> updaters = updaters(node);
    if (initializers.size() == 1) {
        Expression firstInit = initializers.get(0);
        if (updaters.isEmpty()) {
            final Pair<Name, Expression> initPair = decomposeInitializer(firstInit);
            final Name init = initPair.getFirst();
            final MethodInvocation condMi = as(node.getExpression(), MethodInvocation.class);
            final MethodInvocation initMi = as(initPair.getSecond(), MethodInvocation.class);
            if (condMi != null && isSameVariable(init, condMi.getExpression()) && isMethod(initMi, "java.util.Collection", "iterator") && isMethod(condMi, "java.util.Iterator", "hasNext")) {
                return getIteratorOnCollection(initMi.getExpression(), condMi.getExpression());
            }
        } else if (updaters.size() == 1 && isPrimitive(firstInit, "int")) {
            final Pair<Name, Expression> initPair = decomposeInitializer(firstInit);
            final Name init = initPair.getFirst();
            final ForLoopContent forContent = getIndexOnIterable(condition, init);
            final Name updater = getUpdaterOperand(updaters.get(0));
            if (forContent != null && isZero(initPair.getSecond()) && isSameVariable(init, forContent.loopVariable) && isSameVariable(init, updater)) {
                return forContent;
            }
        }
    }
    return null;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) Pair(org.autorefactor.util.Pair)

Aggregations

MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)95 Expression (org.eclipse.jdt.core.dom.Expression)53 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)31 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)27 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)25 AST (org.eclipse.jdt.core.dom.AST)23 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)19 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)16 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 IBinding (org.eclipse.jdt.core.dom.IBinding)14 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)13 Name (org.eclipse.jdt.core.dom.Name)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)13 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)13 Type (org.eclipse.jdt.core.dom.Type)13 ArrayList (java.util.ArrayList)12 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)12