-
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 #16281 from sirf/main
Allow subclasses to be passed as parameter to intercepted method
- Loading branch information
Showing
5 changed files
with
108 additions
and
2 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
23 changes: 23 additions & 0 deletions
23
...arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptor.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,23 @@ | ||
package io.quarkus.arc.test.interceptors.parameters; | ||
|
||
import javax.annotation.Priority; | ||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.Interceptor; | ||
import javax.interceptor.InvocationContext; | ||
|
||
@Simple | ||
@Priority(1) | ||
@Interceptor | ||
public class ParamInterceptor { | ||
|
||
@AroundInvoke | ||
Object interceptParameters(InvocationContext ctx) throws Exception { | ||
|
||
Object[] params = ctx.getParameters(); | ||
if (params.length == 1 && params[0] != null) { | ||
params[0] = params[0].getClass().getSimpleName(); | ||
ctx.setParameters(params); | ||
} | ||
return ctx.proceed(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptorTest.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,42 @@ | ||
package io.quarkus.arc.test.interceptors.parameters; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
import io.quarkus.arc.InstanceHandle; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class ParamInterceptorTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(SimpleBean.class, Simple.class, ParamInterceptor.class); | ||
|
||
@Test | ||
public void testInterception() { | ||
|
||
ArcContainer arc = Arc.container(); | ||
|
||
InstanceHandle<SimpleBean> handle = arc.instance(SimpleBean.class); | ||
SimpleBean simpleBean = handle.get(); | ||
assertNull(simpleBean.getVal()); | ||
|
||
simpleBean.setVal("intercept"); | ||
assertEquals(String.class.getSimpleName(), simpleBean.getVal()); | ||
|
||
simpleBean.setVal(null); | ||
assertNull(simpleBean.getVal()); | ||
|
||
simpleBean.setVal(new StringBuilder("intercept")); | ||
assertEquals(StringBuilder.class.getSimpleName(), simpleBean.getVal()); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
simpleBean.setStringBuilderVal(new StringBuilder("intercept")); | ||
}); | ||
handle.destroy(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/Simple.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,18 @@ | ||
package io.quarkus.arc.test.interceptors.parameters; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.interceptor.InterceptorBinding; | ||
|
||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
public @interface Simple { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...jects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/SimpleBean.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,23 @@ | ||
package io.quarkus.arc.test.interceptors.parameters; | ||
|
||
import javax.enterprise.context.Dependent; | ||
|
||
@Dependent | ||
public class SimpleBean { | ||
|
||
private CharSequence val; | ||
|
||
@Simple | ||
void setVal(CharSequence val) { | ||
this.val = val; | ||
} | ||
|
||
@Simple | ||
void setStringBuilderVal(StringBuilder val) { | ||
this.val = val; | ||
} | ||
|
||
String getVal() { | ||
return val != null ? val.toString() : null; | ||
} | ||
} |