Skip to content

Commit

Permalink
[lombok] Get field names directly from JavaClassImpl
Browse files Browse the repository at this point in the history
Otherwise it fails with recursion on annotation types
#KT-47513 Fixed

(cherry picked from commit 1130344)
  • Loading branch information
Andrey Zinovyev authored and Space committed Jun 30, 2021
1 parent 26c0b3d commit 8538ed4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
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.*
import org.jetbrains.kotlin.lombok.utils.getJavaClass
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.jvm.SyntheticJavaPartsProvider
import java.util.*
Expand Down Expand Up @@ -64,12 +63,8 @@ class LombokSyntheticJavaPartsProvider(config: LombokConfig) : SyntheticJavaPart
addNonExistent(result, constructors)
}

//we process only local java files
private fun extractClass(descriptor: ClassDescriptor): JavaClassImpl? =
(descriptor as? LazyJavaClassDescriptor)?.jClass as? JavaClassImpl

private fun getSyntheticParts(descriptor: ClassDescriptor): SyntheticParts =
extractClass(descriptor)?.let { _ ->
descriptor.getJavaClass()?.let { _ ->
partsCache.getOrPut(descriptor) {
computeSyntheticParts(descriptor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor
import org.jetbrains.kotlin.load.java.lazy.descriptors.isJavaField
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
Expand Down Expand Up @@ -89,10 +91,15 @@ private fun CallableDescriptor.makeValueParameter(param: LombokValueParameter, i
)
}

fun ClassDescriptor.getJavaFields(): List<PropertyDescriptor> =
this.unsubstitutedMemberScope.getVariableNames()
.map {
this.unsubstitutedMemberScope.getContributedVariables(it, NoLookupLocation.FROM_SYNTHETIC_SCOPE).single()
}.filter { it.isJavaField }
fun ClassDescriptor.getJavaFields(): List<PropertyDescriptor> {
val variableNames = getJavaClass()?.fields?.map { it.name } ?: emptyList()
return variableNames
.mapNotNull { this.unsubstitutedMemberScope.getContributedVariables(it, NoLookupLocation.FROM_SYNTHETIC_SCOPE).singleOrNull() }
.filter { it.isJavaField }
}

fun KotlinType.isPrimitiveBoolean(): Boolean = this is SimpleTypeMarker && isBoolean()

//we process local java files only
fun ClassDescriptor.getJavaClass(): JavaClassImpl? =
(this as? LazyJavaClassDescriptor)?.jClass as? JavaClassImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//FILE: Anno.java

import java.lang.*;
import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Anno {
String value() default "sdf";
}

//FILE: Foo.java

import lombok.*;
import org.jetbrains.annotations.*;

public class Foo {
private Integer age = 10;
private String name;
}


//FILE: test.kt

@Anno
class Test {
fun run() {
val obj = Foo()
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8538ed4

Please sign in to comment.