Skip to content

Commit 382d0bb

Browse files
committed
Fixes for @Autowired over constructor
1 parent eb844bf commit 382d0bb

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

eclipse-language-servers/org.springframework.ide.eclipse.editor.support/src/org/springframework/ide/eclipse/editor/support/preferences/AbstractProblemSeverityPreferencesPage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public int compare(ProblemType o1, ProblemType o2) {
7575
private static final String[][] SEVERITY_NAMES_AND_VALUES = {
7676
{"Error", ProblemSeverity.ERROR.toString()},
7777
{"Warning", ProblemSeverity.WARNING.toString()},
78+
{"Info", ProblemSeverity.INFO.toString()},
79+
{"Hint", ProblemSeverity.HINT.toString()},
7880
{"Ignore", ProblemSeverity.IGNORE.toString()}
7981
};
8082

eclipse-language-servers/org.springframework.ide.eclipse.editor.support/src/org/springframework/ide/eclipse/editor/support/reconcile/ProblemSeverity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014, 2016 Pivotal, Inc.
2+
* Copyright (c) 2014, 2022 Pivotal, 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
@@ -16,6 +16,8 @@
1616
public enum ProblemSeverity {
1717

1818
IGNORE,
19+
HINT,
20+
INFO,
1921
WARNING,
2022
ERROR;
2123

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2021 Pivotal, Inc.
2+
* Copyright (c) 2017, 2022 Pivotal, 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
@@ -122,5 +122,42 @@ private static boolean isMetaAnnotation(ITypeBinding candidate, Predicate<String
122122
return findTransitiveSupers(candidate, new HashSet<>())
123123
.anyMatch(sa -> isKeyAnnotationName.test(sa.getQualifiedName()));
124124
}
125+
126+
public static Collection<IAnnotationBinding> getDirectSuperAnnotationBindings(IAnnotationBinding annotationBinding) {
127+
synchronized(lock) {
128+
try {
129+
if (annotationBinding.getAnnotationType() != null) {
130+
IAnnotationBinding[] annotations = annotationBinding.getAnnotationType().getAnnotations();
131+
if (annotations != null && annotations.length != 0) {
132+
ImmutableList.Builder<IAnnotationBinding> superAnnotations = ImmutableList.builder();
133+
for (IAnnotationBinding ab : annotations) {
134+
ITypeBinding sa = ab.getAnnotationType();
135+
if (sa != null) {
136+
if (!ignoreAnnotation(sa.getQualifiedName())) {
137+
superAnnotations.add(ab);
138+
}
139+
}
140+
}
141+
return superAnnotations.build();
142+
}
143+
}
144+
}
145+
catch (AbortCompilation e) {
146+
log.debug("compilation aborted ", e);
147+
// ignore this, it is most likely caused by broken source code, a broken classpath, or some optional dependencies not being on the classpath
148+
}
149+
150+
return ImmutableList.of();
151+
}
152+
}
153+
154+
public static Stream<IAnnotationBinding> findTransitiveSuperAnnotationBindings(
155+
IAnnotationBinding annotationBinding) {
156+
synchronized (lock) {
157+
return Stream.concat(Stream.of(annotationBinding), getDirectSuperAnnotationBindings(annotationBinding)
158+
.stream().flatMap(superBinding -> findTransitiveSuperAnnotationBindings(superBinding)));
159+
}
160+
}
161+
125162

126163
}

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
import java.util.Optional;
1515

1616
import org.eclipse.jdt.core.dom.Annotation;
17+
import org.eclipse.jdt.core.dom.IAnnotationBinding;
18+
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
1719
import org.eclipse.jdt.core.dom.IMethodBinding;
1820
import org.eclipse.jdt.core.dom.ITypeBinding;
1921
import org.eclipse.jdt.core.dom.MethodDeclaration;
2022
import org.springframework.ide.vscode.boot.java.Annotations;
2123
import org.springframework.ide.vscode.boot.java.SpringJavaProblemType;
24+
import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies;
2225
import org.springframework.ide.vscode.commons.java.IJavaProject;
2326
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
2427
import org.springframework.ide.vscode.commons.java.Version;
@@ -65,9 +68,9 @@ static Optional<ITypeBinding> getSingleAutowiredConstructorDeclaringType(Annotat
6568
if (a.getParent() instanceof MethodDeclaration) {
6669
MethodDeclaration method = (MethodDeclaration) a.getParent();
6770
IMethodBinding methodBinding = method.resolveBinding();
68-
if (methodBinding != null) {
71+
if (methodBinding != null && methodBinding.isConstructor()) {
6972
ITypeBinding declaringType = methodBinding.getDeclaringClass();
70-
if (declaringType != null && isOnlyOneConstructor(declaringType)) {
73+
if (declaringType != null && !isBootTestClass(declaringType) && isOnlyOneConstructor(declaringType)) {
7174
return Optional.of(declaringType);
7275
}
7376
}
@@ -90,6 +93,31 @@ private static boolean isOnlyOneConstructor(ITypeBinding t) {
9093
}
9194
return numberOfConstructors == 1;
9295
}
96+
97+
private static boolean isBootTestClass(ITypeBinding typeBinding) {
98+
for (IAnnotationBinding annotation : typeBinding.getAnnotations()) {
99+
boolean found = AnnotationHierarchies.findTransitiveSuperAnnotationBindings(annotation).anyMatch(a -> {
100+
if (a.getAnnotationType() != null) {
101+
if ("org.springframework.test.context.BootstrapWith".equals(a.getAnnotationType().getQualifiedName())) {
102+
for (IMemberValuePairBinding pair : a.getAllMemberValuePairs()) {
103+
if (pair.getValue() instanceof ITypeBinding) {
104+
ITypeBinding type = (ITypeBinding) pair.getValue();
105+
if ("value".equals(pair.getName())
106+
&& "org.springframework.boot.test.context.SpringBootTestContextBootstrapper".equals(type.getQualifiedName())) {
107+
return true;
108+
}
109+
}
110+
}
111+
}
112+
}
113+
return false;
114+
});
115+
if (found) {
116+
return true;
117+
}
118+
}
119+
return false;
120+
}
93121

94122

95123
}

0 commit comments

Comments
 (0)