Skip to content
Merged
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<>();
boolean hasAutowiredConstructor = false;
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;
if (member instanceof VariableTree field && 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 instanceof MethodTree method) {
if (method.symbol().metadata().isAnnotatedWith(SpringUtils.AUTOWIRED_ANNOTATION)) {
hasAutowiredConstructor |= method.is(Tree.Kind.CONSTRUCTOR);
parameterDependencies(method).forEach((type, names) ->
deps.computeIfAbsent(type, k -> new LinkedHashSet<>()).addAll(names));
} else if (method.is(Tree.Kind.CONSTRUCTOR)) {
unannotatedConstructors.add(method);
}
}
}
if (!hasAutowiredConstructor && unannotatedConstructors.size() == 1) {
parameterDependencies(unannotatedConstructors.get(0)).forEach((type, names) ->
deps.computeIfAbsent(type, k -> new LinkedHashSet<>()).addAll(names));
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.
return deps;
}

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
class AutowiredConstructorWithUnannotatedConstructor {

private final ApplicationContext applicationContext;
private final Environment environment;

@Autowired
AutowiredConstructorWithUnannotatedConstructor(ApplicationContext applicationContext, Environment environment) {
this.applicationContext = applicationContext;
this.environment = environment;
}

// Spring ignores this constructor — its parameters must not appear as dependencies
AutowiredConstructorWithUnannotatedConstructor(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.environment = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package checks.spring.context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
class MixedInjectionDependencies {

// Injected via implicit single-constructor injection
private final ApplicationContext applicationContext;

// Injected via field injection
@Autowired
private Environment environment;

MixedInjectionDependencies(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
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 @@ -36,6 +36,7 @@
import org.sonar.plugins.java.api.caching.CacheContext;
import org.sonar.plugins.java.api.caching.JavaReadCache;
import org.sonar.plugins.java.api.caching.JavaWriteCache;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -201,7 +202,38 @@ 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();
}

@ParameterizedTest(name = "{0}")
@MethodSource("mixedInjectionArguments")
void both_injection_sources_collected_when_mixing_injection_styles(String filePath, String beanName) {
scan(filePath);

var beans = model.getBeanDefinitionRegistry().getByName(beanName);
assertThat(beans).hasSize(1);
var deps = beans.get(0).getDependingBeans();
assertThat(deps.get("org.springframework.context.ApplicationContext")).containsOnly("applicationContext");
assertThat(deps.get("org.springframework.core.env.Environment")).containsOnly("environment");
}

static Stream<Arguments> mixedInjectionArguments() {
return Stream.of(
Arguments.of("src/test/files/springcontext/AutowiredConstructorWithUnannotatedConstructor.java", "autowiredConstructorWithUnannotatedConstructor"),
Arguments.of("src/test/files/springcontext/MixedInjectionDependencies.java", "mixedInjectionDependencies")
);
}

Expand Down
Loading