1
votes

I'm working in Kotlin and getting a class like such:

val payloadClass =  try {
    Class.forName("com.something.SomeClass")
} catch (e: ClassNotFoundException) {
   null
}

It's successfully finding the class however when I then go to check whether it implements some interface, it keeps returning false:

To check I'm using the keyword is:

so payloadClass is AGreatInterface.

Is there some reason why this wouldn't work. I've used is elsewhere and it works as expected, wondering if it's something to do with Class.forName.

I used payloadClass.interfaces.contains(AGreatInterface::class.java) and that worked. But why wouldn't is work?

Any ideas appreciated.

Thanks.

1
use (AGreatInterface::class.java).isAssignableFrom(payloadClass)Matt Timmermans

1 Answers

4
votes

The reason is that payloadClass is not a AGreatInterface, but a java.lang.Class instance.

If you would create an instance of the class object represented by payloadClass, let's say payloadInstance, then the payloadInstance is AGreatInterface test would succeed.