Search in sources :

Example 61 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project sts4 by spring-projects.

the class WebfluxRouterSymbolProvider method extractMethods.

private WebfluxRouteElement[] extractMethods(MethodInvocation routerInvocation, TextDocument doc) {
    WebfluxMethodFinder methodFinder = new WebfluxMethodFinder(routerInvocation, doc);
    List<?> arguments = routerInvocation.arguments();
    for (Object argument : arguments) {
        if (argument != null && argument instanceof ASTNode) {
            ((ASTNode) argument).accept(methodFinder);
        }
    }
    final List<WebfluxRouteElement> methods = methodFinder.getMethods();
    extractNestedValue(routerInvocation, methods, (methodInvocation) -> {
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        String methodName = methodBinding.getName();
        try {
            if (WebfluxUtils.REQUEST_PREDICATE_METHOD_METHOD.equals(methodName)) {
                QualifiedName qualifiedName = WebfluxUtils.extractQualifiedNameArgument(methodInvocation);
                if (qualifiedName.getName() != null) {
                    Range range = doc.toRange(qualifiedName.getStartPosition(), qualifiedName.getLength());
                    return new WebfluxRouteElement(qualifiedName.getName().toString(), range);
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        return null;
    });
    return (WebfluxRouteElement[]) methods.toArray(new WebfluxRouteElement[methods.size()]);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 62 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project jbosstools-hibernate by jbosstools.

the class ProcessEntityInfo method addImport.

public boolean addImport(CompilationUnit node, String importDeclaration) {
    // $NON-NLS-1$
    String[] importDeclarations = importDeclaration.split("\\.");
    if (importDeclarations.length <= 1) {
        return false;
    }
    ImportDeclaration importDecl = rewriter.getAST().newImportDeclaration();
    SimpleName simpleName0 = rewriter.getAST().newSimpleName(importDeclarations[0]);
    SimpleName simpleName1 = rewriter.getAST().newSimpleName(importDeclarations[1]);
    QualifiedName qualifiedName0 = rewriter.getAST().newQualifiedName(simpleName0, simpleName1);
    for (int i = 2; i < importDeclarations.length; i++) {
        SimpleName simpleNameI = rewriter.getAST().newSimpleName(importDeclarations[i]);
        qualifiedName0 = rewriter.getAST().newQualifiedName(qualifiedName0, simpleNameI);
    }
    importDecl.setName(qualifiedName0);
    ListRewrite lrw = rewriter.getListRewrite(node, CompilationUnit.IMPORTS_PROPERTY);
    // insert import declaration in the proper place
    // prefer alphabetic order and package separation
    Iterator<?> it = lrw.getRewrittenList().iterator();
    ASTNode insertBeforeNode = null;
    for (; it.hasNext(); ) {
        Object obj = it.next();
        if (!(obj instanceof ImportDeclaration)) {
            continue;
        }
        ImportDeclaration id = (ImportDeclaration) obj;
        String idName = id.getName().getFullyQualifiedName();
        if (idName.compareTo(importDeclaration) > 0) {
            insertBeforeNode = id;
            break;
        }
    }
    if (insertBeforeNode == null) {
        lrw.insertLast(importDecl, null);
    } else {
        lrw.insertBefore(importDecl, insertBeforeNode, null);
    }
    return true;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 63 with QualifiedName

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

the class UnresolvedElementsSubProcessor method addNewTypeProposals.

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    Name node = refNode;
    do {
        String typeName = ASTNodes.getSimpleNameIdentifier(node);
        Name qualifier = null;
        // only propose to create types for qualifiers when the name starts with upper case
        boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
        if (isPossibleName) {
            IPackageFragment enclosingPackage = null;
            IType enclosingType = null;
            if (node.isSimpleName()) {
                enclosingPackage = (IPackageFragment) cu.getParent();
            // don't suggest member type, user can select it in wizard
            } else {
                Name qualifierName = ((QualifiedName) node).getQualifier();
                IBinding binding = qualifierName.resolveBinding();
                if (binding != null && binding.isRecovered()) {
                    binding = null;
                }
                if (binding instanceof ITypeBinding) {
                    enclosingType = (IType) binding.getJavaElement();
                } else if (binding instanceof IPackageBinding) {
                    qualifier = qualifierName;
                    enclosingPackage = (IPackageFragment) binding.getJavaElement();
                } else {
                    IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                    if (res != null && res.length > 0 && res[0] instanceof IType) {
                        enclosingType = (IType) res[0];
                    } else {
                        qualifier = qualifierName;
                        enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
                    }
                }
            }
            int rel = relevance;
            if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
                rel += 3;
            }
            if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
                // new member type
                IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
                if ((kind & TypeKinds.CLASSES) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_CLASS, enclosing, rel + 3));
                }
                if ((kind & TypeKinds.INTERFACES) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_INTERFACE, enclosing, rel + 2));
                }
                if ((kind & TypeKinds.ENUMS) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_ENUM, enclosing, rel));
                }
                if ((kind & TypeKinds.ANNOTATIONS) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_ANNOTATION, enclosing, rel + 1));
                // TODO: addNullityAnnotationTypesProposals(cu, node, proposals);
                }
            }
        }
        node = qualifier;
    } while (node != null);
    // type parameter proposals
    if (refNode.isSimpleName() && (kind & TypeKinds.VARIABLES) != 0) {
        CompilationUnit root = (CompilationUnit) refNode.getRoot();
        String name = ((SimpleName) refNode).getIdentifier();
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
        int baseRel = relevance;
        if (isLikelyTypeParameterName(name)) {
            baseRel += 8;
        }
        while (declaration != null) {
            IBinding binding = null;
            int rel = baseRel;
            if (declaration instanceof MethodDeclaration) {
                binding = ((MethodDeclaration) declaration).resolveBinding();
                if (isLikelyMethodTypeParameterName(name)) {
                    rel += 2;
                }
            } else if (declaration instanceof TypeDeclaration) {
                binding = ((TypeDeclaration) declaration).resolveBinding();
                rel++;
            }
            if (binding != null) {
                AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
                proposals.add(proposal);
            }
            if (!Modifier.isStatic(declaration.getModifiers())) {
                declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
            } else {
                declaration = null;
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) 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) IType(org.eclipse.jdt.core.IType) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 64 with QualifiedName

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

the class ExtractMethodAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    RefactoringStatus status = getStatus();
    superCall: {
        if (status.hasFatalError()) {
            break superCall;
        }
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
                Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
                    break superCall;
                }
            }
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
            break superCall;
        }
        fEnclosingBodyDeclaration = ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
        if (fEnclosingBodyDeclaration == null || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
            break superCall;
        } else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
            break superCall;
        } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
            fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
        }
        if (!isSingleExpressionOrStatementSet()) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
            break superCall;
        }
        if (isExpressionSelected()) {
            ASTNode expression = getFirstSelectedNode();
            if (expression instanceof Name) {
                Name name = (Name) expression;
                if (name.resolveBinding() instanceof ITypeBinding) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IMethodBinding) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IVariableBinding) {
                    StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
                    boolean isPartOfQualifiedName = false;
                    boolean isPartOfQualifier = false;
                    if (locationInParent == QualifiedName.NAME_PROPERTY) {
                        isPartOfQualifiedName = true;
                        QualifiedName qualifiedName = (QualifiedName) name.getParent();
                        QualifiedName currParent = qualifiedName;
                        while (true) {
                            ASTNode parent = currParent.getParent();
                            if (parent instanceof QualifiedName) {
                                currParent = (QualifiedName) parent;
                            } else {
                                break;
                            }
                        }
                        if (!qualifiedName.equals(currParent)) {
                            isPartOfQualifier = true;
                        }
                    }
                    if ((isPartOfQualifiedName && !isPartOfQualifier) || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression))) {
                        status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
                        break superCall;
                    }
                }
                if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
                    break superCall;
                }
            }
            fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
        }
        status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
        computeLastStatementSelected();
    }
    super.endVisit(node);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Message(org.eclipse.jdt.core.dom.Message) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 65 with QualifiedName

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

