-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@BeanTypes
Annotation to Limit Injection Types (#665)
* Adds BeanTypes annotation
- Loading branch information
Showing
14 changed files
with
175 additions
and
19 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
blackbox-test-inject/src/main/java/org/example/myapp/beantypes/AbstractSuperClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.example.myapp.beantypes; | ||
|
||
public abstract class AbstractSuperClass {} |
14 changes: 14 additions & 0 deletions
14
blackbox-test-inject/src/main/java/org/example/myapp/beantypes/BeanTypeComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.example.myapp.beantypes; | ||
|
||
import org.example.myapp.config.AppConfig.SomeInterface; | ||
import org.other.one.SomeOptionalDep; | ||
|
||
import io.avaje.inject.BeanTypes; | ||
import io.avaje.inject.Component; | ||
import jakarta.inject.Named; | ||
|
||
@Component | ||
@Named("type") | ||
@BeanTypes(AbstractSuperClass.class) | ||
public class BeanTypeComponent extends AbstractSuperClass | ||
implements SomeInterface, SomeOptionalDep, LimitedInterface {} |
17 changes: 17 additions & 0 deletions
17
blackbox-test-inject/src/main/java/org/example/myapp/beantypes/LimitedFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.example.myapp.beantypes; | ||
|
||
import io.avaje.inject.Bean; | ||
import io.avaje.inject.BeanTypes; | ||
import io.avaje.inject.Factory; | ||
import jakarta.inject.Named; | ||
|
||
@Factory | ||
public class LimitedFactory { | ||
|
||
@Bean | ||
@Named("factory") | ||
@BeanTypes(LimitedInterface.class) | ||
BeanTypeComponent bean() { | ||
return new BeanTypeComponent(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
blackbox-test-inject/src/main/java/org/example/myapp/beantypes/LimitedInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.example.myapp.beantypes; | ||
|
||
public interface LimitedInterface {} |
21 changes: 21 additions & 0 deletions
21
blackbox-test-inject/src/test/java/org/example/myapp/beantypes/BeanTypeComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.example.myapp.beantypes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.avaje.inject.BeanScope; | ||
|
||
class BeanTypesTest { | ||
|
||
@Test | ||
void testBeanTypesRestrictingInjection() { | ||
try (var scope = BeanScope.builder().build()) { | ||
|
||
assertFalse(scope.contains(BeanTypeComponent.class)); | ||
assertThat(scope.get(AbstractSuperClass.class)).isNotNull(); | ||
assertThat(scope.get(LimitedInterface.class)).isNotNull(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...rator/src/test/java/io/avaje/inject/generator/models/valid/supertypes/LimitedFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.avaje.inject.generator.models.valid.supertypes; | ||
|
||
import io.avaje.inject.Bean; | ||
import io.avaje.inject.BeanTypes; | ||
import io.avaje.inject.Factory; | ||
|
||
@Factory | ||
public class LimitedFactory { | ||
|
||
@Bean | ||
@BeanTypes(SomeInterface.class) | ||
OtherComponent bean() { | ||
return new OtherComponent(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...rc/test/java/io/avaje/inject/generator/models/valid/supertypes/LimitedOtherComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.avaje.inject.generator.models.valid.supertypes; | ||
|
||
import io.avaje.inject.BeanTypes; | ||
import io.avaje.inject.Component; | ||
|
||
@Component | ||
@BeanTypes(SomeInterface2.class) | ||
public class LimitedOtherComponent extends AbstractSuperClass | ||
implements SomeInterface, SomeInterface2 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.avaje.inject; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** Limits the types exposed by this bean to the given types. */ | ||
@Retention(RetentionPolicy.SOURCE) | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
public @interface BeanTypes { | ||
|
||
Class<?>[] value(); | ||
} |