简体   繁体   中英

Ensure type safety with Java generics

I am trying to improve the type safety of the following method:

A<String> method(B<String> arg);

How do I make sure that this method is not called with a raw type B and that compiler always checks for String type specified?

You can't prevent someone from calling a method with a raw type.

The reason is for backward compatibility with pre-generic versions of java.

If the compiler warnings are turned on ( -Xlint:unchecked ), you will get a warning if that method is called with a raw type:

warning: [unchecked] unchecked conversion A a = method(new B());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM