Search in sources :

Example 11 with SuperMethodInvocation

use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project whole by wholeplatform.

the class CompilationUnitBuilder method newSuperMethodInvocation.

public SuperMethodInvocation newSuperMethodInvocation(String method) {
    SuperMethodInvocation callExp = ast.newSuperMethodInvocation();
    callExp.setName(ast.newSimpleName(method));
    return callExp;
}
Also used : SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 12 with SuperMethodInvocation

use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project whole by wholeplatform.

the class AbstractVisitorCompilationUnitBuilder method addFragmentVisitMethod.

public MethodDeclaration addFragmentVisitMethod(String visitSuffix, String typeName) {
    MethodDeclaration method = addVisitMethod(typeName);
    SuperMethodInvocation callExp = newSuperMethodInvocation("visit" + visitSuffix);
    callExp.arguments().add(ast.newSimpleName("entity"));
    method.getBody().statements().add(ast.newExpressionStatement(callExp));
    return method;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 13 with SuperMethodInvocation

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

the class ASTNodes method getTargetType.

/**
 * Derives the target type defined at the location of the given expression if the target context
 * supports poly expressions.
 *
 * @param expression the expression at whose location the target type is required
 * @return the type binding of the target type defined at the location of the given expression
 *         if the target context supports poly expressions, or <code>null</code> if the target
 *         type could not be derived
 *
 * @since 3.10
 */
public static ITypeBinding getTargetType(Expression expression) {
    ASTNode parent = expression.getParent();
    StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
        return ((VariableDeclaration) parent).getName().resolveTypeBinding();
    } else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        return getTargetTypeForReturnStmt((ReturnStatement) parent);
    } else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
        return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
    } else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) parent;
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (methodBinding != null) {
            return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
        }
    } else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
        SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
        IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
        if (superMethodBinding != null) {
            return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
        }
    } else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
        ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
        IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
        if (constructorBinding != null) {
            return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
        }
    } else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
        SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
        IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
        if (superConstructorBinding != null) {
            return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
        }
    } else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
        ClassInstanceCreation creation = (ClassInstanceCreation) parent;
        IMethodBinding creationBinding = creation.resolveConstructorBinding();
        if (creationBinding != null) {
            return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
        }
    } else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
        EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
        IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
        if (enumConstructorBinding != null) {
            return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
        }
    } else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
        IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
        if (methodBinding != null) {
            return methodBinding.getReturnType();
        }
    } else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        return getTargetType((ConditionalExpression) parent);
    } else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
        return ((CastExpression) parent).getType().resolveBinding();
    } else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return getTargetType((ParenthesizedExpression) parent);
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) 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) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 14 with SuperMethodInvocation

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

the class SuperCallRatherThanUselessOverridingCleanUp method visit.

