Transform Enum.values to property
This commit is contained in:
+8
-1
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory.createEnumValueOfMethod
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory.createEnumValuesMethod
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory.createEnumValuesProperty
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
@@ -61,7 +62,7 @@ public class LazyJavaStaticClassScope(
|
||||
}
|
||||
|
||||
override fun getPropertyNames(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<Name> =
|
||||
memberIndex().getAllFieldNames()
|
||||
memberIndex().getAllFieldNames() + (if (jClass.isEnum) listOf(DescriptorUtils.ENUM_VALUES) else emptyList())
|
||||
|
||||
override fun getClassNames(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<Name> = listOf()
|
||||
override fun getClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null
|
||||
@@ -99,6 +100,12 @@ public class LazyJavaStaticClassScope(
|
||||
}
|
||||
|
||||
result.addAll(actualProperties)
|
||||
|
||||
if (jClass.isEnum) {
|
||||
if (name == DescriptorUtils.ENUM_VALUES) {
|
||||
result.add(createEnumValuesProperty(getContainingDeclaration()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getContainingDeclaration() = super.getContainingDeclaration() as LazyJavaClassDescriptor
|
||||
|
||||
+8
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
import org.jetbrains.kotlin.load.java.BuiltinSpecialProperties.getBuiltinSpecialPropertyGetterName
|
||||
import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature.sameAsBuiltinMethodWithErasedValueParameters
|
||||
import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature.getSpecialSignatureInfo
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
|
||||
object BuiltinSpecialProperties {
|
||||
@@ -201,6 +202,13 @@ fun <T : CallableMemberDescriptor> T.getOverriddenBuiltinWithDifferentJvmDescrip
|
||||
}
|
||||
|
||||
fun getJvmMethodNameIfSpecial(callableMemberDescriptor: CallableMemberDescriptor): String? {
|
||||
if (callableMemberDescriptor.propertyIfAccessor.name == DescriptorUtils.ENUM_VALUES) {
|
||||
val containingDeclaration = callableMemberDescriptor.containingDeclaration
|
||||
if (callableMemberDescriptor is PropertyAccessorDescriptor
|
||||
&& containingDeclaration is ClassDescriptor
|
||||
&& containingDeclaration.kind == ClassKind.ENUM_CLASS) return DescriptorUtils.ENUM_VALUES.asString()
|
||||
}
|
||||
|
||||
val builtinOverridden = getBuiltinOverriddenThatAffectsJvmName(callableMemberDescriptor)?.propertyIfAccessor
|
||||
?: return null
|
||||
return when (builtinOverridden) {
|
||||
|
||||
@@ -64,5 +64,10 @@ public class AnnotationsImpl : Annotations {
|
||||
public fun create(annotationsWithTargets: List<AnnotationWithTarget>): AnnotationsImpl {
|
||||
return AnnotationsImpl(annotationsWithTargets, 0)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
public fun createWithNoTarget(vararg annotations: AnnotationDescriptor): AnnotationsImpl {
|
||||
return create(annotations.map { AnnotationWithTarget(it, null) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,44 @@
|
||||
|
||||
package org.jetbrains.kotlin.descriptors.annotations
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.SourceElement
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.constants.AnnotationValue
|
||||
import org.jetbrains.kotlin.resolve.constants.ArrayValue
|
||||
import org.jetbrains.kotlin.resolve.constants.StringValue
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
// Please synchronize this set with JetTokens.ANNOTATION_MODIFIERS_KEYWORDS_ARRAY
|
||||
public val ANNOTATION_MODIFIERS_FQ_NAMES: Set<FqName> =
|
||||
arrayOf("data", "inline", "noinline", "tailrec", "external", "annotation.annotation", "crossinline").map { FqName("kotlin.$it") }.toSet()
|
||||
|
||||
public fun KotlinBuiltIns.createDeprecatedAnnotation(message: String, replaceWith: String): AnnotationDescriptor {
|
||||
val deprecatedAnnotation = deprecatedAnnotation
|
||||
val parameters = deprecatedAnnotation.unsubstitutedPrimaryConstructor!!.valueParameters
|
||||
|
||||
val replaceWithClass = getBuiltInClassByName(Name.identifier("ReplaceWith"))
|
||||
|
||||
val replaceWithParameters = replaceWithClass.unsubstitutedPrimaryConstructor!!.valueParameters
|
||||
return AnnotationDescriptorImpl(
|
||||
deprecatedAnnotation.defaultType,
|
||||
mapOf(
|
||||
parameters["message"] to StringValue(message, this),
|
||||
parameters["replaceWith"] to AnnotationValue(
|
||||
AnnotationDescriptorImpl(
|
||||
replaceWithClass.defaultType,
|
||||
mapOf(
|
||||
replaceWithParameters["expression"] to StringValue(replaceWith, this),
|
||||
replaceWithParameters["imports"] to ArrayValue(
|
||||
emptyList(), getArrayType(Variance.INVARIANT, stringType), this)
|
||||
),
|
||||
SourceElement.NO_SOURCE
|
||||
)
|
||||
)
|
||||
),
|
||||
SourceElement.NO_SOURCE)
|
||||
}
|
||||
|
||||
private operator fun Collection<ValueParameterDescriptor>.get(parameterName: String) = single { it.name.asString() == parameterName }
|
||||
|
||||
@@ -19,7 +19,9 @@ package org.jetbrains.kotlin.resolve;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUtilKt;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl;
|
||||
import org.jetbrains.kotlin.descriptors.impl.*;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver;
|
||||
@@ -112,8 +114,12 @@ public class DescriptorFactory {
|
||||
|
||||
@NotNull
|
||||
public static SimpleFunctionDescriptor createEnumValuesMethod(@NotNull ClassDescriptor enumClass) {
|
||||
AnnotationsImpl annotations = AnnotationsImpl.createWithNoTarget(
|
||||
AnnotationUtilKt.createDeprecatedAnnotation(getBuiltIns(enumClass), "Use 'values' property instead", "this.values")
|
||||
);
|
||||
|
||||
SimpleFunctionDescriptorImpl values =
|
||||
SimpleFunctionDescriptorImpl.create(enumClass, Annotations.Companion.getEMPTY(), DescriptorUtils.ENUM_VALUES,
|
||||
SimpleFunctionDescriptorImpl.create(enumClass, annotations, DescriptorUtils.ENUM_VALUES,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
|
||||
return values.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
|
||||
Collections.<ValueParameterDescriptor>emptyList(),
|
||||
@@ -121,6 +127,26 @@ public class DescriptorFactory {
|
||||
Modality.FINAL, Visibilities.PUBLIC);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PropertyDescriptor createEnumValuesProperty(@NotNull ClassDescriptor enumClass) {
|
||||
PropertyDescriptorImpl values =
|
||||
PropertyDescriptorImpl.create(
|
||||
enumClass, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.PUBLIC, /* isVar */ false,
|
||||
DescriptorUtils.ENUM_VALUES, CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource(),
|
||||
/* lateInit = */ false, /* isConst = */ false
|
||||
);
|
||||
|
||||
JetType type = getBuiltIns(enumClass).getArrayType(Variance.INVARIANT, enumClass.getDefaultType());
|
||||
|
||||
PropertyGetterDescriptorImpl getter = createDefaultGetter(values, Annotations.Companion.getEMPTY());
|
||||
|
||||
values.initialize(getter, null);
|
||||
getter.initialize(type);
|
||||
values.setType(type, Collections.<TypeParameterDescriptor>emptyList(), null, (JetType) null);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static SimpleFunctionDescriptor createEnumValueOfMethod(@NotNull ClassDescriptor enumClass) {
|
||||
SimpleFunctionDescriptorImpl valueOf =
|
||||
|
||||
+16
-6
@@ -16,13 +16,12 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve.scopes
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory.createEnumValueOfMethod
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory.createEnumValuesMethod
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory.createEnumValuesProperty
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import java.util.ArrayList
|
||||
|
||||
@@ -40,10 +39,21 @@ public class StaticScopeForKotlinClass(
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDescriptors(kindFilter: DescriptorKindFilter,
|
||||
nameFilter: (Name) -> Boolean) = functions
|
||||
private val properties: List<PropertyDescriptor> by lazy {
|
||||
if (containingClass.kind != ClassKind.ENUM_CLASS) {
|
||||
listOf<PropertyDescriptor>()
|
||||
}
|
||||
else {
|
||||
listOf(createEnumValuesProperty(containingClass))
|
||||
}
|
||||
}
|
||||
|
||||
override fun getOwnDeclaredDescriptors() = functions
|
||||
override fun getDescriptors(kindFilter: DescriptorKindFilter,
|
||||
nameFilter: (Name) -> Boolean) = functions + properties
|
||||
|
||||
override fun getOwnDeclaredDescriptors() = functions + properties
|
||||
|
||||
override fun getProperties(name: Name, location: LookupLocation) = properties.filterTo(ArrayList(1)) { it.name == name }
|
||||
|
||||
override fun getFunctions(name: Name, location: LookupLocation) = functions.filterTo(ArrayList<FunctionDescriptor>(2)) { it.getName() == name }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user