Simplify TypeConstructorImpl, extend it from AbstractClassTypeConstructor

This commit is contained in:
Alexander Udalov
2016-03-16 13:08:31 +03:00
parent b95e27fd87
commit 13ae3d96ab
10 changed files with 64 additions and 121 deletions
@@ -151,13 +151,8 @@ public class MutableClassDescriptor extends ClassDescriptorBase implements Class
public void createTypeConstructor() { public void createTypeConstructor() {
assert typeConstructor == null : typeConstructor; assert typeConstructor == null : typeConstructor;
this.typeConstructor = TypeConstructorImpl.createForClass( this.typeConstructor = new TypeConstructorImpl(
this, this, Annotations.Companion.getEMPTY(), ModalityKt.isFinalClass(this), typeParameters, supertypes
Annotations.Companion.getEMPTY(),
ModalityKt.isFinalClass(this),
getName().asString(),
typeParameters,
supertypes
); );
for (FunctionDescriptor functionDescriptor : getConstructors()) { for (FunctionDescriptor functionDescriptor : getConstructors()) {
((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType()); ((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType());
@@ -2,10 +2,10 @@ package
public fun main(): kotlin.Unit public fun main(): kotlin.Unit
@[Missing annotation class: missing.Ann]() public open class A { @missing.Ann() public open class A {
public constructor A() public constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@[Missing annotation class: missing.Ann]() public open fun foo(): kotlin.String! @missing.Ann() public open fun foo(): kotlin.String!
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
} }
@@ -5,7 +5,7 @@ public fun test(): kotlin.Unit
public interface A</*0*/ T : kotlin.Any!> { public interface A</*0*/ T : kotlin.Any!> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(/*0*/ @[Missing annotation class: javax.annotation.Nullable]() y: T?): kotlin.Boolean public abstract fun foo(/*0*/ @javax.annotation.Nullable() y: T?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
} }
@@ -293,10 +293,10 @@ package test {
invisible_fake final override /*1*/ /*fake_override*/ fun getFocusTraversalKeys_NoIDCheck(/*0*/ p0: kotlin.Int): kotlin.collections.(Mutable)Set<(raw) kotlin.Any?>! invisible_fake final override /*1*/ /*fake_override*/ fun getFocusTraversalKeys_NoIDCheck(/*0*/ p0: kotlin.Int): kotlin.collections.(Mutable)Set<(raw) kotlin.Any?>!
public open override /*1*/ /*fake_override*/ fun getFocusTraversalPolicy(): [ERROR : Unresolved java classifier: FocusTraversalPolicy]! public open override /*1*/ /*fake_override*/ fun getFocusTraversalPolicy(): [ERROR : Unresolved java classifier: FocusTraversalPolicy]!
public open override /*1*/ /*fake_override*/ fun getFocusableWindowState(): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun getFocusableWindowState(): kotlin.Boolean
@[Missing annotation class: java.beans.Transient]() public open override /*1*/ /*fake_override*/ fun getFont(): [ERROR : Unresolved java classifier: Font]! @java.beans.Transient() public open override /*1*/ /*fake_override*/ fun getFont(): [ERROR : Unresolved java classifier: Font]!
public open override /*1*/ /*fake_override*/ fun getFontMetrics(/*0*/ p0: [ERROR : Unresolved java classifier: Font]!): [ERROR : Unresolved java classifier: FontMetrics]! public open override /*1*/ /*fake_override*/ fun getFontMetrics(/*0*/ p0: [ERROR : Unresolved java classifier: Font]!): [ERROR : Unresolved java classifier: FontMetrics]!
invisible_fake final override /*1*/ /*fake_override*/ fun getFont_NoClientCode(): [ERROR : Unresolved java classifier: Font]! invisible_fake final override /*1*/ /*fake_override*/ fun getFont_NoClientCode(): [ERROR : Unresolved java classifier: Font]!
@[Missing annotation class: java.beans.Transient]() public open override /*1*/ /*fake_override*/ fun getForeground(): [ERROR : Unresolved java classifier: Color]! @java.beans.Transient() public open override /*1*/ /*fake_override*/ fun getForeground(): [ERROR : Unresolved java classifier: Color]!
public open override /*1*/ /*fake_override*/ fun getGlassPane(): java.awt.Component! public open override /*1*/ /*fake_override*/ fun getGlassPane(): java.awt.Component!
public open override /*1*/ /*fake_override*/ fun getGraphics(): [ERROR : Unresolved java classifier: Graphics]! public open override /*1*/ /*fake_override*/ fun getGraphics(): [ERROR : Unresolved java classifier: Graphics]!
public open override /*1*/ /*fake_override*/ fun getGraphicsConfiguration(): [ERROR : Unresolved java classifier: GraphicsConfiguration]! public open override /*1*/ /*fake_override*/ fun getGraphicsConfiguration(): [ERROR : Unresolved java classifier: GraphicsConfiguration]!
@@ -450,7 +450,7 @@ package test {
public open override /*1*/ /*fake_override*/ fun isUndecorated(): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun isUndecorated(): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun isValid(): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun isValid(): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun isValidateRoot(): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun isValidateRoot(): kotlin.Boolean
@[Missing annotation class: java.beans.Transient]() public open override /*1*/ /*fake_override*/ fun isVisible(): kotlin.Boolean @java.beans.Transient() public open override /*1*/ /*fake_override*/ fun isVisible(): kotlin.Boolean
invisible_fake final override /*1*/ /*fake_override*/ fun isVisible_NoClientCode(): kotlin.Boolean invisible_fake final override /*1*/ /*fake_override*/ fun isVisible_NoClientCode(): kotlin.Boolean
@kotlin.Deprecated(message = "Deprecated in Java") public open override /*1*/ /*fake_override*/ fun keyDown(/*0*/ p0: [ERROR : Unresolved java classifier: Event]!, /*1*/ p1: kotlin.Int): kotlin.Boolean @kotlin.Deprecated(message = "Deprecated in Java") public open override /*1*/ /*fake_override*/ fun keyDown(/*0*/ p0: [ERROR : Unresolved java classifier: Event]!, /*1*/ p1: kotlin.Int): kotlin.Boolean
@kotlin.Deprecated(message = "Deprecated in Java") public open override /*1*/ /*fake_override*/ fun keyUp(/*0*/ p0: [ERROR : Unresolved java classifier: Event]!, /*1*/ p1: kotlin.Int): kotlin.Boolean @kotlin.Deprecated(message = "Deprecated in Java") public open override /*1*/ /*fake_override*/ fun keyUp(/*0*/ p0: [ERROR : Unresolved java classifier: Event]!, /*1*/ p1: kotlin.Int): kotlin.Boolean
@@ -151,14 +151,13 @@ class LazyJavaAnnotationDescriptor(
} }
private fun createTypeForMissingDependencies(fqName: FqName) = private fun createTypeForMissingDependencies(fqName: FqName) =
ErrorUtils.createErrorTypeWithCustomConstructor( ErrorUtils.createErrorTypeWithCustomConstructor(
"[Missing annotation class: $fqName]", "[Missing annotation class: $fqName]",
ClassDescriptorImpl( ClassDescriptorImpl(
EmptyPackageFragmentDescriptor(c.module, fqName.parent()), fqName.shortName(), Modality.FINAL, EmptyPackageFragmentDescriptor(c.module, fqName.parent()), fqName.shortName(), Modality.FINAL,
ClassKind.ANNOTATION_CLASS, listOf(c.module.builtIns.anyType), SourceElement.NO_SOURCE, ClassKind.ANNOTATION_CLASS, listOf(c.module.builtIns.anyType), SourceElement.NO_SOURCE
"[Missing annotation class: $fqName]" ).apply {
).apply { initialize(MemberScope.Empty, emptySet(), null)
initialize(MemberScope.Empty, emptySet(), null) }.typeConstructor
}.typeConstructor )
)
} }
@@ -48,25 +48,14 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
@NotNull ClassKind kind, @NotNull ClassKind kind,
@NotNull Collection<KotlinType> supertypes, @NotNull Collection<KotlinType> supertypes,
@NotNull SourceElement source @NotNull SourceElement source
) {
this(containingDeclaration, name, modality, kind, supertypes, source, name.asString());
}
public ClassDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Name name,
@NotNull Modality modality,
@NotNull ClassKind kind,
@NotNull Collection<KotlinType> supertypes,
@NotNull SourceElement source,
@NotNull String debugName
) { ) {
super(LockBasedStorageManager.NO_LOCKS, containingDeclaration, name, source); super(LockBasedStorageManager.NO_LOCKS, containingDeclaration, name, source);
this.modality = modality; this.modality = modality;
this.kind = kind; this.kind = kind;
this.typeConstructor = TypeConstructorImpl.createForClass(this, Annotations.Companion.getEMPTY(), false, debugName, this.typeConstructor = new TypeConstructorImpl(
Collections.<TypeParameterDescriptor>emptyList(), supertypes); this, Annotations.Companion.getEMPTY(), false, Collections.<TypeParameterDescriptor>emptyList(), supertypes
);
} }
public final void initialize( public final void initialize(
@@ -79,9 +79,9 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
assert containingClass.getKind() == ClassKind.ENUM_CLASS; assert containingClass.getKind() == ClassKind.ENUM_CLASS;
this.annotations = annotations; this.annotations = annotations;
this.typeConstructor = this.typeConstructor = new TypeConstructorImpl(
TypeConstructorImpl.createForClass(this, getAnnotations(), true, "enum entry", Collections.<TypeParameterDescriptor>emptyList(), this, getAnnotations(), true, Collections.<TypeParameterDescriptor>emptyList(), Collections.singleton(supertype)
Collections.singleton(supertype)); );
this.scope = new EnumEntryScope(storageManager); this.scope = new EnumEntryScope(storageManager);
this.enumMemberNames = enumMemberNames; this.enumMemberNames = enumMemberNames;
@@ -84,13 +84,8 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
supertypes.add(substitutor.substitute(supertype, Variance.INVARIANT)); supertypes.add(substitutor.substitute(supertype, Variance.INVARIANT));
} }
typeConstructor = TypeConstructorImpl.createForClass( typeConstructor = new TypeConstructorImpl(
this, this, originalTypeConstructor.getAnnotations(), originalTypeConstructor.isFinal(), typeConstructorParameters, supertypes
originalTypeConstructor.getAnnotations(),
originalTypeConstructor.isFinal(),
originalTypeConstructor.toString(),
typeConstructorParameters,
supertypes
); );
} }
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.types; package org.jetbrains.kotlin.types;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns; import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor; import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
@@ -42,7 +41,13 @@ public abstract class AbstractClassTypeConstructor extends AbstractTypeConstruct
public final int hashCode() { public final int hashCode() {
if (!hashCodeComputed) { if (!hashCodeComputed) {
hashCodeComputed = true; hashCodeComputed = true;
hashCode = hashCode(this); ClassifierDescriptor descriptor = getDeclarationDescriptor();
if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
hashCode = DescriptorUtils.getFqName(descriptor).hashCode();
}
else {
hashCode = System.identityHashCode(this);
}
} }
return hashCode; return hashCode;
} }
@@ -58,27 +63,22 @@ public abstract class AbstractClassTypeConstructor extends AbstractTypeConstruct
} }
@Override @Override
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass") public boolean equals(Object other) {
public boolean equals(Object obj) {
return equals(this, obj);
}
public static boolean equals(@NotNull TypeConstructor me, Object other) {
if (!(other instanceof TypeConstructor)) return false; if (!(other instanceof TypeConstructor)) return false;
// performance optimization: getFqName is slow method // performance optimization: getFqName is slow method
if (other.hashCode() != me.hashCode()) return false; if (other.hashCode() != hashCode()) return false;
ClassifierDescriptor myDescriptor = me.getDeclarationDescriptor(); ClassifierDescriptor myDescriptor = getDeclarationDescriptor();
ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor(); ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor();
// descriptor for type is created once per module // descriptor for type is created once per module
if (myDescriptor == otherDescriptor) return true; if (myDescriptor == otherDescriptor) return true;
// All error types have the same descriptor // All error types have the same descriptor
if (myDescriptor != null && !hasMeaningfulFqName(myDescriptor) || if (!hasMeaningfulFqName(myDescriptor) ||
otherDescriptor != null && !hasMeaningfulFqName(otherDescriptor)) { otherDescriptor != null && !hasMeaningfulFqName(otherDescriptor)) {
return me == other; return this == other;
} }
if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) { if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) {
@@ -90,14 +90,6 @@ public abstract class AbstractClassTypeConstructor extends AbstractTypeConstruct
return false; return false;
} }
public static int hashCode(@NotNull TypeConstructor me) {
ClassifierDescriptor descriptor = me.getDeclarationDescriptor();
if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
return DescriptorUtils.getFqName(descriptor).hashCode();
}
return System.identityHashCode(me);
}
private static boolean hasMeaningfulFqName(@NotNull ClassifierDescriptor descriptor) { private static boolean hasMeaningfulFqName(@NotNull ClassifierDescriptor descriptor) {
return !ErrorUtils.isError(descriptor) && return !ErrorUtils.isError(descriptor) &&
!DescriptorUtils.isLocal(descriptor); !DescriptorUtils.isLocal(descriptor);
@@ -17,63 +17,36 @@
package org.jetbrains.kotlin.types; package org.jetbrains.kotlin.types;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor; import org.jetbrains.kotlin.descriptors.SupertypeLoopChecker;
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor; import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.AnnotatedImpl;
import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt; import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.storage.LockBasedStorageManager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public abstract class TypeConstructorImpl extends AnnotatedImpl implements TypeConstructor { public class TypeConstructorImpl extends AbstractClassTypeConstructor implements TypeConstructor {
private final ClassDescriptor classDescriptor;
private final Annotations annotations;
private final List<TypeParameterDescriptor> parameters;
private final Collection<KotlinType> supertypes;
private final boolean isFinal;
@NotNull public TypeConstructorImpl(
public static TypeConstructorImpl createForClass(
@NotNull ClassDescriptor classDescriptor, @NotNull ClassDescriptor classDescriptor,
@NotNull Annotations annotations, @NotNull Annotations annotations,
boolean isFinal, boolean isFinal,
@NotNull String debugName,
@NotNull List<? extends TypeParameterDescriptor> parameters, @NotNull List<? extends TypeParameterDescriptor> parameters,
@NotNull Collection<KotlinType> supertypes @NotNull Collection<KotlinType> supertypes
) { ) {
return new TypeConstructorImpl(classDescriptor, annotations, isFinal, debugName, parameters, supertypes) { super(LockBasedStorageManager.NO_LOCKS);
@Override this.classDescriptor = classDescriptor;
public int hashCode() { this.annotations = annotations;
return AbstractClassTypeConstructor.hashCode(this);
}
@Override
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object obj) {
return AbstractClassTypeConstructor.equals(this, obj);
}
};
}
private final List<TypeParameterDescriptor> parameters;
private final Collection<KotlinType> supertypes;
private final String debugName;
private final boolean isFinal;
private final ClassifierDescriptor classifierDescriptor;
private TypeConstructorImpl(
@NotNull ClassifierDescriptor classifierDescriptor,
@NotNull Annotations annotations,
boolean isFinal,
@NotNull String debugName,
@NotNull List<? extends TypeParameterDescriptor> parameters,
@NotNull Collection<KotlinType> supertypes) {
super(annotations);
this.classifierDescriptor = classifierDescriptor;
this.isFinal = isFinal; this.isFinal = isFinal;
this.debugName = debugName;
this.parameters = Collections.unmodifiableList(new ArrayList<TypeParameterDescriptor>(parameters)); this.parameters = Collections.unmodifiableList(new ArrayList<TypeParameterDescriptor>(parameters));
this.supertypes = Collections.unmodifiableCollection(supertypes); this.supertypes = Collections.unmodifiableCollection(supertypes);
} }
@@ -84,15 +57,9 @@ public abstract class TypeConstructorImpl extends AnnotatedImpl implements TypeC
return parameters; return parameters;
} }
@Override
@NotNull
public Collection<KotlinType> getSupertypes() {
return supertypes;
}
@Override @Override
public String toString() { public String toString() {
return debugName; return DescriptorUtils.getFqName(classDescriptor).asString();
} }
@Override @Override
@@ -106,20 +73,26 @@ public abstract class TypeConstructorImpl extends AnnotatedImpl implements TypeC
} }
@Override @Override
@Nullable @NotNull
public ClassifierDescriptor getDeclarationDescriptor() { public ClassDescriptor getDeclarationDescriptor() {
return classifierDescriptor; return classDescriptor;
} }
@NotNull @NotNull
@Override @Override
public KotlinBuiltIns getBuiltIns() { protected Collection<KotlinType> computeSupertypes() {
return DescriptorUtilsKt.getBuiltIns(classifierDescriptor); return supertypes;
} }
@NotNull
@Override @Override
public abstract int hashCode(); protected SupertypeLoopChecker getSupertypeLoopChecker() {
return SupertypeLoopChecker.EMPTY.INSTANCE;
}
@NotNull
@Override @Override
public abstract boolean equals(Object obj); public Annotations getAnnotations() {
return annotations;
}
} }