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-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.
@@ -217,8 +217,8 @@ public class LaunchedURLClassLoader extends URLClassLoader {
for (URL url : getURLs()) {
try {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
JarFile jarFile = ((JarURLConnection) connection).getJarFile();
if (connection instanceof JarURLConnection jarURLConnection) {
JarFile jarFile = jarURLConnection.getJarFile();
if (jarFile.getEntry(classEntryName) != null && jarFile.getEntry(packageEntryName) != null
&& jarFile.getManifest() != null) {
definePackage(packageName, jarFile.getManifest(), url);

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.
@@ -269,8 +269,8 @@ public class PropertiesLauncher extends Launcher {
}
catch (IOException ex) {
// Close the HTTP connection (if applicable).
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
if (con instanceof HttpURLConnection httpURLConnection) {
httpURLConnection.disconnect();
}
throw ex;
}
@@ -283,8 +283,7 @@ public class PropertiesLauncher extends Launcher {
URLConnection connection = url.openConnection();
try {
connection.setUseCaches(connection.getClass().getSimpleName().startsWith("JNLP"));
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
if (connection instanceof HttpURLConnection httpConnection) {
httpConnection.setRequestMethod("HEAD");
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
@@ -297,8 +296,8 @@ public class PropertiesLauncher extends Launcher {
return (connection.getContentLength() >= 0);
}
finally {
if (connection instanceof HttpURLConnection) {
((HttpURLConnection) connection).disconnect();
if (connection instanceof HttpURLConnection httpURLConnection) {
httpURLConnection.disconnect();
}
}
}

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.
@@ -109,13 +109,13 @@ public class Handler extends URLStreamHandler {
return (connection != null) ? connection : openFallbackHandlerConnection(url);
}
catch (Exception ex) {
if (reason instanceof IOException) {
if (reason instanceof IOException ioException) {
log(false, "Unable to open fallback handler", ex);
throw (IOException) reason;
throw ioException;
}
log(true, "Unable to open fallback handler", ex);
if (reason instanceof RuntimeException) {
throw (RuntimeException) reason;
if (reason instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException(reason);
}

View File

@@ -278,8 +278,8 @@ public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.J
@Override
public synchronized InputStream getInputStream(ZipEntry entry) throws IOException {
ensureOpen();
if (entry instanceof JarEntry) {
return this.entries.getInputStream((JarEntry) entry);
if (entry instanceof JarEntry jarEntry) {
return this.entries.getInputStream(jarEntry);
}
return getInputStream((entry != null) ? entry.getName() : null);
}

View File

@@ -229,7 +229,7 @@ class JarFileEntries implements CentralDirectoryVisitor, Iterable<JarEntry> {
T entry = doGetEntry(name, type, cacheEntry, null);
if (!isMetaInfEntry(name) && isMultiReleaseJar()) {
int version = RUNTIME_VERSION;
AsciiBytes nameAlias = (entry instanceof JarEntry) ? ((JarEntry) entry).getAsciiBytesName()
AsciiBytes nameAlias = (entry instanceof JarEntry jarEntry) ? jarEntry.getAsciiBytesName()
: new AsciiBytes(name.toString());
while (version > BASE_VERSION) {
T versionedEntry = doGetEntry("META-INF/versions/" + version + "/" + name, type, cacheEntry, nameAlias);

View File

@@ -113,11 +113,11 @@ class JarFileWrapper extends AbstractJarFile {
}
static JarFile unwrap(java.util.jar.JarFile jarFile) {
if (jarFile instanceof JarFile) {
return (JarFile) jarFile;
if (jarFile instanceof JarFile file) {
return file;
}
if (jarFile instanceof JarFileWrapper) {
return unwrap(((JarFileWrapper) jarFile).parent);
if (jarFile instanceof JarFileWrapper wrapper) {
return unwrap(wrapper.parent);
}
throw new IllegalStateException("Not a JarFile or Wrapper");
}

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.
@@ -120,10 +120,9 @@ final class StringSequence implements CharSequence {
if (this == obj) {
return true;
}
if (!(obj instanceof CharSequence)) {
if (!(obj instanceof CharSequence other)) {
return false;
}
CharSequence other = (CharSequence) obj;
int n = length();
if (n != other.length()) {
return false;