Search in sources :

Example 11 with SuperFieldAccess

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

use of org.eclipse.jdt.core.dom.SuperFieldAccess 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)

Example 13 with SuperFieldAccess

use of org.eclipse.jdt.core.dom.SuperFieldAccess in project eclipse.jdt.ls by eclipse.

the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.

public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> 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) {
            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.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Aggregations

SuperFieldAccess (org.eclipse.jdt.core.dom.SuperFieldAccess)13 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)5 Name (org.eclipse.jdt.core.dom.Name)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)5 IBinding (org.eclipse.jdt.core.dom.IBinding)4 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)3 SimpleType (org.eclipse.jdt.core.dom.SimpleType)3