-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArC: add InterceptionProxySubclass #44947
Merged
gsmet
merged 1 commit into
quarkusio:main
from
Ladicek:arc-add-interception-proxy-subclass
Dec 7, 2024
+201
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I really like pattern matching for instanceof!