Search in sources :

Example 6 with SuperFieldAccess

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

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isNewHashValid.

private boolean isNewHashValid(final CollectedData data, final Expression newHash) {
    if (newHash instanceof ParenthesizedExpression) {
        ParenthesizedExpression newHashWithoutBrackets = (ParenthesizedExpression) newHash;
        return isNewHashValid(data, newHashWithoutBrackets.getExpression());
    }
    if ((newHash instanceof Name || newHash instanceof FieldAccess || newHash instanceof SuperFieldAccess) && data.isTempValueUsed()) {
        SimpleName fieldName = getField(newHash);
        if (!ASTNodes.isSameVariable(data.getPrimeId(), fieldName) && !ASTNodes.isSameVariable(data.getResultId(), fieldName)) {
            data.getFields().add(ASTNodes.getUnparenthesedExpression(fieldName));
            return true;
        }
    } else if (newHash instanceof ConditionalExpression && data.isTempValueUsed()) {
        ConditionalExpression condition = (ConditionalExpression) newHash;
        return isObjectValid(data, condition) || isBooleanValid(data, condition);
    } else if (newHash instanceof MethodInvocation && data.isTempValueUsed()) {
        MethodInvocation specificMethod = (MethodInvocation) newHash;
        TypeDeclaration innerClass = ASTNodes.getTypedAncestor(newHash, TypeDeclaration.class);
        TypeDeclaration topLevelClass = ASTNodes.getTypedAncestor(innerClass, TypeDeclaration.class);
        if (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(// $NON-NLS-1$
        specificMethod, // $NON-NLS-1$
        Float.class.getCanonicalName(), // $NON-NLS-1$
        "floatToIntBits", float.class.getSimpleName())) {
            SimpleName fieldName = getField((Expression) specificMethod.arguments().get(0));
            if (fieldName != null && !ASTNodes.isSameVariable(fieldName, data.getPrimeId()) && !ASTNodes.isSameVariable(fieldName, data.getResultId())) {
                data.getFields().add(ASTNodes.getUnparenthesedExpression(fieldName));
                return true;
            }
        } else if (ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, boolean[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, byte[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, char[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, double[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, float[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, int[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, Object[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, long[].class.getCanonicalName()) || ASTNodes.usesGivenSignature(specificMethod, Arrays.class.getCanonicalName(), HASH_CODE_METHOD, short[].class.getCanonicalName())) {
            SimpleName fieldName = getField((Expression) specificMethod.arguments().get(0));
            if (fieldName != null && !ASTNodes.isSameVariable(fieldName, data.getPrimeId()) && !ASTNodes.isSameVariable(fieldName, data.getResultId())) {
                data.getFields().add(ASTNodes.getUnparenthesedExpression(specificMethod));
                return true;
            }
        } else if (innerClass != null && innerClass.resolveBinding() != null && topLevelClass != null && topLevelClass.resolveBinding() != null && ASTNodes.usesGivenSignature(specificMethod, topLevelClass.resolveBinding().getQualifiedName(), HASH_CODE_METHOD)) {
            return isEnclosingHashCode(data, specificMethod, innerClass, topLevelClass);
        }
    } else if (newHash instanceof CastExpression) {
        return isGreatNumberValid(data, (CastExpression) newHash);
    }
    return false;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) Arrays(java.util.Arrays) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 7 with SuperFieldAccess

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

the class UnresolvedElementsSubProcessor method getVariableProposals.

public static void getVariableProposals(IInvocationContext context, IProblemLocationCore problem, IVariableBinding resolvedField, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveredNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    // type that defines the variable
    ITypeBinding binding = null;
    ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
    if (declaringTypeBinding == null) {
        return;
    }
    // possible type kind of the node
    boolean suggestVariableProposals = true;
    int typeKind = 0;
    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }
    Name node = null;
    switch(selectedNode.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            node = (SimpleName) selectedNode;
            ASTNode parent = node.getParent();
            StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
            if (locationInParent == ExpressionMethodReference.EXPRESSION_PROPERTY) {
                typeKind = TypeKinds.REF_TYPES;
            } else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
                if (JavaModelUtil.is1d8OrHigher(cu.getJavaProject())) {
                    typeKind = TypeKinds.CLASSES | TypeKinds.INTERFACES | TypeKinds.ENUMS;
                } else {
                    typeKind = TypeKinds.CLASSES;
                }
            } else if (locationInParent == FieldAccess.NAME_PROPERTY) {
                Expression expression = ((FieldAccess) parent).getExpression();
                if (expression != null) {
                    binding = expression.resolveTypeBinding();
                    if (binding == null) {
                        node = null;
                    }
                }
            } else if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
                suggestVariableProposals = false;
                typeKind = TypeKinds.REF_TYPES_AND_VAR;
            } else if (parent instanceof QualifiedName) {
                Name qualifier = ((QualifiedName) parent).getQualifier();
                if (qualifier != node) {
                    binding = qualifier.resolveTypeBinding();
                } else {
                    typeKind = TypeKinds.REF_TYPES;
                }
                ASTNode outerParent = parent.getParent();
                while (outerParent instanceof QualifiedName) {
                    outerParent = outerParent.getParent();
                }
                if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
                    typeKind = TypeKinds.REF_TYPES;
                    suggestVariableProposals = false;
                }
            } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY) {
                ASTNode caseParent = node.getParent().getParent();
                ITypeBinding switchExp = null;
                if (caseParent instanceof SwitchStatement) {
                    switchExp = ((SwitchStatement) caseParent).getExpression().resolveTypeBinding();
                } else if (caseParent instanceof SwitchExpression) {
                    switchExp = ((SwitchExpression) caseParent).getExpression().resolveTypeBinding();
                }
                if (switchExp != null && switchExp.isEnum()) {
                    binding = switchExp;
                }
            } else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
                binding = declaringTypeBinding.getSuperclass();
            }
            break;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qualifierName = (QualifiedName) selectedNode;
            ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
            if (qualifierBinding != null) {
                node = qualifierName.getName();
                binding = qualifierBinding;
            } else {
                node = qualifierName.getQualifier();
                typeKind = TypeKinds.REF_TYPES;
                suggestVariableProposals = node.isSimpleName();
            }
            if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
                typeKind = TypeKinds.REF_TYPES;
                suggestVariableProposals = false;
            }
            break;
        case ASTNode.FIELD_ACCESS:
            FieldAccess access = (FieldAccess) selectedNode;
            Expression expression = access.getExpression();
            if (expression != null) {
                binding = expression.resolveTypeBinding();
                if (binding != null) {
                    node = access.getName();
                }
            }
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            binding = declaringTypeBinding.getSuperclass();
            node = ((SuperFieldAccess) selectedNode).getName();
            break;
        default:
    }
    if (node == null) {
        return;
    }
    // add type proposals
    if (typeKind != 0) {
        if (!JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
            typeKind &= ~(TypeKinds.ANNOTATIONS | TypeKinds.ENUMS | TypeKinds.VARIABLES);
        }
        int relevance = Character.isUpperCase(ASTNodes.getSimpleNameIdentifier(node).charAt(0)) ? IProposalRelevance.VARIABLE_TYPE_PROPOSAL_1 : IProposalRelevance.VARIABLE_TYPE_PROPOSAL_2;
        addSimilarTypeProposals(typeKind, cu, node, relevance + 1, proposals);
        typeKind &= ~TypeKinds.ANNOTATIONS;
        addNewTypeProposals(cu, node, typeKind, relevance, proposals);
    }
    if (!suggestVariableProposals) {
        return;
    }
    SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
    boolean isWriteAccess = ASTResolving.isWriteAccess(node);
    // similar variables
    addSimilarVariableProposals(cu, astRoot, binding, resolvedField, simpleName, isWriteAccess, proposals);
    if (binding == null) {
        addStaticImportFavoriteProposals(context, simpleName, false, proposals);
    }
    if (resolvedField == null || binding == null || resolvedField.getDeclaringClass() != binding.getTypeDeclaration() && Modifier.isPrivate(resolvedField.getModifiers())) {
        // new fields
        addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);
        // new parameters and local variables
        if (binding == null) {
            addNewVariableProposals(cu, node, simpleName, proposals);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 8 with SuperFieldAccess

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

the class CompilationUnitBuilder method newSuperFieldAccess.

public SuperFieldAccess newSuperFieldAccess(String fName) {
    SuperFieldAccess fieldExp = ast.newSuperFieldAccess();
    fieldExp.setName(ast.newSimpleName(fName));
    return fieldExp;
}
Also used : SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 9 with SuperFieldAccess

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

the class UnresolvedElementsSubProcessor method getVariableProposals.

public static void getVariableProposals(IInvocationContext context, IProblemLocation problem, IVariableBinding resolvedField, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveredNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    // type that defines the variable
    ITypeBinding binding = null;
    ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
    if (declaringTypeBinding == null) {
        return;
    }
    // possible type kind of the node
    boolean suggestVariableProposals = true;
    int typeKind = 0;
    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }
    Name node = null;
    switch(selectedNode.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            node = (SimpleName) selectedNode;
            ASTNode parent = node.getParent();
            StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
            if (locationInParent == ExpressionMethodReference.EXPRESSION_PROPERTY) {
                typeKind = SimilarElementsRequestor.REF_TYPES;
            } else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
                if (JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
                    typeKind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES | SimilarElementsRequestor.ENUMS;
                } else {
                    typeKind = SimilarElementsRequestor.CLASSES;
                }
            } else if (locationInParent == FieldAccess.NAME_PROPERTY) {
                Expression expression = ((FieldAccess) parent).getExpression();
                if (expression != null) {
                    binding = expression.resolveTypeBinding();
                    if (binding == null) {
                        node = null;
                    }
                }
            } else if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
                suggestVariableProposals = false;
                typeKind = SimilarElementsRequestor.REF_TYPES_AND_VAR;
            } else if (parent instanceof QualifiedName) {
                Name qualifier = ((QualifiedName) parent).getQualifier();
                if (qualifier != node) {
                    binding = qualifier.resolveTypeBinding();
                } else {
                    typeKind = SimilarElementsRequestor.REF_TYPES;
                }
                ASTNode outerParent = parent.getParent();
                while (outerParent instanceof QualifiedName) {
                    outerParent = outerParent.getParent();
                }
                if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
                    typeKind = SimilarElementsRequestor.REF_TYPES;
                    suggestVariableProposals = false;
                }
            } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
                ITypeBinding switchExp = ((SwitchStatement) node.getParent().getParent()).getExpression().resolveTypeBinding();
                if (switchExp != null && switchExp.isEnum()) {
                    binding = switchExp;
                }
            } else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
                binding = declaringTypeBinding.getSuperclass();
            }
            break;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qualifierName = (QualifiedName) selectedNode;
            ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
            if (qualifierBinding != null) {
                node = qualifierName.getName();
                binding = qualifierBinding;
            } else {
                node = qualifierName.getQualifier();
                typeKind = SimilarElementsRequestor.REF_TYPES;
                suggestVariableProposals = node.isSimpleName();
            }
            if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
                typeKind = SimilarElementsRequestor.REF_TYPES;
                suggestVariableProposals = false;
            }
            break;
        case ASTNode.FIELD_ACCESS:
            FieldAccess access = (FieldAccess) selectedNode;
            Expression expression = access.getExpression();
            if (expression != null) {
                binding = expression.resolveTypeBinding();
                if (binding != null) {
                    node = access.getName();
                }
            }
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            binding = declaringTypeBinding.getSuperclass();
            node = ((SuperFieldAccess) selectedNode).getName();
            break;
        default:
    }
    if (node == null) {
        return;
    }
    // add type proposals
    if (typeKind != 0) {
        if (!JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
            typeKind &= ~(SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS | SimilarElementsRequestor.VARIABLES);
        }
        int relevance = Character.isUpperCase(ASTNodes.getSimpleNameIdentifier(node).charAt(0)) ? IProposalRelevance.VARIABLE_TYPE_PROPOSAL_1 : IProposalRelevance.VARIABLE_TYPE_PROPOSAL_2;
        addSimilarTypeProposals(typeKind, cu, node, relevance + 1, proposals);
        typeKind &= ~SimilarElementsRequestor.ANNOTATIONS;
        addNewTypeProposals(cu, node, typeKind, relevance, proposals);
        ReorgCorrectionsSubProcessor.addProjectSetupFixProposal(context, problem, node.getFullyQualifiedName(), proposals);
    }
    if (!suggestVariableProposals) {
        return;
    }
    SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
    boolean isWriteAccess = ASTResolving.isWriteAccess(node);
    // similar variables
    addSimilarVariableProposals(cu, astRoot, binding, resolvedField, simpleName, isWriteAccess, proposals);
    if (binding == null) {
        addStaticImportFavoriteProposals(context, simpleName, false, proposals);
    }
    if (resolvedField == null || binding == null || resolvedField.getDeclaringClass() != binding.getTypeDeclaration() && Modifier.isPrivate(resolvedField.getModifiers())) {
        // new fields
        addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);
        // new parameters and local variables
        if (binding == null) {
            addNewVariableProposals(cu, node, simpleName, proposals);
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 10 with SuperFieldAccess

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

the class UnresolvedElementsSubProcessor method getVariableProposals.

public static void getVariableProposals(IInvocationContext context, IProblemLocation problem, IVariableBinding resolvedField, Collection<CUCorrectionProposal> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveredNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    // type that defines the variable
    ITypeBinding binding = null;
    ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
    if (declaringTypeBinding == null) {
        return;
    }
    // possible type kind of the node
    boolean suggestVariableProposals = true;
    int typeKind = 0;
    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }
    Name node = null;
    switch(selectedNode.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            node = (SimpleName) selectedNode;
            ASTNode parent = node.getParent();
            StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
            if (locationInParent == ExpressionMethodReference.EXPRESSION_PROPERTY) {
                typeKind = TypeKinds.REF_TYPES;
            } else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
                if (JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
                    typeKind = TypeKinds.CLASSES | TypeKinds.INTERFACES | TypeKinds.ENUMS;
                } else {
                    typeKind = TypeKinds.CLASSES;
                }
            } else if (locationInParent == FieldAccess.NAME_PROPERTY) {
                Expression expression = ((FieldAccess) parent).getExpression();
                if (expression != null) {
                    binding = expression.resolveTypeBinding();
                    if (binding == null) {
                        node = null;
                    }
                }
            } else if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
                suggestVariableProposals = false;
                typeKind = TypeKinds.REF_TYPES_AND_VAR;
            } else if (parent instanceof QualifiedName) {
                Name qualifier = ((QualifiedName) parent).getQualifier();
                if (qualifier != node) {
                    binding = qualifier.resolveTypeBinding();
                } else {
                    typeKind = TypeKinds.REF_TYPES;
                }
                ASTNode outerParent = parent.getParent();
                while (outerParent instanceof QualifiedName) {
                    outerParent = outerParent.getParent();
                }
                if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
                    typeKind = TypeKinds.REF_TYPES;
                    suggestVariableProposals = false;
                }
            } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
                ITypeBinding switchExp = ((SwitchStatement) node.getParent().getParent()).getExpression().resolveTypeBinding();
                if (switchExp != null && switchExp.isEnum()) {
                    binding = switchExp;
                }
            } else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
                binding = declaringTypeBinding.getSuperclass();
            }
            break;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qualifierName = (QualifiedName) selectedNode;
            ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
            if (qualifierBinding != null) {
                node = qualifierName.getName();
                binding = qualifierBinding;
            } else {
                node = qualifierName.getQualifier();
                typeKind = TypeKinds.REF_TYPES;
                suggestVariableProposals = node.isSimpleName();
            }
            if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
                typeKind = TypeKinds.REF_TYPES;
                suggestVariableProposals = false;
            }
            break;
        case ASTNode.FIELD_ACCESS:
            FieldAccess access = (FieldAccess) selectedNode;
            Expression expression = access.getExpression();
            if (expression != null) {
                binding = expression.resolveTypeBinding();
                if (binding != null) {
                    node = access.getName();
                }
            }
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            binding = declaringTypeBinding.getSuperclass();
            node = ((SuperFieldAccess) selectedNode).getName();
            break;
        default:
    }
    if (node == null) {
        return;
    }
    // add type proposals
    if (typeKind != 0) {
        if (!JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
            typeKind &= ~(TypeKinds.ANNOTATIONS | TypeKinds.ENUMS | TypeKinds.VARIABLES);
        }
        int relevance = Character.isUpperCase(ASTNodes.getSimpleNameIdentifier(node).charAt(0)) ? IProposalRelevance.VARIABLE_TYPE_PROPOSAL_1 : IProposalRelevance.VARIABLE_TYPE_PROPOSAL_2;
        addSimilarTypeProposals(typeKind, cu, node, relevance + 1, proposals);
        typeKind &= ~TypeKinds.ANNOTATIONS;
        addNewTypeProposals(cu, node, typeKind, relevance, proposals);
    }
    if (!suggestVariableProposals) {
        return;
    }
    SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
    boolean isWriteAccess = ASTResolving.isWriteAccess(node);
    // similar variables
    addSimilarVariableProposals(cu, astRoot, binding, resolvedField, simpleName, isWriteAccess, proposals);
    if (binding == null) {
        addStaticImportFavoriteProposals(context, simpleName, false, proposals);
    }
    if (resolvedField == null || binding == null || resolvedField.getDeclaringClass() != binding.getTypeDeclaration() && Modifier.isPrivate(resolvedField.getModifiers())) {
        // new fields
        addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);
        // new parameters and local variables
        if (binding == null) {
            addNewVariableProposals(cu, node, simpleName, proposals);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

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