java - How to avoid an unchecked cast when using unbounded generified static functions in a ternary? -


i trying figure out if there way avoid unchecked cast in function (using java 7):

private static <o> valuevalidator<o> newforalways(always valid_orinvalid)  {    @suppresswarnings("unchecked")    valuevalidator<o> vldtr = (valuevalidator<o>)(valid_orinvalid == always.valid       ?  newforalwaysvalid()       :  newforalwaysinvalid());    return  vldtr; } 

here signatures of 2 functions being returned:

private static <o> valuevalidator<o> newforalwaysvalid()  { private static <o> valuevalidator<o> newforalwaysinvalid()  { 

(and here's always enum, boolean substitute:

enum {valid, invalid;}; 

)

all 3 functions have same return type , contain unbounded generic. these 2 questions explain why happens, although both bounded generics:

so though works

valuevalidator<integer> intvldtr = test.<integer>newforalwaysvalid(); 

this doesn't:

private static <o> valuevalidator<o> newforalways(always valid_orinvalid)  {    return  (valid_orinvalid == always.valid       ?  <o>newforalwaysvalid()       :  <o>newforalwaysinvalid()); }  c:\java_code\test.java:15: error: illegal start of expression       ?  <o>newforalwaysvalid()  ...and 8 more similar errors... 

and neither this:

private static <o> valuevalidator<o> newforalways2(always valid_orinvalid)  {    return  (valid_orinvalid == always.valid       ?  newforalwaysvalid()       :  newforalwaysinvalid()); }  c:\java_code\test.java:15: error: incompatible types           ?  newforalwaysvalid()           ^   required: valuevalidator<o>   found:    valuevalidator<object>   o type-variable:     o extends object declared in method <o>newforalways2(always) 

so, repeat question: there alternative unchecked cast? (i'm using java 7.)

sscce:

public class test  {    public static void main(string[] ignored)  {       valuevalidator<integer> intvldtr = test.<integer>newforalwaysvalid();       intvldtr = test.<integer>newforalways(always.valid);    }    private static <o> valuevalidator<o> newforalways(always valid_orinvalid)  {       @suppresswarnings("unchecked")       valuevalidator<o> vldtr = (valuevalidator<o>)(valid_orinvalid == always.valid          ?  newforalwaysvalid()          :  newforalwaysinvalid());       return  vldtr;    }    private static <o> valuevalidator<o> newforalwaysvalid()  {       return  (new alwaysvalid<o>());    }    private static <o> valuevalidator<o> newforalwaysinvalid()  {       return  (new alwaysinvalid<o>());    } } enum {valid, invalid;}; abstract class valuevalidator<o>  {    public abstract boolean isvalid(o to_validate); } class alwaysvalid<o> extends valuevalidator<o>  {    public boolean isvalid(o to_validate)  {       return  true;    } } class alwaysinvalid<o> extends valuevalidator<o>  {    public boolean isvalid(o to_validate)  {       return  false;    } } 

so know there issue generic type argument inferencing conditional operator in java 7, has been fixed in java 8. fix issue, can use explicit type argument.

well, tried it, invalid syntax. when use explicit type argument, have qualify method invocation either object type or class type. change method to:

private static <o> valuevalidator<o> newforalways(always valid_orinvalid)  {    valuevalidator<o> vldtr = valid_orinvalid == always.valid      ?  test.<o>newforalwaysvalid()      :  test.<o>newforalwaysinvalid();   return  vldtr; } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -