jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes Passing null parameter list in getDeclaredMethod Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Passing null parameter list in getDeclaredMethod" Watch "Passing null parameter list in getDeclaredMethod" New topic
Author

Passing null parameter list in getDeclaredMethod

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 208
I am trying to use getDeclaredMethod and get a warning (not an error) when I try to pass null for my parameter list. I'd like to eliminate that warning but I can't seem to come up with the right syntax. The warning wants me to explicitly cast to Class<?> but everything I've tried results in an error.
This is the warning code:

This is apparently what I need but can't get right:

Can someone give me the correct syntax? TIA.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
It doesn’t want null. Try an empty array
(Class<?>[])new Object[]{}
instead.
By the way, what is giving you that warning? Is it an IDE?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
You might not be able to cast that, in which case try
new Class[]{}
You may have to miss out the <?> or you might be able to cast it like this
(Class<?>[])new Class[]{}
You are not supposed to be able to create an array of a parameterised type.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 208
Thanks for the reply and yes it is an IDE giving me that warning. Your solution (new Class[]{}) fixed it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
If you're using JDK 5 or later, you can take advantage of varargs - you don't need to explicitly create an array at all:

Since there are no Class arguments, the compiler inserts code to automatically create an empty array, just as you did.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Passing null parameter list in getDeclaredMethod
 
Similar Threads
doubt generics
Generics & overriding
Reflection : Using getDeclaredMethod without method arguments
Passing an array into a vararg method (in Reflection API)
generic confusion