Skip to content

Commit cdc3c52

Browse files
authored
Replace isAssignableFrom() with isInstance() where feasible
Closes gh-36899 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 0c60266 commit cdc3c52

31 files changed

Lines changed: 37 additions & 47 deletions

File tree

framework-docs/modules/ROOT/pages/integration/jmx/notifications.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ example writes notifications to the console:
3232
}
3333
3434
public boolean isNotificationEnabled(Notification notification) {
35-
return AttributeChangeNotification.class.isAssignableFrom(notification.getClass());
35+
return (notification instanceof AttributeChangeNotification);
3636
}
3737
3838
}

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterThrowingAdvice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void setThrowingName(String name) {
7575
* is only invoked if the thrown exception is a subtype of the given throwing type.
7676
*/
7777
private boolean shouldInvokeOnThrowing(Throwable ex) {
78-
return getDiscoveredThrowingType().isAssignableFrom(ex.getClass());
78+
return getDiscoveredThrowingType().isInstance(ex);
7979
}
8080

8181
}

spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, Bean
159159
return result;
160160
}
161161
catch (IllegalArgumentException ex) {
162-
if (factoryBean != null && !factoryMethod.getDeclaringClass().isAssignableFrom(factoryBean.getClass())) {
162+
if (factoryBean != null && !factoryMethod.getDeclaringClass().isInstance(factoryBean)) {
163163
throw new BeanInstantiationException(factoryMethod,
164164
"Illegal factory instance for factory method '" + factoryMethod.getName() + "'; " +
165165
"instance: " + factoryBean.getClass().getName(), ex);

spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private Object instantiateUserDefinedStrategy(
276276
strategyType.getName() + "]: a zero-argument constructor is required", ex);
277277
}
278278

279-
if (!strategyType.isAssignableFrom(result.getClass())) {
279+
if (!strategyType.isInstance(result)) {
280280
throw new IllegalArgumentException("Provided class name must be an implementation of " + strategyType);
281281
}
282282
return result;

spring-core/src/main/java/org/springframework/util/ReflectionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ private static Field[] getDeclaredFields(Class<?> clazz) {
768768
public static void shallowCopyFieldState(final Object src, final Object dest) {
769769
Assert.notNull(src, "Source for field copy cannot be null");
770770
Assert.notNull(dest, "Destination for field copy cannot be null");
771-
if (!src.getClass().isAssignableFrom(dest.getClass())) {
771+
if (!src.getClass().isInstance(dest)) {
772772
throw new IllegalArgumentException("Destination class [" + dest.getClass().getName() +
773773
"] must be same or subclass as source class [" + src.getClass().getName() + "]");
774774
}

spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati
7272
result = BooleanTypedValue.FALSE; // null is not an instanceof anything
7373
}
7474
else {
75-
result = BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
75+
result = BooleanTypedValue.forValue(rightClass.isInstance(leftValue));
7676
}
7777
this.type = rightClass;
7878
if (rightOperand instanceof TypeReference) {

spring-jdbc/src/main/java/org/springframework/jdbc/support/GeneratedKeyHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public GeneratedKeyHolder(List<Map<String, Object>> keyList) {
7878
Iterator<Object> keyIter = this.keyList.get(0).values().iterator();
7979
if (keyIter.hasNext()) {
8080
Object key = keyIter.next();
81-
if (key == null || !(keyType.isAssignableFrom(key.getClass()))) {
81+
if (key == null || !(keyType.isInstance(key))) {
8282
throw new DataRetrievalFailureException(
8383
"The generated key type is not supported. " +
8484
"Unable to cast [" + (key != null ? key.getClass().getName() : null) +

spring-jdbc/src/test/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistrarTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ void customErrorCodeTranslation() {
4545

4646
DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
4747
assertThat(exFor4200).as("Should have been translated").isNotNull();
48-
assertThat(BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())).as("Should have been instance of BadSqlGrammarException").isTrue();
48+
assertThat(exFor4200).isInstanceOf(BadSqlGrammarException.class);
4949

5050
DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
5151
assertThat(exFor2).as("Should have been translated").isNotNull();
52-
assertThat(TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())).as("Should have been instance of TransientDataAccessResourceException").isTrue();
52+
assertThat(exFor2).isInstanceOf(TransientDataAccessResourceException.class);
5353

5454
DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
5555
assertThat(exFor3).as("Should not have been translated").isNull();

spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected static IdGenerator getIdGenerator() {
205205
if (value == null) {
206206
return null;
207207
}
208-
if (!type.isAssignableFrom(value.getClass())) {
208+
if (!type.isInstance(value)) {
209209
throw new IllegalArgumentException("Incorrect type specified for header '" +
210210
key + "'. Expected [" + type + "] but actual type is [" + value.getClass() + "]");
211211
}

spring-messaging/src/main/java/org/springframework/messaging/handler/CompositeMessageCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public List<MessageCondition<?>> getMessageConditions() {
5757
@SuppressWarnings("unchecked")
5858
public <T extends MessageCondition<T>> T getCondition(Class<T> messageConditionType) {
5959
for (MessageCondition<?> condition : this.messageConditions) {
60-
if (messageConditionType.isAssignableFrom(condition.getClass())) {
60+
if (messageConditionType.isInstance(condition)) {
6161
return (T) condition;
6262
}
6363
}

0 commit comments

Comments
 (0)