Search in sources :

Example 46 with Annotation

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

the class RequestMappingSymbolProvider method getSymbols.

@Override
public Collection<EnhancedSymbolInformation> getSymbols(Annotation node, ITypeBinding annotationType, Collection<ITypeBinding> metaAnnotations, TextDocument doc) {
    if (node.getParent() instanceof MethodDeclaration) {
        try {
            Location location = new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength()));
            String[] path = getPath(node);
            String[] parentPath = getParentPath(node);
            String[] methods = getMethod(node);
            String[] contentTypes = getContentTypes(node);
            String[] acceptTypes = getAcceptTypes(node);
            return (parentPath == null ? Stream.of("") : Arrays.stream(parentPath)).filter(Objects::nonNull).flatMap(parent -> (path == null ? Stream.<String>empty() : Arrays.stream(path)).filter(Objects::nonNull).map(p -> {
                String separator = !parent.endsWith("/") && !p.startsWith("/") ? "/" : "";
                String resultPath = parent + separator + p;
                if (resultPath.endsWith("/")) {
                    resultPath = resultPath.substring(0, resultPath.length() - 1);
                }
                return resultPath.startsWith("/") ? resultPath : "/" + resultPath;
            })).map(p -> RouteUtils.createRouteSymbol(location, p, methods, contentTypes, acceptTypes, null)).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Arrays(java.util.Arrays) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Iterator(java.util.Iterator) Collection(java.util.Collection) SymbolProvider(org.springframework.ide.vscode.boot.java.handlers.SymbolProvider) Annotations(org.springframework.ide.vscode.boot.java.Annotations) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Collectors(java.util.stream.Collectors) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTUtils(org.springframework.ide.vscode.boot.java.utils.ASTUtils) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Expression(org.eclipse.jdt.core.dom.Expression) Annotation(org.eclipse.jdt.core.dom.Annotation) Location(org.eclipse.lsp4j.Location) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Objects(java.util.Objects) Location(org.eclipse.lsp4j.Location)

Example 47 with Annotation

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

the class RequestMappingSymbolProvider method getParentAnnotation.

private Annotation getParentAnnotation(Annotation node) {
    ASTNode parent = node.getParent() != null ? node.getParent().getParent() : null;
    while (parent != null && !(parent instanceof TypeDeclaration)) {
        parent = parent.getParent();
    }
    if (parent != null) {
        TypeDeclaration type = (TypeDeclaration) parent;
        List<?> modifiers = type.modifiers();
        Iterator<?> iterator = modifiers.iterator();
        while (iterator.hasNext()) {
            Object modifier = iterator.next();
            if (modifier instanceof Annotation) {
                Annotation annotation = (Annotation) modifier;
                ITypeBinding resolvedType = annotation.resolveTypeBinding();
                String annotationType = resolvedType.getQualifiedName();
                if (annotationType != null && Annotations.SPRING_REQUEST_MAPPING.equals(annotationType)) {
                    return annotation;
                }
            }
        }
    }
    return null;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation)

Example 48 with Annotation

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

the class RequestMappingSymbolProvider method getMethod.

private String[] getMethod(Annotation node) {
    String[] methods = null;
    if (node.isNormalAnnotation()) {
        NormalAnnotation normNode = (NormalAnnotation) node;
        List<?> values = normNode.values();
        for (Iterator<?> iterator = values.iterator(); iterator.hasNext(); ) {
            Object object = iterator.next();
            if (object instanceof MemberValuePair) {
                MemberValuePair pair = (MemberValuePair) object;
                String valueName = pair.getName().getIdentifier();
                if (valueName != null && valueName.equals("method")) {
                    Expression expression = pair.getValue();
                    methods = ASTUtils.getExpressionValueAsArray(expression);
                    break;
                }
            }
        }
    } else if (node instanceof SingleMemberAnnotation) {
        methods = getRequestMethod((SingleMemberAnnotation) node);
    }
    if (methods == null && node.getParent() instanceof MethodDeclaration) {
        Annotation parentAnnotation = getParentAnnotation(node);
        if (parentAnnotation != null) {
            methods = getMethod(parentAnnotation);
        }
    }
    return methods;
}
Also used : MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Expression(org.eclipse.jdt.core.dom.Expression) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation)

Example 49 with Annotation

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

the class BootJavaReferencesHandler method provideReferencesForAnnotation.

private List<? extends Location> provideReferencesForAnnotation(ASTNode node, int offset, TextDocument doc) {
    Annotation annotation = null;
    while (node != null && !(node instanceof Annotation)) {
        node = node.getParent();
    }
    if (node != null) {
        annotation = (Annotation) node;
        ITypeBinding type = annotation.resolveTypeBinding();
        if (type != null) {
            String qualifiedName = type.getQualifiedName();
            if (qualifiedName != null) {
                ReferenceProvider provider = this.referenceProviders.get(qualifiedName);
                if (provider != null) {
                    return provider.provideReferences(node, annotation, type, offset, doc);
                }
            }
        }
    }
    return null;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 50 with Annotation

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

the class ValueCompletionProcessor method provideCompletions.

@Override
public Collection<ICompletionProposal> provideCompletions(ASTNode node, Annotation annotation, ITypeBinding type, int offset, IDocument doc) {
    List<ICompletionProposal> result = new ArrayList<>();
    try {
        FuzzyMap<PropertyInfo> index = indexProvider.getIndex(doc);
        // case: @Value(<*>)
        if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
            List<Match<PropertyInfo>> matches = findMatches("", index);
            for (Match<PropertyInfo> match : matches) {
                DocumentEdits edits = new DocumentEdits(doc);
                edits.replace(offset, offset, "\"${" + match.data.getId() + "}\"");
                ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match.data.getId(), match.data.getName(), null);
                result.add(proposal);
            }
        } else // case: @Value(prefix<*>)
        if (node instanceof SimpleName && node.getParent() instanceof Annotation) {
            computeProposalsForSimpleName(node, result, offset, doc, index);
        } else // case: @Value(value=<*>)
        if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
            computeProposalsForSimpleName(node, result, offset, doc, index);
        } else // case: @Value("prefix<*>")
        if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
            if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                computeProposalsForStringLiteral(node, result, offset, doc, index);
            }
        } else // case: @Value(value="prefix<*>")
        if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
            if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                computeProposalsForStringLiteral(node, result, offset, doc, index);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Match(org.springframework.ide.vscode.commons.util.FuzzyMap.Match) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Aggregations

Annotation (org.eclipse.jdt.core.dom.Annotation)56 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)19 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)14 SingleMemberAnnotation (org.eclipse.jdt.core.dom.SingleMemberAnnotation)14 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)13 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 ArrayList (java.util.ArrayList)10 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 List (java.util.List)7 AST (org.eclipse.jdt.core.dom.AST)7 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)6 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)5 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)5 Name (org.eclipse.jdt.core.dom.Name)5 Type (org.eclipse.jdt.core.dom.Type)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4