提交 62c25c03 编写于 作者: A Alexey Tsvetkov

Replace `is Array<T>` with `.isArrayOf<T>()`

上级 01cd277d
......@@ -66,6 +66,7 @@ public class IntrinsicMethods {
namedMethods.put("kotlin.arrays.array", new JavaClassArray());
namedMethods.put("kotlin.jvm.internal.unsafe.monitorEnter", MonitorInstruction.MONITOR_ENTER);
namedMethods.put("kotlin.jvm.internal.unsafe.monitorExit", MonitorInstruction.MONITOR_EXIT);
namedMethods.put("kotlin.jvm.isArrayOf", new IsArrayOf());
ImmutableList<Name> primitiveCastMethods = OperatorConventions.NUMBER_CONVERSIONS.asList();
for (Name method : primitiveCastMethods) {
......
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen.intrinsics
import org.jetbrains.kotlin.codegen.Callable
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.types.Variance
class IsArrayOf : IntrinsicMethod() {
override fun toCallable(fd: FunctionDescriptor, isSuper: Boolean, resolvedCall: ResolvedCall<*>, codegen: ExpressionCodegen): Callable {
val typeArguments = resolvedCall.typeArguments
assert(typeArguments.size == 1) { "Expected only one type parameter for Any?.isArrayOf(), got: $typeArguments" }
val typeMapper = codegen.state.typeMapper
val method = typeMapper.mapToCallableMethod(fd, false)
val builtIns = fd.module.builtIns
val elementType = typeArguments.values().first()
val arrayKtType = builtIns.getArrayType(Variance.INVARIANT, elementType)
val arrayType = typeMapper.mapType(arrayKtType)
return createIntrinsicCallable(method) {
it.instanceOf(arrayType)
}
}
}
\ No newline at end of file
fun box() = if(arrayOfNulls<Int>(10) is Array<java.lang.Integer>) "OK" else "fail"
fun f(x: Any?): String {
if (x is Array<String>) {
fun f(x: Any?): Any? {
if (x is Array<*>) {
for (i in x) {
return i
}
......@@ -10,5 +10,5 @@ fun f(x: Any?): String {
fun box(): String {
val a = arrayOfNulls<String>(1) as Array<String>
a[0] = "OK"
return f(a)
return f(a) as String
}
fun box() = if(arrayOfNulls<Int>(10).isArrayOf<java.lang.Integer>()) "OK" else "fail"
fun foo(x : Any): String {
return if(x is Array<String>) x[0] else "fail"
return if(x is Array<*> && x.isArrayOf<String>()) (x as Array<String>)[0] else "fail"
}
fun box(): String {
......
......@@ -2,7 +2,7 @@ import java.util.Arrays
fun box(): String {
val array = Arrays.asList(2, 3, 9).toTypedArray()
if (array !is Array<Int>) return array.javaClass.toString()
if (!array.isArrayOf<Int>()) return array.javaClass.toString()
val str = Arrays.toString(array)
if (str != "[2, 3, 9]") return str
......
......@@ -8,8 +8,8 @@ fun box(): String {
val array1 = collection.toArray()
val array2 = collection.toArray(arrayOfNulls<Int>(3) as Array<Int>)
if (array1 !is Array<Any>) return (array1 as Object).getClass().toString()
if (array2 !is Array<Int>) return (array2 as Object).getClass().toString()
if (!array1.isArrayOf<Any>()) return (array1 as Object).getClass().toString()
if (!array2.isArrayOf<Int>()) return (array2 as Object).getClass().toString()
val s1 = Arrays.toString(array1)
val s2 = Arrays.toString(array2)
......
......@@ -23,8 +23,8 @@ fun box(): String {
val array1 = collection.toArray()
val array2 = collection.toArray(arrayOfNulls<Int>(3) as Array<Int>)
if (array1 !is Array<Any>) return (array1 as Object).getClass().toString()
if (array2 !is Array<Int>) return (array2 as Object).getClass().toString()
if (!array1.isArrayOf<Any>()) return (array1 as Object).getClass().toString()
if (!array2.isArrayOf<Int>()) return (array2 as Object).getClass().toString()
val s1 = Arrays.toString(array1)
val s2 = Arrays.toString(array2)
......
fun f(x: Any): Boolean = x is Array<*> && x.isArrayOf<String>()
// 2 INSTANCEOF
\ No newline at end of file
fun f(a: Array<out Number>) = a is Array<Int>
fun f(a: Array<out Number>) = a.isArrayOf<Int>()
fun ff(a: Any) = a is Array<String>
\ No newline at end of file
fun ff(a: Any) = a is Array<*> && <!DEBUG_INFO_SMARTCAST!>a<!>.isArrayOf<String>()
\ No newline at end of file
......@@ -149,6 +149,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("isArrayOf.kt")
public void testIsArrayOf() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/isArrayOf.kt");
doTest(fileName);
}
@TestMetadata("javaExtensionPropertyIntrinsic.kt")
public void testJavaExtensionPropertyIntrinsic() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/javaExtensionPropertyIntrinsic.kt");
......
......@@ -286,12 +286,6 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("kt602.kt")
public void testKt602() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/arrays/kt602.kt");
doTest(fileName);
}
@TestMetadata("kt7288.kt")
public void testKt7288() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/arrays/kt7288.kt");
......@@ -7409,27 +7403,6 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
}
}
@TestMetadata("compiler/testData/codegen/box/toArray")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ToArray extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInToArray() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/toArray"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("toArray.kt")
public void testToArray() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/toArray/toArray.kt");
doTest(fileName);
}
@TestMetadata("toArrayAlreadyPresent.kt")
public void testToArrayAlreadyPresent() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/toArray/toArrayAlreadyPresent.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/box/traits")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
......
......@@ -256,6 +256,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName);
}
@TestMetadata("kt602.kt")
public void testKt602() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/arrays/kt602.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("kt7009.kt")
public void testKt7009() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/arrays/kt7009.kt");
......@@ -4644,6 +4650,18 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/toArray/returnCopyToArray.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("toArray.kt")
public void testToArray() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/toArray/toArray.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("toArrayAlreadyPresent.kt")
public void testToArrayAlreadyPresent() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/toArray/toArrayAlreadyPresent.kt");
doTestWithStdlib(fileName);
}
}
@TestMetadata("compiler/testData/codegen/boxWithStdlib/vararg")
......
......@@ -227,8 +227,8 @@ fun foo(aa: Any): Int {
}
fun inForLoop(x: Any?) {
if (x is Array<String>) {
for (i in <info descr="Smart cast to kotlin.Array<kotlin.String>">x</info>) {}
if (x is Array<*>) {
for (i in <info descr="Smart cast to kotlin.Array<*>">x</info>) {}
}
for (i in <error descr="[ITERATOR_MISSING] For-loop range must have an iterator() method">x</error>) {}
}
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jvm
import kotlin.jvm.internal.Intrinsic
/**
* Checks if array can contain element of type [T].
*/
@Intrinsic("kotlin.jvm.isArrayOf")
public fun <reified T : Any> Array<*>.isArrayOf(): Boolean =
T::class.java.isAssignableFrom(this.javaClass.componentType)
\ No newline at end of file
......@@ -123,7 +123,7 @@ class CollectionJVMTest {
assertEquals(2, arr.size())
todo {
assertTrue {
arr is Array<String>
arr.isArrayOf<String>()
}
}
}
......
......@@ -403,7 +403,7 @@ class CollectionTest {
assertEquals(2, arr.size())
todo {
assertTrue {
arr is Array<String>
arr.isArrayOf<String>()
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册