Java和Kotlin使用泛型进行投射.丢失类型安全

Tob*_*ens 9 java generics casting kotlin erasure

在使用Kotlin/Java进行编码时,我在使用强制转换和泛型时偶然发现了一些奇怪的东西.似乎有可能让类型系统认为列表是类型List<Foo>,而它实际上是一个List<Object>.

任何人都可以向我解释为什么这是可能的?

以下是Kotlin和Java中的一个例子:

Kotlin中的例子

fun <T> test(obj: Any): List<T> {
    val ts = ArrayList<T>()
    ts.add(obj as T)
    return ts
}

fun <T> test2(obj: Any): T {
    return obj as T
}

fun <T> test3(obj: Any): List<T> {
    val ts = ArrayList<T>()
    ts.add(test2(obj))
    return ts
}


fun main(args: Array<String>) {
    val x = test<Double>(1) // Returns a list of Integers and doesn't error
    println(x)

    val y = test2<Double>(1) // Casts the Int object to a Double.
    println(y)

    val z = test3<Double>(1) // Returns a list of Integers and doesn't error.
    println(z)
}
Run Code Online (Sandbox Code Playgroud)

Java中的示例

public class Test {
    public static <T> List<T> test(Object obj){
        ArrayList<T> ts = new ArrayList<>();
        ts.add((T) obj);
        return ts;
    }

    public static <T> T test2(Object obj){
        return (T) obj;
    }

    public static <T> List<T> test3(Object obj){
        ArrayList<T> ts = new ArrayList<>();
        ts.add(test2(obj));
        return ts;
    }


    public static void main(String[] args) {
        List<Double> x = test(1); // Returns a list of Integers and doesn't error
        System.out.println(x);

        // Double y = test2(1); // Errors in java an Integers cannot be converted into a Double.
        // System.out.println(y);

        List<Double> z = test3(1); // Returns a list of Integers and doesn't error.
        System.out.println(z);
    }
}
Run Code Online (Sandbox Code Playgroud)

HTN*_*TNW 11

Java没有具体化的泛型.也就是说,通用信息在运行时不存在,并且所有通用代码都被称为擦除的过程"简化".当已知泛型类型以确保正确性时,编译器会抛出强制转换.你不能转换为泛型类型,因为泛型类型不足以让运行时知道某个值是否为1,这就是为什么要javac大吼大叫这样做,因为它知道你在问JVM要做一些它不可能做的事情,引入运行时不安全.

public class Test {
    public static List test(Object obj) { // generic types => erasure = raw types
        ArrayList ts = new ArrayList();
        ts.add(obj); // No cast: List.add has erasure (Ljava.lang.Object;)V
        return ts;
    }

    public static Object test2(Object obj) { // T is unbounded => erasure = Object
        return obj; // No cast: all types <: Object
    }

    public static List test3(Object obj) {
        ArrayList ts = new ArrayList();
        ts.add(test2(obj)); // Note: we don't know what T is, so we can't cast to it and ensure test2 returned one.
        return ts;
    }


    public static void main(String[] args) {
        List x = test(1); // Returns a list and doesn't error
        System.out.println(x);

        Double y = (Double) test2(1); // Errors in java as an Integer cannot be converted into a Double
        // This is because the compiler needs to insert casts to make generics work
        System.out.println(y);

        List z = test3(1);
        // Unlike y, there isn't a cast in test3 because test3 doesn't know what T is, so the Integer passes through, uncast, into a List<Double>.
        // The JVM can't detect this, because it doesn't even know what a List<Double> is.
        System.out.println(z);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意如何test2擦除一个美化的身份功能,导致test3完全相同的事情test1,但具有间接的水平.


Vla*_*ich 5

执行未选中强制转换为T时,编译器会发出警告.如果您的程序类型中出现此类警告,则无法保证安全.所以你看到的行为是预料之中的.