forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC: fix in-application build compatible extensions
In case the _application_ contains a build compatible extension that registers a synthetic bean for a _non-application_ class, or a synthetic observer declared "as if" in a _non-application_ class, the generated classes must be forced to also be application classes. If the generated classes were non-application classes (as they were prior to this commit), they would not see the creator/disposer/observer classes in Quarkus dev mode and test mode (where application classes are in a different class loader than non-application classes). Before this commit, it has already been possible to force ArC to generate synthetic bean classes as application classes, but this was not possible for synthetic observers. This commit adds that, too.
- Loading branch information
Showing
14 changed files
with
263 additions
and
14 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
96 changes: 96 additions & 0 deletions
96
...loyment/src/test/java/io/quarkus/arc/test/cdi/bcextensions/SynthBeanForExternalClass.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,96 @@ | ||
package io.quarkus.arc.test.cdi.bcextensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; | ||
import jakarta.enterprise.inject.build.compatible.spi.Parameters; | ||
import jakarta.enterprise.inject.build.compatible.spi.Synthesis; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanCreator; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanDisposer; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.supplement.SomeClassInExternalLibrary; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SynthBeanForExternalClass { | ||
// the test includes an _application_ that declares a build compatible extension | ||
// (in the Runtime CL), which creates a synthetic bean for a class that is _outside_ | ||
// of the application (in the Base Runtime CL) | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyBean.class, MyExtension.class, MySyntheticBeanCreator.class) | ||
.addAsServiceProvider(BuildCompatibleExtension.class, MyExtension.class)) | ||
// we need a non-application archive, so cannot use `withAdditionalDependency()` | ||
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-arc-test-supplement", Version.getVersion()))); | ||
|
||
@Inject | ||
MyBean bean; | ||
|
||
@Test | ||
public void test() { | ||
assertFalse(MySyntheticBeanCreator.created); | ||
assertFalse(MySyntheticBeanDisposer.disposed); | ||
|
||
assertEquals("OK", bean.doSomething()); | ||
assertTrue(MySyntheticBeanCreator.created); | ||
assertTrue(MySyntheticBeanDisposer.disposed); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
Instance<SomeClassInExternalLibrary> someClass; | ||
|
||
public String doSomething() { | ||
SomeClassInExternalLibrary instance = someClass.get(); | ||
instance.toString(); // force instantiating the bean | ||
someClass.destroy(instance); // force destroying the instance | ||
return "OK"; | ||
} | ||
} | ||
|
||
public static class MyExtension implements BuildCompatibleExtension { | ||
@Synthesis | ||
public void synthesis(SyntheticComponents syn) { | ||
syn.addBean(SomeClassInExternalLibrary.class) | ||
.type(SomeClassInExternalLibrary.class) | ||
.scope(Dependent.class) | ||
.createWith(MySyntheticBeanCreator.class) | ||
.disposeWith(MySyntheticBeanDisposer.class); | ||
} | ||
} | ||
|
||
public static class MySyntheticBeanCreator implements SyntheticBeanCreator<SomeClassInExternalLibrary> { | ||
public static boolean created; | ||
|
||
public SomeClassInExternalLibrary create(Instance<Object> lookup, Parameters params) { | ||
SomeClassInExternalLibrary result = new SomeClassInExternalLibrary(); | ||
created = true; | ||
return result; | ||
} | ||
} | ||
|
||
public static class MySyntheticBeanDisposer implements SyntheticBeanDisposer<SomeClassInExternalLibrary> { | ||
public static boolean disposed; | ||
|
||
@Override | ||
public void dispose(SomeClassInExternalLibrary instance, Instance<Object> lookup, Parameters params) { | ||
disposed = true; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
.../src/test/java/io/quarkus/arc/test/cdi/bcextensions/SynthObserverAsIfInExternalClass.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,79 @@ | ||
package io.quarkus.arc.test.cdi.bcextensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Event; | ||
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; | ||
import jakarta.enterprise.inject.build.compatible.spi.Parameters; | ||
import jakarta.enterprise.inject.build.compatible.spi.Synthesis; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticObserver; | ||
import jakarta.enterprise.inject.spi.EventContext; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.supplement.SomeClassInExternalLibrary; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SynthObserverAsIfInExternalClass { | ||
// the test includes an _application_ that declares a build compatible extension | ||
// (in the Runtime CL), which creates a synthetic observer which is "as if" declared | ||
// in a class that is _outside_ of the application (in the Base Runtime CL) | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyBean.class, MyExtension.class, MySyntheticObserver.class) | ||
.addAsServiceProvider(BuildCompatibleExtension.class, MyExtension.class)) | ||
// we need a non-application archive, so cannot use `withAdditionalDependency()` | ||
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-arc-test-supplement", Version.getVersion()))); | ||
|
||
@Inject | ||
MyBean bean; | ||
|
||
@Test | ||
public void test() { | ||
assertFalse(MySyntheticObserver.notified); | ||
|
||
assertEquals("OK", bean.doSomething()); | ||
assertTrue(MySyntheticObserver.notified); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
Event<String> event; | ||
|
||
public String doSomething() { | ||
event.fire(""); // force notifying the observer | ||
return "OK"; | ||
} | ||
} | ||
|
||
public static class MyExtension implements BuildCompatibleExtension { | ||
@Synthesis | ||
public void synthesis(SyntheticComponents syn) { | ||
syn.addObserver(String.class) | ||
.declaringClass(SomeClassInExternalLibrary.class) | ||
.observeWith(MySyntheticObserver.class); | ||
} | ||
} | ||
|
||
public static class MySyntheticObserver implements SyntheticObserver<String> { | ||
public static boolean notified; | ||
|
||
@Override | ||
public void observe(EventContext<String> event, Parameters params) { | ||
notified = true; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-arc-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-arc-test-supplement</artifactId> | ||
<name>Quarkus - ArC - Test Supplement</name> | ||
<description>Supplement archive for ArC tests</description> | ||
|
||
</project> |
4 changes: 4 additions & 0 deletions
4
...t-supplement/src/main/java/io/quarkus/arc/test/supplement/SomeClassInExternalLibrary.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,4 @@ | ||
package io.quarkus.arc.test.supplement; | ||
|
||
public class SomeClassInExternalLibrary { | ||
} |
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
Oops, something went wrong.