Skip to content

Commit 453ef1c

Browse files
authored
Merge pull request #288 from ProjectMapK/ktlint
Update kotlinter and formatting
2 parents c040306 + 3c6f965 commit 453ef1c

File tree

27 files changed

+202
-173
lines changed

27 files changed

+202
-173
lines changed

build.gradle.kts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ java {
5757
}
5858

5959
tasks {
60-
// For ported tests, they are excluded from the formatting because they are not new code.
61-
lintKotlinTest {
62-
exclude { it.path.contains("zPorted") }
63-
}
64-
formatKotlinTest {
65-
exclude { it.path.contains("zPorted") }
66-
}
67-
6860
// Task to generate version file
6961
val generateKogeraVersion by registering(Copy::class) {
7062
val packageStr = "$groupStr.jackson.module.kogera"
@@ -93,6 +85,14 @@ public val kogeraVersion: Version = VersionUtil.parseVersion("$version", "$group
9385
dependsOn.add(generateKogeraVersion)
9486
}
9587

88+
// For ported tests, they are excluded from the formatting because they are not new code.
89+
lintKotlinTest {
90+
exclude { it.path.contains("zPorted") }
91+
}
92+
formatKotlinTest {
93+
exclude { it.path.contains("zPorted") }
94+
}
95+
9696
compileKotlin {
9797
dependsOn.add(generateKogeraVersion)
9898
kotlinOptions.jvmTarget = "1.8"

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jackson-xml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-xm
1919
jackson-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson" }
2020

2121
[plugins]
22-
kotlinter = { id = "org.jmailen.kotlinter", version = "4.4.1" }
22+
kotlinter = { id = "org.jmailen.kotlinter", version = "5.0.1" }

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/Converters.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ internal class ValueClassUnboxConverter<T : Any>(val valueClass: Class<T>) : Std
3333
override fun convert(value: T): Any? = unboxMethod.invoke(value)
3434

3535
override fun getInputType(typeFactory: TypeFactory): JavaType = typeFactory.constructType(valueClass)
36-
override fun getOutputType(typeFactory: TypeFactory): JavaType =
37-
typeFactory.constructType(unboxMethod.genericReturnType)
36+
override fun getOutputType(
37+
typeFactory: TypeFactory
38+
): JavaType = typeFactory.constructType(unboxMethod.genericReturnType)
3839

3940
val delegatingSerializer: StdDelegatingSerializer by lazy { StdDelegatingSerializer(this) }
4041
}

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/Extensions.kt

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,27 @@ public fun jsonMapper(initializer: JsonMapper.Builder.() -> Unit = {}): JsonMapp
3535

3636
// region: JvmOverloads is set for bytecode compatibility for versions below 2.17.
3737
@JvmOverloads
38-
public fun jacksonObjectMapper(initializer: KotlinModule.Builder.() -> Unit = {}): ObjectMapper =
39-
jsonMapper { addModule(kotlinModule(initializer)) }
38+
public fun jacksonObjectMapper(initializer: KotlinModule.Builder.() -> Unit = {}): ObjectMapper = jsonMapper {
39+
addModule(kotlinModule(initializer))
40+
}
4041

4142
@JvmOverloads
42-
public fun jacksonMapperBuilder(initializer: KotlinModule.Builder.() -> Unit = {}): JsonMapper.Builder =
43-
JsonMapper.builder().addModule(kotlinModule(initializer))
43+
public fun jacksonMapperBuilder(initializer: KotlinModule.Builder.() -> Unit = {}): JsonMapper.Builder = JsonMapper
44+
.builder()
45+
.addModule(kotlinModule(initializer))
4446

4547
@JvmOverloads
46-
public fun ObjectMapper.registerKotlinModule(initializer: KotlinModule.Builder.() -> Unit = {}): ObjectMapper =
47-
this.registerModule(kotlinModule(initializer))
48+
public fun ObjectMapper.registerKotlinModule(
49+
initializer: KotlinModule.Builder.() -> Unit = {}
50+
): ObjectMapper = this.registerModule(kotlinModule(initializer))
4851
// endregion
4952

5053
public inline fun <reified T> jacksonTypeRef(): TypeReference<T> = object : TypeReference<T>() {}
5154

5255
public inline fun <reified T> ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
53-
public inline fun <reified T> ObjectMapper.readValues(jp: JsonParser): MappingIterator<T> =
54-
readValues(jp, jacksonTypeRef<T>())
56+
public inline fun <reified T> ObjectMapper.readValues(
57+
jp: JsonParser
58+
): MappingIterator<T> = readValues(jp, jacksonTypeRef<T>())
5559

5660
public inline fun <reified T> ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef<T>())
5761
public inline fun <reified T> ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef<T>())
@@ -60,20 +64,22 @@ public inline fun <reified T> ObjectMapper.readValue(src: Reader): T = readValue
6064
public inline fun <reified T> ObjectMapper.readValue(src: InputStream): T = readValue(src, jacksonTypeRef<T>())
6165
public inline fun <reified T> ObjectMapper.readValue(src: ByteArray): T = readValue(src, jacksonTypeRef<T>())
6266

