|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Broadcom |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.vscode.boot.java.reconcilers; |
| 12 | + |
| 13 | +import static org.springframework.ide.vscode.commons.java.SpringProjectUtil.springBootVersionGreaterOrEqual; |
| 14 | + |
| 15 | +import java.net.URI; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.jdt.core.dom.ASTVisitor; |
| 20 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 21 | +import org.eclipse.jdt.core.dom.IAnnotationBinding; |
| 22 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 23 | +import org.eclipse.jdt.core.dom.MemberValuePair; |
| 24 | +import org.eclipse.jdt.core.dom.NormalAnnotation; |
| 25 | +import org.eclipse.jdt.core.dom.TypeDeclaration; |
| 26 | +import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; |
| 27 | +import org.springframework.ide.vscode.boot.java.Annotations; |
| 28 | +import org.springframework.ide.vscode.boot.java.Boot4JavaProblemType; |
| 29 | +import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies; |
| 30 | +import org.springframework.ide.vscode.boot.java.requestmapping.WebConfigIndexElement; |
| 31 | +import org.springframework.ide.vscode.boot.java.requestmapping.WebConfigIndexer; |
| 32 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 33 | +import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType; |
| 34 | +import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblemImpl; |
| 35 | +import org.springframework.ide.vscode.commons.protocol.spring.Bean; |
| 36 | +import org.springframework.ide.vscode.commons.util.UriUtil; |
| 37 | + |
| 38 | +public class WebApiVersioningReconciler implements JdtAstReconciler { |
| 39 | + |
| 40 | + private static final String PROBLEM_LABEL = "API versioning not configured anywhere"; |
| 41 | + |
| 42 | + private final SpringMetamodelIndex springIndex; |
| 43 | + |
| 44 | + public WebApiVersioningReconciler(SpringMetamodelIndex springIndex) { |
| 45 | + this.springIndex = springIndex; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isApplicable(IJavaProject project) { |
| 50 | + return springBootVersionGreaterOrEqual(4, 0, 0).test(project); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public ProblemType getProblemType() { |
| 55 | + return Boot4JavaProblemType.API_VERSIONING_NOT_CONFIGURED; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ASTVisitor createVisitor(IJavaProject project, URI docUri, CompilationUnit cu, ReconcilingContext context) { |
| 60 | + AnnotationHierarchies annotationHierarchies = AnnotationHierarchies.get(cu); |
| 61 | + |
| 62 | + return new ASTVisitor() { |
| 63 | + |
| 64 | + /** |
| 65 | + * this in the piece of the reconciler that validates the version attribute of annotations on controllers |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public boolean visit(NormalAnnotation annotation) { |
| 69 | + IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding(); |
| 70 | + if (annotationBinding == null) return super.visit(annotation); |
| 71 | + |
| 72 | + ITypeBinding annotationType = annotationBinding.getAnnotationType(); |
| 73 | + if (annotationType == null) return super.visit(annotation); |
| 74 | + |
| 75 | + boolean isWebController = annotationHierarchies.isAnnotatedWith(annotationType, Annotations.SPRING_REQUEST_MAPPING); |
| 76 | + if (!isWebController) return super.visit(annotation); |
| 77 | + |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + List<MemberValuePair> attributes = annotation.values(); |
| 80 | + attributes.stream() |
| 81 | + .filter(pair -> "version".equals(pair.getName().toString())) |
| 82 | + .forEach(pair -> { |
| 83 | + if (!context.isIndexComplete()) { |
| 84 | + throw new RequiredCompleteIndexException(); |
| 85 | + } |
| 86 | + |
| 87 | + if (!isApiVersioningConfigured(project)) { |
| 88 | + ReconcileProblemImpl problem = new ReconcileProblemImpl(getProblemType(), PROBLEM_LABEL, |
| 89 | + pair.getStartPosition(), pair.getLength()); |
| 90 | + |
| 91 | + context.getProblemCollector().accept(problem); |
| 92 | + } |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + return super.visit(annotation); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * this is the piece of the reconciler that looks for changes to web configs and then marks all the potentially affected |
| 101 | + * controller classes for re-indexing |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public boolean visit(TypeDeclaration type) { |
| 105 | + if (WebConfigIndexer.getWebConfig(type) == null) { |
| 106 | + return super.visit(type); |
| 107 | + } |
| 108 | + |
| 109 | + Arrays.stream(springIndex.getBeansOfProject(project.getElementName())) |
| 110 | + .filter(bean -> isAnnotatedWith(bean, Annotations.CONTROLLER)) |
| 111 | + .map(bean -> UriUtil.toFileString(bean.getLocation().getUri())) |
| 112 | + .forEach(file -> context.markForAffetcedFilesIndexing(file)); |
| 113 | + |
| 114 | + return super.visit(type); |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + private boolean isApiVersioningConfigured(IJavaProject project) { |
| 120 | + List<WebConfigIndexElement> webConfigs = springIndex.getNodesOfType(project.getElementName(), WebConfigIndexElement.class); |
| 121 | + for (WebConfigIndexElement webConfig : webConfigs) { |
| 122 | + if (webConfig.getVersionSupportStrategies() != null && webConfig.getVersionSupportStrategies().size() > 0) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + private boolean isAnnotatedWith(Bean bean, String annotationType) { |
| 131 | + return Arrays.stream(bean.getAnnotations()) |
| 132 | + .filter(annotation -> annotation.getAnnotationType().equals(annotationType)) |
| 133 | + .findAny() |
| 134 | + .isPresent(); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments