Class beanClass = ???
If (beanClass.newInstance() instanceof TestBean) ???

The above code is tries to use the API, it tries to find a way to check for inheritance, but it didn’t find a way to do it. So it just created a new instance and uses the instanceof operator. It is dangerous to create an instance of a class you don’t know. You never know what this class does, it could be very costly or the default constructor may not even exist. The right way to do this check is to use the Class.isAssignableFrom(Class) method:

Class beanClass = ???
If (TestBean.class.isAssignableFrom(beanClass)) ???

Source link


Please enter your comment!
Please enter your name here