#`Class<>#isAssignableFrom` does not recognize subinterfaces

1 messages · Page 1 of 1 (latest)

grim siren
#

I realized that Class<>#isasignablefrom does not work in cases like this:

public interface Foo {}

public interface Bar extends Foo {}

public class Baz extends Bar {}

var baz = new Baz()

baz::class.isassignablefrom(Foo::class) // Returns false even having Foo as an inheritance

If the class to be verified implements bar and I check with FOO, it will not be true, even if technically is

In my case, I'm doing it in Kotlin

val buz = if (type.isAssignableFrom(IModBusEvent::class.java)) modBus else FORGE_BUS

Always returns the FORGE_BUS, because verification is always false, the classes verified implement interfaces that extend IModBusvEnt instead of implementing it directly, but does not work in this case

Is there any way to make this work?

onyx bridgeBOT
#

<@&1008423204219531294> please have a look, thanks.

hardy schooner
#

I think you have it backwards...

Class.isAssignableFrom(Class)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Baz is not the same as or a superclass of Foo. Baz implements an interface Bar that extends Foo.

grim siren
#

it worked, that's it, I reversed, hehe, thank you very much

if (IModBusEvent::class.java.isAssignableFrom(type)) modBus else FORGE_BUS