Bound mismatch error and java generic method

前端 未结 5 1506
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 00:32

I am getting the following error:

Bound mismatch: The generic method constructPage(WebDriver, int, Class) of type     
Page is not applicab         


        
相关标签:
5条回答
  • 2021-01-06 00:44

    If you still wish HomePage to extend SecuredPage with the presence of Bounded Generics, please pass the 'Generic Substitution' till HomePage.

    Do not substitute Generic at SecuredPage, instead make SecuredPage as

    public class  SecuredPage<T extends Page<T>> extends Page<T> {
    
    }
    

    and while creating HomePage, declare the value for the generic like this,

    public class HomePage extends SecuredPage<HomePage> {
    
    }
    

    This should essentially solve the error

    0 讨论(0)
  • 2021-01-06 00:52

    Calling login(user, SecuredPage.class) works, but login(user, HomePage.class) does not. The reason is: type parameter T in SecuredPage is SecuredPage. HomePage is a subclass of SecuredPage, so the T type parameter of HomePage is SecuredPage as well.

    Now, you call login with a Page<HomePage>. But such a class does not exist. HomePage is a subclass of Page<SecuredPage>.

    0 讨论(0)
  • 2021-01-06 00:52

    For the purposes of your constructPage method, you could just use

    protected static final <T extends Page<?>> T constructPage(...) 
    {
        Page<?> p = null;
        //...
    }
    
    0 讨论(0)
  • 2021-01-06 00:57

    If you any one still face the same error change JDK 1.8 or latest version in compiler level from Window > Preferences > Java > Compiler > "Compiler Compilation level" -> 1.8

    0 讨论(0)
  • 2021-01-06 01:01

    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>  {
    ...
    }
    
    0 讨论(0)
提交回复
热议问题