Skip to content
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

Allow modifying primitive parameters in an interceptor #16317

Merged
merged 1 commit into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -77,7 +77,7 @@ protected void validateParameters(Object[] params) {
+ ", type: " + parameterTypes[i] + "]");
}
if (params[i] != null) {
if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) {
if (!Types.boxedClass(parameterTypes[i]).isAssignableFrom(Types.boxedClass(params[i].getClass()))) {
throw new IllegalArgumentException("The parameter type [" + params[i].getClass()
+ "] can not be assigned to the type for the target method [" + parameterTypes[i] + "]");
}
Expand Down
Expand Up @@ -15,7 +15,11 @@ Object interceptParameters(InvocationContext ctx) throws Exception {

Object[] params = ctx.getParameters();
if (params.length == 1 && params[0] != null) {
params[0] = params[0].getClass().getSimpleName();
if (params[0] instanceof CharSequence) {
params[0] = params[0].getClass().getSimpleName();
} else if (params[0] instanceof Number) {
params[0] = 123456;
}
ctx.setParameters(params);
}
return ctx.proceed();
Expand Down
Expand Up @@ -37,6 +37,16 @@ public void testInterception() {
assertThrows(IllegalArgumentException.class, () -> {
simpleBean.setStringBuilderVal(new StringBuilder("intercept"));
});

simpleBean.setPrimitiveIntVal(0);
assertEquals("123456", simpleBean.getVal());

simpleBean.setIntVal(1);
assertEquals("123456", simpleBean.getVal());

simpleBean.setNumberVal(2L);
assertEquals("123456", simpleBean.getVal());

handle.destroy();
}
}
Expand Up @@ -17,6 +17,21 @@ void setStringBuilderVal(StringBuilder val) {
this.val = val;
}

@Simple
void setNumberVal(final Number val) {
this.val = val != null ? val.toString() : null;
}

@Simple
void setPrimitiveIntVal(final int val) {
this.val = Integer.toString(val);
}

@Simple
void setIntVal(final Integer val) {
this.val = val != null ? val.toString() : null;
}

String getVal() {
return val != null ? val.toString() : null;
}
Expand Down