问题描述
绑定不匹配:通用方法constructPage(WebDriver,int,Class< T> )类型
Page< T>不适用于参数(WebDriver,int,Class< HomePage>)。
推断的类型主页不是有界参数的有效替代< T extends
Page< T>>
我试图做一个登录并返回一个主页,如果成功的话,如果不使用泛型的LoginPage 。
我有一个基类,页面由SecuredPage为登录墙后的页面扩展。我写了一个通用的帮助器方法,可以构造任何类型的页面。此方法由LoginPage上的登录方法使用。 LoginPage扩展页面和HomePage扩展SecuredPage。 SecuredPage扩展页面。如果LoginPage返回,则登录方法有效,但我在尝试返回HomePage时遇到上述错误。由于HomePage是页面的子类,因为它的父类扩展了Page,所以我很困惑为什么HomePage不是有界替代的有界参数< T extends Page< T>>< / code>。
public abstract class Page< T extends Page< T>>延伸SlowLoadableComponent< T> {
protected static final< T extends Page< T>> T constructPage(WebDriver驱动程序,
int timeoutInSeconds,java.lang.Class< T> pageClass)
{
Page< T> p = null;
尝试{
构造函数< T> pageConstructor = pageClass.getConstructor(
WebDriver.class,String.class,Integer.TYPE);
p = pageConstructor.newInstance(driver,driver.getCurrentUrl(),
timeoutInSeconds);
p.get();
} catch(例外e){
}
返回pageClass.cast(p);
$ b $ p $这是SecuredPage类:public class SecuredPage扩展页面< SecuredPage> {
.....
}
是主页:
public final class HomePage extends SecuredPage {
......
}
这是LoginPage:
public final class LoginPage extends Page< LoginPage> {
public final HomePageWithGoodCredentials(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登录(最终用户用户,final类< T>
expectedPage){
enterUsername(user.getUsername());
enterPassword(user.getPassword());
loginButton.click();
返回Page.constructPage(getDriver(),getTimeoutInSeconds(),
expectedPage);
}
}
解决方案问题在于 HomePage 是页面< SecuredPage> ,而不是页面< HomePage> 。 login 方法将从其通用签名中返回页面< HomePage> 。
您必须使主页与其自身相关的通用参数,而不是 SecuredPage 。这将解决编译器错误。保持 SecuredPage 泛型,但要确保其绑定扩展 SecuredPage< T> 。然后在 HomePage 中为通用参数 T 分配 HomePage 。
class SecuredPage< T扩展SecuredPage< T>>扩展Page< T> {
...
}
class HomePage extends SecuredPage< HomePage> {
...
}
I am getting the following error:
Bound mismatch: The generic method constructPage(WebDriver, int, Class<T>) of type Page<T> is not applicable for the arguments (WebDriver, int, Class<HomePage>). The inferred type HomePage is not a valid substitute for the bounded parameter <T extends Page<T>>I am trying to do a login and return a HomePage, if successful and a LoginPage if not using generics.
I have a base class, Page which is extended by SecuredPage for pages behind the login wall. I wrote a generic helper method that would construct pages of any type. This method is used by the login method on the LoginPage. LoginPage extends Page and HomePage extends SecuredPage. SecuredPage extends Page. The login method works if the LoginPage is returned, but I get the above error trying to return a HomePage. Since HomePage is a subclass of page because its parent class extends Page, I am confused as to why HomePage is not a valid substitute for the bounded parameter <T extends Page<T>>.
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); } }This is the SecuredPage class:
public class SecuredPage extends Page<SecuredPage> { ..... }And this is HomePage:
public final class HomePage extends SecuredPage { ...... }This is LoginPage:
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); } }解决方案The problem is that HomePage is a Page<SecuredPage> and not a Page<HomePage>. The login method would return a Page<HomePage> from its generic signature.
You must make the generic parameter of HomePage related to itself, not SecuredPage. This will resolve the compiler error. Keep SecuredPage generic, but make sure its bound extends SecuredPage<T>. Then assign HomePage itself for the generic parameter T in HomePage.
class SecuredPage<T extends SecuredPage<T>> extends Page<T> { ... } class HomePage extends SecuredPage<HomePage> { ... }
这篇关于绑定不匹配错误和java通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!