From 637d7678b8fe1e6fdc26ad73fa505a41867188f3 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 3 Aug 2022 13:37:28 +0300 Subject: [PATCH] [Lombok K1] Fix name convention for @With on boolean fields ^KT-53370 Fixed --- .../kotlin/lombok/processor/WithProcessor.kt | 10 ++++- .../lombok/testData/box/withBooleanField.kt | 42 +++++++++++++++++++ ...BlackBoxCodegenTestForLombokGenerated.java | 6 +++ ...BlackBoxCodegenTestForLombokGenerated.java | 6 +++ ...BlackBoxCodegenTestForLombokGenerated.java | 6 +++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 plugins/lombok/testData/box/withBooleanField.kt diff --git a/plugins/lombok/lombok.k1/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt b/plugins/lombok/lombok.k1/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt index 6e9c9bb22b1..1f63acad032 100644 --- a/plugins/lombok/lombok.k1/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt +++ b/plugins/lombok/lombok.k1/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.lombok.config.LombokAnnotations.With import org.jetbrains.kotlin.lombok.config.toDescriptorVisibility import org.jetbrains.kotlin.lombok.utils.* import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.types.typeUtil.isBoolean class WithProcessor : Processor { override fun contribute(classDescriptor: ClassDescriptor, partsBuilder: SyntheticPartsBuilder) { @@ -32,7 +33,14 @@ class WithProcessor : Processor { ): SimpleFunctionDescriptor? { if (with.visibility == AccessLevel.NONE) return null - val functionName = "with" + toPropertyNameCapitalized(field.name.identifier) + val rawPropertyName = field.name.identifier + val propertyName = if (field.type.isBoolean() && rawPropertyName.startsWith("is")) { + rawPropertyName.removePrefix("is") + } else { + rawPropertyName + } + + val functionName = "with" + toPropertyNameCapitalized(propertyName) return classDescriptor.createFunction( Name.identifier(functionName), diff --git a/plugins/lombok/testData/box/withBooleanField.kt b/plugins/lombok/testData/box/withBooleanField.kt new file mode 100644 index 00000000000..656da4aa7ba --- /dev/null +++ b/plugins/lombok/testData/box/withBooleanField.kt @@ -0,0 +1,42 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// ISSUE: KT-53451, KT-53370 + +// FILE: UserData.java +import lombok.*; + +public class UserData { + @With @Getter + private final String name; + + @With @Getter + private final boolean isSome; + + @With @Getter + private final boolean hasFlag; + + @With @Getter + private final boolean justAnother; + + public UserData(String name, boolean isSome, boolean hasFlag, boolean justAnother) { + this.name = name; + this.isSome = isSome; + this.hasFlag = hasFlag; + this.justAnother = justAnother; + } +} + +// FILE: main.kt +fun box(): String { + val user = UserData("Bob", false, false, false) + val modifiedUser = user + .withName("Alex") + .withSome(true) + .withHasFlag(true) + .withJustAnother(true) + + return if (modifiedUser.name == "Alex" && modifiedUser.isSome && modifiedUser.isHasFlag && modifiedUser.isJustAnother) { + "OK" + } else { + "Error" + } +} diff --git a/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/BlackBoxCodegenTestForLombokGenerated.java b/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/BlackBoxCodegenTestForLombokGenerated.java index 50191ce64c2..f5c2dd8d331 100644 --- a/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/BlackBoxCodegenTestForLombokGenerated.java +++ b/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/BlackBoxCodegenTestForLombokGenerated.java @@ -162,4 +162,10 @@ public class BlackBoxCodegenTestForLombokGenerated extends AbstractBlackBoxCodeg public void testWith() throws Exception { runTest("plugins/lombok/testData/box/with.kt"); } + + @Test + @TestMetadata("withBooleanField.kt") + public void testWithBooleanField() throws Exception { + runTest("plugins/lombok/testData/box/withBooleanField.kt"); + } } diff --git a/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/FirBlackBoxCodegenTestForLombokGenerated.java b/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/FirBlackBoxCodegenTestForLombokGenerated.java index 83b6af9a5b1..cf6d220fffa 100644 --- a/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/FirBlackBoxCodegenTestForLombokGenerated.java +++ b/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/FirBlackBoxCodegenTestForLombokGenerated.java @@ -162,4 +162,10 @@ public class FirBlackBoxCodegenTestForLombokGenerated extends AbstractFirBlackBo public void testWith() throws Exception { runTest("plugins/lombok/testData/box/with.kt"); } + + @Test + @TestMetadata("withBooleanField.kt") + public void testWithBooleanField() throws Exception { + runTest("plugins/lombok/testData/box/withBooleanField.kt"); + } } diff --git a/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/IrBlackBoxCodegenTestForLombokGenerated.java b/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/IrBlackBoxCodegenTestForLombokGenerated.java index 70c1bd92ad2..6381d1bed9a 100644 --- a/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/IrBlackBoxCodegenTestForLombokGenerated.java +++ b/plugins/lombok/tests-gen/org/jetbrains/kotlin/lombok/IrBlackBoxCodegenTestForLombokGenerated.java @@ -162,4 +162,10 @@ public class IrBlackBoxCodegenTestForLombokGenerated extends AbstractIrBlackBoxC public void testWith() throws Exception { runTest("plugins/lombok/testData/box/with.kt"); } + + @Test + @TestMetadata("withBooleanField.kt") + public void testWithBooleanField() throws Exception { + runTest("plugins/lombok/testData/box/withBooleanField.kt"); + } }