Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.getMethod when parameter is varargs

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?

like image 807
Synesso Avatar asked Apr 15 '11 05:04

Synesso


1 Answers

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.

like image 186
Jon Skeet Avatar answered Oct 05 '22 23:10

Jon Skeet