63-
public inline fun <reified T> ObjectMapper.treeToValue(n: TreeNode): T =
64-
readValue(this.treeAsTokens(n), jacksonTypeRef<T>())
67+
public inline fun <reified T> ObjectMapper.treeToValue(n: TreeNode): T = readValue(treeAsTokens(n), jacksonTypeRef<T>())
6568
public inline fun <reified T> ObjectMapper.convertValue(from: Any?): T = convertValue(from, jacksonTypeRef<T>())
6669

6770
public inline fun <reified T> ObjectReader.readValueTyped(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
68-
public inline fun <reified T> ObjectReader.readValuesTyped(jp: JsonParser): Iterator<T> =
69-
readValues(jp, jacksonTypeRef<T>())
70-
public inline fun <reified T> ObjectReader.treeToValue(n: TreeNode): T? =
71-
readValue(this.treeAsTokens(n), jacksonTypeRef<T>())
72-
73-
public inline fun <reified T, reified U> ObjectMapper.addMixIn(): ObjectMapper =
74-
this.addMixIn(T::class.java, U::class.java)
75-
public inline fun <reified T, reified U> JsonMapper.Builder.addMixIn(): JsonMapper.Builder =
76-
this.addMixIn(T::class.java, U::class.java)
71+
public inline fun <reified T> ObjectReader.readValuesTyped(
72+
jp: JsonParser
73+
): Iterator<T> = readValues(jp, jacksonTypeRef<T>())
74+
public inline fun <reified T> ObjectReader.treeToValue(
75+
n: TreeNode
76+
): T? = readValue(treeAsTokens(n), jacksonTypeRef<T>())
77+
78+
public inline fun <reified T, reified U> ObjectMapper.addMixIn(): ObjectMapper = addMixIn(T::class.java, U::class.java)
79+
public inline fun <reified T, reified U> JsonMapper.Builder.addMixIn(): JsonMapper.Builder = addMixIn(
80+
T::class.java,
81+
U::class.java
82+
)
7783

7884
public operator fun ArrayNode.plus(element: Boolean) {
7985
add(element)

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/InternalCommons.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ internal fun Array<Class<*>>.toDescBuilder(): StringBuilder = this
5454
.fold(StringBuilder("(")) { acc, cur -> acc.appendDescriptor(cur) }
5555
.append(')')
5656

57-
internal fun Constructor<*>.toSignature(): JvmMethodSignature =
58-
JvmMethodSignature("<init>", parameterTypes.toDescBuilder().append('V').toString())
57+
internal fun Constructor<*>.toSignature(): JvmMethodSignature = JvmMethodSignature(
58+
"<init>",
59+
parameterTypes.toDescBuilder().append('V').toString()
60+
)
5961

60-
internal fun Method.toSignature(): JvmMethodSignature =
61-
JvmMethodSignature(this.name, parameterTypes.toDescBuilder().appendDescriptor(this.returnType).toString())
62+
internal fun Method.toSignature(): JvmMethodSignature = JvmMethodSignature(
63+
this.name,
64+
parameterTypes.toDescBuilder().appendDescriptor(this.returnType).toString()
65+
)
6266

6367
internal val defaultConstructorMarker: Class<*> by lazy {
6468
Class.forName("kotlin.jvm.internal.DefaultConstructorMarker")
@@ -84,8 +88,7 @@ internal fun String.reconstructClass(): Class<*> {
8488
}
8589

8690
internal fun KmType.reconstructClassOrNull(): Class<*>? = (classifier as? KmClassifier.Class)?.reconstructClassOrNull()
87-
internal fun KmClassifier.Class.reconstructClassOrNull(): Class<*>? =
88-
runCatching { name.reconstructClass() }.getOrNull()
91+
internal fun KmClassifier.Class.reconstructClassOrNull(): Class<*>? = runCatching { name.reconstructClass() }.getOrNull()
8992

9093
internal fun AnnotatedElement.hasCreatorAnnotation(): Boolean = getAnnotation(JSON_CREATOR_CLASS)
9194
?.let { it.mode != JsonCreator.Mode.DISABLED }

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/KotlinModule.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,13 @@ public class KotlinModule private constructor(
166166
bitSet.andNot(feature.bitSet)
167167
}
168168

169-
public fun configure(feature: KotlinFeature, enabled: Boolean): Builder =
170-
when {
171-
enabled -> enable(feature)
172-
else -> disable(feature)
173-
}
169+
public fun configure(feature: KotlinFeature, enabled: Boolean): Builder = when {
170+
enabled -> enable(feature)
171+
else -> disable(feature)
172+
}
174173

175-
public fun isEnabled(feature: KotlinFeature): Boolean =
176-
bitSet.intersects(feature.bitSet)
174+
public fun isEnabled(feature: KotlinFeature): Boolean = bitSet.intersects(feature.bitSet)
177175

178-
public fun build(): KotlinModule =
179-
KotlinModule(this)
176+
public fun build(): KotlinModule = KotlinModule(this)
180177
}
181178
}

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/ReflectionCache.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ internal class ReflectionCache(initialCacheSize: Int, maxCacheSize: Int) : Seria
2222

2323
// The comparison was implemented directly because the decompiled results showed subtle efficiency.
2424

25-
final override fun equals(other: Any?): Boolean =
26-
(other as? OtherCacheKey<*, *>)?.let { it::class == this::class && it.key == key } ?: false
25+
final override fun equals(other: Any?): Boolean = (other as? OtherCacheKey<*, *>)
26+
?.let { it::class == this::class && it.key == key } == true
2727

2828
// If the hashCode matches the raw key, the search efficiency is reduced, so it is displaced.
2929
final override fun hashCode(): Int = key.hashCode() * 31

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/SpreadWrapperDelegates.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.lang.reflect.Method
66
internal fun <T> Constructor<T>.call(args: Array<*>): T = SpreadWrapper.newInstance(this, args)
77
internal fun Method.call(instance: Any?, args: Array<*>): Any? = SpreadWrapper.invoke(this, instance, args)
88

9-
internal fun <T> Class<T>.getDeclaredConstructorBy(parameterTypes: Array<Class<*>>): Constructor<T> =
10-
SpreadWrapper.getDeclaredConstructor(this, parameterTypes)
11-
internal fun Class<*>.getDeclaredMethodBy(name: String, parameterTypes: Array<Class<*>>): Method =
12-
SpreadWrapper.getDeclaredMethod(this, name, parameterTypes)
9+
internal fun <T> Class<T>.getDeclaredConstructorBy(parameterTypes: Array<Class<*>>): Constructor<T> = SpreadWrapper
10+
.getDeclaredConstructor(this, parameterTypes)
11+
internal fun Class<*>.getDeclaredMethodBy(name: String, parameterTypes: Array<Class<*>>): Method = SpreadWrapper
12+
.getDeclaredMethod(this, name, parameterTypes)

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/annotationIntrospector/KotlinFallbackAnnotationIntrospector.kt

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ internal class KotlinFallbackAnnotationIntrospector(
3838
private val useJavaDurationConversion: Boolean,
3939
private val cache: ReflectionCache
4040
) : NopAnnotationIntrospector() {
41-
private fun findKotlinParameter(param: AnnotatedParameter): JmValueParameter? =
42-
when (val owner = param.owner.member) {
43-
is Constructor<*> -> cache.getJmClass(param.declaringClass)?.findJmConstructor(owner)?.valueParameters
44-
is Method -> if (Modifier.isStatic(owner.modifiers)) {
45-
cache.getJmClass(param.declaringClass)
46-
?.companion
47-
?.let { it.findFunctionByMethod(owner)?.valueParameters }
48-
} else {
49-
null
50-
}
51-
else -> null
52-
}?.let { it[param.index] }
41+
private fun findKotlinParameter(
42+
param: AnnotatedParameter
43+
): JmValueParameter? = when (val owner = param.owner.member) {
44+
is Constructor<*> -> cache.getJmClass(param.declaringClass)?.findJmConstructor(owner)?.valueParameters
45+
is Method -> if (Modifier.isStatic(owner.modifiers)) {
46+
cache.getJmClass(param.declaringClass)
47+
?.companion
48+
?.let { it.findFunctionByMethod(owner)?.valueParameters }
49+
} else {
50+
null
51+
}
52+
else -> null
53+
}?.let { it[param.index] }
5354

54-
private fun findKotlinParameter(param: Annotated): JmValueParameter? =
55-
(param as? AnnotatedParameter)?.let { findKotlinParameter(it) }
55+
private fun findKotlinParameter(param: Annotated): JmValueParameter? = (param as? AnnotatedParameter)
56+
?.let { findKotlinParameter(it) }
5657

5758
// since 2.4
5859
override fun findImplicitPropertyName(member: AnnotatedMember): String? = when (member) {
@@ -65,13 +66,16 @@ internal class KotlinFallbackAnnotationIntrospector(
6566
else -> null
6667
}
6768

68-
override fun refineDeserializationType(config: MapperConfig<*>, a: Annotated, baseType: JavaType): JavaType =
69-
findKotlinParameter(a)?.let { param ->
70-
val rawType = a.rawType
71-
param.reconstructedClassOrNull
72-
?.takeIf { it.isUnboxableValueClass() && it != rawType }
73-
?.let { config.constructType(it) }
74-
} ?: baseType
69+
override fun refineDeserializationType(
70+
config: MapperConfig<*>,
71+
a: Annotated,
72+
baseType: JavaType
73+
): JavaType = findKotlinParameter(a)?.let { param ->
74+
val rawType = a.rawType
75+
param.reconstructedClassOrNull
76+
?.takeIf { it.isUnboxableValueClass() && it != rawType }
77+
?.let { config.constructType(it) }
78+
} ?: baseType
7579

7680
override fun findSerializationConverter(a: Annotated): Converter<*, *>? = when (a) {
7781
// Find a converter to handle the case where the getter returns an unboxed value from the value class.
@@ -163,9 +167,10 @@ private fun JmValueParameter.isNullishTypeAt(index: Int): Boolean = arguments.ge
163167
it === KmTypeProjection.STAR || it.type!!.isNullable
164168
} != false // If a type argument cannot be taken, treat it as nullable to avoid unexpected failure.
165169

166-
private fun JmValueParameter.requireStrictNullCheck(type: JavaType): Boolean =
167-
((type.isArrayType || type.isCollectionLikeType) && !this.isNullishTypeAt(0)) ||
168-
(type.isMapLikeType && !this.isNullishTypeAt(1))
170+
private fun JmValueParameter.requireStrictNullCheck(
171+
type: JavaType
172+
): Boolean = ((type.isArrayType || type.isCollectionLikeType) && !this.isNullishTypeAt(0)) ||
173+
(type.isMapLikeType && !this.isNullishTypeAt(1))
169174

170175
private fun JmClass.primarilyConstructor() = constructors.find { !it.isSecondary } ?: constructors.singleOrNull()
171176

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/annotationIntrospector/KotlinPrimaryAnnotationIntrospector.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ internal class KotlinPrimaryAnnotationIntrospector(
4444

4545
// Functions that call this may return incorrect results for value classes whose value type is Collection or Map,
4646
// but this is a rare case and difficult to handle, so it is not supported.
47-
private fun JavaType.hasDefaultEmptyValue() =
48-
(nullToEmptyCollection && isCollectionLikeType) || (nullToEmptyMap && isMapLikeType)
47+
private fun JavaType.hasDefaultEmptyValue() = (nullToEmptyCollection && isCollectionLikeType) ||
48+
(nullToEmptyMap && isMapLikeType)
4949

5050
// The nullToEmpty option also affects serialization,
5151
// but deserialization is preferred because there is currently no way to distinguish between contexts.
@@ -63,19 +63,20 @@ internal class KotlinPrimaryAnnotationIntrospector(
6363

6464
private fun JmProperty.isRequiredByNullability(): Boolean = !this.returnType.isNullable
6565

66-
private fun AnnotatedMethod.getRequiredMarkerFromCorrespondingAccessor(jmClass: JmClass): Boolean? =
67-
when (parameterCount) {
68-
0 -> jmClass.findPropertyByGetter(member)?.isRequiredByNullability()
69-
1 -> {
70-
if (this.getParameter(0).type.hasDefaultEmptyValue()) {
71-
false
72-
} else {
73-
val memberSignature = member.toSignature()
74-
jmClass.properties.find { it.setterSignature == memberSignature }?.isRequiredByNullability()
75-
}
66+
private fun AnnotatedMethod.getRequiredMarkerFromCorrespondingAccessor(
67+
jmClass: JmClass
68+
): Boolean? = when (parameterCount) {
69+
0 -> jmClass.findPropertyByGetter(member)?.isRequiredByNullability()
70+
1 -> {
71+
if (this.getParameter(0).type.hasDefaultEmptyValue()) {
72+
false
73+
} else {
74+
val memberSignature = member.toSignature()
75+
jmClass.properties.find { it.setterSignature == memberSignature }?.isRequiredByNullability()
7676
}
77-
else -> null
7877
}
78+
else -> null
79+
}
7980

8081
private fun AnnotatedParameter.hasRequiredMarker(jmClass: JmClass): Boolean? {
8182
val paramDef = when (val member = member) {

0 commit comments

Comments
 (0)