[lombok] @With support

This commit is contained in:
Andrey Zinovyev
2021-03-25 18:20:17 +03:00
committed by TeamCityServer
parent 7fd8f7b3bc
commit e0a95ff556
6 changed files with 98 additions and 5 deletions
@@ -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<Processor> =
listOf(
GetterProcessor(config),
SetterProcessor(config)
SetterProcessor(config),
WithProcessor(config),
)
private val partsCache: MutableMap<ClassDescriptor, Parts> = WeakHashMap()
@@ -59,3 +59,14 @@ data class Setter(val visibility: DescriptorVisibility) {
)
}
}
data class With(val visibility: DescriptorVisibility) {
companion object : AnnotationCompanion<With>() {
override val name: FqName = LombokNames.WITH
override fun extract(annotation: AnnotationDescriptor): With =
With(
visibility = getVisibility(annotation)
)
}
}
@@ -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
)
}
}
@@ -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")
}
@@ -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")
}
}
@@ -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");
}
}