I need to call Class.getMethod(java.lang.String, java.lang.Class...) to get a method where one of the varargs parameters is a varargs.
Currently I'm trying:
// to get jdbcTemplate.queryForObject(RowMapper, Object...)
jdbcTemplate.getClass().getMethod("queryForObject", RowMapper.class, Object.class);
Which results, not surprisingly in
Caused by: java.lang.NoSuchMethodException: org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForObject(java.lang.String, org.springframework.jdbc.core.RowMapper, java.lang.Object)
at java.lang.Class.throwNoSuchMethodException(Class.java:283)
at java.lang.Class.getMethod(Class.java:825)
How can I do this?
You need to provide an array type instead:
getMethod("queryForObject", RowMapper.class, Object[].class);
Basically a varargs parameter is an array, just with an extra bit of metadata telling the compiler to allow that array to be specified as a sequence of elements instead of a single expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With