Skip to content

Commit

Permalink
Merge pull request #16281 from sirf/main
Browse files Browse the repository at this point in the history
Allow subclasses to be passed as parameter to intercepted method
  • Loading branch information
gsmet authored Apr 7, 2021
2 parents e37f1d2 + 1e3096b commit 1ddedc0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ protected void validateParameters(Object[] params) {
+ ", type: " + parameterTypes[i] + "]");
}
if (params[i] != null) {
if (!params[i].getClass().equals(parameterTypes[i])) {
if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) {
throw new IllegalArgumentException("The parameter type [" + params[i].getClass()
+ "] does not match the type for the target method [" + parameterTypes[i] + "]");
+ "] can not be assigned to the type for the target method [" + parameterTypes[i] + "]");
}
}
}
Expand Down
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();
}
}
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();
}
}
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 {

}
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;
}
}

0 comments on commit 1ddedc0

Please sign in to comment.