mirror of
https://github.com/apple/pkl.git
synced 2026-05-25 16:19:20 +02:00
pkl-config-java: Migrate nullness to jSpecify (#1528)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package org.pkl.config.kotlin
|
||||
|
||||
import kotlin.reflect.jvm.javaType
|
||||
import kotlin.reflect.typeOf
|
||||
import org.pkl.config.java.Config
|
||||
import org.pkl.config.java.ConfigEvaluator
|
||||
import org.pkl.config.java.ConfigEvaluatorBuilder
|
||||
import org.pkl.config.java.JavaType
|
||||
import org.pkl.config.java.mapper.ConversionException
|
||||
import org.pkl.config.java.mapper.ValueMapperBuilder
|
||||
import org.pkl.config.kotlin.mapper.KotlinConversions
|
||||
@@ -30,23 +31,21 @@ import org.pkl.config.kotlin.mapper.KotlinConverterFactories
|
||||
*
|
||||
* To allow `null` values, specify a nullable type, for example `to<String?>()`.
|
||||
*
|
||||
* Kotlin code should prefer this method over [Config. as] for the following reasons:
|
||||
* Kotlin code should prefer this method over [Config.as] for the following reasons:
|
||||
* * does not clash with Kotlin's `as` keyword
|
||||
* * throws [ConversionException] if conversion to non-nullable type returns `null`
|
||||
* * easier to use with parameterized types: `to<List<String>>()` vs.
|
||||
* `as(JavaType.listOf(String::class.java))`
|
||||
*/
|
||||
inline fun <reified T> Config.to(): T {
|
||||
val javaType = object : JavaType<T>() {}
|
||||
// `as T?` may no longer be required after switching to JSpecify
|
||||
val result = `as`<T>(javaType.type) as T?
|
||||
val result = `as`<T>(typeOf<T>().javaType)
|
||||
if (result == null && null !is T) {
|
||||
throw ConversionException(
|
||||
"Expected a non-null value but got `null`. " +
|
||||
"To allow null values, convert to a nullable Kotlin type, for example `String?`."
|
||||
)
|
||||
}
|
||||
return result as T
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -48,16 +48,16 @@ internal class PPairToKotlinPair : ConverterFactory {
|
||||
) : Converter<PPair<Any, Any>, Pair<F, S>> {
|
||||
|
||||
private var firstCachedType = PClassInfo.Unavailable
|
||||
private var firstCachedConverter: Converter<Any, F>? = null
|
||||
private lateinit var firstCachedConverter: Converter<Any, F>
|
||||
|
||||
private var secondCachedType = PClassInfo.Unavailable
|
||||
private var secondCachedConverter: Converter<Any, S>? = null
|
||||
private lateinit var secondCachedConverter: Converter<Any, S>
|
||||
|
||||
override fun convert(value: PPair<Any, Any>, valueMapper: ValueMapper): Pair<F, S> {
|
||||
val first = value.first
|
||||
if (!firstCachedType.isExactClassOf(first)) {
|
||||
firstCachedType = PClassInfo.forValue(first)
|
||||
firstCachedConverter = valueMapper.getConverter(firstCachedType, firstTargetType)
|
||||
firstCachedConverter = valueMapper.getConverter<Any, F>(firstCachedType, firstTargetType)
|
||||
}
|
||||
|
||||
val second = value.second
|
||||
@@ -67,8 +67,8 @@ internal class PPairToKotlinPair : ConverterFactory {
|
||||
}
|
||||
|
||||
return Pair(
|
||||
firstCachedConverter!!.convert(first, valueMapper),
|
||||
secondCachedConverter!!.convert(second, valueMapper),
|
||||
firstCachedConverter.convert(first, valueMapper),
|
||||
secondCachedConverter.convert(second, valueMapper),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user