Skip to content

#140 - Are scope requirements meant to be transitive? #141

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
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
4 changes: 2 additions & 2 deletions inject-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-parent</artifactId>
<version>6.5</version>
<version>6.6-SNAPSHOT</version>
</parent>

<artifactId>avaje-inject-generator</artifactId>
Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>6.5</version>
<version>6.6-SNAPSHOT</version>
</dependency>

<!-- test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@ private void writeModuleCustomServicesFile() {
if (jfo != null) {
Writer writer = jfo.openWriter();
for (Data value : scopeAnnotations.values()) {
writer.write(value.moduleFullName());
writer.write("\n");
final String moduleFullName = value.moduleFullName();
if (moduleFullName == null) {
// an empty module, custom scope with no beans
final TypeElement typeElement = value.annotationType();
if (typeElement != null) {
context.logWarn("Empty module for "+typeElement);
}
} else {
writer.write(moduleFullName);
writer.write("\n");
}
}
writer.close();
}
Expand Down Expand Up @@ -120,5 +129,9 @@ void write(boolean processingOver) {
String moduleFullName() {
return scopeInfo.moduleFullName();
}

TypeElement annotationType() {
return scopeInfo.annotationType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class ScopeInfo {
this.annotationType = type;
}

@Override
public String toString() {
return "ScopeInfo{" +
"name=" + name +
", metaData=" + metaData +
'}';
}

void details(String name, Element contextElement) {
if (name == null || name.isEmpty()) {
final String simpleName = contextElement.getSimpleName().toString();
Expand Down Expand Up @@ -85,6 +93,10 @@ void initialiseName(String topPackage) throws IOException {
}
}

TypeElement annotationType() {
return annotationType;
}

JavaFileObject moduleFile() {
return moduleFile;
}
Expand Down Expand Up @@ -267,7 +279,7 @@ void buildAtInjectModule(Append writer) {
writer.append("@InjectModule(");
boolean leadingComma = false;
if (!provides.isEmpty()) {
attributeClasses(leadingComma, writer, "provides", provides);
attributeClasses(false, writer, "provides", provides);
leadingComma = true;
}
if (!requires.isEmpty()) {
Expand Down Expand Up @@ -357,7 +369,7 @@ boolean providedByOtherModule(String dependency) {
final ScopeInfo requiredScope = scopes.get(require);
if (requiredScope != null) {
if (requiredScope.providesDependency(dependency)) {
// context.logWarn("dependency "+dependency+" provided by other scope "+requiredScope.name);
// context.logWarn("dependency " + dependency + " provided by other scope " + requiredScope.name);
return true;
}
}
Expand All @@ -369,6 +381,9 @@ boolean providedByOtherModule(String dependency) {
* Return true if this module provides the dependency.
*/
boolean providesDependency(String dependency) {
if (requires.contains(dependency)) {
return true;
}
for (MetaData meta : metaData.values()) {
if (dependency.equals(meta.getType())) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion inject-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-parent</artifactId>
<version>6.5</version>
<version>6.6-SNAPSHOT</version>
</parent>

<artifactId>avaje-inject-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.customext0;

@Ext0Scope
public class Ext0Other {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.customext0;

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

@Scope
@InjectModule(requires = {Ext0iface.class, Ext0conc.class})
public @interface Ext0Scope {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.customext0;

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

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

class Ext0ScopeTest {

@Test
void wire() {

final BeanScope scope = BeanScope.newBuilder()
.withBean(Ext0iface.class, new If0())
.withBean(Ext0conc.class, new Ext0conc())
.withModules(new Ext0Module())
.build();

final Ext0Other other = scope.get(Ext0Other.class);
assertThat(other).isNotNull();
}

static class If0 implements Ext0iface {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.customext0;

public class Ext0conc {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.customext0;

public interface Ext0iface {
}
43 changes: 43 additions & 0 deletions inject-test/src/test/java/org/example/customext1/Ext1Bean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.example.customext1;

import org.example.customext0.Ext0Other;
import org.example.customext0.Ext0conc;
import org.example.customext0.Ext0iface;

@Ext1Scope
public class Ext1Bean {

final Ext0iface ext0iface;
final Ext0conc ext0conc;
final Ext0Other ext0Other;
final Ext1iface ext1iface;
final Ext1conc ext1conc;

public Ext1Bean(Ext0iface ext0iface, Ext0conc ext0conc, Ext0Other ext0Other, Ext1iface ext1iface, Ext1conc ext1conc) {
this.ext0iface = ext0iface;
this.ext0conc = ext0conc;
this.ext0Other = ext0Other;
this.ext1iface = ext1iface;
this.ext1conc = ext1conc;
}

public Ext0iface ext0iface() {
return ext0iface;
}

public Ext0conc ext0conc() {
return ext0conc;
}

public Ext0Other ext0Other() {
return ext0Other;
}

public Ext1iface ext1iface() {
return ext1iface;
}

public Ext1conc ext1conc() {
return ext1conc;
}
}
10 changes: 10 additions & 0 deletions inject-test/src/test/java/org/example/customext1/Ext1Scope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.example.customext1;

import io.avaje.inject.InjectModule;
import jakarta.inject.Scope;
import org.example.customext0.Ext0Scope;

@Scope
@InjectModule(requires = {Ext0Scope.class, Ext1iface.class, Ext1conc.class})
public @interface Ext1Scope {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.example.customext1;

import io.avaje.inject.BeanScope;
import org.example.customext0.Ext0Other;
import org.example.customext0.Ext0conc;
import org.example.customext0.Ext0iface;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class Ext1ScopeTest {

/*
Unfortunately with both modules in test-classes plus this test it can not compile.
So this wireParentChild() works when scopes are in src/main (and the test here in src/test).

@Test
void wireParentChild() {

final BeanScope parentScope = BeanScope.newBuilder()
.withBean(Ext0iface.class, new If0())
.withBean(Ext0conc.class, new Ext0conc())
.withModules(new Ext0Module())
.build();

final BeanScope scope = BeanScope.newBuilder()
.withBean(Ext1iface.class, new If1())
.withBean(Ext1conc.class, new Ext1conc())
.withModules(new Ext1Module())
.withParent(parentScope)
.build();

final Ext1Bean ext1Bean = scope.get(Ext1Bean.class);
assertThat(ext1Bean).isNotNull();

assertNotNull(ext1Bean.ext0iface());
assertNotNull(ext1Bean.ext0conc());
assertNotNull(ext1Bean.ext0Other());
assertNotNull(ext1Bean.ext1iface());
assertNotNull(ext1Bean.ext1conc());
}
*/

@Test
void wireBoth() {

// wire everything using only Ext1Module so simulating
// Ext0Module via external dependencies
final BeanScope scope = BeanScope.newBuilder()
.withBean(Ext0iface.class, new If0())
.withBean(Ext0conc.class, new Ext0conc())
.withBean(Ext0Other.class, new Ext0Other())
.withBean(Ext1iface.class, new If1())
.withBean(Ext1conc.class, new Ext1conc())
.withModules(new Ext1Module()) //new Ext0Module(),
.build();

final Ext1Bean ext1Bean = scope.get(Ext1Bean.class);
assertThat(ext1Bean).isNotNull();

assertNotNull(ext1Bean.ext0iface());
assertNotNull(ext1Bean.ext0conc());
assertNotNull(ext1Bean.ext0Other());
assertNotNull(ext1Bean.ext1iface());
assertNotNull(ext1Bean.ext1conc());
}

static class If0 implements Ext0iface {

}
static class If1 implements Ext1iface {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.customext1;

public class Ext1conc {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.customext1;

public interface Ext1iface {
}
2 changes: 1 addition & 1 deletion inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-parent</artifactId>
<version>6.5</version>
<version>6.6-SNAPSHOT</version>
</parent>

<artifactId>avaje-inject</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions inject/src/main/java/io/avaje/inject/spi/DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ public <T> T get(Class<T> cls, String name) {
if (!beanList.isEmpty()) {
msg += ". Check @Named or Qualifier being used";
}
msg += ". Check for missing module? [ missing beanScopeBuilder.withModules() ]";
msg += ". If it is expected to be externally provided, missing beanScopeBuilder.withBean() ?";
throw new IllegalStateException(msg);
}
return bean;
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>io.avaje</groupId>
<artifactId>avaje-inject-parent</artifactId>
<version>6.5</version>
<version>6.6-SNAPSHOT</version>
<packaging>pom</packaging>
<name>avaje inject parent</name>
<description>parent pom for avaje inject library</description>
Expand All @@ -20,7 +20,7 @@
</scm>

<properties>
<nexus.staging.autoReleaseAfterClose>false</nexus.staging.autoReleaseAfterClose>
<nexus.staging.autoReleaseAfterClose>true</nexus.staging.autoReleaseAfterClose>
</properties>

<modules>
Expand Down