Use pattern matching for instanceof where appropriate

See gh-31475
This commit is contained in:
dreis2211
2022-06-20 18:14:16 +02:00
committed by Andy Wilkinson
parent a7b98e7312
commit 5db04da275
278 changed files with 1049 additions and 1126 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -89,8 +89,7 @@ public final class ChangedFile {
if (obj == null) {
return false;
}
if (obj instanceof ChangedFile) {
ChangedFile other = (ChangedFile) obj;
if (obj instanceof ChangedFile other) {
return this.file.equals(other.file) && this.type.equals(other.type);
}
return super.equals(obj);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -69,8 +69,7 @@ public final class ChangedFiles implements Iterable<ChangedFile> {
if (obj == this) {
return true;
}
if (obj instanceof ChangedFiles) {
ChangedFiles other = (ChangedFiles) obj;
if (obj instanceof ChangedFiles other) {
return this.sourceDirectory.equals(other.sourceDirectory) && this.files.equals(other.files);
}
return super.equals(obj);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -119,8 +119,8 @@ class DirectorySnapshot {
if (obj == null) {
return false;
}
if (obj instanceof DirectorySnapshot) {
return equals((DirectorySnapshot) obj, null);
if (obj instanceof DirectorySnapshot other) {
return equals(other, null);
}
return super.equals(obj);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -56,8 +56,7 @@ class FileSnapshot {
if (obj == null) {
return false;
}
if (obj instanceof FileSnapshot) {
FileSnapshot other = (FileSnapshot) obj;
if (obj instanceof FileSnapshot other) {
boolean equals = this.file.equals(other.file);
equals = equals && this.exists == other.exists;
equals = equals && this.length == other.length;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -62,8 +62,8 @@ public final class DevToolsLogFactory {
public void onApplicationEvent(ApplicationPreparedEvent event) {
synchronized (logs) {
logs.forEach((log, source) -> {
if (log instanceof DeferredLog) {
((DeferredLog) log).switchTo(source);
if (log instanceof DeferredLog deferredLog) {
deferredLog.switchTo(source);
}
});
logs.clear();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -102,8 +102,8 @@ final class ChangeableUrls implements Iterable<URL> {
}
private static URL[] urlsFromClassLoader(ClassLoader classLoader) {
if (classLoader instanceof URLClassLoader) {
return ((URLClassLoader) classLoader).getURLs();
if (classLoader instanceof URLClassLoader urlClassLoader) {
return urlClassLoader.getURLs();
}
return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
.map(ChangeableUrls::toURL).toArray(URL[]::new);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -46,17 +46,17 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationStartingEvent) {
onApplicationStartingEvent((ApplicationStartingEvent) event);
if (event instanceof ApplicationStartingEvent startingEvent) {
onApplicationStartingEvent(startingEvent);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
if (event instanceof ApplicationPreparedEvent preparedEvent) {
onApplicationPreparedEvent(preparedEvent);
}
if (event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) {
Restarter.getInstance().finish();
}
if (event instanceof ApplicationFailedEvent) {
onApplicationFailedEvent((ApplicationFailedEvent) event);
if (event instanceof ApplicationFailedEvent failedEvent) {
onApplicationFailedEvent(failedEvent);
}
}

View File

@@ -411,8 +411,8 @@ public class Restarter {
if (applicationContext != null && applicationContext.getParent() != null) {
return;
}
if (applicationContext instanceof GenericApplicationContext) {
prepare((GenericApplicationContext) applicationContext);
if (applicationContext instanceof GenericApplicationContext genericContext) {
prepare(genericContext);
}
this.rootContexts.add(applicationContext);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
@@ -36,8 +36,8 @@ class SilentExitExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable exception) {
if (exception instanceof SilentExitException || (exception instanceof InvocationTargetException
&& ((InvocationTargetException) exception).getTargetException() instanceof SilentExitException)) {
if (exception instanceof SilentExitException || (exception instanceof InvocationTargetException targetException
&& targetException.getTargetException() instanceof SilentExitException)) {
if (isJvmExiting(thread)) {
preventNonZeroExitCode();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@@ -138,8 +138,8 @@ public class RestartServer {
Set<URL> urls = new LinkedHashSet<>();
ClassLoader classLoader = this.classLoader;
while (classLoader != null) {
if (classLoader instanceof URLClassLoader) {
Collections.addAll(urls, ((URLClassLoader) classLoader).getURLs());
if (classLoader instanceof URLClassLoader urlClassLoader) {
Collections.addAll(urls, urlClassLoader.getURLs());
}
classLoader = classLoader.getParent();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@@ -90,8 +90,8 @@ public class MockClientHttpRequestFactory implements ClientHttpRequestFactory {
protected ClientHttpResponse executeInternal() throws IOException {
MockClientHttpRequestFactory.this.executedRequests.add(this);
Object response = MockClientHttpRequestFactory.this.responses.pollFirst();
if (response instanceof IOException) {
throw (IOException) response;
if (response instanceof IOException ioException) {
throw ioException;
}
if (response == null) {
response = new Response(0, null, HttpStatus.GONE);