Search in sources :

Example 36 with VariableDeclaration

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

the class ASTResolving method getPossibleReferenceBinding.

private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
    ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case ASTNode.ASSIGNMENT:
            Assignment assignment = (Assignment) parent;
            if (node.equals(assignment.getLeftHandSide())) {
                // field write access: xx= expression
                return assignment.getRightHandSide().resolveTypeBinding();
            }
            // read access
            return assignment.getLeftHandSide().resolveTypeBinding();
        case ASTNode.INFIX_EXPRESSION:
            InfixExpression infix = (InfixExpression) parent;
            InfixExpression.Operator op = infix.getOperator();
            if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("boolean");
            } else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("int");
            }
            if (node.equals(infix.getLeftOperand())) {
                // xx operation expression
                ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
                if (rigthHandBinding != null) {
                    return rigthHandBinding;
                }
            } else {
                // expression operation xx
                ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
                if (leftHandBinding != null) {
                    return leftHandBinding;
                }
            }
            if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.INSTANCEOF_EXPRESSION:
            InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
            return instanceofExpression.getRightOperand().resolveBinding();
        case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
            VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
            if (frag.getInitializer().equals(node)) {
                return frag.getName().resolveTypeBinding();
            }
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
            IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
            if (superMethodBinding != null) {
                return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
            }
            break;
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation methodInvocation = (MethodInvocation) parent;
            IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
            if (methodBinding != null) {
                return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
            }
            break;
        case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            {
                SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
                IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
                if (superBinding != null) {
                    return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
                }
                break;
            }
        case ASTNode.CONSTRUCTOR_INVOCATION:
            {
                ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
                IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
                if (constrBinding != null) {
                    return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
                }
                break;
            }
        case ASTNode.CLASS_INSTANCE_CREATION:
            {
                ClassInstanceCreation creation = (ClassInstanceCreation) parent;
                IMethodBinding creationBinding = creation.resolveConstructorBinding();
                if (creationBinding != null) {
                    return getParameterTypeBinding(node, creation.arguments(), creationBinding);
                }
                break;
            }
        case ASTNode.PARENTHESIZED_EXPRESSION:
            return guessBindingForReference(parent);
        case ASTNode.ARRAY_ACCESS:
            if (((ArrayAccess) parent).getIndex().equals(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            } else {
                ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
                if (parentBinding == null) {
                    // $NON-NLS-1$
                    parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
                }
                return parentBinding.createArrayType(1);
            }
        case ASTNode.ARRAY_CREATION:
            if (((ArrayCreation) parent).dimensions().contains(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.ARRAY_INITIALIZER:
            ASTNode initializerParent = parent.getParent();
            int dim = 1;
            while (initializerParent instanceof ArrayInitializer) {
                initializerParent = initializerParent.getParent();
                dim++;
            }
            Type creationType = null;
            if (initializerParent instanceof ArrayCreation) {
                creationType = ((ArrayCreation) initializerParent).getType();
            } else if (initializerParent instanceof VariableDeclaration) {
                VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
                creationType = ASTNodes.getType(varDecl);
                dim -= varDecl.getExtraDimensions();
            } else if (initializerParent instanceof MemberValuePair) {
                String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
                IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
                if (annotMember != null) {
                    return getReducedDimensionBinding(annotMember.getReturnType(), dim);
                }
            }
            if (creationType instanceof ArrayType) {
                ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
                if (creationTypeBinding != null) {
                    return Bindings.getComponentType(creationTypeBinding, dim);
                }
            }
            break;
        case ASTNode.CONDITIONAL_EXPRESSION:
            ConditionalExpression expression = (ConditionalExpression) parent;
            if (node.equals(expression.getExpression())) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            if (node.equals(expression.getElseExpression())) {
                return expression.getThenExpression().resolveTypeBinding();
            }
            return expression.getElseExpression().resolveTypeBinding();
        case ASTNode.POSTFIX_EXPRESSION:
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("int");
        case ASTNode.PREFIX_EXPRESSION:
            if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("int");
        case ASTNode.IF_STATEMENT:
        case ASTNode.WHILE_STATEMENT:
        case ASTNode.DO_STATEMENT:
            if (node instanceof Expression) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            break;
        case ASTNode.SWITCH_STATEMENT:
            if (((SwitchStatement) parent).getExpression().equals(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.RETURN_STATEMENT:
            MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
            if (decl != null && !decl.isConstructor()) {
                return decl.getReturnType2().resolveBinding();
            }
            LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
            if (lambdaExpr != null) {
                IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
                if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
                    return lambdaMethodBinding.getReturnType();
                }
            }
            break;
        case ASTNode.CAST_EXPRESSION:
            return ((CastExpression) parent).getType().resolveBinding();
        case ASTNode.THROW_STATEMENT:
        case ASTNode.CATCH_CLAUSE:
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("java.lang.Exception");
        case ASTNode.FIELD_ACCESS:
            if (node.equals(((FieldAccess) parent).getName())) {
                return getPossibleReferenceBinding(parent);
            }
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            return getPossibleReferenceBinding(parent);
        case ASTNode.QUALIFIED_NAME:
            if (node.equals(((QualifiedName) parent).getName())) {
                return getPossibleReferenceBinding(parent);
            }
            break;
        case ASTNode.SWITCH_CASE:
            if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
                return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
            }
            break;
        case ASTNode.ASSERT_STATEMENT:
            if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("java.lang.String");
        case ASTNode.SINGLE_MEMBER_ANNOTATION:
            {
                // $NON-NLS-1$
                IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
                if (annotMember != null) {
                    return annotMember.getReturnType();
                }
                break;
            }
        case ASTNode.MEMBER_VALUE_PAIR:
            {
                String name = ((MemberValuePair) parent).getName().getIdentifier();
                IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
                if (annotMember != null) {
                    return annotMember.getReturnType();
                }
                break;
            }
        default:
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) 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) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Annotation(org.eclipse.jdt.core.dom.Annotation) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) 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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 37 with VariableDeclaration

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

