diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java index 37922364e5f..232c53234c6 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java @@ -340,13 +340,13 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC * @see #extractStereotype(TypeFilter) */ private boolean indexSupportsIncludeFilter(TypeFilter filter) { - if (filter instanceof AnnotationTypeFilter) { - Class annotation = ((AnnotationTypeFilter) filter).getAnnotationType(); - return (AnnotationUtils.isAnnotationDeclaredLocally(Indexed.class, annotation) || - annotation.getName().startsWith("javax.")); + if (filter instanceof AnnotationTypeFilter annotationTypeFilter) { + Class annotationType = annotationTypeFilter.getAnnotationType(); + return (AnnotationUtils.isAnnotationDeclaredLocally(Indexed.class, annotationType) || + annotationType.getName().startsWith("javax.")); } - if (filter instanceof AssignableTypeFilter) { - Class target = ((AssignableTypeFilter) filter).getTargetType(); + if (filter instanceof AssignableTypeFilter assignableTypeFilter) { + Class target = assignableTypeFilter.getTargetType(); return AnnotationUtils.isAnnotationDeclaredLocally(Indexed.class, target); } return false; @@ -361,11 +361,11 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC */ @Nullable private String extractStereotype(TypeFilter filter) { - if (filter instanceof AnnotationTypeFilter) { - return ((AnnotationTypeFilter) filter).getAnnotationType().getName(); + if (filter instanceof AnnotationTypeFilter annotationTypeFilter) { + return annotationTypeFilter.getAnnotationType().getName(); } - if (filter instanceof AssignableTypeFilter) { - return ((AssignableTypeFilter) filter).getTargetType().getName(); + if (filter instanceof AssignableTypeFilter assignableTypeFilter) { + return assignableTypeFilter.getTargetType().getName(); } return null; } @@ -536,10 +536,10 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC * Clear the local metadata cache, if any, removing all cached class metadata. */ public void clearCache() { - if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { + if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory cmrf) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it'll be cleared by the ApplicationContext. - ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); + cmrf.clearCache(); } } diff --git a/spring-context/src/test/java/example/scannable/DefaultNamedComponent.java b/spring-context/src/test/java/example/scannable/DefaultNamedComponent.java index 1f9f65ff798..1047c936587 100644 --- a/spring-context/src/test/java/example/scannable/DefaultNamedComponent.java +++ b/spring-context/src/test/java/example/scannable/DefaultNamedComponent.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package example.scannable; - /** * @author Juergen Hoeller */ diff --git a/spring-context/src/testFixtures/java/org/springframework/context/testfixture/index/CandidateComponentsTestClassLoader.java b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/index/CandidateComponentsTestClassLoader.java index f59759f5108..122bb95c94d 100644 --- a/spring-context/src/testFixtures/java/org/springframework/context/testfixture/index/CandidateComponentsTestClassLoader.java +++ b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/index/CandidateComponentsTestClassLoader.java @@ -24,6 +24,7 @@ import java.util.stream.Stream; import org.springframework.context.index.CandidateComponentsIndexLoader; import org.springframework.core.io.Resource; +import org.springframework.lang.Nullable; /** * A test {@link ClassLoader} that can be used in a testing context to control the @@ -35,7 +36,7 @@ import org.springframework.core.io.Resource; public class CandidateComponentsTestClassLoader extends ClassLoader { /** - * Create a test {@link ClassLoader} that disable the use of the index, even + * Create a test {@link ClassLoader} that disables the use of the index, even * if resources are present at the standard location. * @param classLoader the classloader to use for all other operations * @return a test {@link ClassLoader} that has no index @@ -48,8 +49,9 @@ public class CandidateComponentsTestClassLoader extends ClassLoader { /** * Create a test {@link ClassLoader} that creates an index with the - * specified {@link Resource} instances + * specified {@link Resource} instances. * @param classLoader the classloader to use for all other operations + * @param resources the resources for index files * @return a test {@link ClassLoader} with an index built based on the * specified resources. */ @@ -66,8 +68,10 @@ public class CandidateComponentsTestClassLoader extends ClassLoader { } + @Nullable private final Enumeration resourceUrls; + @Nullable private final IOException cause; public CandidateComponentsTestClassLoader(ClassLoader classLoader, Enumeration resourceUrls) {