-
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 #44947 from Ladicek/arc-add-interception-proxy-sub…
…class ArC: add InterceptionProxySubclass
- Loading branch information
Showing
5 changed files
with
201 additions
and
7 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
33 changes: 33 additions & 0 deletions
33
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InterceptionProxySubclass.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,33 @@ | ||
package io.quarkus.arc; | ||
|
||
/** | ||
* Represents an interception proxy. Typically, interception is performed by creating a subclass | ||
* of the original class and arranging bean instantiation such that the contextual instance | ||
* is in fact an instance of the subclass, but that isn't always possible. In case of | ||
* {@link InterceptionProxy}, interception is performed by a proxy that delegates to the actual | ||
* contextual instance. Such proxy implements this interface. | ||
*/ | ||
public interface InterceptionProxySubclass extends Subclass { | ||
/** | ||
* @return the contextual instance | ||
*/ | ||
Object arc_delegate(); | ||
|
||
/** | ||
* Attempts to unwrap the object if it represents an interception proxy. | ||
* <p> | ||
* This method should only be used with caution. If you unwrap an interception proxy, | ||
* then certain key functionality will not work as expected. | ||
* | ||
* @param <T> the type of the object to unwrap | ||
* @param obj the object to unwrap | ||
* @return the contextual instance if the object represents an interception proxy, the object otherwise | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
static <T> T unwrap(T obj) { | ||
if (obj instanceof InterceptionProxySubclass proxy) { | ||
return (T) proxy.arc_delegate(); | ||
} | ||
return obj; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
.../io/quarkus/arc/test/interceptors/producer/InterceptionProxySubclassNormalScopedTest.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,83 @@ | ||
package io.quarkus.arc.test.interceptors.producer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNotSame; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ClientProxy; | ||
import io.quarkus.arc.InterceptionProxy; | ||
import io.quarkus.arc.InterceptionProxySubclass; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InterceptionProxySubclassNormalScopedTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(MyBinding.class, MyInterceptor.class, MyProducer.class); | ||
|
||
@Test | ||
public void test() { | ||
MyNonbean nonbean = Arc.container().instance(MyNonbean.class).get(); | ||
assertEquals("intercepted: hello", nonbean.hello()); | ||
|
||
assertInstanceOf(ClientProxy.class, nonbean); | ||
assertNotNull(ClientProxy.unwrap(nonbean)); | ||
assertNotSame(nonbean, ClientProxy.unwrap(nonbean)); | ||
|
||
MyNonbean unwrapped = ClientProxy.unwrap(nonbean); | ||
|
||
assertInstanceOf(InterceptionProxySubclass.class, unwrapped); | ||
assertNotNull(InterceptionProxySubclass.unwrap(unwrapped)); | ||
assertNotSame(unwrapped, InterceptionProxySubclass.unwrap(unwrapped)); | ||
assertNotSame(nonbean, InterceptionProxySubclass.unwrap(unwrapped)); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR }) | ||
@InterceptorBinding | ||
@interface MyBinding { | ||
} | ||
|
||
@MyBinding | ||
@Priority(1) | ||
@Interceptor | ||
static class MyInterceptor { | ||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
return "intercepted: " + ctx.proceed(); | ||
} | ||
} | ||
|
||
static class MyNonbean { | ||
@MyBinding | ||
String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
@Dependent | ||
static class MyProducer { | ||
@Produces | ||
@ApplicationScoped | ||
MyNonbean produce(InterceptionProxy<MyNonbean> proxy) { | ||
return proxy.create(new MyNonbean()); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.../io/quarkus/arc/test/interceptors/producer/InterceptionProxySubclassPseudoScopedTest.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.interceptors.producer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNotSame; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.inject.Singleton; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.InterceptionProxy; | ||
import io.quarkus.arc.InterceptionProxySubclass; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InterceptionProxySubclassPseudoScopedTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(MyBinding.class, MyInterceptor.class, MyProducer.class); | ||
|
||
@Test | ||
public void test() { | ||
MyNonbean nonbean = Arc.container().instance(MyNonbean.class).get(); | ||
assertEquals("intercepted: hello", nonbean.hello()); | ||
|
||
assertInstanceOf(InterceptionProxySubclass.class, nonbean); | ||
assertNotNull(InterceptionProxySubclass.unwrap(nonbean)); | ||
assertNotSame(nonbean, InterceptionProxySubclass.unwrap(nonbean)); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR }) | ||
@InterceptorBinding | ||
@interface MyBinding { | ||
} | ||
|
||
@MyBinding | ||
@Priority(1) | ||
@Interceptor | ||
static class MyInterceptor { | ||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
return "intercepted: " + ctx.proceed(); | ||
} | ||
} | ||
|
||
static class MyNonbean { | ||
@MyBinding | ||
String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
@Dependent | ||
static class MyProducer { | ||
@Produces | ||
@Singleton | ||
MyNonbean produce(InterceptionProxy<MyNonbean> proxy) { | ||
return proxy.create(new MyNonbean()); | ||
} | ||
} | ||
} |