the class AbstractClassSubstituteCleanUp method canVarOccurrenceBeRefactored0.

private boolean canVarOccurrenceBeRefactored0(final Block node, final List<VariableDeclaration> varDecls, final List<MethodInvocation> methodCallsToRefactor, final List<VariableDeclaration> otherVarDecls) {
    for (VariableDeclaration varDecl : varDecls) {
        VarOccurrenceVisitor varOccurrenceVisitor = new VarOccurrenceVisitor(varDecl);
        Statement parent = ASTNodes.getTypedAncestor(varDecl, Statement.class);
        Statement nextSibling = ASTNodes.getNextSibling(parent);
        while (nextSibling != null) {
            varOccurrenceVisitor.traverseNodeInterruptibly(nextSibling);
            nextSibling = ASTNodes.getNextSibling(nextSibling);
        }
        if (varOccurrenceVisitor.isUsedInAnnonymousClass()) {
            return false;
        }
        for (SimpleName varOccurrence : varOccurrenceVisitor.getVarOccurrences()) {
            List<VariableDeclaration> subVarDecls = new ArrayList<>();
            if (!canBeRefactored(node, varOccurrence, varOccurrence.resolveTypeBinding(), subVarDecls, methodCallsToRefactor)) {
                return false;
            }
            otherVarDecls.addAll(subVarDecls);
        }
    }
    return true;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Example 38 with VariableDeclaration

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

the class AbstractClassSubstituteCleanUp method replaceClass.

private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactor, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    // $NON-NLS-1$
    TextEditGroup group = new TextEditGroup("");
    Type substituteType = substituteType(originalInstanceCreation.getType(), originalInstanceCreation, classesToUseWithImport, importsToAdd);
    if (substituteType != null) {
        ASTNodes.replaceButKeepComment(rewrite, originalInstanceCreation.getType(), substituteType, group);
        originalInstanceCreation.setType(substituteType);
    }
    for (MethodInvocation methodCall : methodCallsToRefactor) {
        MethodInvocation copyOfMethodCall = ast.copySubtree(methodCall);
        refactorMethod(methodCall, copyOfMethodCall);
        rewrite.replace(methodCall, copyOfMethodCall, group);
    }
    for (VariableDeclaration variableDecl : variableDecls) {
        VariableDeclarationStatement oldDeclareStatement = (VariableDeclarationStatement) variableDecl.getParent();
        Type substituteVarType = substituteType(oldDeclareStatement.getType(), (ASTNode) oldDeclareStatement.fragments().get(0), classesToUseWithImport, importsToAdd);
        if (substituteVarType != null) {
            ASTNodes.replaceButKeepComment(rewrite, oldDeclareStatement.getType(), substituteVarType, group);
        }
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 39 with VariableDeclaration

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

the class ObsoleteRedundantComparatorCleanUp method isClassToRemove.

private boolean isClassToRemove(final ClassInstanceCreation classInstanceCreation, final boolean isForward) {
    AnonymousClassDeclaration anonymousClassDecl = classInstanceCreation.getAnonymousClassDeclaration();
    Type type = classInstanceCreation.getType();
    if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && classInstanceCreation.arguments().isEmpty() && anonymousClassDecl != null) {
        List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
        ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
        if (bodies != null && bodies.size() == 1 && typeArgument != null) {
            BodyDeclaration body = bodies.get(0);
            if (body instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) body;
                ReturnStatement returnStatement = ASTNodes.as(methodDecl.getBody(), ReturnStatement.class);
                if (returnStatement != null && returnStatement.getExpression() != null && // $NON-NLS-1$
                ASTNodes.usesGivenSignature(// $NON-NLS-1$
                methodDecl, // $NON-NLS-1$
                Comparator.class.getCanonicalName(), // $NON-NLS-1$
                "compare", typeArgument.getQualifiedName(), typeArgument.getQualifiedName())) {
                    VariableDeclaration object1 = (VariableDeclaration) methodDecl.parameters().get(0);
                    VariableDeclaration object2 = (VariableDeclaration) methodDecl.parameters().get(1);
                    return isReturnedExpressionToRemove(object1.getName(), object2.getName(), returnStatement.getExpression(), isForward);
                }
            }
        }
    }
    return false;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 40 with VariableDeclaration

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method maybeRefactorMethod.

private boolean maybeRefactorMethod(final ClassInstanceCreation visited, final ITypeBinding typeArgument, final MethodDeclaration methodDecl, final Set<String> classesToUseWithImport) {
    Block methodBody = methodDecl.getBody();
    if (// $NON-NLS-1$
    ASTNodes.usesGivenSignature(// $NON-NLS-1$
    methodDecl, // $NON-NLS-1$
    Comparator.class.getCanonicalName(), // $NON-NLS-1$
    "compare", // $NON-NLS-1$
    typeArgument.getQualifiedName(), typeArgument.getQualifiedName())) {
        VariableDeclaration object1 = (VariableDeclaration) methodDecl.parameters().get(0);
        VariableDeclaration object2 = (VariableDeclaration) methodDecl.parameters().get(1);
        List<Statement> statements = methodBody.statements();
        return maybeRefactorBody(visited, typeArgument, classesToUseWithImport, object1, object2, statements);
    }
    return true;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) Comparator(java.util.Comparator)

Aggregations

VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)40 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 ArrayList (java.util.ArrayList)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 Type (org.eclipse.jdt.core.dom.Type)7 AST (org.eclipse.jdt.core.dom.AST)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)5