Generating stub methods for read-only map and entry.

This commit is contained in:
Evgeny Gerashchenko
2013-09-25 00:11:31 +04:00
parent d1cfbbbad5
commit b4bb08c013
6 changed files with 143 additions and 4 deletions
@@ -470,7 +470,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
if (isSubclass(descriptor, builtIns.getCollection())) {
ClassifierDescriptor classifier = getSubstituteForTypeParameterOf(builtIns.getCollection());
ClassifierDescriptor classifier = getSubstituteForTypeParameterOf(builtIns.getCollection(), 0);
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("add"),
builtIns.getBoolean(), classifier) == null) {
@@ -503,7 +503,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
if (isSubclass(descriptor, builtIns.getList())) {
ClassifierDescriptor classifier = getSubstituteForTypeParameterOf(builtIns.getList());
ClassifierDescriptor classifier = getSubstituteForTypeParameterOf(builtIns.getList(), 0);
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("set"),
classifier, builtIns.getInt(), classifier) == null) {
@@ -521,6 +521,39 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
}
if (isSubclass(descriptor, builtIns.getMap())) {
ClassifierDescriptor keyClassifier = getSubstituteForTypeParameterOf(builtIns.getMap(), 0);
ClassifierDescriptor valueClassifier = getSubstituteForTypeParameterOf(builtIns.getMap(), 1);
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("put"),
valueClassifier, keyClassifier, valueClassifier) == null) {
result.add("put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
}
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("remove"),
valueClassifier, builtIns.getAny()) == null) {
result.add("remove(Ljava/lang/Object;)Ljava/lang/Object;");
}
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("putAll"),
builtIns.getUnit(), builtIns.getMap()) == null) {
result.add("putAll(Ljava/util/Map;)V");
}
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("clear"), builtIns.getUnit()) == null) {
result.add("clear()V");
}
}
if (isSubclass(descriptor, builtIns.getMapEntry())) {
ClassifierDescriptor valueClassifier = getSubstituteForTypeParameterOf(builtIns.getMapEntry(), 1);
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("setValue"),
valueClassifier, valueClassifier) == null) {
result.add("setValue(Ljava/lang/Object;)Ljava/lang/Object;");
}
}
if (isSubclass(descriptor, builtIns.getIterator())) {
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("remove"),
builtIns.getUnit()) == null) {
@@ -532,8 +565,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
@NotNull
private ClassifierDescriptor getSubstituteForTypeParameterOf(@NotNull ClassDescriptor trait) {
TypeParameterDescriptor listTypeParameter = trait.getTypeConstructor().getParameters().get(0);
private ClassifierDescriptor getSubstituteForTypeParameterOf(@NotNull ClassDescriptor trait, int index) {
TypeParameterDescriptor listTypeParameter = trait.getTypeConstructor().getParameters().get(index);
TypeSubstitutor deepSubstitutor = SubstitutionUtils.buildDeepSubstitutor(descriptor.getDefaultType());
TypeProjection substitute = deepSubstitutor.substitute(new TypeProjection(listTypeParameter.getDefaultType()));
assert substitute != null : "Couldn't substitute: " + descriptor;
@@ -0,0 +1,30 @@
class MyMap<K, V>: Map<K, V> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun containsKey(key: Any?): Boolean = false
override fun containsValue(value: Any?): Boolean = false
override fun get(key: Any?): V? = null
override fun keySet(): Set<K> = throw UnsupportedOperationException()
override fun values(): Collection<V> = throw UnsupportedOperationException()
override fun entrySet(): Set<Map.Entry<K, V>> = throw UnsupportedOperationException()
}
fun expectUoe(block: () -> Unit) {
try {
block()
throw AssertionError()
} catch (e: UnsupportedOperationException) {
}
}
fun box(): String {
val map = MyMap<String, Int>() as MutableMap<String, Int>
expectUoe { map.put("", 1) }
expectUoe { map.remove("") }
expectUoe { map.putAll(map) }
expectUoe { map.clear() }
return "OK"
}
@@ -0,0 +1,15 @@
class MyMapEntry<K, V>: Map.Entry<K, V> {
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
override fun getKey(): K = throw UnsupportedOperationException()
override fun getValue(): V = throw UnsupportedOperationException()
}
fun box(): String {
try {
(MyMapEntry<String, Int>() as MutableMap.MutableEntry<String, Int>).setValue(1)
throw AssertionError()
} catch (e: UnsupportedOperationException) {
return "OK"
}
}
@@ -0,0 +1,14 @@
class MyMapEntry<K, V>: Map.Entry<K, V> {
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
override fun getKey(): K = throw UnsupportedOperationException()
override fun getValue(): V = throw UnsupportedOperationException()
public fun setValue(value: V): V = value
}
fun box(): String {
(MyMapEntry<String, Int>() as MutableMap.MutableEntry<String, Int>).setValue(1)
return "OK"
}
@@ -0,0 +1,27 @@
class MyMap<K, V>: Map<K, V> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun containsKey(key: Any?): Boolean = false
override fun containsValue(value: Any?): Boolean = false
override fun get(key: Any?): V? = null
override fun keySet(): Set<K> = throw UnsupportedOperationException()
override fun values(): Collection<V> = throw UnsupportedOperationException()
override fun entrySet(): Set<Map.Entry<K, V>> = throw UnsupportedOperationException()
public fun put(key: K, value: V): V? = null
public fun remove(key: Any?): V? = null
public fun putAll(m: Map<out K, V>) {}
public fun clear() {}
}
fun box(): String {
val map = MyMap<String, Int>() as MutableMap<String, Int>
map.put("", 1)
map.remove("")
map.putAll(map)
map.clear()
return "OK"
}
@@ -454,6 +454,26 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest("compiler/testData/codegen/box/builtinStubMethods/ListWithAllInheritedImplementations.kt");
}
@TestMetadata("Map.kt")
public void testMap() throws Exception {
doTest("compiler/testData/codegen/box/builtinStubMethods/Map.kt");
}
@TestMetadata("MapEntry.kt")
public void testMapEntry() throws Exception {
doTest("compiler/testData/codegen/box/builtinStubMethods/MapEntry.kt");
}
@TestMetadata("MapEntryWithSetValue.kt")
public void testMapEntryWithSetValue() throws Exception {
doTest("compiler/testData/codegen/box/builtinStubMethods/MapEntryWithSetValue.kt");
}
@TestMetadata("MapWithAllImplementations.kt")
public void testMapWithAllImplementations() throws Exception {
doTest("compiler/testData/codegen/box/builtinStubMethods/MapWithAllImplementations.kt");
}
@TestMetadata("SubstitutedList.kt")
public void testSubstitutedList() throws Exception {
doTest("compiler/testData/codegen/box/builtinStubMethods/SubstitutedList.kt");