Search in sources :

Example 26 with SuperMethodInvocation

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

the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.

public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind, int relevance) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    IBinding binding = null;
    switch(selectedNode.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            binding = ((SimpleName) selectedNode).resolveBinding();
            break;
        case ASTNode.QUALIFIED_NAME:
            binding = ((QualifiedName) selectedNode).resolveBinding();
            break;
        case ASTNode.SIMPLE_TYPE:
            binding = ((SimpleType) selectedNode).resolveBinding();
            break;
        case ASTNode.NAME_QUALIFIED_TYPE:
            binding = ((NameQualifiedType) selectedNode).resolveBinding();
            break;
        case ASTNode.METHOD_INVOCATION:
            binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.FIELD_ACCESS:
            binding = ((FieldAccess) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
            break;
        case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
            break;
        default:
            return;
    }
    ITypeBinding typeBinding = null;
    String name;
    IBinding bindingDecl;
    boolean isLocalVar = false;
    if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
        binding = ((IVariableBinding) binding).getType();
    }
    if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
        binding = ((IMethodBinding) binding).getReturnType();
    }
    if (binding instanceof IMethodBinding) {
        IMethodBinding methodDecl = (IMethodBinding) binding;
        if (methodDecl.isDefaultConstructor()) {
            // UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
            return;
        }
        bindingDecl = methodDecl.getMethodDeclaration();
        typeBinding = methodDecl.getDeclaringClass();
        // $NON-NLS-1$
        name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
    } else if (binding instanceof IVariableBinding) {
        IVariableBinding varDecl = (IVariableBinding) binding;
        typeBinding = varDecl.getDeclaringClass();
        name = BasicElementLabels.getJavaElementName(binding.getName());
        isLocalVar = !varDecl.isField();
        bindingDecl = varDecl.getVariableDeclaration();
    } else if (binding instanceof ITypeBinding) {
        typeBinding = (ITypeBinding) binding;
        bindingDecl = typeBinding.getTypeDeclaration();
        name = BasicElementLabels.getJavaElementName(binding.getName());
    } else {
        return;
    }
    if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
        int includedModifiers = 0;
        int excludedModifiers = 0;
        String label;
        switch(kind) {
            case TO_VISIBLE:
                excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
                break;
            case TO_STATIC:
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
                includedModifiers = Modifier.STATIC;
                if (bindingDecl.getKind() == IBinding.METHOD) {
                    excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
                }
                break;
            case TO_NON_STATIC:
                if (typeBinding != null && typeBinding.isInterface())
                    return;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
                excludedModifiers = Modifier.STATIC;
                break;
            case TO_NON_PRIVATE:
                int visibility;
                if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
                    visibility = Modifier.NONE;
                    excludedModifiers = Modifier.PRIVATE;
                } else {
                    visibility = Modifier.PUBLIC;
                    includedModifiers = Modifier.PUBLIC;
                    excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                }
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
                break;
            case TO_NON_FINAL:
                if (typeBinding != null && typeBinding.isInterface())
                    return;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
                excludedModifiers = Modifier.FINAL;
                break;
            default:
                // $NON-NLS-1$
                throw new IllegalArgumentException("not supported");
        }
        ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
        if (targetCU != null) {
            // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance));
        }
    }
    if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
    // UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IBinding(org.eclipse.jdt.core.dom.IBinding) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 27 with SuperMethodInvocation

use of org.eclipse.jdt.core.dom.SuperMethodInvocation 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 28 with SuperMethodInvocation

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

the class StubUtility method getBaseNameFromExpression.

private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
    String name = null;
    if (assignedExpression instanceof CastExpression) {
        assignedExpression = ((CastExpression) assignedExpression).getExpression();
    }
    if (assignedExpression instanceof Name) {
        Name simpleNode = (Name) assignedExpression;
        IBinding binding = simpleNode.resolveBinding();
        if (binding instanceof IVariableBinding)
            return getBaseName((IVariableBinding) binding, project);
        return ASTNodes.getSimpleNameIdentifier(simpleNode);
    } else if (assignedExpression instanceof MethodInvocation) {
        name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof SuperMethodInvocation) {
        name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof FieldAccess) {
        return ((FieldAccess) assignedExpression).getName().getIdentifier();
    } else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
        String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
        StringBuffer res = new StringBuffer();
        boolean needsUnderscore = false;
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
                    res.append('_');
                }
                res.append(ch);
                needsUnderscore = false;
            } else {
                needsUnderscore = res.length() > 0;
            }
        }
        if (res.length() > 0) {
            return res.toString();
        }
    }
    if (name != null) {
        for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
            String curr = KNOWN_METHOD_NAME_PREFIXES[i];
            if (name.startsWith(curr)) {
                if (name.equals(curr)) {
                    // don't suggest 'get' as variable name
                    return null;
                } else if (Character.isUpperCase(name.charAt(curr.length()))) {
                    return name.substring(curr.length());
                }
            }
        }
    }
    return name;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 29 with SuperMethodInvocation

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

