Skip to content

(Purposefully failing) Automatically import #799

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion blackbox-multi-scope/pom.xml
Original file line number Diff line number Diff line change
@@ -31,7 +31,12 @@
<version>1.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.12.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.multi.crosscut;

import org.multi.moda.BeanInModA;
import org.multi.modb.BeanInModB;
import org.multi.modc.modb.BeanInModC;
import org.multi.modc.BeanInModC;
import org.multi.scope.CrossCutScope;

@CrossCutScope
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.multi.crosscut;

import org.multi.modc.modb.COther;
import org.multi.modc.COther;
import org.multi.scope.CrossCutScope;

@CrossCutScope
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.multi.main;

import io.avaje.inject.BeanScope;
import org.multi.crosscut.BeanCross;
import org.multi.crosscut.BeanCross2;
import org.multi.crosscut.BeanCross3;
@@ -9,7 +8,9 @@
import org.multi.moda.ModAModule;
import org.multi.modb.BeanInModB;
import org.multi.modb.ModBModule;
import org.multi.modc.modb.ModCModule;
import org.multi.modc.ModCModule;

import io.avaje.inject.BeanScope;

public class CrossCutMain {

20 changes: 20 additions & 0 deletions blackbox-multi-scope/src/main/java/org/multi/many/BeanInMany.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.multi.many;

import org.multi.moda.BeanInModA;
import org.multi.modc.COther;
import org.multi.mode.BeanInModE;
import org.multi.scope.ManyScope;

@ManyScope
public class BeanInMany {

private final BeanInModE beanInModE;
private final COther cOther;
private final BeanInModA modA;

public BeanInMany(final BeanInModE beanInModE, final COther cOther, final BeanInModA modA) {
this.beanInModE = beanInModE;
this.cOther = cOther;
this.modA = modA;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.multi.modb;

import org.multi.modc.modb.COther;
import org.multi.modc.COther;
import org.multi.scope.ModBScope;

@ModBScope
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.multi.modc.modb;
package org.multi.modc;

import org.multi.scope.ModCScope;

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.multi.modc.modb;
package org.multi.modc;

import org.multi.scope.ModCScope;

14 changes: 14 additions & 0 deletions blackbox-multi-scope/src/main/java/org/multi/modd/BeanInModD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.multi.modd;

import org.multi.moda.BeanInModA;
import org.multi.scope.ModDScope;

@ModDScope
public class BeanInModD {

private final BeanInModA beanA;

public BeanInModD(final BeanInModA beanInModA){
this.beanA = beanInModA;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.multi.mode;

import org.multi.scope.ModEScope;

@ModEScope
public class BeanInModE {
}
Original file line number Diff line number Diff line change
@@ -2,9 +2,8 @@

import io.avaje.inject.InjectModule;
import jakarta.inject.Scope;
import org.multi.modb.BeanInModB;

@Scope
@InjectModule(requires = {ModAScope.class, ModBScope.class}, strictWiring = true)
@InjectModule(automaticallyImport = {ModAScope.class, ModBScope.class}, strictWiring = true)
public @interface CrossCutScope {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.multi.scope;

import io.avaje.inject.InjectModule;
import jakarta.inject.Scope;

@Scope
@InjectModule(automaticallyImport = {ModDScope.class, CrossCutScope.class, ModEScope.class}, strictWiring = true)
public @interface ManyScope {
}
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@
import jakarta.inject.Scope;

@Scope
@InjectModule(requires = ModCScope.class, strictWiring = true)
@InjectModule(automaticallyImport = ModCScope.class, strictWiring = true)
public @interface ModBScope {
}
10 changes: 10 additions & 0 deletions blackbox-multi-scope/src/main/java/org/multi/scope/ModDScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.multi.scope;

import io.avaje.inject.InjectModule;
import jakarta.inject.Scope;

@Scope
@InjectModule(automaticallyImport = {ModAScope.class}, strictWiring = true)
public @interface ModDScope {

}
10 changes: 10 additions & 0 deletions blackbox-multi-scope/src/main/java/org/multi/scope/ModEScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.multi.scope;

import io.avaje.inject.InjectModule;
import jakarta.inject.Scope;

@Scope
@InjectModule(strictWiring = true)
public @interface ModEScope {

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,100 @@
package org.multi.crosscut;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.avaje.inject.BeanScope;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.multi.many.BeanInMany;
import org.multi.many.ManyModule;
import org.multi.moda.BeanInModA;
import org.multi.moda.ModAModule;
import org.multi.modb.BOther;
import org.multi.modb.BeanInModB;
import org.multi.modb.ModBModule;
import org.multi.modc.BeanInModC;
import org.multi.modc.COther;
import org.multi.modc.ModCModule;
import org.multi.modd.BeanInModD;
import org.multi.modd.ModDModule;
import org.multi.mode.BeanInModE;
import org.multi.mode.ModEModule;

import static org.assertj.core.api.Assertions.assertThat;
import io.avaje.inject.BeanScope;
import io.avaje.inject.spi.AvajeModule;

class BeanCrossTest {

@Test
void bootstrap() {
private static Stream<Arguments> allModulesDefined() {
return Stream.of(
Arguments.of(Named.of("In working order A", new AvajeModule[]{
new ModAModule(),
new ModCModule(),
new ModBModule(),
new CrossCutModule(),
new ModDModule(),
new ModEModule(),
new ManyModule(),
})),
Arguments.of(Named.of("In alphabetical order", new AvajeModule[]{
new CrossCutModule(),
new ManyModule(),
new ModAModule(),
new ModBModule(),
new ModCModule(),
new ModDModule(),
new ModEModule(),
})),
Arguments.of(Named.of("In reverse alphabetical order", new AvajeModule[]{
new ModEModule(),
new ModDModule(),
new ModCModule(),
new ModBModule(),
new ModAModule(),
new ManyModule(),
new CrossCutModule(),
})),
Arguments.of(Named.of("In shuffled order", new AvajeModule[]{
new ModEModule(),
new ModBModule(),
new ModDModule(),
new ManyModule(),
new ModCModule(),
new ModAModule(),
new CrossCutModule(),
})),
Arguments.of(Named.of("Only the end module is required", new AvajeModule[]{
new ManyModule()
}))
);
}

try (BeanScope beanScope = BeanScope.builder()
// .modules(new CrossCutModule())
.build()) {
private static final Class[] CHECKABLE = {
BeanCross.class,
BeanCross2.class,
BeanCross3.class,
BeanInMany.class,
BeanInModA.class,
BeanInModB.class,
BOther.class,
BeanInModC.class,
COther.class,
BeanInModD.class,
BeanInModE.class
};

// var beanInModB = beanScope.get(BeanInModB.class);
// assertThat(beanInModB).isNotNull();
}
@ParameterizedTest(name = "Multi Scope Test: {1}")
@MethodSource("allModulesDefined")
void bootstrap(AvajeModule... modules) {

try (BeanScope beanScope = assertDoesNotThrow(() -> BeanScope.builder().modules(modules).build())) {
for (final Class<?> clazz : CHECKABLE) {
assertTrue(beanScope.getOptional(clazz).isPresent(), "Bean not found: " + clazz.getSimpleName());
}
}
}
}
16 changes: 16 additions & 0 deletions inject/src/main/java/io/avaje/inject/InjectModule.java
Original file line number Diff line number Diff line change
@@ -105,6 +105,22 @@
*/
Class<?>[] requiresPackages() default {};

/**
* Modules and scopes that are expected to be automatically imported when this module is requested manually
* <p>
* Like {@code requires}, references to beans created in the imported module/scope are not treated as missing.
* Unlike {@code requires} though, referenced items will be provided automatically.
* <p>
* Any item that a referenced module/scope {@code requires} must either:
* <ul>
* <li>Be provided by another {@code automaticallyImport}ed item</li>
* <li>Be required by this module as well</li>
* </ul>
* <p>
* Circular dependencies are not permitted
*/
Class<?>[] automaticallyImport() default {};

/**
* Internal use only - identifies the custom scope annotation associated to this module.
* <p>