Skip to content

Commit

Permalink
Kotlin 1.4 dependency upgrade and language features (#5947)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Sep 6, 2020
1 parent 1589741 commit a76c40a
Show file tree
Hide file tree
Showing 37 changed files with 103 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
@@ -0,0 +1,2 @@
[*.kt]
indent_size = 2
42 changes: 11 additions & 31 deletions build.gradle
Expand Up @@ -15,10 +15,10 @@ buildscript {
'jnrUnixsocket': '0.28',
'jsoup': '1.13.1',
'junit': '4.13',
'kotlin': '1.3.72',
'kotlin': '1.4.0',
'moshi': '1.9.2',
'okio': '2.7.0',
'ktlint': '0.36.0',
'okio': '2.8.0',
'ktlint': '0.38.0',
'picocli': '4.2.0',
'openjsse': '1.1.0'
]
Expand Down Expand Up @@ -47,11 +47,9 @@ 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.0"
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'
classpath "com.android.tools.build:gradle:4.0.1"
}

repositories {
Expand All @@ -62,9 +60,11 @@ buildscript {
}

plugins {
id 'ru.vyarus.animalsniffer' version '1.5.0'
id 'com.github.johnrengelman.shadow' version '5.2.0'
id 'me.champeau.gradle.japicmp' version '0.2.9'
id "ru.vyarus.animalsniffer" version "1.5.0"
id "com.github.johnrengelman.shadow" version "5.2.0"
id "me.champeau.gradle.japicmp" version "0.2.9"
id "com.diffplug.spotless" version "5.1.2"
id "net.ltgt.errorprone" version "1.2.1"
}

/** Returns the artifact ID for the project, or null if it is not published. */
Expand Down Expand Up @@ -112,14 +112,12 @@ subprojects { project ->
if (project.name == 'android-test') return
if (project.name == 'okhttp-bom') return

apply plugin: "org.jetbrains.kotlin.jvm"
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'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'com.diffplug.gradle.spotless'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

Expand Down Expand Up @@ -152,13 +150,6 @@ subprojects { project ->
signature 'org.codehaus.mojo.signature:java18:1.0@signature'
}

spotless {
kotlin {
target "**/*.kt"
ktlint(versions.ktlint).userData(['indent_size': '2', 'continuation_indent_size': '2'])
}
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
Expand Down Expand Up @@ -205,20 +196,9 @@ subprojects { project ->
}
}

dependencies {
//noinspection GradleDynamicVersion
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
}
}

dokka {
Expand Down
6 changes: 1 addition & 5 deletions okcurl/src/main/kotlin/okhttp3/curl/Main.kt
Expand Up @@ -168,11 +168,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()
Expand Down
Expand Up @@ -104,7 +104,7 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
BODY
}

interface Logger {
fun interface Logger {
fun log(message: String)

companion object {
Expand Down
Expand Up @@ -107,7 +107,7 @@ private void setLevel(Level level) {
try {
applicationInterceptor.setLevel(null);
fail();
} catch (IllegalArgumentException expected) {
} catch (NullPointerException expected) {
}
}

Expand Down
2 changes: 1 addition & 1 deletion okhttp-sse/src/main/kotlin/okhttp3/sse/EventSource.kt
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions okhttp-sse/src/main/kotlin/okhttp3/sse/EventSources.kt
Expand Up @@ -16,25 +16,22 @@
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 {
val actualRequest =
if (request.header("Accept") == null) {
request.newBuilder().addHeader("Accept", "text/event-stream").build()
} else {
request
}

return RealEventSource(actualRequest, listener).apply {
connect(client)
return EventSource.Factory { request, listener ->
val actualRequest =
if (request.header("Accept") == null) {
request.newBuilder().addHeader("Accept", "text/event-stream").build()
} else {
request
}

RealEventSource(actualRequest, listener).apply {
connect(client)
}
}
}
Expand Down
Expand Up @@ -92,14 +92,11 @@ class OkHttpClientTestRule : TestRule {
Logger.getLogger("javax.net.ssl").fn()
}

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.
Expand All @@ -115,9 +112,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
}
Expand Down
2 changes: 2 additions & 0 deletions okhttp/build.gradle
Expand Up @@ -108,6 +108,8 @@ task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask, dependsOn: 'jar') {
'okhttp3.OkHttpClient#socketFactory()',
'okhttp3.OkHttpClient#sslSocketFactory()',
'okhttp3.OkHttpClient#writeTimeoutMillis()',
'okhttp3.OkHttpClient#writeTimeoutMillis()',
'okhttp3.Request$Builder#delete()',
]
}
check.dependsOn(japicmp)
2 changes: 1 addition & 1 deletion okhttp/src/main/kotlin/okhttp3/Authenticator.kt
Expand Up @@ -112,7 +112,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.
Expand Down
13 changes: 6 additions & 7 deletions okhttp/src/main/kotlin/okhttp3/Cache.kt
Expand Up @@ -386,12 +386,12 @@ class Cache internal constructor(

@Synchronized fun requestCount(): Int = requestCount

private inner class RealCacheRequest internal constructor(
private inner class RealCacheRequest(
private val editor: DiskLruCache.Editor
) : CacheRequest {
private val cacheOut: Sink = editor.newSink(ENTRY_BODY)
private val body: Sink
internal var done = false
var done = false

init {
this.body = object : ForwardingSink(cacheOut) {
Expand Down Expand Up @@ -489,8 +489,7 @@ class Cache internal constructor(
* each on their own line. A length of -1 is used to encode a null array. The last line is
* optional. If present, it contains the TLS version.
*/
@Throws(IOException::class)
internal constructor(rawSource: Source) {
@Throws(IOException::class) constructor(rawSource: Source) {
try {
val source = rawSource.buffer()
url = source.readUtf8LineStrict()
Expand Down Expand Up @@ -542,7 +541,7 @@ class Cache internal constructor(
}
}

internal constructor(response: Response) {
constructor(response: Response) {
this.url = response.request.url.toString()
this.varyHeaders = response.varyHeaders()
this.requestMethod = response.request.method
Expand Down Expand Up @@ -665,8 +664,8 @@ class Cache internal constructor(
}
}

private class CacheResponseBody internal constructor(
internal val snapshot: DiskLruCache.Snapshot,
private class CacheResponseBody(
val snapshot: DiskLruCache.Snapshot,
private val contentType: String?,
private val contentLength: String?
) : ResponseBody() {
Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/kotlin/okhttp3/Call.kt
Expand Up @@ -93,7 +93,7 @@ interface Call : Cloneable {
*/
public override fun clone(): Call

interface Factory {
fun interface Factory {
fun newCall(request: Request): Call
}
}
2 changes: 1 addition & 1 deletion okhttp/src/main/kotlin/okhttp3/EventListener.kt
Expand Up @@ -457,7 +457,7 @@ abstract class EventListener {
open fun cacheConditionalHit(call: Call, cachedResponse: Response) {
}

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].
Expand Down
4 changes: 2 additions & 2 deletions okhttp/src/main/kotlin/okhttp3/Handshake.kt
Expand Up @@ -52,7 +52,7 @@ class Handshake internal constructor(
try {
peerCertificatesFn()
} catch (spue: SSLPeerUnverifiedException) {
listOf<Certificate>()
listOf()
}
}

Expand Down Expand Up @@ -160,7 +160,7 @@ class Handshake internal constructor(
val peerCertificatesCopy = try {
peerCertificates.toImmutableList()
} catch (_: SSLPeerUnverifiedException) {
listOf<Certificate>()
listOf()
}

return Handshake(tlsVersion, cipherSuite,
Expand Down
6 changes: 2 additions & 4 deletions okhttp/src/main/kotlin/okhttp3/Interceptor.kt
Expand Up @@ -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

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/kotlin/okhttp3/WebSocket.kt
Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions okhttp/src/main/kotlin/okhttp3/internal/Util.kt
Expand Up @@ -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
Expand Down Expand Up @@ -300,9 +299,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
Expand Down Expand Up @@ -378,18 +375,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.
}
}

Expand Down Expand Up @@ -473,7 +470,7 @@ fun <T> List<T>.toImmutableList(): List<T> {
/** Returns an immutable list containing [elements]. */
@SafeVarargs
fun <T> immutableListOf(vararg elements: T): List<T> {
return Collections.unmodifiableList(Arrays.asList(*elements.clone()))
return Collections.unmodifiableList(listOf(*elements.clone()))
}

/** Returns an immutable copy of this. */
Expand Down
Expand Up @@ -198,7 +198,7 @@ class Exchange(
}

/** A request body that fires events when it completes. */
private inner class RequestBodySink internal constructor(
private inner class RequestBodySink(
delegate: Sink,
/** The exact number of bytes to be written, or -1L if that is unknown. */
private val contentLength: Long
Expand Down

0 comments on commit a76c40a

Please sign in to comment.