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

Class<?> Feeds me from a third-party .jar related to the type of the method parameter. I need to perform different actions based on the fact that this is actually a Class<?> .

If this is some kind of subclass of java.lang.Number (e.g. Integer or BigDecimal ), I need to follow a special course of action.

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

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

Then it will execute if Class<?> Is just Number . How can I include inheritance in a mix to get all subtypes of Number ?

Thanks in advance!

+4
source share
1 answer
 Number.class.isAssignableFrom(someClass); 

That should do the trick

+14
source

Source: https://habr.com/ru/post/1391121/


All Articles