-
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
9 changed files
with
199 additions
and
5 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 io.quarkus.arc.InjectableReferenceProvider; | ||
import jakarta.enterprise.context.spi.Contextual; | ||
import jakarta.enterprise.context.spi.CreationalContext; | ||
import jakarta.enterprise.inject.Decorated; | ||
import jakarta.enterprise.inject.Intercepted; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
|
||
import static io.quarkus.arc.impl.CreationalContextImpl.unwrap; | ||
|
||
/** | ||
* {@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; | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...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,73 @@ | ||
package io.quarkus.arc.test.decorators.decorated; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import io.quarkus.arc.test.MyQualifier; | ||
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 java.util.Comparator; | ||
|
||
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(); | ||
} | ||
|
||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...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,49 @@ | ||
package io.quarkus.arc.test.decorators.decorated; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import io.quarkus.arc.test.MyQualifier; | ||
import io.quarkus.arc.test.decorators.validation.DecoratorWithProducerMethodTest; | ||
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 java.util.Comparator; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
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; | ||
|
||
} | ||
} |
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