public class UncheckedWarningWithCast{ interface NotGenericInterface { } interface GenericInterface { } interface ChildGenericInterface extends GenericInterface { } interface NotChildGenericInterface { } interface ChildNotRelatedGenericInterface extends GenericInterface { } interface ChildNotGeneric extends GenericInterface { } interface NotChildNotGeneric { } final class NotChildNotGenericClass { } final class ChildNotGenericClass implements GenericInterface { } class ChildNotGenericClassNotCompatible implements GenericInterface { } class ChildNotGenericNotFinalClassNotCompatible implements GenericInterface { } final class ChildGenericClass implements GenericInterface { } public interface Function { R apply(T t); } public interface Function2 extends Function { } public interface Function3 extends Function2 { } public interface UnaryOperator extends Function { } public interface UnaryOperator2 extends Function { } class ExtendFunction implements Function{ @Override public T apply(CharSequence charSequence) { return null; } } class ClassMapper implements UnaryOperator { private T in; private T out; ClassMapper(T in, T out) { this.in = in; this.out = out; } public T apply(T arg) { return in.equals(arg) ? out : null; } } void test(UnaryOperator op, Object ob) { //Changed Example 18.5.5-1. Record Pattern Type Inference JLS with Record Patterns (Second Preview) ClassMapper seq = (ClassMapper) op; //unchecked ClassMapper seq2 = (ClassMapper) op; } void test2(UnaryOperator op, Object ob) { ClassMapper seq = (ClassMapper) op; } void test3(Function op, Object ob) { //unchecked ClassMapper seq = (ClassMapper) op; //unchecked UnaryOperator seq2 = (UnaryOperator) op; //unchecked UnaryOperator2 seq3 = (UnaryOperator2) op; } void test4(Function op, Object ob) { Function3 seq = (Function3) op; } void test5(Function op, Object ob) { ExtendFunction seq = (ExtendFunction) op; } void test6(Function op, Object ob) { //unchecked ExtendFunction seq = (ExtendFunction) op; } void test7(Function op, Object ob) { Function seq = (Function) op; } void test8(UnaryOperator op, Object ob) { ClassMapper seq = (ClassMapper) op; ClassMapper seq2 = (ClassMapper) op; } public void testChildGenericInterface(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, NotGenericInterface notGenericInterface) { //unchecked ChildGenericInterface notRaw = (ChildGenericInterface) raw; ChildGenericInterface superWildcardGenericCast = (ChildGenericInterface) superGeneric; ChildGenericInterface extendWildcardGenericCast = (ChildGenericInterface) extendGeneric; ChildGenericInterface superEqualGenericCast = (ChildGenericInterface) superGeneric; ChildGenericInterface extendEqualGenericCast = (ChildGenericInterface) extendGeneric; //unchecked ChildGenericInterface superUpperBoundGenericCast = (ChildGenericInterface) superGeneric; //unchecked ChildGenericInterface extendUpperBoundGenericCast = (ChildGenericInterface) extendGeneric; //unchecked ChildGenericInterface superNotRelatedGenericCast = (ChildGenericInterface) superGeneric; //unchecked ChildGenericInterface extendNotCoveredGenericCast = (ChildGenericInterface) extendGeneric; //unchecked ChildGenericInterface superNotGenericCast = (ChildGenericInterface) notGenericInterface; //unchecked ChildGenericInterface extendNotGenericCast = (ChildGenericInterface) notGenericInterface; } public void testChildGenericClass(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, NotGenericInterface notGenericInterface) { //unchecked ChildGenericClass notRaw = (ChildGenericClass) raw; ChildGenericClass superWildcardGenericCast = (ChildGenericClass) superGeneric; ChildGenericClass extendWildcardGenericCast = (ChildGenericClass) extendGeneric; ChildGenericClass superEqualGenericCast = (ChildGenericClass) superGeneric; ChildGenericClass extendEqualGenericCast = (ChildGenericClass) extendGeneric; //unchecked ChildGenericClass superUpperBoundGenericCast = (ChildGenericClass) superGeneric; //unchecked ChildGenericClass extendUpperBoundGenericCast = (ChildGenericClass) extendGeneric; //unchecked ChildGenericClass superNotRelatedGenericCast = (ChildGenericClass) superGeneric; //unchecked ChildGenericClass extendNotCoveredGenericCast = (ChildGenericClass) extendGeneric; //not compile ChildGenericClass superNotGenericCast = (ChildGenericClass) notGenericInterface; //not compile ChildGenericClass extendNotGenericCast = (ChildGenericClass) notGenericInterface; } public void testNotChildGenericInterface(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, NotGenericInterface notGenericInterface) { //unchecked NotChildGenericInterface notRaw = (NotChildGenericInterface) raw; NotChildGenericInterface superWildcardGenericCast = (NotChildGenericInterface) superGeneric; NotChildGenericInterface extendWildcardGenericCast = (NotChildGenericInterface) extendGeneric; //unchecked NotChildGenericInterface superEqualGenericCast = (NotChildGenericInterface) superGeneric; //unchecked NotChildGenericInterface extendEqualGenericCast = (NotChildGenericInterface) extendGeneric; //unchecked NotChildGenericInterface superUpperBoundGenericCast = (NotChildGenericInterface) superGeneric; //unchecked NotChildGenericInterface extendUpperBoundGenericCast = (NotChildGenericInterface) extendGeneric; //unchecked NotChildGenericInterface superNotRelatedGenericCast = (NotChildGenericInterface) superGeneric; //unchecked NotChildGenericInterface extendNotCoveredGenericCast = (NotChildGenericInterface) extendGeneric; //unchecked NotChildGenericInterface superNotGenericCast = (NotChildGenericInterface) notGenericInterface; //unchecked NotChildGenericInterface extendNotGenericCast = (NotChildGenericInterface) notGenericInterface; } public void testChildNotRelatedGenericInterface(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, NotGenericInterface notGenericInterface) { //unchecked ChildNotRelatedGenericInterface notRaw = (ChildNotRelatedGenericInterface) raw; ChildNotRelatedGenericInterface superWildcardGenericCast = (ChildNotRelatedGenericInterface) superGeneric; ChildNotRelatedGenericInterface extendWildcardGenericCast = (ChildNotRelatedGenericInterface) extendGeneric; //unchecked ChildNotRelatedGenericInterface superEqualGenericCast = (ChildNotRelatedGenericInterface) superGeneric; //unchecked ChildNotRelatedGenericInterface extendEqualGenericCast = (ChildNotRelatedGenericInterface) extendGeneric; //unchecked ChildNotRelatedGenericInterface superUpperBoundGenericCast = (ChildNotRelatedGenericInterface) superGeneric; //unchecked ChildNotRelatedGenericInterface extendUpperBoundGenericCast = (ChildNotRelatedGenericInterface) extendGeneric; //unchecked ChildNotRelatedGenericInterface superNotRelatedGenericCast = (ChildNotRelatedGenericInterface) superGeneric; //unchecked ChildNotRelatedGenericInterface extendNotCoveredGenericCast = (ChildNotRelatedGenericInterface) extendGeneric; //unchecked ChildNotRelatedGenericInterface superNotGenericCast = (ChildNotRelatedGenericInterface) notGenericInterface; //unchecked ChildNotRelatedGenericInterface extendNotGenericCast = (ChildNotRelatedGenericInterface) notGenericInterface; } public void testChildNotGeneric(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, GenericInterface superGenericInteger, GenericInterface extendGenericInteger, NotGenericInterface notGenericInterface) { ChildNotGeneric notRaw = (ChildNotGeneric) raw; ChildNotGeneric superCast = (ChildNotGeneric) superGeneric; ChildNotGeneric extendCast = (ChildNotGeneric) extendGeneric; //not compile ChildNotGeneric extendNumberCast = (ChildNotGeneric) extendGenericInteger; //not compile ChildNotGeneric superNumberCast = (ChildNotGeneric) superGenericInteger; ChildNotGeneric superNotGenericCast = (ChildNotGeneric) notGenericInterface; ChildNotGeneric extendNotGenericCast = (ChildNotGeneric) notGenericInterface; } public void testNotChildNotGeneric(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, GenericInterface superGenericInteger, GenericInterface extendGenericInteger, NotGenericInterface notGenericInterface) { NotChildNotGeneric notRaw = (NotChildNotGeneric) raw; NotChildNotGeneric superCast = (NotChildNotGeneric) superGeneric; NotChildNotGeneric extendCast = (NotChildNotGeneric) extendGeneric; NotChildNotGeneric extendNumberCast = (NotChildNotGeneric) extendGenericInteger; NotChildNotGeneric superNumberCast = (NotChildNotGeneric) superGenericInteger; NotChildNotGeneric superNotGenericCast = (NotChildNotGeneric) notGenericInterface; NotChildNotGeneric extendNotGenericCast = (NotChildNotGeneric) notGenericInterface; } public void testNotChildNotGenericClass(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, GenericInterface superGenericInteger, GenericInterface extendGenericInteger, NotGenericInterface notGenericInterface) { //not compile NotChildNotGenericClass notRaw = (NotChildNotGenericClass) raw; //not compile NotChildNotGenericClass superCast = (NotChildNotGenericClass) superGeneric; //not compile NotChildNotGenericClass extendCast = (NotChildNotGenericClass) extendGeneric; //not compile NotChildNotGenericClass extendNumberCast = (NotChildNotGenericClass) extendGenericInteger; //not compile NotChildNotGenericClass superNumberCast = (NotChildNotGenericClass) superGenericInteger; //not compile NotChildNotGenericClass superNotGenericCast = (NotChildNotGenericClass) notGenericInterface; //not compile NotChildNotGenericClass extendNotGenericCast = (NotChildNotGenericClass) notGenericInterface; } public void testChildNotGenericClass(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, GenericInterface superGenericInteger, GenericInterface extendGenericInteger, NotGenericInterface notGenericInterface) { ChildNotGenericClass notRaw = (ChildNotGenericClass) raw; ChildNotGenericClass superCast = (ChildNotGenericClass) superGeneric; ChildNotGenericClass extendCast = (ChildNotGenericClass) extendGeneric; //not compile ChildNotGenericClass extendNumberCast = (ChildNotGenericClass) extendGenericInteger; //not compile ChildNotGenericClass superNumberCast = (ChildNotGenericClass) superGenericInteger; //not compile ChildNotGenericClass superNotGenericCast = (ChildNotGenericClass) notGenericInterface; //not compile ChildNotGenericClass extendNotGenericCast = (ChildNotGenericClass) notGenericInterface; } public void testChildGenericNotCompatibleClass(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, GenericInterface superGenericInteger, GenericInterface extendGenericInteger, NotGenericInterface notGenericInterface) { ChildNotGenericClassNotCompatible notRaw = (ChildNotGenericClassNotCompatible) raw; //not compile ChildNotGenericClassNotCompatible superCast = (ChildNotGenericClassNotCompatible) superGeneric; //not compile ChildNotGenericClassNotCompatible extendCast = (ChildNotGenericClassNotCompatible) extendGeneric; ChildNotGenericClassNotCompatible extendNumberCast = (ChildNotGenericClassNotCompatible) extendGenericInteger; ChildNotGenericClassNotCompatible superNumberCast = (ChildNotGenericClassNotCompatible) superGenericInteger; ChildNotGenericClassNotCompatible superNotGenericCast = (ChildNotGenericClassNotCompatible) notGenericInterface; ChildNotGenericClassNotCompatible extendNotGenericCast = (ChildNotGenericClassNotCompatible) notGenericInterface; } public void testChildGenericNotFinalNotCompatibleClass(GenericInterface raw, GenericInterface superGeneric, GenericInterface extendGeneric, GenericInterface superGenericInteger, GenericInterface extendGenericInteger, NotGenericInterface notGenericInterface) { ChildNotGenericNotFinalClassNotCompatible notRaw = (ChildNotGenericNotFinalClassNotCompatible) raw; //not compile ChildNotGenericNotFinalClassNotCompatible superCast = (ChildNotGenericNotFinalClassNotCompatible) superGeneric; ChildNotGenericNotFinalClassNotCompatible superNumberCast = (ChildNotGenericNotFinalClassNotCompatible) superGenericInteger; ChildNotGenericNotFinalClassNotCompatible superNotGenericCast = (ChildNotGenericNotFinalClassNotCompatible) notGenericInterface; ChildNotGenericNotFinalClassNotCompatible extendNotGenericCast = (ChildNotGenericNotFinalClassNotCompatible) notGenericInterface; } static class ClassMapperNumber implements UnaryOperator { private Number in; private Number out; ClassMapperNumber(Number in, Number out) { this.in = in; this.out = out; } public Number apply(Number arg) { return in.equals(arg) ? out : null; } } }