Skip to content

Commit f709133

Browse files
committed
GH-1635: check for lombok constructor annotations to avoid validation errors while checking for superfluous autowired annotations
1 parent 5142a39 commit f709133

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/Annotations.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,11 @@ public class Annotations {
132132
);
133133

134134
public static final String JMOLECULES_STEREOTYPE = "org.jmolecules.stereotype.Stereotype";
135-
135+
136+
public static final Set<String> LOMBOK_CONSTRUCTOR_ANNOTATIONS = Set.of(
137+
"lombok.RequiredArgsConstructor",
138+
"lombok.NoArgsConstructor",
139+
"lombok.AllArgsConstructor"
140+
);
141+
136142
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/reconcilers/NoAutowiredOnConstructorReconciler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.ide.vscode.boot.java.Annotations;
2727
import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType;
2828
import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies;
29+
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
2930
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
3031
import org.springframework.ide.vscode.commons.java.IJavaProject;
3132
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
@@ -67,6 +68,15 @@ public boolean visit(TypeDeclaration typeDecl) {
6768
if (IClasspathUtil.getProjectJavaSourceFoldersWithoutTests(project.getClasspath())
6869
.anyMatch(f -> sourceFile.startsWith(f.toPath()))) {
6970

71+
// lombok annotation around -> don't check for constructors
72+
if (ASTUtils.getAnnotations(typeDecl).stream()
73+
.map(annotation -> annotation.resolveTypeBinding().getQualifiedName())
74+
.filter(annotationType -> Annotations.LOMBOK_CONSTRUCTOR_ANNOTATIONS.contains(annotationType))
75+
.findAny().isPresent()) {
76+
return super.visit(typeDecl);
77+
}
78+
79+
// more than one constructor ?
7080
int constructorCount = 0;
7181
MethodDeclaration constructor = null;
7282
for (MethodDeclaration method : typeDecl.getMethods()) {
@@ -79,7 +89,8 @@ public boolean visit(TypeDeclaration typeDecl) {
7989
}
8090
}
8191
}
82-
92+
93+
// one constructor -> check for autowired annotation
8394
if (constructor != null) {
8495
Annotation autowiredAnnotation = ReconcileUtils.findAnnotation(annotationHierarchies, constructor,
8596
Annotations.AUTOWIRED, false);

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/reconcilers/test/NoAutowiredOnConstructorReconcilerTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 VMware, Inc.
2+
* Copyright (c) 2023, 2025 VMware, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -98,6 +98,27 @@ class A {
9898
List<ReconcileProblem> problems = reconcile("A.java", source, false);
9999

100100
assertEquals(0, problems.size());
101+
}
102+
103+
@Test
104+
void multipleConstructorsViaLombok() throws Exception {
105+
String source = """
106+
package example.demo;
107+
108+
import org.springframework.beans.factory.annotation.Autowired;
109+
import lombok.RequiredArgsConstructor;
110+
111+
@RequiredArgsConstructor
112+
class A {
113+
114+
@Autowired
115+
A() {};
116+
117+
}
118+
""";
119+
List<ReconcileProblem> problems = reconcile("A.java", source, false);
101120

121+
assertEquals(0, problems.size());
102122
}
123+
103124
}

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-spring-validations/pom.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="https://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56

67
<groupId>com.example</groupId>
78
<artifactId>test-spring-validations</artifactId>
89
<version>0.0.1-SNAPSHOT</version>
910
<packaging>jar</packaging>
10-
11+
1112
<parent>
1213
<groupId>org.springframework.boot</groupId>
1314
<artifactId>spring-boot-starter-parent</artifactId>
1415
<version>3.4.4</version>
15-
<relativePath/> <!-- lookup parent from repository -->
16+
<relativePath /> <!-- lookup parent from repository -->
1617
</parent>
1718

1819
<properties>
@@ -48,8 +49,14 @@
4849
<artifactId>spring-boot-starter-test</artifactId>
4950
<scope>test</scope>
5051
</dependency>
52+
<dependency>
53+
<groupId>org.projectlombok</groupId>
54+
<artifactId>lombok</artifactId>
55+
<version>1.18.40</version>
56+
<scope>provided</scope>
57+
</dependency>
5158
</dependencies>
52-
<!--
59+
<!--
5360
<build>
5461
<plugins>
5562
<plugin>

0 commit comments

Comments
 (0)