Skip to content

Commit e2ec066

Browse files
Merge pull request #40085 from FelixDes
* pr/40085: Apply instanceof pattern matching Closes gh-40085
2 parents 6825bda + 8a42935 commit e2ec066

File tree

23 files changed

+76
-78
lines changed

23 files changed

+76
-78
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/version/AbstractDependencyVersion.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public int compareTo(DependencyVersion other) {
4040

4141
@Override
4242
public boolean isUpgrade(DependencyVersion candidate, boolean movingToSnapshots) {
43-
ComparableVersion comparableCandidate = (candidate instanceof AbstractDependencyVersion)
44-
? ((AbstractDependencyVersion) candidate).comparableVersion
45-
: new ComparableVersion(candidate.toString());
43+
ComparableVersion comparableCandidate = (candidate instanceof AbstractDependencyVersion abstractDependencyVersion)
44+
? abstractDependencyVersion.comparableVersion : new ComparableVersion(candidate.toString());
4645
return comparableCandidate.compareTo(this.comparableVersion) > 0;
4746
}
4847

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/graphql/tester/HttpGraphQlTesterContextCustomizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
6666

6767
private void registerHttpGraphQlTester(ConfigurableApplicationContext context) {
6868
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
69-
if (beanFactory instanceof BeanDefinitionRegistry) {
70-
registerHttpGraphQlTester((BeanDefinitionRegistry) beanFactory);
69+
if (beanFactory instanceof BeanDefinitionRegistry beanDefinitionRegistry) {
70+
registerHttpGraphQlTester(beanDefinitionRegistry);
7171
}
7272
}
7373

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ static ModifiedClassPathClassLoader get(Class<?> testClass, Method testMethod, L
106106
private static Collection<AnnotatedElement> getAnnotatedElements(Object[] array) {
107107
Set<AnnotatedElement> result = new LinkedHashSet<>();
108108
for (Object item : array) {
109-
if (item instanceof AnnotatedElement) {
110-
result.add((AnnotatedElement) item);
109+
if (item instanceof AnnotatedElement annotatedElement) {
110+
result.add(annotatedElement);
111111
}
112112
else if (ObjectUtils.isArray(item)) {
113113
result.addAll(getAnnotatedElements(ObjectUtils.toObjectArray(item)));

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ private static class ClassExcludeFilter extends AbstractTypeHierarchyTraversingF
305305
ClassExcludeFilter(Object... sources) {
306306
super(false, false);
307307
for (Object source : sources) {
308-
if (source instanceof Class<?>) {
309-
this.classNames.add(((Class<?>) source).getName());
308+
if (source instanceof Class<?> classSource) {
309+
this.classNames.add(classSource.getName());
310310
}
311311
}
312312
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre
8686
*/
8787
protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) {
8888
MutablePropertySources sources = new MutablePropertySources();
89-
if (environment instanceof ConfigurableEnvironment) {
90-
((ConfigurableEnvironment) environment).getPropertySources().forEach(sources::addLast);
89+
if (environment instanceof ConfigurableEnvironment configurableEnvironment) {
90+
configurableEnvironment.getPropertySources().forEach(sources::addLast);
9191
}
9292
sources.addLast(getTitleSource(sourceClass));
9393
sources.addLast(getAnsiSource());

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private String getSourceDescription(Object source) {
9898

9999
@SuppressWarnings("unchecked")
100100
private Class<? extends AvailabilityState> getStateType(AvailabilityState state) {
101-
Class<?> type = (state instanceof Enum) ? ((Enum<?>) state).getDeclaringClass() : state.getClass();
101+
Class<?> type = (state instanceof Enum<?> enumState) ? enumState.getDeclaringClass() : state.getClass();
102102
return (Class<? extends AvailabilityState>) type;
103103
}
104104

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/AvailabilityChangeEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public ResolvableType getResolvableType() {
6060

6161
private Class<?> getStateType() {
6262
S state = getState();
63-
if (state instanceof Enum) {
64-
return ((Enum<?>) state).getDeclaringClass();
63+
if (state instanceof Enum<?> enumState) {
64+
return enumState.getDeclaringClass();
6565
}
6666
return state.getClass();
6767
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ private boolean isAutoDetected(ConfigurableEnvironment environment) {
127127
&& environmentPropertySource.containsProperty(KUBERNETES_SERVICE_PORT)) {
128128
return true;
129129
}
130-
if (environmentPropertySource instanceof EnumerablePropertySource) {
131-
return isAutoDetected((EnumerablePropertySource<?>) environmentPropertySource);
130+
if (environmentPropertySource instanceof EnumerablePropertySource<?> enumerablePropertySource) {
131+
return isAutoDetected(enumerablePropertySource);
132132
}
133133
}
134134
return false;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ else if (event instanceof ApplicationEnvironmentPreparedEvent environmentPrepare
225225
else if (event instanceof ApplicationPreparedEvent preparedEvent) {
226226
onApplicationPreparedEvent(preparedEvent);
227227
}
228-
else if (event instanceof ContextClosedEvent) {
229-
onContextClosedEvent((ContextClosedEvent) event);
228+
else if (event instanceof ContextClosedEvent contextClosedEvent) {
229+
onContextClosedEvent(contextClosedEvent);
230230
}
231231
else if (event instanceof ApplicationFailedEvent) {
232232
onApplicationFailedEvent();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ private static boolean hasSystemEnvironmentName(PropertySource<?> source) {
167167

168168
private static boolean isFullEnumerable(PropertySource<?> source) {
169169
PropertySource<?> rootSource = getRootSource(source);
170-
if (rootSource.getSource() instanceof Map) {
170+
if (rootSource.getSource() instanceof Map<?, ?> map) {
171171
// Check we're not security restricted
172172
try {
173-
((Map<?, ?>) rootSource.getSource()).size();
173+
map.size();
174174
}
175175
catch (UnsupportedOperationException ex) {
176176
return false;
@@ -180,8 +180,8 @@ private static boolean isFullEnumerable(PropertySource<?> source) {
180180
}
181181

182182
private static PropertySource<?> getRootSource(PropertySource<?> source) {
183-
while (source.getSource() != null && source.getSource() instanceof PropertySource) {
184-
source = (PropertySource<?>) source.getSource();
183+
while (source.getSource() != null && source.getSource() instanceof PropertySource<?> propertySource) {
184+
source = propertySource;
185185
}
186186
return source;
187187
}

0 commit comments

Comments
 (0)