[lombok] Basic @Value support

This commit is contained in:
Andrey Zinovyev
2021-03-30 13:40:08 +03:00
committed by TeamCityServer
parent 97fc187e77
commit c7b2c731af
7 changed files with 80 additions and 11 deletions
@@ -31,7 +31,9 @@ Features support:
[x] [@Data](https://projectlombok.org/features/Data)
[ ] [@Value](https://projectlombok.org/features/Value)
[~] [@Value](https://projectlombok.org/features/Value)
- [x] generate getters and constructors
- [ ] make class final, make fields private and final
[ ] [@Builder](https://projectlombok.org/features/Builder)
@@ -96,8 +96,8 @@ data class NoArgsConstructor(
}
data class AllArgsConstructor(
override val visibility: DescriptorVisibility,
override val staticName: String?
override val visibility: DescriptorVisibility = DescriptorVisibilities.PUBLIC,
override val staticName: String? = null
) : ConstructorAnnotation {
companion object : AnnotationCompanion<AllArgsConstructor>() {
override val name: FqName = LombokNames.ALL_ARGS_CONSTRUCTOR
@@ -145,3 +145,22 @@ data class Data(val staticConstructor: String?) {
}
}
data class Value(val staticConstructor: String?) {
fun asGetter(): Getter = Getter()
fun asAllArgsConstructor(): AllArgsConstructor = AllArgsConstructor(
staticName = staticConstructor
)
companion object : AnnotationCompanion<Value>() {
override val name: FqName = LombokNames.VALUE
override fun extract(annotation: AnnotationDescriptor): Value =
Value(
staticConstructor = annotation.getNonBlankStringArgument("staticConstructor")
)
}
}
@@ -8,12 +8,13 @@ package org.jetbrains.kotlin.lombok.processor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.lombok.config.AllArgsConstructor
import org.jetbrains.kotlin.lombok.config.Value
import org.jetbrains.kotlin.lombok.utils.getJavaFields
class AllArgsConstructorProcessor : AbstractConstructorProcessor<AllArgsConstructor>() {
override fun getAnnotation(classDescriptor: ClassDescriptor): AllArgsConstructor? =
AllArgsConstructor.getOrNull(classDescriptor)
AllArgsConstructor.getOrNull(classDescriptor) ?: Value.getOrNull(classDescriptor)?.asAllArgsConstructor()
override fun getPropertiesForParameters(classDescriptor: ClassDescriptor): List<PropertyDescriptor> =
classDescriptor.getJavaFields()
@@ -9,10 +9,7 @@ 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.Accessors
import org.jetbrains.kotlin.lombok.config.Data
import org.jetbrains.kotlin.lombok.config.Getter
import org.jetbrains.kotlin.lombok.config.LombokConfig
import org.jetbrains.kotlin.lombok.config.*
import org.jetbrains.kotlin.lombok.utils.collectWithNotNull
import org.jetbrains.kotlin.lombok.utils.createFunction
import org.jetbrains.kotlin.lombok.utils.getJavaFields
@@ -28,7 +25,10 @@ class GetterProcessor(private val config: LombokConfig) : Processor {
override fun contribute(classDescriptor: ClassDescriptor, jClass: JavaClassImpl): Parts {
val clAccessors = Accessors.getOrNull(classDescriptor)
val clGetter = Getter.getOrNull(classDescriptor) ?: Data.getOrNull(classDescriptor)?.asGetter()
val clGetter =
Getter.getOrNull(classDescriptor)
?: Data.getOrNull(classDescriptor)?.asGetter()
?: Value.getOrNull(classDescriptor)?.asGetter()
val functions = classDescriptor
.getJavaFields()
@@ -11,8 +11,8 @@ import lombok.*;
@ToString(includeFieldNames=true)
@Data(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private final T value;
private final String name;
private final T value;
}
public static void usage() {
@@ -0,0 +1,42 @@
//FILE: ValueExample.java
import lombok.*;
@Value public class ValueExample {
private final String name;
private int age;
private double score;
@ToString(includeFieldNames=true)
@Value(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private T value;
}
public static void usage() {
val obj = new ValueExample("name", 12, 4.5);
obj.getName();
obj.getAge();
obj.getScore();
Exercise<Integer> ex = Exercise.of("name", 12);
ex.getName();
ex.getValue();
}
}
//FILE: test.kt
class Test {
fun run() {
val obj = ValueExample("name", 12, 4.5)
obj.getName()
val name = obj.name
obj.getAge()
val age = obj.age
val score = obj.score
val ex: ValueExample.Exercise<Int> = ValueExample.Exercise.of("name", 12)
}
}
@@ -126,6 +126,11 @@ public class LombokCompileTestGenerated extends AbstractLombokCompileTest {
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/simple.kt");
}
@TestMetadata("value.kt")
public void testValue() throws Exception {
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/value.kt");
}
@TestMetadata("with.kt")
public void testWith() throws Exception {
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt");