From de74e0e25a195084745891419f0c4a8ad286560c Mon Sep 17 00:00:00 2001 From: Andrey Turbanov Date: Thu, 19 May 2022 21:43:32 +0000 Subject: [PATCH] 8280035: Use Class.isInstance instead of Class.isAssignableFrom where applicable Reviewed-by: prr, rriggs --- .../classes/java/lang/reflect/AccessibleObject.java | 2 +- .../internal/reflect/UnsafeObjectFieldAccessorImpl.java | 4 ++-- .../reflect/UnsafeQualifiedObjectFieldAccessorImpl.java | 4 ++-- .../UnsafeQualifiedStaticObjectFieldAccessorImpl.java | 4 ++-- .../reflect/UnsafeStaticObjectFieldAccessorImpl.java | 4 ++-- .../classes/com/sun/beans/decoder/NewElementHandler.java | 4 ++-- .../share/classes/javax/imageio/spi/ServiceRegistry.java | 9 ++++----- .../classes/javax/swing/plaf/nimbus/Defaults.template | 6 ++---- .../classes/sun/tools/jconsole/inspector/Utils.java | 4 ++-- .../share/classes/jdk/jfr/internal/TypeLibrary.java | 6 +++--- 10 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java b/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java index 2b620967a8a..0fd1ab9d911 100644 --- a/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java +++ b/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java @@ -481,7 +481,7 @@ public class AccessibleObject implements AnnotatedElement { } // if this object is an instance member, the given object // must be a subclass of the declaring class of this reflected object - if (!declaringClass.isAssignableFrom(obj.getClass())) { + if (!declaringClass.isInstance(obj)) { throw new IllegalArgumentException("object is not an instance of " + declaringClass.getName()); } diff --git a/src/java.base/share/classes/jdk/internal/reflect/UnsafeObjectFieldAccessorImpl.java b/src/java.base/share/classes/jdk/internal/reflect/UnsafeObjectFieldAccessorImpl.java index 40fad3fd027..0b267d86a15 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/UnsafeObjectFieldAccessorImpl.java +++ b/src/java.base/share/classes/jdk/internal/reflect/UnsafeObjectFieldAccessorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -77,7 +77,7 @@ class UnsafeObjectFieldAccessorImpl extends UnsafeFieldAccessorImpl { throwFinalFieldIllegalAccessException(value); } if (value != null) { - if (!field.getType().isAssignableFrom(value.getClass())) { + if (!field.getType().isInstance(value)) { throwSetIllegalArgumentException(value); } } diff --git a/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedObjectFieldAccessorImpl.java b/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedObjectFieldAccessorImpl.java index 22f007e3f26..da95cb3bbf1 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedObjectFieldAccessorImpl.java +++ b/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedObjectFieldAccessorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -79,7 +79,7 @@ class UnsafeQualifiedObjectFieldAccessorImpl throwFinalFieldIllegalAccessException(value); } if (value != null) { - if (!field.getType().isAssignableFrom(value.getClass())) { + if (!field.getType().isInstance(value)) { throwSetIllegalArgumentException(value); } } diff --git a/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedStaticObjectFieldAccessorImpl.java b/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedStaticObjectFieldAccessorImpl.java index 7ea53e866a8..ed4ba40450d 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedStaticObjectFieldAccessorImpl.java +++ b/src/java.base/share/classes/jdk/internal/reflect/UnsafeQualifiedStaticObjectFieldAccessorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -77,7 +77,7 @@ class UnsafeQualifiedStaticObjectFieldAccessorImpl throwFinalFieldIllegalAccessException(value); } if (value != null) { - if (!field.getType().isAssignableFrom(value.getClass())) { + if (!field.getType().isInstance(value)) { throwSetIllegalArgumentException(value); } } diff --git a/src/java.base/share/classes/jdk/internal/reflect/UnsafeStaticObjectFieldAccessorImpl.java b/src/java.base/share/classes/jdk/internal/reflect/UnsafeStaticObjectFieldAccessorImpl.java index 4b80b669a7b..bf9bf6b0dbd 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/UnsafeStaticObjectFieldAccessorImpl.java +++ b/src/java.base/share/classes/jdk/internal/reflect/UnsafeStaticObjectFieldAccessorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -75,7 +75,7 @@ class UnsafeStaticObjectFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl throwFinalFieldIllegalAccessException(value); } if (value != null) { - if (!field.getType().isAssignableFrom(value.getClass())) { + if (!field.getType().isInstance(value)) { throwSetIllegalArgumentException(value); } } diff --git a/src/java.desktop/share/classes/com/sun/beans/decoder/NewElementHandler.java b/src/java.desktop/share/classes/com/sun/beans/decoder/NewElementHandler.java index f753e04e83a..6614be2c902 100644 --- a/src/java.desktop/share/classes/com/sun/beans/decoder/NewElementHandler.java +++ b/src/java.desktop/share/classes/com/sun/beans/decoder/NewElementHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -188,7 +188,7 @@ class NewElementHandler extends ElementHandler { return arguments; } Class type = types[index]; - if (type.isAssignableFrom(argument.getClass())) { + if (type.isInstance(argument)) { return arguments; } } diff --git a/src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java b/src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java index 64f71f734a9..3e0bc06b361 100644 --- a/src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java +++ b/src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,7 +25,6 @@ package javax.imageio.spi; -import java.io.File; import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; @@ -228,7 +227,7 @@ public class ServiceRegistry { private Iterator getSubRegistries(Object provider) { List l = new ArrayList<>(); for (Class c : categoryMap.keySet()) { - if (c.isAssignableFrom(provider.getClass())) { + if (c.isInstance(provider)) { l.add(categoryMap.get(c)); } } @@ -270,7 +269,7 @@ public class ServiceRegistry { if (reg == null) { throw new IllegalArgumentException("category unknown!"); } - if (!category.isAssignableFrom(provider.getClass())) { + if (!category.isInstance(provider)) { throw new ClassCastException(); } @@ -373,7 +372,7 @@ public class ServiceRegistry { if (reg == null) { throw new IllegalArgumentException("category unknown!"); } - if (!category.isAssignableFrom(provider.getClass())) { + if (!category.isInstance(provider)) { throw new ClassCastException(); } return reg.deregisterServiceProvider(provider); diff --git a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/Defaults.template b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/Defaults.template index 8a27e35b208..b0b78b554f9 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/Defaults.template +++ b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/Defaults.template @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -50,14 +50,12 @@ import java.awt.image.BufferedImage; import static java.awt.image.BufferedImage.*; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.lang.ref.WeakReference; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.WeakHashMap; import javax.swing.border.Border; import javax.swing.plaf.UIResource; @@ -565,7 +563,7 @@ ${UI_DEFAULT_INIT} //type registered, then check to make sure c is of the //right type; Class clazz = parts[partIndex].c; - if (clazz != null && clazz.isAssignableFrom(c.getClass())) { + if (clazz != null && clazz.isInstance(c)) { //so far so good, recurse return matches(c.getParent(), partIndex - 1); } else if (clazz == null && diff --git a/src/jdk.jconsole/share/classes/sun/tools/jconsole/inspector/Utils.java b/src/jdk.jconsole/share/classes/sun/tools/jconsole/inspector/Utils.java index b321cd3da86..01946219ab9 100644 --- a/src/jdk.jconsole/share/classes/sun/tools/jconsole/inspector/Utils.java +++ b/src/jdk.jconsole/share/classes/sun/tools/jconsole/inspector/Utils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -140,7 +140,7 @@ public class Utils { return false; } for (Object o : c) { - if (o == null || !e.isAssignableFrom(o.getClass())) { + if (o == null || !e.isInstance(o)) { return false; } } diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java index 1555bcc2aac..bd8e3ac4850 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -401,8 +401,8 @@ public final class TypeLibrary { Class ct = returnType.getComponentType(); if (Annotation.class.isAssignableFrom(ct) && ct.getAnnotation(Repeatable.class) != null) { Object res = m.invoke(a, new Object[0]); - if (res != null && Annotation[].class.isAssignableFrom(res.getClass())) { - for (Annotation rep : (Annotation[]) m.invoke(a, new Object[0])) { + if (res instanceof Annotation[] anns) { + for (Annotation rep : anns) { annos.add(rep); } repeated = true;