From 647813705c6a29a4e3451b47ce81bb5cfc116621 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sun, 12 Apr 2020 15:14:12 +0100 Subject: [PATCH] Testing with kotlin 1.4 --- build.gradle | 36 +++++++++++-------- okcurl/src/main/kotlin/okhttp3/curl/Main.kt | 6 +--- .../okhttp3/logging/HttpLoggingInterceptor.kt | 8 ++--- .../logging/HttpLoggingInterceptorTest.java | 2 ++ .../main/kotlin/okhttp3/sse/EventSource.kt | 2 +- .../main/kotlin/okhttp3/sse/EventSources.kt | 9 ++--- .../kotlin/okhttp3/OkHttpClientTestRule.kt | 16 ++++----- .../kotlin/okhttp3/RecordingEventListener.kt | 2 +- .../src/main/kotlin/okhttp3/Authenticator.kt | 2 +- okhttp/src/main/kotlin/okhttp3/Call.kt | 2 +- .../src/main/kotlin/okhttp3/EventListener.kt | 2 +- okhttp/src/main/kotlin/okhttp3/Handshake.kt | 4 +-- okhttp/src/main/kotlin/okhttp3/Interceptor.kt | 6 ++-- okhttp/src/main/kotlin/okhttp3/WebSocket.kt | 2 +- .../src/main/kotlin/okhttp3/internal/Util.kt | 15 ++++---- .../okhttp3/internal/http2/Http2Connection.kt | 6 ++-- .../okhttp3/internal/http2/Http2Reader.kt | 2 +- .../internal/platform/BouncyCastlePlatform.kt | 2 +- .../okhttp3/internal/platform/Jdk9Platform.kt | 2 +- .../internal/platform/OpenJSSEPlatform.kt | 2 +- .../platform/android/AndroidSocketAdapter.kt | 9 +++-- .../publicsuffix/PublicSuffixDatabase.kt | 4 +-- .../tls/BasicCertificateChainCleaner.kt | 2 +- .../internal/ws/WebSocketExtensions.kt | 6 ++-- .../okhttp3/internal/ws/WebSocketReader.kt | 6 ++-- .../test/java/okhttp3/CipherSuiteTest.java | 2 ++ okhttp/src/test/java/okhttp3/CookieTest.java | 8 +++++ okhttp/src/test/java/okhttp3/HeadersTest.java | 10 ++++++ .../test/java/okhttp3/OkHttpClientTest.java | 4 +++ okhttp/src/test/java/okhttp3/RequestTest.java | 8 +++++ .../test/java/okhttp3/URLConnectionTest.java | 12 +++++++ .../PublicSuffixDatabaseTest.java | 2 ++ .../internal/ws/WebSocketHttpTest.java | 2 ++ .../okhttp3/compare/JavaHttpClientTest.kt | 2 +- 34 files changed, 121 insertions(+), 84 deletions(-) diff --git a/build.gradle b/build.gradle index 32e643f72af5..2050475171a8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -import net.ltgt.gradle.errorprone.CheckSeverity +//import net.ltgt.gradle.errorprone.CheckSeverity buildscript { ext.versions = [ @@ -15,7 +15,7 @@ buildscript { 'jnrUnixsocket': '0.28', 'jsoup': '1.13.1', 'junit': '4.13', - 'kotlin': '1.3.71', + 'kotlin': '1.4-M1', 'moshi': '1.9.2', 'okio': '2.6.0', 'ktlint': '0.36.0', @@ -39,7 +39,7 @@ buildscript { 'jsoup': "org.jsoup:jsoup:${versions.jsoup}", 'jsr305': "com.google.code.findbugs:jsr305:${versions.findbugs}", 'junit': "junit:junit:${versions.junit}", - 'kotlinStdlib': "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}", + 'kotlinStdlib': "org.jetbrains.kotlin:kotlin-stdlib:1.4-M1", 'moshi': "com.squareup.moshi:moshi:${versions.moshi}", 'moshiKotlin': "com.squareup.moshi:moshi-kotlin-codegen:${versions.moshi}", 'okio': "com.squareup.okio:okio:${versions.okio}", @@ -48,7 +48,7 @@ buildscript { dependencies { classpath 'net.ltgt.gradle:gradle-errorprone-plugin:1.1.1' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4-M1" classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.10.1" classpath 'com.diffplug.spotless:spotless-plugin-gradle:3.27.1' classpath 'com.android.tools.build:gradle:3.6.2' @@ -58,6 +58,9 @@ buildscript { mavenCentral() gradlePluginPortal() google() + maven { + url = "https://dl.bintray.com/kotlin/kotlin-eap/" + } } } @@ -97,6 +100,9 @@ allprojects { url 'https://dl.bintray.com/kotlin/dokka' } google() + maven { + url = "https://dl.bintray.com/kotlin/kotlin-eap/" + } } task downloadDependencies() { @@ -112,12 +118,14 @@ subprojects { project -> if (project.name == 'android-test') return if (project.name == 'okhttp-bom') return + apply plugin: "kotlin" apply plugin: 'java' apply plugin: 'java-library' apply plugin: 'org.jetbrains.kotlin.platform.jvm' apply plugin: 'checkstyle' apply plugin: 'ru.vyarus.animalsniffer' - apply plugin: 'net.ltgt.errorprone' + // TODO raise issue +// apply plugin: 'net.ltgt.errorprone' apply plugin: 'org.jetbrains.dokka' apply plugin: 'com.diffplug.gradle.spotless' sourceCompatibility = JavaVersion.VERSION_1_8 @@ -205,20 +213,20 @@ subprojects { project -> } } - dependencies { +// dependencies { //noinspection GradleDynamicVersion - errorproneJavac 'com.google.errorprone:javac:9+181-r4173-1' - errorprone 'com.google.errorprone:error_prone_core:2.3.4' - } +// errorproneJavac 'com.google.errorprone:javac:9+181-r4173-1' +// errorprone 'com.google.errorprone:error_prone_core:2.3.4' +// } tasks.withType(JavaCompile).configureEach { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 - options.errorprone { - check("MissingFail", CheckSeverity.ERROR) - check("MissingOverride", CheckSeverity.ERROR) - enabled = JavaVersion.current() < JavaVersion.VERSION_12 - } +// options.errorprone { +// check("MissingFail", CheckSeverity.ERROR) +// check("MissingOverride", CheckSeverity.ERROR) +// enabled = JavaVersion.current() < JavaVersion.VERSION_12 +// } } dokka { diff --git a/okcurl/src/main/kotlin/okhttp3/curl/Main.kt b/okcurl/src/main/kotlin/okhttp3/curl/Main.kt index afc4edbd40de..1047f01f448b 100644 --- a/okcurl/src/main/kotlin/okhttp3/curl/Main.kt +++ b/okcurl/src/main/kotlin/okhttp3/curl/Main.kt @@ -161,11 +161,7 @@ class Main : Runnable { builder.hostnameVerifier(createInsecureHostnameVerifier()) } if (verbose) { - val logger = object : HttpLoggingInterceptor.Logger { - override fun log(message: String) { - println(message) - } - } + val logger = HttpLoggingInterceptor.Logger(::println) builder.eventListenerFactory(LoggingEventListener.Factory(logger)) } return builder.build() diff --git a/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt b/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt index c1a681448097..0fd5e4e3e0ab 100644 --- a/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt +++ b/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt @@ -104,17 +104,13 @@ class HttpLoggingInterceptor @JvmOverloads constructor( BODY } - interface Logger { + fun interface Logger { fun log(message: String) companion object { /** A [Logger] defaults output appropriate for the current platform. */ @JvmField - val DEFAULT: Logger = object : Logger { - override fun log(message: String) { - Platform.get().log(message) - } - } + val DEFAULT: Logger = Logger { Platform.get().log(it) } } } diff --git a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java index 105dd5c4472f..6e017cdac68a 100644 --- a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java +++ b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java @@ -107,6 +107,8 @@ private void setLevel(Level level) { try { applicationInterceptor.setLevel(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } diff --git a/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSource.kt b/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSource.kt index 38174e1ab042..ec280d5b5df9 100644 --- a/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSource.kt +++ b/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSource.kt @@ -27,7 +27,7 @@ interface EventSource { */ fun cancel() - interface Factory { + fun interface Factory { /** * Creates a new event source and immediately returns it. Creating an event source initiates an * asynchronous process to connect the socket. Once that succeeds or fails, `listener` will be diff --git a/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSources.kt b/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSources.kt index e7297a500c73..269ff0db4491 100644 --- a/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSources.kt +++ b/okhttp-sse/src/main/kotlin/okhttp3/sse/EventSources.kt @@ -16,18 +16,15 @@ package okhttp3.sse import okhttp3.OkHttpClient -import okhttp3.Request import okhttp3.Response import okhttp3.internal.sse.RealEventSource object EventSources { @JvmStatic fun createFactory(client: OkHttpClient): EventSource.Factory { - return object : EventSource.Factory { - override fun newEventSource(request: Request, listener: EventSourceListener): EventSource { - return RealEventSource(request, listener).apply { - connect(client) - } + return EventSource.Factory { request, listener -> + RealEventSource(request, listener).apply { + connect(client) } } } diff --git a/okhttp-testing-support/src/main/kotlin/okhttp3/OkHttpClientTestRule.kt b/okhttp-testing-support/src/main/kotlin/okhttp3/OkHttpClientTestRule.kt index 0f0cca95ab36..e65ebb34a0e0 100644 --- a/okhttp-testing-support/src/main/kotlin/okhttp3/OkHttpClientTestRule.kt +++ b/okhttp-testing-support/src/main/kotlin/okhttp3/OkHttpClientTestRule.kt @@ -39,14 +39,11 @@ class OkHttpClientTestRule : TestRule { private var uncaughtException: Throwable? = null var logger: Logger? = null - fun wrap(eventListener: EventListener) = object : EventListener.Factory { - override fun create(call: Call) = ClientRuleEventListener(eventListener) { addEvent(it) } - } + fun wrap(eventListener: EventListener) = + EventListener.Factory { ClientRuleEventListener(eventListener, ::addEvent) } - fun wrap(eventListenerFactory: EventListener.Factory) = object : EventListener.Factory { - override fun create(call: Call) = - ClientRuleEventListener(eventListenerFactory.create(call)) { addEvent(it) } - } + fun wrap(eventListenerFactory: EventListener.Factory) = + EventListener.Factory { call -> ClientRuleEventListener(eventListenerFactory.create(call), ::addEvent) } /** * Returns an OkHttpClient for tests to use as a starting point. @@ -62,9 +59,8 @@ class OkHttpClientTestRule : TestRule { if (client == null) { client = OkHttpClient.Builder() .dns(SINGLE_INET_ADDRESS_DNS) // Prevent unexpected fallback addresses. - .eventListenerFactory(object : EventListener.Factory { - override fun create(call: Call) = ClientRuleEventListener { addEvent(it) } - }) + .eventListenerFactory( + EventListener.Factory { ClientRuleEventListener(logger = ::addEvent) }) .build() testClient = client } diff --git a/okhttp-testing-support/src/main/kotlin/okhttp3/RecordingEventListener.kt b/okhttp-testing-support/src/main/kotlin/okhttp3/RecordingEventListener.kt index 48ea178e083e..e65cd0424c30 100644 --- a/okhttp-testing-support/src/main/kotlin/okhttp3/RecordingEventListener.kt +++ b/okhttp-testing-support/src/main/kotlin/okhttp3/RecordingEventListener.kt @@ -125,7 +125,7 @@ open class RecordingEventListener : EventListener() { for (lock in forbiddenLocks) { assertThat(Thread.holdsLock(lock)) .overridingErrorMessage(lock.toString()) - .isFalse() + .isFalse } val startEvent = e.closes(-1L) diff --git a/okhttp/src/main/kotlin/okhttp3/Authenticator.kt b/okhttp/src/main/kotlin/okhttp3/Authenticator.kt index 43b2b416e7be..40f62766f539 100644 --- a/okhttp/src/main/kotlin/okhttp3/Authenticator.kt +++ b/okhttp/src/main/kotlin/okhttp3/Authenticator.kt @@ -96,7 +96,7 @@ import okhttp3.internal.authenticator.JavaNetAuthenticator * * [1]: https://tools.ietf.org/html/rfc2817 */ -interface Authenticator { +fun interface Authenticator { /** * Returns a request that includes a credential to satisfy an authentication challenge in * [response]. Returns null if the challenge cannot be satisfied. diff --git a/okhttp/src/main/kotlin/okhttp3/Call.kt b/okhttp/src/main/kotlin/okhttp3/Call.kt index 6df8da0e9cbd..66677f62726c 100644 --- a/okhttp/src/main/kotlin/okhttp3/Call.kt +++ b/okhttp/src/main/kotlin/okhttp3/Call.kt @@ -93,7 +93,7 @@ interface Call : Cloneable { */ public override fun clone(): Call - interface Factory { + fun interface Factory { fun newCall(request: Request): Call } } diff --git a/okhttp/src/main/kotlin/okhttp3/EventListener.kt b/okhttp/src/main/kotlin/okhttp3/EventListener.kt index f89485916450..5dbd3b736e72 100644 --- a/okhttp/src/main/kotlin/okhttp3/EventListener.kt +++ b/okhttp/src/main/kotlin/okhttp3/EventListener.kt @@ -422,7 +422,7 @@ abstract class EventListener { ) { } - interface Factory { + fun interface Factory { /** * Creates an instance of the [EventListener] for a particular [Call]. The returned * [EventListener] instance will be used during the lifecycle of [call]. diff --git a/okhttp/src/main/kotlin/okhttp3/Handshake.kt b/okhttp/src/main/kotlin/okhttp3/Handshake.kt index 4bc98acc1b69..e030b1be4b8f 100644 --- a/okhttp/src/main/kotlin/okhttp3/Handshake.kt +++ b/okhttp/src/main/kotlin/okhttp3/Handshake.kt @@ -52,7 +52,7 @@ class Handshake internal constructor( try { peerCertificatesFn() } catch (spue: SSLPeerUnverifiedException) { - listOf() + listOf() } } @@ -160,7 +160,7 @@ class Handshake internal constructor( val peerCertificatesCopy = try { peerCertificates.toImmutableList() } catch (_: SSLPeerUnverifiedException) { - listOf() + listOf() } return Handshake(tlsVersion, cipherSuite, diff --git a/okhttp/src/main/kotlin/okhttp3/Interceptor.kt b/okhttp/src/main/kotlin/okhttp3/Interceptor.kt index 2452cff367f7..8da5b803d468 100644 --- a/okhttp/src/main/kotlin/okhttp3/Interceptor.kt +++ b/okhttp/src/main/kotlin/okhttp3/Interceptor.kt @@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit * responses coming back in. Typically interceptors add, remove, or transform headers on the request * or response. */ -interface Interceptor { +fun interface Interceptor { @Throws(IOException::class) fun intercept(chain: Chain): Response @@ -39,9 +39,7 @@ interface Interceptor { * ``` */ inline operator fun invoke(crossinline block: (chain: Chain) -> Response): Interceptor = - object : Interceptor { - override fun intercept(chain: Chain) = block(chain) - } + Interceptor { block(it) } } interface Chain { diff --git a/okhttp/src/main/kotlin/okhttp3/WebSocket.kt b/okhttp/src/main/kotlin/okhttp3/WebSocket.kt index 01d2faa245d0..ac731e62e1b1 100644 --- a/okhttp/src/main/kotlin/okhttp3/WebSocket.kt +++ b/okhttp/src/main/kotlin/okhttp3/WebSocket.kt @@ -109,7 +109,7 @@ interface WebSocket { */ fun cancel() - interface Factory { + fun interface Factory { /** * Creates a new web socket and immediately returns it. Creating a web socket initiates an * asynchronous process to connect the socket. Once that succeeds or fails, `listener` will be diff --git a/okhttp/src/main/kotlin/okhttp3/internal/Util.kt b/okhttp/src/main/kotlin/okhttp3/internal/Util.kt index eccae85da25e..7578955a02e9 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/Util.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/Util.kt @@ -29,7 +29,6 @@ import java.nio.charset.Charset import java.nio.charset.StandardCharsets.UTF_16BE import java.nio.charset.StandardCharsets.UTF_16LE import java.nio.charset.StandardCharsets.UTF_8 -import java.util.Arrays import java.util.Collections import java.util.Comparator import java.util.LinkedHashMap @@ -299,9 +298,7 @@ fun HttpUrl.canReuseConnectionFor(other: HttpUrl): Boolean = host == other.host port == other.port && scheme == other.scheme -fun EventListener.asFactory() = object : EventListener.Factory { - override fun create(call: Call): EventListener = this@asFactory -} +fun EventListener.asFactory() = EventListener.Factory { this } infix fun Byte.and(mask: Int): Int = toInt() and mask infix fun Short.and(mask: Int): Int = toInt() and mask @@ -377,18 +374,18 @@ fun Socket.peerName(): String { * @param source the source used to read bytes from the socket. */ fun Socket.isHealthy(source: BufferedSource): Boolean { - try { + return try { val readTimeout = soTimeout try { soTimeout = 1 - return !source.exhausted() + !source.exhausted() } finally { soTimeout = readTimeout } } catch (_: SocketTimeoutException) { - return true // Read timed out; socket is good. + true // Read timed out; socket is good. } catch (_: IOException) { - return false // Couldn't read; socket is closed. + false // Couldn't read; socket is closed. } } @@ -472,7 +469,7 @@ fun List.toImmutableList(): List { /** Returns an immutable list containing [elements]. */ @SafeVarargs fun immutableListOf(vararg elements: T): List { - return Collections.unmodifiableList(Arrays.asList(*elements.clone())) + return Collections.unmodifiableList(listOf(*elements.clone())) } /** Returns an immutable copy of this. */ diff --git a/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Connection.kt b/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Connection.kt index 80cd5875dc3a..3f5c0714174a 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Connection.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Connection.kt @@ -737,10 +737,10 @@ class Http2Connection internal constructor(builder: Builder) : Closeable { synchronized(writer) { synchronized(this@Http2Connection) { val previousPeerSettings = peerSettings - if (clearPrevious) { - newPeerSettings = settings + newPeerSettings = if (clearPrevious) { + settings } else { - newPeerSettings = Settings().apply { + Settings().apply { merge(previousPeerSettings) merge(settings) } diff --git a/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Reader.kt b/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Reader.kt index 73a9c6e0de9d..5c28ebc9ebe7 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Reader.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/http2/Http2Reader.kt @@ -74,7 +74,7 @@ class Http2Reader( } } else { // The server reads the CONNECTION_PREFACE byte string. - val connectionPreface = source.readByteString(Http2.CONNECTION_PREFACE.size.toLong()) + val connectionPreface = source.readByteString(CONNECTION_PREFACE.size.toLong()) if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION ${connectionPreface.hex()}")) if (CONNECTION_PREFACE != connectionPreface) { throw IOException("Expected a connection header but was ${connectionPreface.utf8()}") diff --git a/okhttp/src/main/kotlin/okhttp3/internal/platform/BouncyCastlePlatform.kt b/okhttp/src/main/kotlin/okhttp3/internal/platform/BouncyCastlePlatform.kt index d53ebe46cf16..e0bcb5530450 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/platform/BouncyCastlePlatform.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/platform/BouncyCastlePlatform.kt @@ -48,7 +48,7 @@ class BouncyCastlePlatform private constructor() : Platform() { return trustManagers[0] as X509TrustManager } - public override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? = + override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? = throw UnsupportedOperationException( "clientBuilder.sslSocketFactory(SSLSocketFactory) not supported with BouncyCastle") diff --git a/okhttp/src/main/kotlin/okhttp3/internal/platform/Jdk9Platform.kt b/okhttp/src/main/kotlin/okhttp3/internal/platform/Jdk9Platform.kt index fd4f0b56de1a..fd29f340a44e 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/platform/Jdk9Platform.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/platform/Jdk9Platform.kt @@ -53,7 +53,7 @@ open class Jdk9Platform : Platform() { } } - public override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? { + override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? { // Not supported due to access checks on JDK 9+: // java.lang.reflect.InaccessibleObjectException: Unable to make member of class // sun.security.ssl.SSLSocketFactoryImpl accessible: module java.base does not export diff --git a/okhttp/src/main/kotlin/okhttp3/internal/platform/OpenJSSEPlatform.kt b/okhttp/src/main/kotlin/okhttp3/internal/platform/OpenJSSEPlatform.kt index 15818d289e6a..040585cbe153 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/platform/OpenJSSEPlatform.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/platform/OpenJSSEPlatform.kt @@ -50,7 +50,7 @@ class OpenJSSEPlatform private constructor() : Platform() { return trustManagers[0] as X509TrustManager } - public override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? = + override fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? = throw UnsupportedOperationException( "clientBuilder.sslSocketFactory(SSLSocketFactory) not supported with OpenJSSE") diff --git a/okhttp/src/main/kotlin/okhttp3/internal/platform/android/AndroidSocketAdapter.kt b/okhttp/src/main/kotlin/okhttp3/internal/platform/android/AndroidSocketAdapter.kt index 38b8508f30f7..280667764ff8 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/platform/android/AndroidSocketAdapter.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/platform/android/AndroidSocketAdapter.kt @@ -79,11 +79,10 @@ open class AndroidSocketAdapter(private val sslSocketClass: Class) val alpnResult = getAlpnSelectedProtocol.invoke(sslSocket) as ByteArray? if (alpnResult != null) String(alpnResult, StandardCharsets.UTF_8) else null } catch (e: NullPointerException) { - when { - // https://github.com/square/okhttp/issues/5587 - e.message == "ssl == null" -> null - else -> throw e - } + when (e.message) { + "ssl == null" -> null // https://github.com/square/okhttp/issues/5587 + else -> throw e + } } catch (e: IllegalAccessException) { throw AssertionError(e) } catch (e: InvocationTargetException) { diff --git a/okhttp/src/main/kotlin/okhttp3/internal/publicsuffix/PublicSuffixDatabase.kt b/okhttp/src/main/kotlin/okhttp3/internal/publicsuffix/PublicSuffixDatabase.kt index 589436c21fc2..82402af52fcb 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/publicsuffix/PublicSuffixDatabase.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/publicsuffix/PublicSuffixDatabase.kt @@ -191,8 +191,8 @@ class PublicSuffixDatabase { @Throws(IOException::class) private fun readTheList() { - var publicSuffixListBytes: ByteArray? = null - var publicSuffixExceptionListBytes: ByteArray? = null + var publicSuffixListBytes: ByteArray? + var publicSuffixExceptionListBytes: ByteArray? val resource = PublicSuffixDatabase::class.java.getResourceAsStream(PUBLIC_SUFFIX_RESOURCE) ?: return diff --git a/okhttp/src/main/kotlin/okhttp3/internal/tls/BasicCertificateChainCleaner.kt b/okhttp/src/main/kotlin/okhttp3/internal/tls/BasicCertificateChainCleaner.kt index 35d7f721fdf6..ee8a78414120 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/tls/BasicCertificateChainCleaner.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/tls/BasicCertificateChainCleaner.kt @@ -46,7 +46,7 @@ class BasicCertificateChainCleaner( */ @Throws(SSLPeerUnverifiedException::class) override fun clean(chain: List, hostname: String): List { - val queue: Deque = ArrayDeque(chain) + val queue: Deque = ArrayDeque(chain) val result = mutableListOf() result.add(queue.removeFirst()) var foundTrustedCertificate = false diff --git a/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketExtensions.kt b/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketExtensions.kt index 850e7abc7341..4e82c290f8dc 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketExtensions.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketExtensions.kt @@ -80,10 +80,10 @@ data class WebSocketExtensions( ) { fun noContextTakeover(clientOriginated: Boolean): Boolean { - if (clientOriginated) { - return clientNoContextTakeover // Client is deflating. + return if (clientOriginated) { + clientNoContextTakeover // Client is deflating. } else { - return serverNoContextTakeover // Server is deflating. + serverNoContextTakeover // Server is deflating. } } diff --git a/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketReader.kt b/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketReader.kt index 92b75fc63ca6..6d306cbcd9b4 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketReader.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/ws/WebSocketReader.kt @@ -133,11 +133,11 @@ class WebSocketReader( val reservedFlag1 = b0 and B0_FLAG_RSV1 != 0 when (opcode) { OPCODE_TEXT, OPCODE_BINARY -> { - if (reservedFlag1) { + readingCompressedMessage = if (reservedFlag1) { if (!perMessageDeflate) throw ProtocolException("Unexpected rsv1 flag") - readingCompressedMessage = true + true } else { - readingCompressedMessage = false + false } } else -> { diff --git a/okhttp/src/test/java/okhttp3/CipherSuiteTest.java b/okhttp/src/test/java/okhttp3/CipherSuiteTest.java index d70d7d4b17f4..660f979d30d6 100644 --- a/okhttp/src/test/java/okhttp3/CipherSuiteTest.java +++ b/okhttp/src/test/java/okhttp3/CipherSuiteTest.java @@ -31,6 +31,8 @@ public class CipherSuiteTest { try { forJavaName(null); fail("Should have thrown"); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } diff --git a/okhttp/src/test/java/okhttp3/CookieTest.java b/okhttp/src/test/java/okhttp3/CookieTest.java index bf2add65e3a9..13792257b6fd 100644 --- a/okhttp/src/test/java/okhttp3/CookieTest.java +++ b/okhttp/src/test/java/okhttp3/CookieTest.java @@ -427,6 +427,8 @@ public final class CookieTest { try { new Cookie.Builder().name(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { @@ -440,6 +442,8 @@ public final class CookieTest { try { new Cookie.Builder().value(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { @@ -485,6 +489,8 @@ public final class CookieTest { try { new Cookie.Builder().hostOnlyDomain(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { @@ -518,6 +524,8 @@ public final class CookieTest { try { new Cookie.Builder().path(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { diff --git a/okhttp/src/test/java/okhttp3/HeadersTest.java b/okhttp/src/test/java/okhttp3/HeadersTest.java index 6ada63c49147..79a9861d067f 100644 --- a/okhttp/src/test/java/okhttp3/HeadersTest.java +++ b/okhttp/src/test/java/okhttp3/HeadersTest.java @@ -201,6 +201,8 @@ public final class HeadersTest { try { Headers.of(Collections.singletonMap("User-Agent", null)); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (TypeCastException expected) { } } @@ -831,6 +833,8 @@ public final class HeadersTest { .add("testDate", (Date) null) .build(); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } @@ -850,6 +854,8 @@ public final class HeadersTest { .add("Test-Instant", (Instant) null) .build(); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } @@ -870,6 +876,8 @@ public final class HeadersTest { .set("testDate", (Date) null) .build(); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } @@ -890,6 +898,8 @@ public final class HeadersTest { .set("Test-Instant", (Instant) null) .build(); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } diff --git a/okhttp/src/test/java/okhttp3/OkHttpClientTest.java b/okhttp/src/test/java/okhttp3/OkHttpClientTest.java index 1724af3ba31c..93b0a866d47e 100644 --- a/okhttp/src/test/java/okhttp3/OkHttpClientTest.java +++ b/okhttp/src/test/java/okhttp3/OkHttpClientTest.java @@ -169,6 +169,8 @@ public final class OkHttpClientTest { try { builder.addInterceptor(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } @@ -178,6 +180,8 @@ public final class OkHttpClientTest { try { builder.addNetworkInterceptor(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } diff --git a/okhttp/src/test/java/okhttp3/RequestTest.java b/okhttp/src/test/java/okhttp3/RequestTest.java index 9638b3ba4043..49a7e9468dfe 100644 --- a/okhttp/src/test/java/okhttp3/RequestTest.java +++ b/okhttp/src/test/java/okhttp3/RequestTest.java @@ -195,21 +195,29 @@ public final class RequestTest { try { builder.header(null, "Value"); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { builder.addHeader(null, "Value"); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { builder.header("Name", null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { builder.addHeader("Name", null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } diff --git a/okhttp/src/test/java/okhttp3/URLConnectionTest.java b/okhttp/src/test/java/okhttp3/URLConnectionTest.java index 18ddaa44263d..1e0f8b838662 100644 --- a/okhttp/src/test/java/okhttp3/URLConnectionTest.java +++ b/okhttp/src/test/java/okhttp3/URLConnectionTest.java @@ -156,24 +156,32 @@ public final class URLConnectionTest { new Request.Builder() .header(null, "j"); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { new Request.Builder() .addHeader(null, "k"); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { new Request.Builder() .addHeader("NullValue", null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } try { new Request.Builder() .addHeader("AnotherNullValue", null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } @@ -211,6 +219,8 @@ public final class URLConnectionTest { try { response.header(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } Headers responseHeaders = response.headers(); @@ -3578,6 +3588,8 @@ private void zeroLengthPayload(String method) throws Exception { try { client.newBuilder().sslSocketFactory(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } } diff --git a/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixDatabaseTest.java b/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixDatabaseTest.java index 46a107e46f33..2c986d494800 100644 --- a/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixDatabaseTest.java +++ b/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixDatabaseTest.java @@ -269,6 +269,8 @@ private void checkPublicSuffix(String domain, String registrablePart) { try { publicSuffixDatabase.getEffectiveTldPlusOne(null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } return; diff --git a/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java b/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java index 226477f9cf4f..c4dd496c04e8 100644 --- a/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java +++ b/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java @@ -147,6 +147,8 @@ public final class WebSocketHttpTest { try { webSocket.send((ByteString) null); fail(); + } catch (NullPointerException expected) { + // Kotlin 1.4 } catch (IllegalArgumentException expected) { } diff --git a/samples/compare/src/test/kotlin/okhttp3/compare/JavaHttpClientTest.kt b/samples/compare/src/test/kotlin/okhttp3/compare/JavaHttpClientTest.kt index 12b111ba41ff..d862505788b3 100644 --- a/samples/compare/src/test/kotlin/okhttp3/compare/JavaHttpClientTest.kt +++ b/samples/compare/src/test/kotlin/okhttp3/compare/JavaHttpClientTest.kt @@ -56,7 +56,7 @@ class JavaHttpClientTest { assertThat(recorded.getHeader("Accept-Encoding")).isNull() // No built-in gzip. assertThat(recorded.getHeader("Connection")).isEqualTo("Upgrade, HTTP2-Settings") assertThat(recorded.getHeader("Content-Length")).isEqualTo("0") - assertThat(recorded.getHeader("HTTP2-Settings")).isNotNull() + assertThat(recorded.getHeader("HTTP2-Settings")).isNotNull assertThat(recorded.getHeader("Upgrade")).isEqualTo("h2c") // HTTP/2 over plaintext! assertThat(recorded.getHeader("User-Agent")).matches("Java-http-client/.*") }