Enter a security warning when parameterizing a descendant of a parameterized class

I have a method with a signature:

public static <T, E extends PxViewFull<T> > E create(Class<T> dtoClass, Class<E> viewClass) 

When I call it for a parameterized second parameter, I get a type safety warning. Method call:

 private PxViewFull<Output40942DTO> output40813 = PxViewFull.create(Output40813DTO.class, ServiceOutputView.class); 

Signature ServiceOutputView:

 public class ServiceOutputView<T extends ServiceCall<?>> extends PxViewFull<T> 

How can I get arguments of a general type to get rid of the warning?

+4
source share
1 answer

@ Paul Bellora answered your question.

ServiceOutputView.class is of type Class [ServiceOutputView [?]], And your parameter on the call site expects Class [ServiceOutputView [Output40942DTO]], so you get an unverified method call exception when you try to pass Class [ServiceView [? ] to the class [ServiceOutputView [Output40942DTO]], which, as your method type parameters say, expects.

UPDATE: There is an agreement here that will not give a warning:

 public class Parameter { public static void main(String... args) throws Exception { PxViewFull<OutputDTO> output40813 = PxViewFull.create(OutputDTO.class, ServiceOutputView.class); } } class ServiceCall<X> { } class OutputDTO extends ServiceCall<OutputDTO> { public OutputDTO serviceCall() { // i guess i pupulate myself? return this; } } class PxViewFull<X> { private X dtoObject; private Class<X> dtoObjectType; public static <P1, P2 extends PxViewFull<?>, RESULT extends PxViewFull<P1>> RESULT create( Class<P1> dtoClass, Class<P2> viewClass) throws Exception { return null; } public void setDtoObjectType(final Class<X> dtoObjectType) { this.dtoObjectType = dtoObjectType; } public void setModel(Object o) { if (!dtoObjectType.isInstance(o)) throw new IllegalArgumentException(); this.dtoObject = dtoObjectType.cast(o); } } class ServiceOutputView<T extends ServiceCall<?>> extends PxViewFull<T> { } 

The create method should contain uncontrolled selection, but it’s best to just put @SuppressWarning on it and make sure that the reason is explicitly specified in javadoc for the method.

0
source

Source: https://habr.com/ru/post/1495761/


All Articles