Search in sources :

Example 11 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class SourceProvider method updateImplicitReceivers.

private void updateImplicitReceivers(ASTRewrite rewriter, CallContext context) {
    if (context.receiver == null)
        return;
    List<Expression> implicitReceivers = fAnalyzer.getImplicitReceivers();
    for (Iterator<Expression> iter = implicitReceivers.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, context.importer);
        if (node instanceof MethodInvocation) {
            final MethodInvocation inv = (MethodInvocation) node;
            rewriter.set(inv, MethodInvocation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, (IMethodBinding) inv.getName().resolveBinding(), importRewriteContext), null);
        } else if (node instanceof ClassInstanceCreation) {
            final ClassInstanceCreation inst = (ClassInstanceCreation) node;
            rewriter.set(inst, ClassInstanceCreation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, inst.resolveConstructorBinding(), importRewriteContext), null);
        } else if (node instanceof ThisExpression) {
            rewriter.replace(node, rewriter.createStringPlaceholder(context.receiver, ASTNode.METHOD_INVOCATION), null);
        } else if (node instanceof FieldAccess) {
            final FieldAccess access = (FieldAccess) node;
            rewriter.set(access, FieldAccess.EXPRESSION_PROPERTY, createReceiver(rewriter, context, access.resolveFieldBinding(), importRewriteContext), null);
        } else if (node instanceof SimpleName && ((SimpleName) node).resolveBinding() instanceof IVariableBinding) {
            IVariableBinding vb = (IVariableBinding) ((SimpleName) node).resolveBinding();
            if (vb.isField()) {
                Expression receiver = createReceiver(rewriter, context, vb, importRewriteContext);
                if (receiver != null) {
                    FieldAccess access = node.getAST().newFieldAccess();
                    ASTNode target = rewriter.createMoveTarget(node);
                    access.setName((SimpleName) target);
                    access.setExpression(receiver);
                    rewriter.replace(node, access, null);
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 12 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ModifierCorrectionSubProcessor method addRemoveInvalidModifiersProposal.

public static void addRemoveInvalidModifiersProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int relevance) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof MethodDeclaration) {
        selectedNode = ((MethodDeclaration) selectedNode).getName();
    }
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding != null) {
        String methodName = BasicElementLabels.getJavaElementName(binding.getName());
        String label = null;
        int problemId = problem.getProblemId();
        int excludedModifiers = 0;
        int includedModifiers = 0;
        switch(problemId) {
            case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
            case IProblem.UnexpectedStaticModifierForMethod:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodName);
                break;
            case IProblem.UnexpectedStaticModifierForField:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changefieldmodifiertononstatic_description, methodName);
                break;
            case IProblem.IllegalModifierCombinationFinalVolatileForField:
                excludedModifiers = Modifier.VOLATILE;
                label = CorrectionMessages.ModifierCorrectionSubProcessor_removevolatile_description;
                break;
            case IProblem.IllegalModifierForInterfaceMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
                break;
            case IProblem.IllegalModifierForInterfaceMethod18:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP | Modifier.DEFAULT | Modifier.STATIC);
                break;
            case IProblem.IllegalModifierForInterface:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForInterfaceField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
                break;
            case IProblem.IllegalModifierForMemberInterface:
            case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForMemberClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForLocalClass:
                excludedModifiers = ~(Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForArgument:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT);
                break;
            case IProblem.IllegalModifierForMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.STRICTFP | Modifier.SYNCHRONIZED);
                break;
            case IProblem.IllegalModifierForConstructor:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
                break;
            case IProblem.IllegalModifierForVariable:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForEnumConstant:
                excludedModifiers = ~Modifier.NONE;
                break;
            case IProblem.IllegalModifierForEnumConstructor:
                excludedModifiers = ~Modifier.PRIVATE;
                break;
            case IProblem.IllegalModifierForMemberEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC | Modifier.STRICTFP);
                break;
            default:
                //$NON-NLS-1$
                Assert.isTrue(false, "not supported");
                return;
        }
        if (label == null)
            label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_removeinvalidmodifiers_description, methodName);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, includedModifiers, excludedModifiers, relevance, image));
        if (problemId == IProblem.IllegalModifierCombinationFinalVolatileForField) {
            proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_removefinal_description, cu, binding, selectedNode, 0, Modifier.FINAL, relevance + 1, image));
        }
        if (problemId == IProblem.UnexpectedStaticModifierForField && binding instanceof IVariableBinding) {
            ITypeBinding declClass = ((IVariableBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostaticfinal_description, cu, binding, selectedNode, Modifier.FINAL, Modifier.VOLATILE, relevance + 1, image));
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1, image));
                }
            }
        }
        if (problemId == IProblem.UnexpectedStaticModifierForMethod && binding instanceof IMethodBinding) {
            ITypeBinding declClass = ((IMethodBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1, image));
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Image(org.eclipse.swt.graphics.Image) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 13 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ModifierCorrectionSubProcessor method addNonFinalLocalProposal.

public static void addNonFinalLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding instanceof IVariableBinding) {
        binding = ((IVariableBinding) binding).getVariableDeclaration();
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_TO_FINAL, image));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Image(org.eclipse.swt.graphics.Image)

Example 14 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che 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, image));
        }
    }
    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) Image(org.eclipse.swt.graphics.Image) 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 15 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class NullAnnotationsCorrectionProcessor method findProblemFieldName.

private static SimpleName findProblemFieldName(ASTNode selectedNode, int problemID) {
    // with expected type declared @NonNull we first need to find the SimpleName inside:
    if (selectedNode instanceof FieldAccess)
        selectedNode = ((FieldAccess) selectedNode).getName();
    else if (selectedNode instanceof QualifiedName)
        selectedNode = ((QualifiedName) selectedNode).getName();
    if (selectedNode instanceof SimpleName) {
        SimpleName name = (SimpleName) selectedNode;
        if (problemID == IProblem.NullableFieldReference)
            return name;
        // not field dereference, but compatibility issue - is value a field reference?
        IBinding binding = name.resolveBinding();
        if ((binding instanceof IVariableBinding) && ((IVariableBinding) binding).isField())
            return name;
    }
    return null;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)101 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)48 IBinding (org.eclipse.jdt.core.dom.IBinding)45 SimpleName (org.eclipse.jdt.core.dom.SimpleName)41 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)28 ArrayList (java.util.ArrayList)22 Expression (org.eclipse.jdt.core.dom.Expression)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 Name (org.eclipse.jdt.core.dom.Name)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)12 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)10 AST (org.eclipse.jdt.core.dom.AST)9 Assignment (org.eclipse.jdt.core.dom.Assignment)9