Search in sources :

Example 36 with QualifiedName

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

the class UnresolvedElementsSubProcessor method getTypeProposals.

public static void getTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode;
    if (problem.getProblemId() == IProblem.UndefinedType && cu.getBuffer() != null && cu.getBuffer().getChar(problem.getOffset()) == '@') {
        int offset = problem.getOffset() + 1;
        int length = problem.getLength() - 1;
        while (offset < cu.getBuffer().getLength() && length >= 0 && Character.isWhitespace(cu.getBuffer().getChar(offset))) {
            offset++;
            length--;
        }
        NodeFinder finder = new NodeFinder(context.getASTRoot(), offset, length);
        selectedNode = finder.getCoveringNode();
    } else {
        selectedNode = problem.getCoveringNode(context.getASTRoot());
    }
    if (selectedNode == null) {
        return;
    }
    int kind = evauateTypeKind(selectedNode, cu.getJavaProject());
    if (kind == TypeKinds.REF_TYPES) {
        addEnhancedForWithoutTypeProposals(cu, selectedNode, proposals);
    }
    while (selectedNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
        selectedNode = selectedNode.getParent();
    }
    Name node = null;
    if (selectedNode instanceof Annotation) {
        selectedNode = ((Annotation) selectedNode).getTypeName();
    }
    if (selectedNode instanceof SimpleType) {
        node = ((SimpleType) selectedNode).getName();
    } else if (selectedNode instanceof NameQualifiedType) {
        node = ((NameQualifiedType) selectedNode).getName();
    } else if (selectedNode instanceof ArrayType) {
        Type elementType = ((ArrayType) selectedNode).getElementType();
        if (elementType.isSimpleType()) {
            node = ((SimpleType) elementType).getName();
        } else if (elementType.isNameQualifiedType()) {
            node = ((NameQualifiedType) elementType).getName();
        } else {
            return;
        }
    } else if (selectedNode instanceof Name) {
        node = (Name) selectedNode;
    } else {
        return;
    }
    // change to similar type proposals
    addSimilarTypeProposals(kind, cu, node, IProposalRelevance.SIMILAR_TYPE, proposals);
    while (node.getParent() instanceof QualifiedName) {
        node = (Name) node.getParent();
    }
    if (selectedNode != node) {
        kind = evauateTypeKind(node, cu.getJavaProject());
    }
    if ((kind & (TypeKinds.CLASSES | TypeKinds.INTERFACES)) != 0) {
        // only propose annotations when there are no other suggestions
        kind &= ~TypeKinds.ANNOTATIONS;
    }
    addNewTypeProposals(cu, node, kind, IProposalRelevance.NEW_TYPE, proposals);
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NodeFinder(org.eclipse.jdt.core.dom.NodeFinder) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 37 with QualifiedName

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

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

the class RefactorProcessor method getAddStaticImportProposals.

/**
 * Create static import proposal, which converts invocations to static import.
 *
 * @param context
 *            the invocation context
 * @param node
 *            the node to work on
 * @param proposals
 *            the receiver of proposals, may be {@code null}
 * @return {@code true} if the operation could or has been performed,
 *         {@code false otherwise}
 */