the class ObsoleteLambdaCleanUp method visit.

@Override
public boolean visit(final LambdaExpression node) {
    if (node.hasParentheses() && node.parameters().size() == 1 && node.parameters().get(0) instanceof VariableDeclarationFragment) {
        // TODO it should also be possible to deal with a SingleVariableDeclaration
        // when the type matches the expected inferred type
        // To do this, we should visit the whole block and check the target type
        removeParamParentheses(node);
        return false;
    }
    if (node.getBody() instanceof Block) {
        List<Statement> statements = ASTNodes.asList((Block) node.getBody());
        if (statements.size() == 1 && statements.get(0) instanceof ReturnStatement) {
            removeReturnAndBrackets(node, statements);
            return false;
        }
    } else if (node.getBody() instanceof ClassInstanceCreation) {
        ClassInstanceCreation ci = (ClassInstanceCreation) node.getBody();
        List<Expression> arguments = ci.arguments();
        if (ci.resolveTypeBinding() != null && ci.getAnonymousClassDeclaration() == null && node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceByCreationReference(node, ci);
            return false;
        }
    } else if (node.getBody() instanceof SuperMethodInvocation) {
        SuperMethodInvocation smi = (SuperMethodInvocation) node.getBody();
        List<Expression> arguments = smi.arguments();
        if (node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceBySuperMethodReference(node, smi);
            return false;
        }
    } else if (node.getBody() instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) node.getBody();
        Expression calledExpression = methodInvocation.getExpression();
        ITypeBinding calledType = ASTNodes.getCalledType(methodInvocation);
        List<Expression> arguments = methodInvocation.arguments();
        if (node.parameters().size() == arguments.size()) {
            if (!areSameIdentifiers(node, arguments)) {
                return true;
            }
            if (isStaticMethod(methodInvocation)) {
                if (!arguments.isEmpty()) {
                    String[] remainingParams = new String[arguments.size() - 1];
                    for (int i = 0; i < arguments.size() - 1; i++) {
                        ITypeBinding argumentBinding = arguments.get(i + 1).resolveTypeBinding();
                        if (argumentBinding == null) {
                            return true;
                        }
                        remainingParams[i] = argumentBinding.getQualifiedName();
                    }
                    for (IMethodBinding methodBinding : calledType.getDeclaredMethods()) {
                        if ((methodBinding.getModifiers() & Modifier.STATIC) == 0 && ASTNodes.usesGivenSignature(methodBinding, calledType.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                            return true;
                        }
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
            if (calledExpression == null || calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral || calledExpression instanceof ThisExpression) {
                replaceByMethodReference(node, methodInvocation);
                return false;
            }
            if (calledExpression instanceof FieldAccess) {
                FieldAccess fieldAccess = (FieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            } else if (calledExpression instanceof SuperFieldAccess) {
                SuperFieldAccess fieldAccess = (SuperFieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            }
        } else if (calledExpression instanceof SimpleName && node.parameters().size() == arguments.size() + 1) {
            SimpleName calledObject = (SimpleName) calledExpression;
            if (isSameIdentifier(node, 0, calledObject)) {
                for (int i = 0; i < arguments.size(); i++) {
                    SimpleName expression = ASTNodes.as(arguments.get(i), SimpleName.class);
                    if (expression == null || !isSameIdentifier(node, i + 1, expression)) {
                        return true;
                    }
                }
                ITypeBinding klass = calledExpression.resolveTypeBinding();
                if (klass == null) {
                    return true;
                }
                String[] remainingParams = new String[arguments.size() + 1];
                remainingParams[0] = klass.getQualifiedName();
                for (int i = 0; i < arguments.size(); i++) {
                    ITypeBinding argumentBinding = arguments.get(i).resolveTypeBinding();
                    if (argumentBinding == null) {
                        return true;
                    }
                    remainingParams[i + 1] = argumentBinding.getQualifiedName();
                }
                for (IMethodBinding methodBinding : klass.getDeclaredMethods()) {
                    if ((methodBinding.getModifiers() & Modifier.STATIC) != 0 && ASTNodes.usesGivenSignature(methodBinding, klass.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                        return true;
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
        }
    }
    return true;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

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