java - Type mismatch error in method with generic return (Why do I have to do a cast to the generic type in this method, but not in similar ones?) -
i getting following error method in need generic return type:
description resource path location type type mismatch: cannot convert pagetypeone p securedpage.java
i can rid of error casting object generic type parameter, don't understand why required in particular method, not in other similar methods have written elsewhere in other classes.
the basic structure of project this:
a base class page unsecured web pages
base class securedpage secured web pages
i had similar question earlier today solved stack overflow community: bound mismatch error , java generic method.
i having problem similar method.
the base page class w/ helper method builds pages this:
public abstract class page<t extends page<t>> extends slowloadablecomponent<t> { protected static final <t extends page<t>> t constructpage(webdriver driver, int timeoutinseconds, java.lang.class<t> pageclass) { page<t> p = null; try { constructor<t> pageconstructor = pageclass.getconstructor( webdriver.class, string.class, integer.type); p = pageconstructor.newinstance(driver, driver.getcurrenturl(), timeoutinseconds); p.get(); } catch(exception e) { } return pageclass.cast(p); } }
securedpage class follows:
public class securedpage<t extends securedpage<t>> extends page<t> { ..... }
this method trying implement within securedpage -- want able have single method return both types of pages result opening link in method in web application:
public final <p extends securedpage<p>> p loadcontext(final string context) throws nosuchelementexception { menu m = pageheader.getmenu(); webelement link = m.clicklink(context); string linkhref = link.getattribute("href"); if (linkhref.contains("somematchtext")) { return page.constructpage(getdriver(), gettimeoutinseconds(), pagetypeone.class); <==== error } else if (linkhref.contains("someothermatchtext")) { return page.constructpage(getdriver(), gettimeoutinseconds(), pagetypetwo.class); <==== wheere error } }
pagetypeone , pagetypetwo both extend securedpage:
public final class pagetypeone extends securedpage<pagetypeone> { ..... } public final class pagetypetwo extends securedpage<pagetypetwo> { ..... }
i have similar method in loginpage class returns loginpage if attempts log in invalid credentials, , homepage (which extends securedpage), if log in valid credentials. don't error method. nor error constructpage method in page class, don't understand why getting error loadcontext method in securedpage:
public final class loginpage extends page<loginpage> { public final homepage loginwithgoodcredentials(final user user) { return login(user, homepage.class); } public final loginpage loginwithbadcredentials(final user user) { return login(user, loginpage.class); } public final <t extends page<t>> t login(final user user, final class<t> expectedpage) { enterusername(user.getusername()); enterpassword(user.getpassword()); loginbutton.click(); return page.constructpage(getdriver(), gettimeoutinseconds(), expectedpage); } }
usually compiler infer method generic type (p
in case) method signature (loadcontext
in case). loadcontext
method doesn't use p
on parameters compiler infer p
.
instead forcing compiler infer type p
within method. want make believe pagetypeone
. not working this.
imagine specified generic parameter on method call:
securedpageinstance.<somecompliantclasswithpbounds>loadcontext(...)
somecompliantclasswithpbounds
might different pagetypeone
.
your second example works because have class<t>
on login
method signature. compiler knows t
, sure return type same.
Comments
Post a Comment