Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a class is of type java.lang.Number

Tags:

java

I'm being fed a Class<?> from a 3rd party .jar pertaining to a method's parameter type. I need to take different actions based on what this Class<?> actually is.

If it is anything that subclass java.lang.Number (such as Integer, or BigDecimal) I need to take a special course of action.

I can't use instanceof. And I'm afraid that if I use:

Class<?> someClass = getParameterTypeFrom3rdParty();
if(someClass == Number.class)
    // ...

Then it will execute if Class<?> is only a Number. How can I incorporate inheritance into the mix so I get all of Number's subtypes?

Thanks in advance!

like image 659
IAmYourFaja Avatar asked Dec 04 '22 05:12

IAmYourFaja


1 Answers

Number.class.isAssignableFrom(someClass); 

That should do the trick

like image 168
John Vint Avatar answered Dec 15 '22 00:12

John Vint