@Override
public boolean visit(final MethodDeclaration visited) {
    if (visited.getBody() == null) {
        return true;
    }
    List<Statement> bodyStatements = visited.getBody().statements();
    if (bodyStatements.size() == 1) {
        SuperMethodInvocation bodyMi = ASTNodes.asExpression(bodyStatements.get(0), SuperMethodInvocation.class);
        if (bodyMi != null) {
            IMethodBinding bodyMethodBinding = bodyMi.resolveMethodBinding();
            IMethodBinding declMethodBinding = visited.resolveBinding();
            if (declMethodBinding != null && bodyMethodBinding != null && declMethodBinding.overrides(bodyMethodBinding) && !hasSignificantAnnotations(declMethodBinding) && haveSameModifiers(bodyMethodBinding, declMethodBinding) && haveSameParameters(visited, bodyMi)) {
                if (!Modifier.isProtected(declMethodBinding.getModifiers()) || declaredInSamePackage(bodyMethodBinding, declMethodBinding) || // protected also means package visibility, so check if it is required
                !isMethodUsedInItsPackage(declMethodBinding, visited)) {
                    TextEditGroup group = new TextEditGroup(MultiFixMessages.SuperCallRatherThanUselessOverridingCleanUp_description);
                    ASTRewrite rewrite = cuRewrite.getASTRewrite();
                    rewrite.remove(visited, group);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 15 with SuperMethodInvocation

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

the class CFGBuilder method addVariableAccess.

/**
 * @return whether the current variable access can throw an exception.
 */
private boolean addVariableAccess(final CFGBasicBlock basicBlock, final Expression node, final int flags, final ThrowerBlocks throwers) {
    if (node == null) {
        return false;
    }
    switch(node.getNodeType()) {
        case ASTNode.ARRAY_ACCESS:
            ArrayAccess aa = (ArrayAccess) node;
            addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
            addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
            throwers.addThrow(aa, newException(node, ArrayIndexOutOfBoundsException.class.getCanonicalName()));
            return true;
        case ASTNode.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 ASTNode.ARRAY_INITIALIZER:
            ArrayInitializer ai = (ArrayInitializer) node;
            return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
        case ASTNode.ASSIGNMENT:
            Assignment a = (Assignment) node;
            boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), VariableAccess.WRITE, throwers);
            boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), VariableAccess.READ, throwers);
            return aMightThrow1 || aMightThrow2;
        case ASTNode.BOOLEAN_LITERAL:
        case ASTNode.CHARACTER_LITERAL:
        case ASTNode.NULL_LITERAL:
        case ASTNode.NUMBER_LITERAL:
        case ASTNode.STRING_LITERAL:
        case ASTNode.TYPE_LITERAL:
            // Nothing to do
            return false;
        case ASTNode.CAST_EXPRESSION:
            CastExpression cae = (CastExpression) node;
            return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
        case ASTNode.CLASS_INSTANCE_CREATION:
            ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) node;
            addVariableAccess(basicBlock, classInstanceCreation.getExpression(), flags, throwers);
            addVariableAccesses(basicBlock, classInstanceCreation.arguments(), flags, throwers);
            IMethodBinding cicBinding = classInstanceCreation.resolveConstructorBinding();
            if (cicBinding != null) {
                ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
                throwers.addThrow(classInstanceCreation, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case ASTNode.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 ASTNode.FIELD_ACCESS:
            FieldAccess fa = (FieldAccess) node;
            boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
            basicBlock.addVariableAccess(new VariableAccess(fa, flags));
            if (is(flags, VariableAccess.READ)) {
                throwers.addThrow(fa, newException(node, NullPointerException.class.getCanonicalName()));
                mightThrow = true;
            }
            return mightThrow;
        case ASTNode.INFIX_EXPRESSION:
            InfixExpression infixExpression = (InfixExpression) node;
            boolean ieMightThrow1 = addVariableAccess(basicBlock, infixExpression.getLeftOperand(), flags, throwers);
            boolean ieMightThrow2 = addVariableAccess(basicBlock, infixExpression.getRightOperand(), flags, throwers);
            return ieMightThrow1 || ieMightThrow2;
        case ASTNode.INSTANCEOF_EXPRESSION:
            InstanceofExpression ioe = (InstanceofExpression) node;
            return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation methodInvocation = (MethodInvocation) node;
            addVariableAccess(basicBlock, methodInvocation.getExpression(), flags, throwers);
            addVariableAccesses(basicBlock, methodInvocation.arguments(), flags, throwers);
            IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
            if (methodBinding != null) {
                ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
                throwers.addThrow(methodInvocation, declaredThrows);
                return declaredThrows.length > 0;
            }
            return false;
        case ASTNode.SIMPLE_NAME:
            SimpleName sn = (SimpleName) node;
            basicBlock.addVariableAccess(new VariableAccess(sn, flags));
            if (is(flags, VariableAccess.READ)) {
                throwers.addThrow(sn, newException(node, NullPointerException.class.getCanonicalName()));
                return true;
            }
            return false;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qn = (QualifiedName) node;
            basicBlock.addVariableAccess(new VariableAccess(qn, flags));
            throwers.addThrow(qn, newException(node, NullPointerException.class.getCanonicalName()));
            return true;
        case ASTNode.PARENTHESIZED_EXPRESSION:
            ParenthesizedExpression pe = (ParenthesizedExpression) node;
            return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
        case ASTNode.POSTFIX_EXPRESSION:
            PostfixExpression poe = (PostfixExpression) node;
            return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
        case ASTNode.PREFIX_EXPRESSION:
            PrefixExpression pre = (PrefixExpression) node;
            return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
        case ASTNode.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 ASTNode.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 ASTNode.THIS_EXPRESSION:
            ThisExpression te = (ThisExpression) node;
            // TODO JNR remember use of "this" here
            return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
        case ASTNode.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) 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)

Aggregations

SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)29 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)19 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)12 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Expression (org.eclipse.jdt.core.dom.Expression)9 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 IBinding (org.eclipse.jdt.core.dom.IBinding)8 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)7 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)7 Name (org.eclipse.jdt.core.dom.Name)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 AST (org.eclipse.jdt.core.dom.AST)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)5 Block (org.eclipse.jdt.core.dom.Block)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 SuperFieldAccess (org.eclipse.jdt.core.dom.SuperFieldAccess)4