|
| 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.Set; |
| 17 | + |
| 18 | +import org.eclipse.jdt.core.dom.ASTVisitor; |
| 19 | +import org.eclipse.jdt.core.dom.Annotation; |
| 20 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 21 | +import org.eclipse.jdt.core.dom.FieldDeclaration; |
| 22 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 23 | +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
| 24 | +import org.springframework.ide.vscode.boot.java.Annotations; |
| 25 | +import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType; |
| 26 | +import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies; |
| 27 | +import org.springframework.ide.vscode.boot.java.utils.ASTUtils; |
| 28 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 29 | +import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType; |
| 30 | +import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblemImpl; |
| 31 | + |
| 32 | +/** |
| 33 | + * Validates that @Value annotations with "classpath:" or "classpath*:" prefixes |
| 34 | + * are used with compatible types that can receive Spring Resource objects. |
| 35 | + * |
| 36 | + * Valid types for classpath resources include: |
| 37 | + * - org.springframework.core.io.Resource (and Resource[]) |
| 38 | + * - java.io.InputStream (and InputStream[]) |
| 39 | + * - java.io.File (and File[]) |
| 40 | + * - java.net.URL (and URL[]) |
| 41 | + * - java.nio.file.Path (and Path[]) |
| 42 | + * |
| 43 | + * Note: Array types of any valid resource type are also accepted. |
| 44 | + * |
| 45 | + * @author Martin Lippert |
| 46 | + */ |
| 47 | +public class ClasspathResourceTypeReconciler implements JdtAstReconciler { |
| 48 | + |
| 49 | + // Valid types for classpath resource injection |
| 50 | + private static final Set<String> VALID_RESOURCE_TYPES = Set.of( |
| 51 | + "org.springframework.core.io.Resource", |
| 52 | + "org.springframework.core.io.Resource[]", |
| 53 | + "java.io.InputStream", |
| 54 | + "java.io.File", |
| 55 | + "java.net.URL", |
| 56 | + "java.nio.file.Path" |
| 57 | + ); |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isApplicable(IJavaProject project) { |
| 61 | + // Apply to all Spring Boot 2.0+ projects |
| 62 | + return springBootVersionGreaterOrEqual(2, 0, 0).test(project); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public ProblemType getProblemType() { |
| 67 | + return Boot2JavaProblemType.VALUE_CLASSPATH_RESOURCE_TYPE; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public ASTVisitor createVisitor(IJavaProject project, URI docUri, CompilationUnit cu, ReconcilingContext context) { |
| 72 | + AnnotationHierarchies annotationHierarchies = AnnotationHierarchies.get(cu); |
| 73 | + |
| 74 | + return new ASTVisitor() { |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean visit(FieldDeclaration fieldDecl) { |
| 78 | + // Check annotations on the field |
| 79 | + for (Annotation annotation : ASTUtils.getAnnotations(fieldDecl)) { |
| 80 | + checkValueAnnotation(annotation, annotationHierarchies, fieldDecl.getType().resolveBinding(), context); |
| 81 | + } |
| 82 | + return super.visit(fieldDecl); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean visit(SingleVariableDeclaration param) { |
| 87 | + ITypeBinding paramType = param.getType().resolveBinding(); |
| 88 | + for (Annotation annotation : ASTUtils.getAnnotations(param)) { |
| 89 | + checkValueAnnotation(annotation, annotationHierarchies, paramType, context); |
| 90 | + } |
| 91 | + return super.visit(param); |
| 92 | + } |
| 93 | + |
| 94 | + private void checkValueAnnotation(Annotation annotation, AnnotationHierarchies hierarchies, |
| 95 | + ITypeBinding typeBinding, ReconcilingContext context) { |
| 96 | + |
| 97 | + // Check if it's a @Value annotation |
| 98 | + if (!hierarchies.isAnnotatedWith(annotation.resolveTypeBinding(), Annotations.VALUE)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // Extract the value string from the annotation |
| 103 | + String valueString = extractValueString(annotation); |
| 104 | + if (valueString == null || valueString.isEmpty()) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Check if the value starts with "classpath:" or "classpath*:" |
| 109 | + if (!isClasspathResource(valueString)) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Validate the type binding |
| 114 | + if (typeBinding == null) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (!isValidResourceType(typeBinding)) { |
| 119 | + String typeName = typeBinding.getQualifiedName(); |
| 120 | + String message = String.format( |
| 121 | + "Type '%s' is not compatible with classpath resource injection. " + |
| 122 | + "Use org.springframework.core.io.Resource, java.io.InputStream, java.io.File, " + |
| 123 | + "java.net.URL, or java.nio.file.Path", |
| 124 | + typeName |
| 125 | + ); |
| 126 | + |
| 127 | + ReconcileProblemImpl problem = new ReconcileProblemImpl( |
| 128 | + getProblemType(), |
| 129 | + message, |
| 130 | + annotation.getStartPosition(), |
| 131 | + annotation.getLength() |
| 132 | + ); |
| 133 | + context.getProblemCollector().accept(problem); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private String extractValueString(Annotation annotation) { |
| 138 | + // getAttribute handles both @Value("...") and @Value(value="...") cases |
| 139 | + return ASTUtils.getAttribute(annotation, "value") |
| 140 | + .map(expr -> ASTUtils.getExpressionValueAsString(expr, v -> {})) |
| 141 | + .orElse(null); |
| 142 | + } |
| 143 | + |
| 144 | + private boolean isClasspathResource(String value) { |
| 145 | + // Remove ${} placeholder wrappers to check the actual value |
| 146 | + String trimmed = value.trim(); |
| 147 | + if (trimmed.startsWith("${") && trimmed.endsWith("}")) { |
| 148 | + trimmed = trimmed.substring(2, trimmed.length() - 1).trim(); |
| 149 | + } |
| 150 | + |
| 151 | + return trimmed.startsWith("classpath:") || trimmed.startsWith("classpath*:"); |
| 152 | + } |
| 153 | + |
| 154 | + private boolean isValidResourceType(ITypeBinding typeBinding) { |
| 155 | + String qualifiedName = typeBinding.getQualifiedName(); |
| 156 | + |
| 157 | + // Direct match with valid types |
| 158 | + if (VALID_RESOURCE_TYPES.contains(qualifiedName)) { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + // Check for array types - arrays of any valid resource type are also valid |
| 163 | + if (typeBinding.isArray()) { |
| 164 | + ITypeBinding elementType = typeBinding.getElementType(); |
| 165 | + if (elementType != null) { |
| 166 | + String elementTypeName = elementType.getQualifiedName(); |
| 167 | + // Check if the element type is one of the valid resource types |
| 168 | + if (VALID_RESOURCE_TYPES.contains(elementTypeName)) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return false; |
| 175 | + } |
| 176 | + }; |
| 177 | + } |
| 178 | +} |
| 179 | + |
0 commit comments