9

在使用 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)
}

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);
    }
}
4

3 回答 3

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);
    }
}

请注意如何test2擦除到美化的恒等函数,导致test3执行与 完全相同的事情test1,但具有一定程度的间接性。

于 2017-08-20T16:07:53.677 回答
5

当执行未经检查的对 T 的强制转换时,编译器会发出警告。当您的程序中出现此类警告时,无法保证类型安全。所以你看到的行为是预期的。

于 2017-08-20T15:55:00.210 回答
-3

尝试像这样更改第二个测试:

    Double y = test2(1.0);
    System.out.println(y);

在您的代码test2(1)中,参数1被自动装箱为Integer,而不能强制转换为Double.

于 2017-08-20T15:57:29.623 回答