oop - Java generic design -
i've errorfactory
class based on error code creates instance of different type of errors. have output
class might contains 1 of error types.
code below shows how trying utilize it, unfortunately getting error. please help me figure out wrong there , suggest improve design. help!
outputlike output = new outputlike(); errorfactory errorfactory = new errorfactory(); int errorcode = ((long)error.get("error_code")).intvalue(); output.seterror(errorfactory.<error>geterror(errorcode, error)); error:(97, 27) java: method seterror in class com.electronsoftware.vkliker.responseoutput.output.output<t> cannot applied given types; required: com.electronsoftware.vkliker.error.error found: java.lang.object reason: actual argument java.lang.object cannot converted com.electronsoftware.vkliker.error.error method invocation conversion
======
public class errorfactory <t extends error> { public <t> t geterror(int error_code, jsonobject errorbody){ error.errortype errortype = error.errortype.values()[error_code]; switch(errortype){ case captcha_needed:{ homecoming (t) parser.parsecaptcharequirederror(errorbody); } case user_authorization_failed:{ break; } case too_many_requests:{ break; } case not_enough_permissions:{ break; } case unknown:{ break; } } homecoming null; } }
====
public class outputlike <e extends error> extends output <e> { .... } public abstract class output <t extends error> { private boolean iserror; private t error; public boolean iserror(){ homecoming this.iserror; } public t geterror(){ homecoming this.error; } public void seterror(t error){ this.error = error; } }
====
public static errorcaptcharequired parsecaptcharequirederror(jsonobject error){ errorcaptcharequired captchaerror = null; .... captchaerror = new errorcaptcharequired(msg, captchaid, captchaurl); homecoming captchaerror; }
====
public class errorcaptcharequired extends error { ........ }
two problems:
a) using raw (untyped) errorfactory
, strips generic info instance. alter to:
errorfactory<error> errorfactory = new errorfactory<error>();
b) if prepare a), have typed geterror()
<t>
hiding type t
of class. change
public <t> t geterror(int error_code, jsonobject errorbody){
to
public t geterror(int error_code, jsonobject errorbody){
unless need type factory, create untyped , homecoming error.
also, naming class same java.lang class bad idea. phone call problem or something.
java oop generics inheritance
No comments:
Post a Comment