Skip to content

Commit

Permalink
Merge pull request #16317 from sirf/fix-16268
Browse files Browse the repository at this point in the history
Allow modifying primitive parameters in an interceptor
  • Loading branch information
gsmet authored Apr 8, 2021
2 parents 3e5ae81 + beb843d commit 599979f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
}
Original file line number Diff line number Diff line change
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

0 comments on commit 599979f

Please sign in to comment.