Introduce ClassDescriptor#getDefaultObjectDescriptor()
ClassDescriptor#getClassObjectDescriptor (with old semantics) is now a utility Process usages and replace with calls to a new method where appropriate
This commit is contained in:
+1
-2
@@ -85,8 +85,7 @@ class LazyJavaClassDescriptor(
|
||||
|
||||
override fun getUnsubstitutedPrimaryConstructor(): ConstructorDescriptor? = null
|
||||
|
||||
override fun getClassObjectDescriptor(): ClassDescriptor? = null
|
||||
override fun getClassObjectType(): JetType? = getClassObjectDescriptor()?.let { d -> d.getDefaultType() }
|
||||
override fun getDefaultObjectDescriptor(): ClassDescriptor? = null
|
||||
|
||||
override fun getConstructors() = scopeForMemberLookup.constructors()
|
||||
|
||||
|
||||
@@ -60,8 +60,11 @@ public interface ClassDescriptor extends ClassifierDescriptor, MemberDescriptor,
|
||||
@Override
|
||||
JetType getClassObjectType();
|
||||
|
||||
/**
|
||||
* @return nested object declared as 'default' if one is present.
|
||||
*/
|
||||
@Nullable
|
||||
ClassDescriptor getClassObjectDescriptor();
|
||||
ClassDescriptor getDefaultObjectDescriptor();
|
||||
|
||||
@NotNull
|
||||
ClassKind getKind();
|
||||
|
||||
+2
-3
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage;
|
||||
import org.jetbrains.kotlin.resolve.scopes.InnerClassesScopeWrapper;
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope;
|
||||
import org.jetbrains.kotlin.resolve.scopes.SubstitutingScope;
|
||||
@@ -31,8 +32,6 @@ import org.jetbrains.kotlin.types.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumClass;
|
||||
|
||||
public abstract class AbstractClassDescriptor implements ClassDescriptor {
|
||||
private final Name name;
|
||||
protected final NotNullLazyValue<JetType> defaultType;
|
||||
@@ -79,7 +78,7 @@ public abstract class AbstractClassDescriptor implements ClassDescriptor {
|
||||
@Nullable
|
||||
@Override
|
||||
public JetType getClassObjectType() {
|
||||
ClassDescriptor classObject = getClassObjectDescriptor();
|
||||
ClassDescriptor classObject = DescriptorUtilPackage.getClassObjectDescriptor(this);
|
||||
return classObject == null ? null : classObject.getDefaultType();
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ClassDescriptor getClassObjectDescriptor() {
|
||||
public ClassDescriptor getDefaultObjectDescriptor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+2
-5
@@ -47,7 +47,6 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
|
||||
private final ConstructorDescriptor primaryConstructor;
|
||||
private final JetScope scope;
|
||||
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
|
||||
private final ClassDescriptor classObjectDescriptor;
|
||||
private final NotNullLazyValue<Collection<Name>> enumMemberNames;
|
||||
|
||||
/**
|
||||
@@ -88,8 +87,6 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
|
||||
ConstructorDescriptorImpl primaryConstructor = DescriptorFactory.createPrimaryConstructorForObject(this, source);
|
||||
primaryConstructor.setReturnType(getDefaultType());
|
||||
this.primaryConstructor = primaryConstructor;
|
||||
|
||||
this.classObjectDescriptor = containingClass;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -118,8 +115,8 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ClassDescriptor getClassObjectDescriptor() {
|
||||
return classObjectDescriptor;
|
||||
public ClassDescriptor getDefaultObjectDescriptor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
+2
-2
@@ -169,8 +169,8 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassDescriptor getClassObjectDescriptor() {
|
||||
return original.getClassObjectDescriptor();
|
||||
public ClassDescriptor getDefaultObjectDescriptor() {
|
||||
return original.getDefaultObjectDescriptor();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -320,10 +320,6 @@ public class DescriptorUtils {
|
||||
return superClassDescriptor.equals(KotlinBuiltIns.getInstance().getAny());
|
||||
}
|
||||
|
||||
public static boolean isSyntheticClassObject(@NotNull DeclarationDescriptor descriptor) {
|
||||
return isClassObject(descriptor) && isSingleton(descriptor.getContainingDeclaration());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Visibility getDefaultConstructorVisibility(@NotNull ClassDescriptor classDescriptor) {
|
||||
ClassKind classKind = classDescriptor.getKind();
|
||||
@@ -337,11 +333,6 @@ public class DescriptorUtils {
|
||||
return Visibilities.PUBLIC;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Visibility getSyntheticClassObjectVisibility() {
|
||||
return Visibilities.PUBLIC;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ClassDescriptor getInnerClassByName(@NotNull ClassDescriptor classDescriptor, @NotNull String innerClassName) {
|
||||
ClassifierDescriptor classifier = classDescriptor.getDefaultType().getMemberScope().getClassifier(Name.identifier(innerClassName));
|
||||
|
||||
@@ -22,12 +22,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
public fun ClassDescriptor.getClassObjectReferenceTarget(): ClassDescriptor {
|
||||
val classObjectDescriptor = getClassObjectDescriptor()
|
||||
return if (classObjectDescriptor == null || hasSyntheticClassObject()) this else classObjectDescriptor
|
||||
}
|
||||
|
||||
public fun ClassDescriptor.hasSyntheticClassObject(): Boolean = getKind() in setOf(ENUM_ENTRY, OBJECT)
|
||||
public fun ClassDescriptor.getClassObjectReferenceTarget(): ClassDescriptor = getDefaultObjectDescriptor() ?: this
|
||||
|
||||
public fun DeclarationDescriptor.getImportableDescriptor(): DeclarationDescriptor {
|
||||
return when {
|
||||
@@ -59,4 +54,18 @@ public val ClassDescriptor.classId: ClassId
|
||||
return owner.classId.createNestedClassId(getName())
|
||||
}
|
||||
throw IllegalStateException("Illegal container: $owner")
|
||||
}
|
||||
|
||||
/** If a literal of this class can be used as a value returns the class which represents the type of this value */
|
||||
public val ClassDescriptor.classObjectDescriptor: ClassDescriptor?
|
||||
get() {
|
||||
return when (this.getKind()) {
|
||||
CLASS_OBJECT, OBJECT -> this
|
||||
ENUM_ENTRY -> {
|
||||
val container = this.getContainingDeclaration()
|
||||
assert(container is ClassDescriptor && container.getKind() == ENUM_CLASS)
|
||||
container as ClassDescriptor
|
||||
}
|
||||
else -> getDefaultObjectDescriptor()
|
||||
}
|
||||
}
|
||||
@@ -130,8 +130,8 @@ public class DescriptorSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
ClassDescriptor classObject = classDescriptor.getClassObjectDescriptor();
|
||||
if (classObject != null && isClassObject(classObject)) {
|
||||
ClassDescriptor classObject = classDescriptor.getDefaultObjectDescriptor();
|
||||
if (classObject != null) {
|
||||
builder.setClassObjectName(stringTable.getSimpleNameIndex(classObject.getName()));
|
||||
}
|
||||
|
||||
|
||||
+1
-4
@@ -110,16 +110,13 @@ public class DeserializedClassDescriptor(
|
||||
}
|
||||
|
||||
private fun computeClassObjectDescriptor(): ClassDescriptor? {
|
||||
if (getKind() == ClassKind.OBJECT || getKind() == ClassKind.CLASS_OBJECT) {
|
||||
return this
|
||||
}
|
||||
if (!classProto.hasClassObjectName()) return null
|
||||
|
||||
val classObjectName = c.nameResolver.getName(classProto.getClassObjectName())
|
||||
return memberScope.getClassifier(classObjectName) as? ClassDescriptor
|
||||
}
|
||||
|
||||
override fun getClassObjectDescriptor(): ClassDescriptor? = classObjectDescriptor()
|
||||
override fun getDefaultObjectDescriptor(): ClassDescriptor? = classObjectDescriptor()
|
||||
|
||||
private fun computeSuperTypes(): Collection<JetType> {
|
||||
val supertypes = ArrayList<JetType>(classProto.getSupertypeCount())
|
||||
|
||||
Reference in New Issue
Block a user