Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,27 @@ private void collectBeanMethod(MethodTree method, String pkg) {

private static Map<String, Set<String>> collectAutowiredDependencies(ClassTree classTree) {
Map<String, Set<String>> deps = new LinkedHashMap<>();
List<MethodTree> unannotatedConstructors = new ArrayList<>();
for (Tree member : classTree.members()) {
if (member instanceof VariableTree field) {
if (field.symbol().metadata().isAnnotatedWith(SpringUtils.AUTOWIRED_ANNOTATION)) {
String typeFqn = field.symbol().type().fullyQualifiedName();
String name = dependencyKey(field.simpleName().name(), extractQualifier(field.symbol().metadata()));
deps.computeIfAbsent(typeFqn, k -> new LinkedHashSet<>()).add(name);
}
} else if (member.is(Tree.Kind.CONSTRUCTOR, Tree.Kind.METHOD)) {
MethodTree method = (MethodTree) member;
} else if (member instanceof MethodTree method) {
if (method.symbol().metadata().isAnnotatedWith(SpringUtils.AUTOWIRED_ANNOTATION)) {
parameterDependencies(method).forEach((type, names) ->
deps.computeIfAbsent(type, k -> new LinkedHashSet<>()).addAll(names));
} else if (method.is(Tree.Kind.CONSTRUCTOR)) {
unannotatedConstructors.add(method);
}
}
}
if (deps.isEmpty() && unannotatedConstructors.size() == 1) {
parameterDependencies(unannotatedConstructors.get(0)).forEach((type, names) ->
deps.computeIfAbsent(type, k -> new LinkedHashSet<>()).addAll(names));
}
return deps;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package checks.spring.context;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
class MultipleConstructorsNoDependencies {

private final ApplicationContext applicationContext;
private final Environment environment;

MultipleConstructorsNoDependencies(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.environment = null;
}

MultipleConstructorsNoDependencies(ApplicationContext applicationContext, Environment environment) {
this.applicationContext = applicationContext;
this.environment = environment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package checks.spring.context;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
class SingleConstructorDependencies {

private final ApplicationContext applicationContext;
private final Environment environment;

SingleConstructorDependencies(ApplicationContext applicationContext, Environment environment) {
this.applicationContext = applicationContext;
this.environment = environment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,22 @@ static Stream<Arguments> dependencyCollectionArguments() {
return Stream.of(
Arguments.of("src/test/files/springcontext/AutowiredDependencies.java", "autowiredDependencies"),
Arguments.of("src/test/files/springcontext/AutowiredConstructorDependencies.java", "autowiredConstructorDependencies"),
Arguments.of("src/test/files/springcontext/BeanMethodWithDependencies.java", "myBean")
Arguments.of("src/test/files/springcontext/BeanMethodWithDependencies.java", "myBean"),
Arguments.of("src/test/files/springcontext/SingleConstructorDependencies.java", "singleConstructorDependencies")
);
}

// ---- Implicit single-constructor injection --------------------------------

@Test
void multiple_constructors_without_autowired_yields_no_dependencies() {
scan("src/test/files/springcontext/MultipleConstructorsNoDependencies.java");

var beans = model.getBeanDefinitionRegistry().getByName("multipleConstructorsNoDependencies");
assertThat(beans).hasSize(1);
assertThat(beans.get(0).getDependingBeans()).isEmpty();
}

// ---- @Qualifier handling --------------------------------------------------

@ParameterizedTest(name = "{0}")
Expand Down
Loading