[lombok] Basic @Value support
This commit is contained in:
committed by
TeamCityServer
parent
97fc187e77
commit
c7b2c731af
@@ -31,7 +31,9 @@ Features support:
|
|||||||
|
|
||||||
[x] [@Data](https://projectlombok.org/features/Data)
|
[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)
|
[ ] [@Builder](https://projectlombok.org/features/Builder)
|
||||||
|
|
||||||
|
|||||||
+21
-2
@@ -96,8 +96,8 @@ data class NoArgsConstructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
data class AllArgsConstructor(
|
data class AllArgsConstructor(
|
||||||
override val visibility: DescriptorVisibility,
|
override val visibility: DescriptorVisibility = DescriptorVisibilities.PUBLIC,
|
||||||
override val staticName: String?
|
override val staticName: String? = null
|
||||||
) : ConstructorAnnotation {
|
) : ConstructorAnnotation {
|
||||||
companion object : AnnotationCompanion<AllArgsConstructor>() {
|
companion object : AnnotationCompanion<AllArgsConstructor>() {
|
||||||
override val name: FqName = LombokNames.ALL_ARGS_CONSTRUCTOR
|
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")
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -8,12 +8,13 @@ package org.jetbrains.kotlin.lombok.processor
|
|||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||||
import org.jetbrains.kotlin.lombok.config.AllArgsConstructor
|
import org.jetbrains.kotlin.lombok.config.AllArgsConstructor
|
||||||
|
import org.jetbrains.kotlin.lombok.config.Value
|
||||||
import org.jetbrains.kotlin.lombok.utils.getJavaFields
|
import org.jetbrains.kotlin.lombok.utils.getJavaFields
|
||||||
|
|
||||||
class AllArgsConstructorProcessor : AbstractConstructorProcessor<AllArgsConstructor>() {
|
class AllArgsConstructorProcessor : AbstractConstructorProcessor<AllArgsConstructor>() {
|
||||||
|
|
||||||
override fun getAnnotation(classDescriptor: ClassDescriptor): AllArgsConstructor? =
|
override fun getAnnotation(classDescriptor: ClassDescriptor): AllArgsConstructor? =
|
||||||
AllArgsConstructor.getOrNull(classDescriptor)
|
AllArgsConstructor.getOrNull(classDescriptor) ?: Value.getOrNull(classDescriptor)?.asAllArgsConstructor()
|
||||||
|
|
||||||
override fun getPropertiesForParameters(classDescriptor: ClassDescriptor): List<PropertyDescriptor> =
|
override fun getPropertiesForParameters(classDescriptor: ClassDescriptor): List<PropertyDescriptor> =
|
||||||
classDescriptor.getJavaFields()
|
classDescriptor.getJavaFields()
|
||||||
|
|||||||
+5
-5
@@ -9,10 +9,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
|||||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
|
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
|
||||||
import org.jetbrains.kotlin.lombok.config.Accessors
|
import org.jetbrains.kotlin.lombok.config.*
|
||||||
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.utils.collectWithNotNull
|
import org.jetbrains.kotlin.lombok.utils.collectWithNotNull
|
||||||
import org.jetbrains.kotlin.lombok.utils.createFunction
|
import org.jetbrains.kotlin.lombok.utils.createFunction
|
||||||
import org.jetbrains.kotlin.lombok.utils.getJavaFields
|
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 {
|
override fun contribute(classDescriptor: ClassDescriptor, jClass: JavaClassImpl): Parts {
|
||||||
val clAccessors = Accessors.getOrNull(classDescriptor)
|
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
|
val functions = classDescriptor
|
||||||
.getJavaFields()
|
.getJavaFields()
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import lombok.*;
|
|||||||
@ToString(includeFieldNames=true)
|
@ToString(includeFieldNames=true)
|
||||||
@Data(staticConstructor="of")
|
@Data(staticConstructor="of")
|
||||||
public static class Exercise<T> {
|
public static class Exercise<T> {
|
||||||
private final String name;
|
private final String name;
|
||||||
private final T value;
|
private final T value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void usage() {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -126,6 +126,11 @@ public class LombokCompileTestGenerated extends AbstractLombokCompileTest {
|
|||||||
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/simple.kt");
|
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")
|
@TestMetadata("with.kt")
|
||||||
public void testWith() throws Exception {
|
public void testWith() throws Exception {
|
||||||
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt");
|
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/with.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user