RFR: 8280035: Use Class.isInstance instead of Class.isAssignableFrom where applicable

Andrey Turbanov aturbanov at openjdk.java.net
Mon Feb 21 12:19:52 UTC 2022


On Thu, 20 Jan 2022 01:34:20 GMT, Phil Race <prr at openjdk.org> wrote:

>In short I see insufficient value in the changes here and would prefer you drop the client part so I don't have to worry about it.

I think, usage of `isInstance` is much clear for most java developers. Everyone knows about java _instanceof_ operator. And `isInstance` method javadoc is very straight forward: `This method is the dynamic equivalent of the Java language instanceof operator.`

Method `isAssignableFrom` is opposite: it brings unnecessary complexity in the code. And it's easy to confuse orders of parameters. Even JBS confirms that:
1. [Wrong isAssignableFrom test when adding Principal to Subject ](https://bugs.openjdk.java.net/browse/JDK-8034820)
2. [isAssignableFrom checks in KeyFactorySpi.engineGetKeySpec appear to be backwards](https://bugs.openjdk.java.net/browse/JDK-8254717)
3. [isAssignableFrom checks in AlgorithmParametersSpi.engineGetParameterSpec appear to be backwards](https://bugs.openjdk.java.net/browse/JDK-8279800)

So, it gives all 3 points to prefer isInstance method: it's shorter, it's clearer for most java developers, it's faster.

-------------

PR: https://git.openjdk.java.net/jdk/pull/7061



More information about the client-libs-dev mailing list