Skip to content
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

#555 No qualifier injection point prefers no qualifier bean (over named bean) #562

Merged
merged 1 commit into from
May 6, 2024
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,18 @@
package org.example.coffee.qualifier;

import jakarta.inject.Singleton;

@Singleton
public class MegaStoreManager {

final SomeStore green;
final SomeStore blue;
final SomeStore noQualifier;

public MegaStoreManager(@Green SomeStore green, @Blue SomeStore blue, SomeStore noQualifier) {
this.green = green;
this.blue = blue;
this.noQualifier = noQualifier;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.example.coffee.qualifier;

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

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

class MetaStoreManagerTest {

@Test
void test() {
try (BeanScope context = BeanScope.builder().build()) {
MegaStoreManager manager = context.get(MegaStoreManager.class);

assertThat(manager.green).isInstanceOf(GreenStore.class);
assertThat(manager.blue).isInstanceOf(BlueStore.class);
assertThat(manager.noQualifier).isInstanceOf(NoNameStore.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.example.coffee.qualifier;

import jakarta.inject.Singleton;

// no qualifier on this one
@Singleton
public class NoNameStore implements SomeStore {

@Override
public String store() {
return "noName";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ void test() {

SomeStore greenStore = beanScope.get(SomeStore.class, "green");
SomeStore blueStore = beanScope.get(SomeStore.class, "blue");
SomeStore unNamedStore = beanScope.get(SomeStore.class);
assertThat(blueStore.store()).isEqualTo("blue");
assertThat(greenStore.store()).isEqualTo("green");
assertThat(unNamedStore.store()).isEqualTo("noName");
assertThat(greenStore).isInstanceOf(GreenStore.class);
assertThat(blueStore).isInstanceOf(BlueStore.class);
assertThat(unNamedStore).isInstanceOf(NoNameStore.class);

Map<String, SomeStore> stores = beanScope.map(SomeStore.class);

SomeStore green = stores.get("Green");
Expand All @@ -42,7 +50,7 @@ void mapParent() {

try (BeanScope beanScope = BeanScope.builder().parent(parent).build()) {
Map<String, SomeStore> stores = beanScope.map(SomeStore.class);
assertThat(stores).hasSize(3);
assertThat(stores).hasSize(4);
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions inject/src/main/java/io/avaje/inject/spi/DContextEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,20 @@ private Object match(List<DContextEntryBean> entries) {

private DContextEntryBean findMatch(List<DContextEntryBean> entries) {
for (DContextEntryBean entry : entries) {
if (entry.isNameMatch(name)) {
if (entry.isNameEqual(name)) {
checkMatch(entry);
}
}
if (match == null && impliedName) {
// search again as if the implied name wasn't there, name = null
// match without implied name, name = null to match against beans with no qualifier
for (DContextEntryBean entry : entries) {
if (entry.isNameEqual(null)) {
checkMatch(entry);
}
}
}
if (match == null && (name == null || impliedName)) {
// match no qualifier injection point to any beans
for (DContextEntryBean entry : entries) {
checkMatch(entry);
}
Expand Down
Loading