Why calling method with generic return on a generic class is considered unsafe by javac?

百般思念 提交于 2019-11-26 21:47:16

问题


Consider the following code:

public class Main {
    public static class NormalClass {
        public Class<Integer> method() {
            return Integer.class;
        }
    }

    public static class GenericClass<T> {
        public Class<Integer> method() {
            return Integer.class;
        }
    }

    public static void main(String... args) {
        NormalClass safeInstance = new NormalClass();
        Class<Integer> safeValue = safeInstance.method();

        GenericClass unsafeInstance = new GenericClass();
        Class<Integer> unsafeValue = unsafeInstance.method();
    }
}

If I compile it with:

$ javac -Xlint:unchecked Main.java 

It returns:

Main.java:16: warning: [unchecked] unchecked conversion
        Class<Integer> unsafeValue = unsafeInstance.method();
                                                          ^
  required: Class<Integer>
  found:    Class
1 warning

Please note that only the generic method is considered unsafe, even if no generic type is referenced on the return type.

Is this a javac bug? Or there is a deeper reason for this I'm not taking into account?


回答1:


Raw types were allowed to ensure compatibility with code written before generics were introduced. Raw types work by simply ignoring all type information from all method arguments and return types, even type information that is not related to the type parameter of the class. This can lead to strange results, as you have found. But it gets even stranger than this. For example, this compiles.

public class Main {

    public static class GenericClass<T> {
        public void foo(Class<Integer> clazz) {
        }
    }

    public static void main(String... args) {
        GenericClass unsafeInstance = new GenericClass();
        unsafeInstance.foo(String.class);
    }
}



回答2:


The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.

Java Language Specification




回答3:


In order to be compatible with Java 1.4 compiler assumes that if you drop the generic arguments on instance type declaration, then you work with the special version of the class where no generics exist at all. And it issues a warning if you mix a Java 1.4 non-generic code with Java 1.5+ generic code. That's easier than trying to figure out whether generic return type of your method is actually independent from parameters. You can always @SuppressWarning if you don't like it.




回答4:


This might have to do with the instantiation of GenericClass which is a parameterized type and hence type arguments are needed in it. The warning vanishes if we do something like the following

GenericClass<String> unsafeInstance = new GenericClass<String>();
                          OR
GenericClass<?> unsafeInstance = new GenericClass();

As per my point of view, the reference "unsafeInstance" refers to a class which is generic in nature as opposed to "safeInstance". Thus the compiler may want the type information be associated to the reference before any method is called using it in code.



来源:https://stackoverflow.com/questions/30005025/why-calling-method-with-generic-return-on-a-generic-class-is-considered-unsafe-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!