Generating stub methods for read-only collection.
This commit is contained in:
@@ -469,13 +469,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
List<String> result = Lists.newArrayList();
|
||||
|
||||
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
|
||||
if (isSubclass(descriptor, builtIns.getList())) {
|
||||
TypeParameterDescriptor listTypeParameter = builtIns.getList().getTypeConstructor().getParameters().get(0);
|
||||
TypeSubstitutor deepSubstitutor = SubstitutionUtils.buildDeepSubstitutor(descriptor.getDefaultType());
|
||||
TypeProjection substitute = deepSubstitutor.substitute(new TypeProjection(listTypeParameter.getDefaultType()));
|
||||
assert substitute != null : "Couldn't substitute: " + descriptor;
|
||||
ClassifierDescriptor classifier = substitute.getType().getConstructor().getDeclarationDescriptor();
|
||||
assert classifier != null : "No classifier: " + substitute.getType();
|
||||
if (isSubclass(descriptor, builtIns.getCollection())) {
|
||||
ClassifierDescriptor classifier = getSubstituteForTypeParameterOf(builtIns.getCollection());
|
||||
|
||||
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("add"),
|
||||
builtIns.getBoolean(), classifier) == null) {
|
||||
@@ -505,6 +500,10 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("clear"), builtIns.getUnit()) == null){
|
||||
result.add("clear()V");
|
||||
}
|
||||
}
|
||||
|
||||
if (isSubclass(descriptor, builtIns.getList())) {
|
||||
ClassifierDescriptor classifier = getSubstituteForTypeParameterOf(builtIns.getList());
|
||||
|
||||
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("set"),
|
||||
classifier, builtIns.getInt(), classifier) == null) {
|
||||
@@ -532,6 +531,17 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private ClassifierDescriptor getSubstituteForTypeParameterOf(@NotNull ClassDescriptor trait) {
|
||||
TypeParameterDescriptor listTypeParameter = trait.getTypeConstructor().getParameters().get(0);
|
||||
TypeSubstitutor deepSubstitutor = SubstitutionUtils.buildDeepSubstitutor(descriptor.getDefaultType());
|
||||
TypeProjection substitute = deepSubstitutor.substitute(new TypeProjection(listTypeParameter.getDefaultType()));
|
||||
assert substitute != null : "Couldn't substitute: " + descriptor;
|
||||
ClassifierDescriptor classifier = substitute.getType().getConstructor().getDeclarationDescriptor();
|
||||
assert classifier != null : "No classifier: " + substitute.getType();
|
||||
return classifier;
|
||||
}
|
||||
|
||||
|
||||
private void generateBuiltinMethodStubs() {
|
||||
for (String signature : getBuiltinMethodStubSignatures()) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
class MyCollection<T>: Collection<T> {
|
||||
override fun size(): Int = 0
|
||||
override fun isEmpty(): Boolean = true
|
||||
override fun contains(o: Any?): Boolean = false
|
||||
override fun iterator(): Iterator<T> = throw UnsupportedOperationException()
|
||||
override fun toArray(): Array<Any?> = throw UnsupportedOperationException()
|
||||
override fun <E> toArray(a: Array<out E>): Array<E> = throw UnsupportedOperationException()
|
||||
override fun containsAll(c: Collection<Any?>): Boolean = false
|
||||
override fun hashCode(): Int = 0
|
||||
override fun equals(other: Any?): Boolean = false
|
||||
}
|
||||
|
||||
fun expectUoe(block: () -> Any) {
|
||||
try {
|
||||
block()
|
||||
throw AssertionError()
|
||||
} catch (e: UnsupportedOperationException) {
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val collection = MyCollection<String>() as MutableCollection<String>
|
||||
|
||||
expectUoe { collection.add("") }
|
||||
expectUoe { collection.remove("") }
|
||||
expectUoe { collection.addAll(collection) }
|
||||
expectUoe { collection.removeAll(collection) }
|
||||
expectUoe { collection.retainAll(collection) }
|
||||
expectUoe { collection.clear() }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -424,6 +424,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/box/builtinStubMethods"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("Collection.kt")
|
||||
public void testCollection() throws Exception {
|
||||
doTest("compiler/testData/codegen/box/builtinStubMethods/Collection.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("Iterator.kt")
|
||||
public void testIterator() throws Exception {
|
||||
doTest("compiler/testData/codegen/box/builtinStubMethods/Iterator.kt");
|
||||
|
||||
Reference in New Issue
Block a user