the class SimilarElementsRequestor method findSimilarElement.

public static SimilarElement[] findSimilarElement(ICompilationUnit cu, Name name, int kind) throws JavaModelException {
    int pos = name.getStartPosition();
    int nArguments = -1;
    String identifier = ASTNodes.getSimpleNameIdentifier(name);
    String returnType = null;
    ICompilationUnit preparedCU = null;
    try {
        if (name.isQualifiedName()) {
            pos = ((QualifiedName) name).getName().getStartPosition();
        } else {
            // first letter must be included, other
            pos = name.getStartPosition() + 1;
        }
        Javadoc javadoc = (Javadoc) ASTNodes.getParent(name, ASTNode.JAVADOC);
        if (javadoc != null) {
            preparedCU = createPreparedCU(cu, javadoc, name.getStartPosition());
            cu = preparedCU;
        }
        SimilarElementsRequestor requestor = new SimilarElementsRequestor(identifier, kind, nArguments, returnType, !isTestSource(cu));
        requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, true);
        requestor.setIgnored(CompletionProposal.KEYWORD, true);
        requestor.setIgnored(CompletionProposal.LABEL_REF, true);
        requestor.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.PACKAGE_REF, true);
        requestor.setIgnored(CompletionProposal.MODULE_REF, true);
        requestor.setIgnored(CompletionProposal.MODULE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_REF, true);
        requestor.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, true);
        requestor.setIgnored(CompletionProposal.FIELD_REF, true);
        requestor.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, true);
        requestor.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
        return requestor.process(cu, pos);
    } finally {
        if (preparedCU != null) {
            preparedCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Javadoc(org.eclipse.jdt.core.dom.Javadoc)

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