-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support @decorated bean injection in decorator
- Loading branch information
Showing
8 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
...projects/arc/runtime/src/main/java/io/quarkus/arc/impl/DecoratedBeanMetadataProvider.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,28 @@ | ||
package io.quarkus.arc.impl; | ||
|
||
import static io.quarkus.arc.impl.CreationalContextImpl.unwrap; | ||
|
||
import jakarta.enterprise.context.spi.Contextual; | ||
import jakarta.enterprise.context.spi.CreationalContext; | ||
import jakarta.enterprise.inject.Decorated; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
|
||
import io.quarkus.arc.InjectableReferenceProvider; | ||
|
||
/** | ||
* {@link Decorated} {@link Bean} metadata provider. | ||
*/ | ||
public class DecoratedBeanMetadataProvider implements InjectableReferenceProvider<Contextual<?>> { | ||
|
||
@Override | ||
public Contextual<?> get(CreationalContext<Contextual<?>> creationalContext) { | ||
// First attempt to obtain the creational context of the decorator bean and then the creational context of the decorated bean | ||
CreationalContextImpl<?> parent = unwrap(creationalContext).getParent(); | ||
if (parent != null) { | ||
parent = parent.getParent(); | ||
return parent != null ? parent.getContextual() : null; | ||
} | ||
return null; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...jects/arc/tests/src/test/java/io/quarkus/arc/test/decorators/decorated/DecoratedTest.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,75 @@ | ||
package io.quarkus.arc.test.decorators.decorated; | ||
|
||
import java.util.Comparator; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.decorator.Decorator; | ||
import jakarta.decorator.Delegate; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Any; | ||
import jakarta.enterprise.inject.Decorated; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import io.quarkus.arc.test.MyQualifier; | ||
|
||
public class DecoratedTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(Converter.class, DecoratedBean.class, | ||
TrimConverterDecorator.class, MyQualifier.class); | ||
|
||
interface Converter<T> { | ||
|
||
T convert(T value); | ||
|
||
} | ||
|
||
@Test | ||
public void testDecoration() { | ||
DecoratedBean bean = Arc.container().instance(DecoratedBean.class, new MyQualifier.Literal()).get(); | ||
Assertions.assertEquals( | ||
DecoratedBean.class.getName() + " [@io.quarkus.arc.test.MyQualifier(), @jakarta.enterprise.inject.Any()]", | ||
bean.convert("any value")); | ||
} | ||
|
||
@ApplicationScoped | ||
@MyQualifier | ||
static class DecoratedBean implements Converter<String> { | ||
|
||
@Override | ||
public String convert(String value) { | ||
return "Replaced by the decorator"; | ||
} | ||
|
||
} | ||
|
||
@Dependent | ||
@Priority(1) | ||
@Decorator | ||
static class TrimConverterDecorator implements Converter<String> { | ||
|
||
@Inject | ||
@Any | ||
@Delegate | ||
Converter<String> delegate; | ||
|
||
@Inject | ||
@Decorated | ||
Bean<?> decorated; | ||
|
||
@Override | ||
public String convert(String value) { | ||
return decorated.getBeanClass().getName() + " " + decorated.getQualifiers().stream() | ||
.sorted(Comparator.comparing(a -> a.annotationType().getName())).toList(); | ||
} | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rc/tests/src/test/java/io/quarkus/arc/test/decorators/decorated/InvalidDecoratedTest.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,40 @@ | ||
package io.quarkus.arc.test.decorators.decorated; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.Decorated; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InvalidDecoratedTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = ArcTestContainer.builder() | ||
.beanClasses(InvalidBean.class) | ||
.shouldFail() | ||
.build(); | ||
|
||
@Test | ||
public void testDecoration() { | ||
assertNotNull(container.getFailure()); | ||
assertTrue(container.getFailure().getMessage().startsWith( | ||
"Invalid injection of @Decorated Bean<T>, can only be injected into decorators but was detected in: " | ||
+ InvalidBean.class.getName() + "#decorated")); | ||
} | ||
|
||
@ApplicationScoped | ||
static class InvalidBean { | ||
|
||
@Inject | ||
@Decorated | ||
Bean<?> decorated; | ||
|
||
} | ||
} |