private static boolean getAddStaticImportProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
    if (!(node instanceof SimpleName)) {
        return false;
    }
    final SimpleName name = (SimpleName) node;
    final IBinding binding;
    final ITypeBinding declaringClass;
    if (name.getParent() instanceof MethodInvocation) {
        MethodInvocation mi = (MethodInvocation) name.getParent();
        Expression expression = mi.getExpression();
        if (expression == null || expression.equals(name)) {
            return false;
        }
        binding = mi.resolveMethodBinding();
        if (binding == null) {
            return false;
        }
        declaringClass = ((IMethodBinding) binding).getDeclaringClass();
    } else if (name.getParent() instanceof QualifiedName) {
        QualifiedName qn = (QualifiedName) name.getParent();
        if (name.equals(qn.getQualifier()) || qn.getParent() instanceof ImportDeclaration) {
            return false;
        }
        binding = qn.resolveBinding();
        if (!(binding instanceof IVariableBinding)) {
            return false;
        }
        declaringClass = ((IVariableBinding) binding).getDeclaringClass();
    } else {
        return false;
    }
    if (!Modifier.isStatic(binding.getModifiers())) {
        // only work with static bindings
        return false;
    }
    boolean needImport = false;
    if (!isDirectlyAccessible(name, declaringClass)) {
        if (Modifier.isPrivate(declaringClass.getModifiers())) {
            return false;
        }
        needImport = true;
    }
    if (proposals == null) {
        // return early, just testing if we could do it
        return true;
    }
    try {
        ImportRewrite importRewrite = StubUtility.createImportRewrite(context.getCompilationUnit(), true);
        ASTRewrite astRewrite = ASTRewrite.create(node.getAST());
        ASTRewrite astRewriteReplaceAllOccurrences = ASTRewrite.create(node.getAST());
        ImportRemover remover = new ImportRemover(context.getCompilationUnit().getJavaProject(), context.getASTRoot());
        ImportRemover removerAllOccurences = new ImportRemover(context.getCompilationUnit().getJavaProject(), context.getASTRoot());
        MethodInvocation mi = null;
        QualifiedName qn = null;
        if (name.getParent() instanceof MethodInvocation) {
            mi = (MethodInvocation) name.getParent();
            // convert the method invocation
            astRewrite.remove(mi.getExpression(), null);
            remover.registerRemovedNode(mi.getExpression());
            removerAllOccurences.registerRemovedNode(mi.getExpression());
            mi.typeArguments().forEach(typeObject -> {
                Type type = (Type) typeObject;
                astRewrite.remove(type, null);
                remover.registerRemovedNode(type);
                removerAllOccurences.registerRemovedNode(type);
            });
        } else if (name.getParent() instanceof QualifiedName) {
            qn = (QualifiedName) name.getParent();
            // convert the field access
            astRewrite.replace(qn, ASTNodeFactory.newName(node.getAST(), name.getFullyQualifiedName()), null);
            remover.registerRemovedNode(qn);
            removerAllOccurences.registerRemovedNode(qn);
        } else {
            return false;
        }
        MethodInvocation miFinal = mi;
        name.getRoot().accept(new ASTVisitor() {

            @Override
            public boolean visit(MethodInvocation methodInvocation) {
                Expression methodInvocationExpression = methodInvocation.getExpression();
                if (methodInvocationExpression == null) {
                    return super.visit(methodInvocation);
                }
                if (methodInvocationExpression instanceof Name) {
                    String fullyQualifiedName = ((Name) methodInvocationExpression).getFullyQualifiedName();
                    if (miFinal != null && miFinal.getExpression() instanceof Name && ((Name) miFinal.getExpression()).getFullyQualifiedName().equals(fullyQualifiedName) && miFinal.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
                        methodInvocation.typeArguments().forEach(type -> {
                            astRewriteReplaceAllOccurrences.remove((Type) type, null);
                            removerAllOccurences.registerRemovedNode((Type) type);
                        });
                        astRewriteReplaceAllOccurrences.remove(methodInvocationExpression, null);
                        removerAllOccurences.registerRemovedNode(methodInvocationExpression);
                    }
                }
                return super.visit(methodInvocation);
            }
        });
        QualifiedName qnFinal = qn;
        name.getRoot().accept(new ASTVisitor() {

            @Override
            public boolean visit(QualifiedName qualifiedName) {
                if (qnFinal != null && qualifiedName.getFullyQualifiedName().equals(qnFinal.getFullyQualifiedName())) {
                    astRewriteReplaceAllOccurrences.replace(qualifiedName, ASTNodeFactory.newName(node.getAST(), name.getFullyQualifiedName()), null);
                    removerAllOccurences.registerRemovedNode(qualifiedName);
                }
                return super.visit(qualifiedName);
            }
        });
        if (needImport) {
            importRewrite.addStaticImport(binding);
        }
        ASTRewriteRemoveImportsCorrectionProposal proposal = new ASTRewriteRemoveImportsCorrectionProposal(CorrectionMessages.QuickAssistProcessor_convert_to_static_import, CodeActionKind.Refactor, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT);
        proposal.setImportRewrite(importRewrite);
        proposal.setImportRemover(remover);
        proposals.add(proposal);
        ASTRewriteRemoveImportsCorrectionProposal proposalReplaceAllOccurrences = new ASTRewriteRemoveImportsCorrectionProposal(CorrectionMessages.QuickAssistProcessor_convert_to_static_import_replace_all, CodeActionKind.Refactor, context.getCompilationUnit(), astRewriteReplaceAllOccurrences, IProposalRelevance.ADD_STATIC_IMPORT);
        proposalReplaceAllOccurrences.setImportRewrite(importRewrite);
        proposalReplaceAllOccurrences.setImportRemover(removerAllOccurences);
        proposals.add(proposalReplaceAllOccurrences);
    } catch (IllegalArgumentException e) {
        // Wrong use of ASTRewrite or ImportRewrite API, see bug 541586
        JavaLanguageServerPlugin.logException("Failed to get static import proposal", e);
        return false;
    } catch (JavaModelException e) {
        return false;
    }
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Dimension(org.eclipse.jdt.core.dom.Dimension) RefactoringAvailabilityTesterCore(org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore) Arrays(java.util.Arrays) CleanUpRequirementsCore(org.eclipse.jdt.core.manipulation.CleanUpRequirementsCore) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer) CoreException(org.eclipse.core.runtime.CoreException) MultiFixMessages(org.eclipse.jdt.internal.ui.fix.MultiFixMessages) InlineConstantRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring) StubUtility(org.eclipse.jdt.internal.core.manipulation.StubUtility) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) InlineTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring) Expression(org.eclipse.jdt.core.dom.Expression) TypeChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal) Map(java.util.Map) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Bindings(org.eclipse.jdt.internal.corext.dom.Bindings) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) ASTNodes(org.eclipse.jdt.internal.corext.dom.ASTNodes) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProposalRelevance(org.eclipse.jdt.ls.core.internal.corrections.proposals.IProposalRelevance) ConvertLoopFixCore(org.eclipse.jdt.internal.corext.fix.ConvertLoopFixCore) IJavaProject(org.eclipse.jdt.core.IJavaProject) ASTRewriteRemoveImportsCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ASTRewriteRemoveImportsCorrectionProposal) Messages(org.eclipse.jdt.internal.corext.util.Messages) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Collection(java.util.Collection) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) IBinding(org.eclipse.jdt.core.dom.IBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) JavaModelUtil(org.eclipse.jdt.internal.corext.util.JavaModelUtil) AbstractCleanUpCore(org.eclipse.jdt.internal.ui.fix.AbstractCleanUpCore) IJavaModelMarker(org.eclipse.jdt.core.IJavaModelMarker) IType(org.eclipse.jdt.core.IType) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) CleanUpOptionsCore(org.eclipse.jdt.core.manipulation.CleanUpOptionsCore) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) List(java.util.List) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) CleanUpConstants(org.eclipse.jdt.internal.corext.fix.CleanUpConstants) ImportRemover(org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ICleanUpFixCore(org.eclipse.jdt.core.manipulation.ICleanUpFixCore) RefactoringCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal) IField(org.eclipse.jdt.core.IField) LinkedProposalModelCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore) JavaModelException(org.eclipse.jdt.core.JavaModelException) ConvertAnonymousToNestedRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring) MethodReference(org.eclipse.jdt.core.dom.MethodReference) HashMap(java.util.HashMap) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) InlineMethodRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring) ArrayList(java.util.ArrayList) Type(org.eclipse.jdt.core.dom.Type) RefactorProposalUtility(org.eclipse.jdt.ls.core.internal.text.correction.RefactorProposalUtility) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Name(org.eclipse.jdt.core.dom.Name) LambdaExpressionsCleanUpCore(org.eclipse.jdt.internal.ui.fix.LambdaExpressionsCleanUpCore) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) ASTNodeFactory(org.eclipse.jdt.internal.corext.dom.ASTNodeFactory) Modifier(org.eclipse.jdt.core.dom.Modifier) JavaCore(org.eclipse.jdt.core.JavaCore) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ForStatement(org.eclipse.jdt.core.dom.ForStatement) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CleanUpContextCore(org.eclipse.jdt.core.manipulation.CleanUpContextCore) ActionMessages(org.eclipse.jdt.ls.core.internal.text.correction.ActionMessages) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) CUCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal) IJavaElement(org.eclipse.jdt.core.IJavaElement) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICleanUpCore(org.eclipse.jdt.internal.corext.fix.ICleanUpCore) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) IMethod(org.eclipse.jdt.core.IMethod) LambdaExpressionsFixCore(org.eclipse.jdt.internal.corext.fix.LambdaExpressionsFixCore) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Collections(java.util.Collections) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ASTRewriteRemoveImportsCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ASTRewriteRemoveImportsCorrectionProposal) ImportRemover(org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover)

