diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/LombokSyntheticJavaPartsProvider.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/LombokSyntheticJavaPartsProvider.kt index 5259aa26c3c..55f5a23881f 100644 --- a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/LombokSyntheticJavaPartsProvider.kt +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/LombokSyntheticJavaPartsProvider.kt @@ -10,10 +10,7 @@ import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl import org.jetbrains.kotlin.lombok.config.LombokConfig -import org.jetbrains.kotlin.lombok.processor.GetterProcessor -import org.jetbrains.kotlin.lombok.processor.Parts -import org.jetbrains.kotlin.lombok.processor.Processor -import org.jetbrains.kotlin.lombok.processor.SetterProcessor +import org.jetbrains.kotlin.lombok.processor.* import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.jvm.SyntheticJavaPartsProvider import java.util.* @@ -25,7 +22,8 @@ class LombokSyntheticJavaPartsProvider(private val config: LombokConfig) : Synth private fun initProcessors(): List = listOf( GetterProcessor(config), - SetterProcessor(config) + SetterProcessor(config), + WithProcessor(config), ) private val partsCache: MutableMap = WeakHashMap() diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/config/annotationConfig.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/config/annotationConfig.kt index b1729d57883..32408149201 100644 --- a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/config/annotationConfig.kt +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/config/annotationConfig.kt @@ -59,3 +59,14 @@ data class Setter(val visibility: DescriptorVisibility) { ) } } + +data class With(val visibility: DescriptorVisibility) { + companion object : AnnotationCompanion() { + override val name: FqName = LombokNames.WITH + + override fun extract(annotation: AnnotationDescriptor): With = + With( + visibility = getVisibility(annotation) + ) + } +} diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt new file mode 100644 index 00000000000..4e2f26463de --- /dev/null +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/WithProcessor.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.lombok.processor + +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor +import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl +import org.jetbrains.kotlin.lombok.config.LombokConfig +import org.jetbrains.kotlin.lombok.config.With +import org.jetbrains.kotlin.lombok.utils.* +import org.jetbrains.kotlin.name.Name + +class WithProcessor(private val config: LombokConfig) : Processor { + override fun contribute(classDescriptor: ClassDescriptor, jClass: JavaClassImpl): Parts { + + val clWith = With.getOrNull(classDescriptor) + + val functions = classDescriptor + .getVariables() + .collectWithNotNull { With.getOrNull(it) ?: clWith } + .mapNotNull { (field, annotation) -> createWith(classDescriptor, field, annotation) } + + return Parts(functions) + } + + private fun createWith( + classDescriptor: ClassDescriptor, + field: PropertyDescriptor, + with: With + ): SimpleFunctionDescriptor? { + + val functionName = "with" + toPreparedBase(field.name.identifier) + + return classDescriptor.createFunction( + Name.identifier(functionName), + listOf(ValueParameter(field.name, field.type)), + classDescriptor.defaultType, + visibility = with.visibility + ) + } +} diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/LombokNames.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/LombokNames.kt index 22c95a22d3c..42f3f1f7ba1 100644 --- a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/LombokNames.kt +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/LombokNames.kt @@ -12,5 +12,6 @@ object LombokNames { val ACCESSORS = FqName("lombok.experimental.Accessors") val GETTER = FqName("lombok.Getter") val SETTER = FqName("lombok.Setter") + val WITH = FqName("lombok.With") } diff --git a/plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt b/plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt new file mode 100644 index 00000000000..b463472e97f --- /dev/null +++ b/plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt @@ -0,0 +1,33 @@ +//FILE: WithExample.java + +import lombok.AccessLevel; +import lombok.With; + +public class WithExample { + + //todo replace constructors with annotations when supported + public WithExample() { + + } + + public WithExample(int age, String name) { + + } + + @With private int age = 10; + + @With private String name; + + static void test() { + WithExample obj = new WithExample().withAge(16).withName("fooo"); + } +} + + +//FILE: test.kt + +object Test { + fun usage() { + val obj: WithExample = WithExample().withAge(16).withName("fooo") + } +} diff --git a/plugins/lombok/lombok-compiler-plugin/tests/org/jetbrains/kotlin/lombok/LombokCompileTestGenerated.java b/plugins/lombok/lombok-compiler-plugin/tests/org/jetbrains/kotlin/lombok/LombokCompileTestGenerated.java index 53a026b2443..c5780835c09 100644 --- a/plugins/lombok/lombok-compiler-plugin/tests/org/jetbrains/kotlin/lombok/LombokCompileTestGenerated.java +++ b/plugins/lombok/lombok-compiler-plugin/tests/org/jetbrains/kotlin/lombok/LombokCompileTestGenerated.java @@ -70,4 +70,9 @@ public class LombokCompileTestGenerated extends AbstractLombokCompileTest { public void testSimple() throws Exception { runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/simple.kt"); } + + @TestMetadata("with.kt") + public void testWith() throws Exception { + runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt"); + } }