Skip to content

Detect clash with multiple @Bean methods returning same type #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
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
@@ -0,0 +1,6 @@
package org.example.myapp.config;

public interface BFace {

String hi();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.example.myapp.config;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import jakarta.inject.Named;

import java.util.Optional;

@Factory
public class BFactory {

@Named
@Bean
BFace one() {
return new TheBFace("one");
}

@Named
@Bean
BFace two() {
return new TheBFace("two");
}

@Named
@Bean
Optional<BFace> three() {
return Optional.of(new TheBFace("three"));
}


static class TheBFace implements BFace {

private final String msg;

TheBFace(String msg) {
this.msg = msg;
}

@Override
public String hi() {
return msg;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.example.myapp.config;

import io.avaje.inject.BeanScope;
import io.avaje.inject.test.TestBeanScope;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

class BFactoryTest {

@Test
void beanMethodsOfSameType() {
try (BeanScope scope = TestBeanScope.builder().build()) {
BFace one = scope.get(BFace.class, "one");
BFace two = scope.get(BFace.class, "two");
BFace three = scope.get(BFace.class, "three");
assertThat(one).isNotNull();
assertThat(two).isNotNull();
assertThat(three).isNotNull();

List<BFace> list = scope.list(BFace.class);
assertThat(list).hasSize(3);

List<String> hi = list.stream().map(BFace::hi).collect(Collectors.toList());
assertThat(hi).containsOnly("one", "two", "three");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,14 @@ MethodParam observeParam() {
return observeParameter;
}

String qualifiedKey() {
return name + ':' + genericType.full();
}

boolean isVoid() {
return isVoid;
}

static class MethodParam {

private final VariableElement element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,24 @@ DestroyMethods.DestroyMethod matchPreDestroy(String returnTypeRaw) {
}

void validate() {
validateFactoryMethodDuplicates();
for (DestroyMethods.DestroyMethod destroyMethod : factoryPreDestroyMethods.unmatched()) {
logError(destroyMethod.element(), "Unused @PreDestroy method, no matching @Bean method for type " + destroyMethod.matchType());
}
}

void validateFactoryMethodDuplicates() {
var map = new HashMap<String, MethodReader>();
for (MethodReader method : factoryMethods) {
if (!method.isVoid()) {
var clashMethod = map.put(method.qualifiedKey(), method);
if (clashMethod != null) {
var msg = String.format("@Bean method %s() returns the same type as with method %s() without a unique name qualifier." +
" Add @Named or a qualifier annotation to allow both @Bean methods.",
method.name(), clashMethod.name());
logError(method.element(), msg);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ static String commonParent(String currentTop, String aPackage) {
static String named(Element p) {
final NamedPrism named = NamedPrism.getInstanceOn(p);
if (named != null) {
return named.value().replace("\"", "\\\"");
String raw = named.value();
if (raw.isEmpty()) {
// default to the method name
raw = p.getSimpleName().toString();
}
return raw.replace("\"", "\\\"");
}
for (final AnnotationMirror annotationMirror : p.getAnnotationMirrors()) {
final DeclaredType annotationType = annotationMirror.getAnnotationType();
Expand Down