Example 39 with QualifiedName

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

the class SemanticTokensVisitor method visitSimpleNameOfType.

/**
 * Helper method to find and visit the simple name of a {@code Type} node
 * using the specified {@link NodeVisitor}.
 *
 * @param type A type node (may be {@code null}).
 * @param visitor The {@link NodeVisitor} to use.
 */
private void visitSimpleNameOfType(Type type, NodeVisitor<SimpleName> visitor) {
    if (type == null) {
        return;
    } else if (type instanceof SimpleType) {
        SimpleType simpleType = (SimpleType) type;
        acceptNodeList(simpleType.annotations());
        Name simpleTypeName = simpleType.getName();
        if (simpleTypeName instanceof SimpleName) {
            visitor.visit((SimpleName) simpleTypeName);
        } else {
            QualifiedName qualifiedName = (QualifiedName) simpleTypeName;
            qualifiedName.getQualifier().accept(this);
            visitor.visit(qualifiedName.getName());
        }
    } else if (type instanceof QualifiedType) {
        QualifiedType qualifiedType = (QualifiedType) type;
        qualifiedType.getQualifier().accept(this);
        acceptNodeList(qualifiedType.annotations());
        visitor.visit(qualifiedType.getName());
    } else if (type instanceof NameQualifiedType) {
        NameQualifiedType nameQualifiedType = (NameQualifiedType) type;
        nameQualifiedType.getQualifier().accept(this);
        acceptNodeList(nameQualifiedType.annotations());
        visitor.visit(nameQualifiedType.getName());
    } else if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        visitSimpleNameOfType(parameterizedType.getType(), visitor);
        visitNodeList(parameterizedType.typeArguments(), this::typeArgumentVisitor);
    } else {
        type.accept(this);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 40 with QualifiedName

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

the class NewVariableCorrectionProposal method evaluateFieldModifiers.

private int evaluateFieldModifiers(ASTNode newTypeDecl) {
    if (fSenderBinding.isAnnotation()) {
        return 0;
    }
    if (fSenderBinding.isInterface()) {
        // for interface members copy the modifiers from an existing field
        FieldDeclaration[] fieldDecls = ((TypeDeclaration) newTypeDecl).getFields();
        if (fieldDecls.length > 0) {
            return fieldDecls[0].getModifiers();
        }
        return 0;
    }
    int modifiers = 0;
    if (fVariableKind == CONST_FIELD) {
        modifiers |= Modifier.FINAL | Modifier.STATIC;
    } else {
        ASTNode parent = fOriginalNode.getParent();
        if (parent instanceof QualifiedName) {
            IBinding qualifierBinding = ((QualifiedName) parent).getQualifier().resolveBinding();
            if (qualifierBinding instanceof ITypeBinding) {
                modifiers |= Modifier.STATIC;
            }
        } else if (ASTResolving.isInStaticContext(fOriginalNode)) {
            modifiers |= Modifier.STATIC;
        }
    }
    ASTNode node = ASTResolving.findParentType(fOriginalNode, true);
    if (newTypeDecl.equals(node)) {
        modifiers |= Modifier.PRIVATE;
    } else if (node instanceof AnonymousClassDeclaration) {
        modifiers |= Modifier.PROTECTED;
    } else {
        modifiers |= Modifier.PUBLIC;
    }
    return modifiers;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)68 SimpleName (org.eclipse.jdt.core.dom.SimpleName)47 ASTNode (org.eclipse.jdt.core.dom.ASTNode)36 Name (org.eclipse.jdt.core.dom.Name)26 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)22 Expression (org.eclipse.jdt.core.dom.Expression)20 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)18 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)17 IBinding (org.eclipse.jdt.core.dom.IBinding)16 SimpleType (org.eclipse.jdt.core.dom.SimpleType)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 ArrayList (java.util.ArrayList)13 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)12 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)10 Type (org.eclipse.jdt.core.dom.Type)10 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)9 IType (org.eclipse.jdt.core.IType)8 Assignment (org.eclipse.jdt.core.dom.Assignment)8