Coerce arguments to match the method's parameter types. Handles common numeric type mismatches from JSON parsing (e.g., Integer→Long, Integer→Double, Long→Double).
(Method m, Object[] args)
| 682 | * (e.g., Integer→Long, Integer→Double, Long→Double). |
| 683 | */ |
| 684 | private static void coerceArgs(Method m, Object[] args) { |
| 685 | Class<?>[] ptypes = m.getParameterTypes(); |
| 686 | int count = Math.min(args.length, ptypes.length); |
| 687 | for (int i = 0; i < count; i++) { |
| 688 | if (args[i] == null) continue; |
| 689 | Class<?> expected = ptypes[i]; |
| 690 | if (expected == Object.class || expected == Object[].class) continue; |
| 691 | if (expected.isInstance(args[i])) continue; |
| 692 | |
| 693 | // Numeric coercion |
| 694 | if (args[i] instanceof Number n) { |
| 695 | if (expected == Long.class || expected == long.class) { |
| 696 | args[i] = n.longValue(); |
| 697 | } else if (expected == Double.class || expected == double.class) { |
| 698 | args[i] = n.doubleValue(); |
| 699 | } else if (expected == Integer.class || expected == int.class) { |
| 700 | args[i] = n.intValue(); |
| 701 | } else if (expected == Float.class || expected == float.class) { |
| 702 | args[i] = n.floatValue(); |
| 703 | } |
| 704 | } |
| 705 | // String coercion for numeric strings |
| 706 | else if (args[i] instanceof String s && (expected == Long.class || expected == Double.class)) { |
| 707 | try { |
| 708 | if (expected == Long.class) args[i] = Long.parseLong(s); |
| 709 | else args[i] = Double.parseDouble(s); |
| 710 | } catch (NumberFormatException ignored) {} |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | private static Object[] adaptForVarArgs(Method m, Object[] args) { |
| 716 | if (!m.isVarArgs()) return args; |
no test coverage detected