-
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.
Merge pull request #6098 from mkouba/issue-6024
ArC - introduce ObserverTransformer build extension
- Loading branch information
Showing
21 changed files
with
989 additions
and
360 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
20 changes: 20 additions & 0 deletions
20
.../arc/deployment/src/main/java/io/quarkus/arc/deployment/ObserverTransformerBuildItem.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,20 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import io.quarkus.arc.processor.ObserverTransformer; | ||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
/** | ||
* This build item is used to register an {@link ObserverTransformer} instance. | ||
*/ | ||
public final class ObserverTransformerBuildItem extends MultiBuildItem { | ||
|
||
private final ObserverTransformer transformer; | ||
|
||
public ObserverTransformerBuildItem(ObserverTransformer transformer) { | ||
this.transformer = transformer; | ||
} | ||
|
||
public ObserverTransformer getInstance() { | ||
return transformer; | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...ns/arc/deployment/src/test/java/io/quarkus/arc/test/observer/ObserverTransformerTest.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,122 @@ | ||
package io.quarkus.arc.test.observer; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import javax.enterprise.event.Event; | ||
import javax.enterprise.event.Observes; | ||
import javax.inject.Qualifier; | ||
import javax.inject.Singleton; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Type; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.google.inject.Inject; | ||
|
||
import io.quarkus.arc.deployment.ObserverTransformerBuildItem; | ||
import io.quarkus.arc.processor.ObserverTransformer; | ||
import io.quarkus.builder.BuildChainBuilder; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ObserverTransformerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(MyObserver.class, AlphaQualifier.class, BravoQualifier.class)) | ||
.addBuildChainCustomizer(buildCustomizer()); | ||
|
||
static Consumer<BuildChainBuilder> buildCustomizer() { | ||
return new Consumer<BuildChainBuilder>() { | ||
|
||
@Override | ||
public void accept(BuildChainBuilder builder) { | ||
builder.addBuildStep(new BuildStep() { | ||
|
||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(new ObserverTransformerBuildItem(new ObserverTransformer() { | ||
|
||
@Override | ||
public boolean appliesTo(Type observedType, Set<AnnotationInstance> qualifiers) { | ||
return observedType.name().equals(DotName.createSimple(MyEvent.class.getName())); | ||
} | ||
|
||
@Override | ||
public void transform(TransformationContext context) { | ||
if (context.getMethod().name().equals("")) { | ||
context.transform().removeAll().done(); | ||
} | ||
} | ||
|
||
})); | ||
} | ||
}).produces(ObserverTransformerBuildItem.class).build(); | ||
} | ||
}; | ||
} | ||
|
||
@BravoQualifier | ||
@Inject | ||
Event<MyEvent> event; | ||
|
||
@Test | ||
public void testTransformation() { | ||
MyEvent myEvent = new MyEvent(); | ||
event.fire(myEvent); | ||
// MyObserver.onMyEventRemoveQualifiers() would not match without transformation | ||
assertEquals(1, myEvent.log.size()); | ||
assertEquals("onMyEventRemoveQualifiers", myEvent.log.get(0)); | ||
} | ||
|
||
@Singleton | ||
static class MyObserver { | ||
|
||
void onMyEventRemoveQualifiers(@Observes @BravoQualifier MyEvent event) { | ||
event.log.add("onMyEventRemoveQualifiers"); | ||
} | ||
|
||
} | ||
|
||
@Qualifier | ||
@Inherited | ||
@Target({ TYPE, METHOD, FIELD, PARAMETER }) | ||
@Retention(RUNTIME) | ||
public @interface AlphaQualifier { | ||
|
||
} | ||
|
||
@Qualifier | ||
@Inherited | ||
@Target({ TYPE, METHOD, FIELD, PARAMETER }) | ||
@Retention(RUNTIME) | ||
public @interface BravoQualifier { | ||
|
||
} | ||
|
||
static class MyEvent { | ||
|
||
final List<String> log = new ArrayList<>(); | ||
|
||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
...e/src/main/java/io/quarkus/smallrye/faulttolerance/runtime/HystrixInitializerStarter.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,24 @@ | ||
package io.quarkus.smallrye.faulttolerance.runtime; | ||
|
||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.event.Observes; | ||
|
||
import io.quarkus.runtime.StartupEvent; | ||
import io.smallrye.faulttolerance.HystrixInitializer; | ||
|
||
@Dependent | ||
public class HystrixInitializerStarter { | ||
|
||
/** | ||
* This is a replacement for <code>io.smallrye.faulttolerance.HystrixInitializer.init(Object)</code> observer method which | ||
* is vetoed because we don't want initialize Hystrix during static init. | ||
* | ||
* @param event | ||
* @param initializer | ||
*/ | ||
void startup(@Observes StartupEvent event, HystrixInitializer initializer) { | ||
// HystrixInitializer is a normal scoped bean so we have to invoke a method upon the injected proxy to force the instantiation | ||
initializer.toString(); | ||
} | ||
|
||
} |
Oops, something went wrong.