[lombok] Generate no-arg static factory method
This commit is contained in:
committed by
TeamCityServer
parent
37926f333e
commit
59f936fdef
+3
-3
@@ -78,7 +78,7 @@ class LazyJavaClassMemberScope(
|
|||||||
addAll(declaredMemberIndex().getMethodNames())
|
addAll(declaredMemberIndex().getMethodNames())
|
||||||
addAll(declaredMemberIndex().getRecordComponentNames())
|
addAll(declaredMemberIndex().getRecordComponentNames())
|
||||||
addAll(computeClassNames(kindFilter, nameFilter))
|
addAll(computeClassNames(kindFilter, nameFilter))
|
||||||
addAll(c.components.syntheticPartsProvider.getSyntheticFunctionNames(ownerDescriptor))
|
addAll(c.components.syntheticPartsProvider.getMethodNames(ownerDescriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
internal val constructors = c.storageManager.createLazyValue {
|
internal val constructors = c.storageManager.createLazyValue {
|
||||||
@@ -99,7 +99,7 @@ class LazyJavaClassMemberScope(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.components.syntheticPartsProvider.generateSyntheticConstructors(ownerDescriptor, result)
|
c.components.syntheticPartsProvider.generateConstructors(ownerDescriptor, result)
|
||||||
|
|
||||||
c.components.signatureEnhancement.enhanceSignatures(
|
c.components.signatureEnhancement.enhanceSignatures(
|
||||||
c,
|
c,
|
||||||
@@ -500,7 +500,7 @@ class LazyJavaClassMemberScope(
|
|||||||
result.add(resolveRecordComponentToFunctionDescriptor(declaredMemberIndex().findRecordComponentByName(name)!!))
|
result.add(resolveRecordComponentToFunctionDescriptor(declaredMemberIndex().findRecordComponentByName(name)!!))
|
||||||
}
|
}
|
||||||
|
|
||||||
c.components.syntheticPartsProvider.generateSyntheticMethods(ownerDescriptor, name, result)
|
c.components.syntheticPartsProvider.generateMethods(ownerDescriptor, name, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resolveRecordComponentToFunctionDescriptor(recordComponent: JavaRecordComponent): JavaMethodDescriptor {
|
private fun resolveRecordComponentToFunctionDescriptor(recordComponent: JavaRecordComponent): JavaMethodDescriptor {
|
||||||
|
|||||||
+5
@@ -48,6 +48,7 @@ class LazyJavaStaticClassScope(
|
|||||||
if (jClass.isEnum) {
|
if (jClass.isEnum) {
|
||||||
addAll(listOf(StandardNames.ENUM_VALUE_OF, StandardNames.ENUM_VALUES))
|
addAll(listOf(StandardNames.ENUM_VALUE_OF, StandardNames.ENUM_VALUES))
|
||||||
}
|
}
|
||||||
|
addAll(c.components.syntheticPartsProvider.getStaticFunctionNames(ownerDescriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun computePropertyNames(kindFilter: DescriptorKindFilter, nameFilter: ((Name) -> Boolean)?) =
|
override fun computePropertyNames(kindFilter: DescriptorKindFilter, nameFilter: ((Name) -> Boolean)?) =
|
||||||
@@ -81,6 +82,10 @@ class LazyJavaStaticClassScope(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun computeImplicitlyDeclaredFunctions(result: MutableCollection<SimpleFunctionDescriptor>, name: Name) {
|
||||||
|
c.components.syntheticPartsProvider.generateStaticFunctions(ownerDescriptor, name, result)
|
||||||
|
}
|
||||||
|
|
||||||
override fun computeNonDeclaredProperties(name: Name, result: MutableCollection<PropertyDescriptor>) {
|
override fun computeNonDeclaredProperties(name: Name, result: MutableCollection<PropertyDescriptor>) {
|
||||||
val propertiesFromSupertypes = flatMapJavaStaticSupertypesScopes(ownerDescriptor, mutableSetOf()) {
|
val propertiesFromSupertypes = flatMapJavaStaticSupertypesScopes(ownerDescriptor, mutableSetOf()) {
|
||||||
it.getContributedVariables(name, NoLookupLocation.WHEN_GET_SUPER_MEMBERS)
|
it.getContributedVariables(name, NoLookupLocation.WHEN_GET_SUPER_MEMBERS)
|
||||||
|
|||||||
+24
-9
@@ -16,31 +16,46 @@ interface SyntheticJavaPartsProvider {
|
|||||||
val EMPTY = CompositeSyntheticJavaPartsProvider(emptyList())
|
val EMPTY = CompositeSyntheticJavaPartsProvider(emptyList())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSyntheticFunctionNames(thisDescriptor: ClassDescriptor): List<Name>
|
fun getMethodNames(thisDescriptor: ClassDescriptor): List<Name>
|
||||||
|
|
||||||
fun generateSyntheticMethods(
|
fun generateMethods(
|
||||||
thisDescriptor: ClassDescriptor,
|
thisDescriptor: ClassDescriptor,
|
||||||
name: Name,
|
name: Name,
|
||||||
result: MutableCollection<SimpleFunctionDescriptor>
|
result: MutableCollection<SimpleFunctionDescriptor>
|
||||||
)
|
)
|
||||||
|
|
||||||
fun generateSyntheticConstructors(thisDescriptor: ClassDescriptor, result: MutableList<ClassConstructorDescriptor>)
|
fun getStaticFunctionNames(thisDescriptor: ClassDescriptor): List<Name>
|
||||||
|
|
||||||
|
fun generateStaticFunctions(
|
||||||
|
thisDescriptor: ClassDescriptor,
|
||||||
|
name: Name,
|
||||||
|
result: MutableCollection<SimpleFunctionDescriptor>
|
||||||
|
)
|
||||||
|
|
||||||
|
fun generateConstructors(thisDescriptor: ClassDescriptor, result: MutableList<ClassConstructorDescriptor>)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class CompositeSyntheticJavaPartsProvider(private val inner: List<SyntheticJavaPartsProvider>) : SyntheticJavaPartsProvider {
|
class CompositeSyntheticJavaPartsProvider(private val inner: List<SyntheticJavaPartsProvider>) : SyntheticJavaPartsProvider {
|
||||||
override fun getSyntheticFunctionNames(thisDescriptor: ClassDescriptor): List<Name> =
|
override fun getMethodNames(thisDescriptor: ClassDescriptor): List<Name> =
|
||||||
inner.flatMap { it.getSyntheticFunctionNames(thisDescriptor) }
|
inner.flatMap { it.getMethodNames(thisDescriptor) }
|
||||||
|
|
||||||
override fun generateSyntheticMethods(
|
override fun generateMethods(
|
||||||
thisDescriptor: ClassDescriptor,
|
thisDescriptor: ClassDescriptor,
|
||||||
name: Name,
|
name: Name,
|
||||||
result: MutableCollection<SimpleFunctionDescriptor>
|
result: MutableCollection<SimpleFunctionDescriptor>
|
||||||
) {
|
) {
|
||||||
inner.forEach { it.generateSyntheticMethods(thisDescriptor, name, result) }
|
inner.forEach { it.generateMethods(thisDescriptor, name, result) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generateSyntheticConstructors(thisDescriptor: ClassDescriptor, result: MutableList<ClassConstructorDescriptor>) {
|
override fun getStaticFunctionNames(thisDescriptor: ClassDescriptor): List<Name> =
|
||||||
inner.forEach { it.generateSyntheticConstructors(thisDescriptor, result) }
|
inner.flatMap { it.getStaticFunctionNames(thisDescriptor) }
|
||||||
|
|
||||||
|
override fun generateStaticFunctions(thisDescriptor: ClassDescriptor, name: Name, result: MutableCollection<SimpleFunctionDescriptor>) {
|
||||||
|
inner.forEach { it.generateStaticFunctions(thisDescriptor, name, result) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generateConstructors(thisDescriptor: ClassDescriptor, result: MutableList<ClassConstructorDescriptor>) {
|
||||||
|
inner.forEach { it.generateConstructors(thisDescriptor, result) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ Features support:
|
|||||||
- [x] Basic support
|
- [x] Basic support
|
||||||
- [ ] Copy annotations
|
- [ ] Copy annotations
|
||||||
|
|
||||||
[ ] [@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor](https://projectlombok.org/features/constructor)
|
[~] [@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor](https://projectlombok.org/features/constructor)
|
||||||
- [ ] Generate constructors
|
- [x] @NoArgsConstructor
|
||||||
- [ ] Generate static factory method
|
- [ ] @AllArgsConstructor
|
||||||
|
- [ ] @RequiredArgsConstructor
|
||||||
|
|
||||||
[ ] [@Data](https://projectlombok.org/features/Data)
|
[ ] [@Data](https://projectlombok.org/features/Data)
|
||||||
|
|
||||||
|
|||||||
+11
-3
@@ -30,10 +30,10 @@ class LombokSyntheticJavaPartsProvider(private val config: LombokConfig) : Synth
|
|||||||
|
|
||||||
private val partsCache: MutableMap<ClassDescriptor, Parts> = WeakHashMap()
|
private val partsCache: MutableMap<ClassDescriptor, Parts> = WeakHashMap()
|
||||||
|
|
||||||
override fun getSyntheticFunctionNames(thisDescriptor: ClassDescriptor): List<Name> =
|
override fun getMethodNames(thisDescriptor: ClassDescriptor): List<Name> =
|
||||||
getSyntheticParts(thisDescriptor).methods.map { it.name }
|
getSyntheticParts(thisDescriptor).methods.map { it.name }
|
||||||
|
|
||||||
override fun generateSyntheticMethods(
|
override fun generateMethods(
|
||||||
thisDescriptor: ClassDescriptor,
|
thisDescriptor: ClassDescriptor,
|
||||||
name: Name,
|
name: Name,
|
||||||
result: MutableCollection<SimpleFunctionDescriptor>
|
result: MutableCollection<SimpleFunctionDescriptor>
|
||||||
@@ -42,7 +42,15 @@ class LombokSyntheticJavaPartsProvider(private val config: LombokConfig) : Synth
|
|||||||
result.addAll(methods)
|
result.addAll(methods)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generateSyntheticConstructors(thisDescriptor: ClassDescriptor, result: MutableList<ClassConstructorDescriptor>) {
|
override fun getStaticFunctionNames(thisDescriptor: ClassDescriptor): List<Name> =
|
||||||
|
getSyntheticParts(thisDescriptor).staticFunctions.map { it.name }
|
||||||
|
|
||||||
|
override fun generateStaticFunctions(thisDescriptor: ClassDescriptor, name: Name, result: MutableCollection<SimpleFunctionDescriptor>) {
|
||||||
|
val functions = getSyntheticParts(thisDescriptor).staticFunctions.filter { it.name == name }
|
||||||
|
result.addAll(functions)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generateConstructors(thisDescriptor: ClassDescriptor, result: MutableList<ClassConstructorDescriptor>) {
|
||||||
val constructors = getSyntheticParts(thisDescriptor).constructors
|
val constructors = getSyntheticParts(thisDescriptor).constructors
|
||||||
result.addAll(constructors)
|
result.addAll(constructors)
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -9,6 +9,8 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
|||||||
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
|
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
|
||||||
import org.jetbrains.kotlin.lombok.config.NoArgsConstructor
|
import org.jetbrains.kotlin.lombok.config.NoArgsConstructor
|
||||||
import org.jetbrains.kotlin.lombok.utils.createConstructor
|
import org.jetbrains.kotlin.lombok.utils.createConstructor
|
||||||
|
import org.jetbrains.kotlin.lombok.utils.createFunction
|
||||||
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
|
||||||
class NoArgsConstructorProcessor : Processor {
|
class NoArgsConstructorProcessor : Processor {
|
||||||
|
|
||||||
@@ -21,7 +23,14 @@ class NoArgsConstructorProcessor : Processor {
|
|||||||
)
|
)
|
||||||
Parts(constructors = listOfNotNull(constructor))
|
Parts(constructors = listOfNotNull(constructor))
|
||||||
} else {
|
} else {
|
||||||
null
|
val function = classDescriptor.createFunction(
|
||||||
|
Name.identifier(annotation.staticName),
|
||||||
|
emptyList(),
|
||||||
|
classDescriptor.defaultType,
|
||||||
|
visibility = annotation.visibility,
|
||||||
|
receiver = null
|
||||||
|
)
|
||||||
|
Parts(staticFunctions = listOf(function))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result ?: Parts.Empty
|
return result ?: Parts.Empty
|
||||||
|
|||||||
+6
-1
@@ -10,10 +10,15 @@ import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
|||||||
|
|
||||||
data class Parts(
|
data class Parts(
|
||||||
val methods: List<SimpleFunctionDescriptor> = emptyList(),
|
val methods: List<SimpleFunctionDescriptor> = emptyList(),
|
||||||
|
val staticFunctions: List<SimpleFunctionDescriptor> = emptyList(),
|
||||||
val constructors: List<ClassConstructorDescriptor> = emptyList()
|
val constructors: List<ClassConstructorDescriptor> = emptyList()
|
||||||
) {
|
) {
|
||||||
|
|
||||||
operator fun plus(other: Parts): Parts = Parts(methods + other.methods, constructors + other.constructors)
|
operator fun plus(other: Parts): Parts = Parts(
|
||||||
|
methods + other.methods,
|
||||||
|
staticFunctions + other.staticFunctions,
|
||||||
|
constructors + other.constructors
|
||||||
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val Empty = Parts()
|
val Empty = Parts()
|
||||||
|
|||||||
+2
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.resolve.constants.BooleanValue
|
import org.jetbrains.kotlin.resolve.constants.BooleanValue
|
||||||
import org.jetbrains.kotlin.resolve.constants.EnumValue
|
import org.jetbrains.kotlin.resolve.constants.EnumValue
|
||||||
|
import org.jetbrains.kotlin.resolve.constants.StringValue
|
||||||
|
|
||||||
internal fun getVisibility(annotation: AnnotationDescriptor, field: String = "value"): DescriptorVisibility {
|
internal fun getVisibility(annotation: AnnotationDescriptor, field: String = "value"): DescriptorVisibility {
|
||||||
val value = annotation.getStringArgument(field) ?: "PUBLIC"
|
val value = annotation.getStringArgument(field) ?: "PUBLIC"
|
||||||
@@ -32,6 +33,7 @@ internal fun AnnotationDescriptor.getStringArgument(argumentName: String): Strin
|
|||||||
|
|
||||||
return when (argument) {
|
return when (argument) {
|
||||||
is EnumValue -> argument.enumEntryName.identifier
|
is EnumValue -> argument.enumEntryName.identifier
|
||||||
|
is StringValue -> argument.value
|
||||||
else -> throw RuntimeException("Argument $argument is not supported")
|
else -> throw RuntimeException("Argument $argument is not supported")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -21,7 +21,8 @@ internal fun ClassDescriptor.createFunction(
|
|||||||
valueParameters: List<ValueParameter>,
|
valueParameters: List<ValueParameter>,
|
||||||
returnType: KotlinType?,
|
returnType: KotlinType?,
|
||||||
modality: Modality? = Modality.OPEN,
|
modality: Modality? = Modality.OPEN,
|
||||||
visibility: DescriptorVisibility = DescriptorVisibilities.PUBLIC
|
visibility: DescriptorVisibility = DescriptorVisibilities.PUBLIC,
|
||||||
|
receiver: ReceiverParameterDescriptor? = this.thisAsReceiverParameter
|
||||||
): SimpleFunctionDescriptor {
|
): SimpleFunctionDescriptor {
|
||||||
val methodDescriptor = SimpleFunctionDescriptorImpl.create(
|
val methodDescriptor = SimpleFunctionDescriptorImpl.create(
|
||||||
this,
|
this,
|
||||||
@@ -35,7 +36,7 @@ internal fun ClassDescriptor.createFunction(
|
|||||||
|
|
||||||
methodDescriptor.initialize(
|
methodDescriptor.initialize(
|
||||||
null,
|
null,
|
||||||
this.thisAsReceiverParameter,
|
receiver,
|
||||||
mutableListOf(),
|
mutableListOf(),
|
||||||
paramDescriptors,
|
paramDescriptors,
|
||||||
returnType,
|
returnType,
|
||||||
|
|||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
//FILE: ConstructorExample.java
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@NoArgsConstructor(staticName = "make")
|
||||||
|
public class ConstructorExample {
|
||||||
|
|
||||||
|
public ConstructorExample(String arg) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter @Setter private int age = 10;
|
||||||
|
|
||||||
|
@Getter(AccessLevel.PROTECTED) private String name;
|
||||||
|
|
||||||
|
static void javaUsage() {
|
||||||
|
ConstructorExample existing = new ConstructorExample("existing");
|
||||||
|
ConstructorExample generated = ConstructorExample.make();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//FILE: test.kt
|
||||||
|
|
||||||
|
object Test {
|
||||||
|
fun usage() {
|
||||||
|
val existing: ConstructorExample = ConstructorExample("existing")
|
||||||
|
val generated: ConstructorExample = ConstructorExample.make()
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -51,6 +51,11 @@ public class LombokCompileTestGenerated extends AbstractLombokCompileTest {
|
|||||||
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/noArgsConstructor.kt");
|
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/noArgsConstructor.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("noArgsConstructorStatic.kt")
|
||||||
|
public void testNoArgsConstructorStatic() throws Exception {
|
||||||
|
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/noArgsConstructorStatic.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("getters.kt")
|
@TestMetadata("getters.kt")
|
||||||
public void testGetters() throws Exception {
|
public void testGetters() throws Exception {
|
||||||
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/getters.kt");
|
runTest("plugins/lombok/lombok-compiler-plugin/testData/compile/getters.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user