FIC: Move sam related methods to ClassDescriptor, fix JVM backend part

This commit is contained in:
Mikhail Zarechenskiy
2019-11-18 11:42:25 +03:00
parent 87e79e72a9
commit 562f0e62a3
10 changed files with 88 additions and 19 deletions
@@ -16,18 +16,7 @@
package org.jetbrains.kotlin.load.java.descriptors;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.types.SimpleType;
public interface JavaClassDescriptor extends ClassDescriptor {
// Use SingleAbstractMethodUtils.getFunctionTypeForSamInterface() where possible. This is only a fallback
@Nullable
SimpleType getDefaultFunctionTypeForSamInterface();
/**
* May return false even in case when the class is not SAM interface, but returns true only if it's definitely not a SAM.
* But it should work much faster than the exact check.
*/
boolean isDefinitelyNotSamInterface();
}
@@ -98,4 +98,14 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,
@NotNull
@Override
ClassDescriptor getOriginal();
// Use SingleAbstractMethodUtils.getFunctionTypeForSamInterface() where possible. This is only a fallback
@Nullable
SimpleType getDefaultFunctionTypeForSamInterface();
/**
* May return false even in case when the class is not SAM interface, but returns true only if it's definitely not a SAM.
* But it should work much faster than the exact check.
*/
boolean isDefinitelyNotSamInterface();
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.descriptors.impl;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
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.DescriptorUtils;
@@ -176,4 +177,15 @@ public abstract class AbstractClassDescriptor extends ModuleAwareClassDescriptor
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitClassDescriptor(this, data);
}
@Nullable
@Override
public SimpleType getDefaultFunctionTypeForSamInterface() {
return null;
}
@Override
public boolean isDefinitelyNotSamInterface() {
return false;
}
}
@@ -308,4 +308,25 @@ public class LazySubstitutingClassDescriptor extends ModuleAwareClassDescriptor
public Collection<ClassDescriptor> getSealedSubclasses() {
return original.getSealedSubclasses();
}
@Nullable
@Override
public SimpleType getDefaultFunctionTypeForSamInterface() {
SimpleType type = original.getDefaultFunctionTypeForSamInterface();
if (type == null || originalSubstitutor.isEmpty()) return type;
TypeSubstitutor substitutor = getSubstitutor();
KotlinType substitutedType = substitutor.substitute(type, Variance.INVARIANT);
assert substitutedType instanceof SimpleType :
"Substitution for SimpleType should also be a SimpleType, but it is " + substitutedType + "\n" +
"Unsubstituted: " + type;
return (SimpleType) substitutedType;
}
@Override
public boolean isDefinitelyNotSamInterface() {
return original.isDefinitelyNotSamInterface();
}
}