Java generic interface implementation: Compiler complains that one method does not override interface method, but is okay with the other methods

家住魔仙堡 提交于 2019-12-24 18:08:13

问题


I have a builder interface:

@ParametersAreNonnullByDefault
public interface ILoadableBuilder<C extends ILoadable,T extends ILoadableBean> {
    public @Nonnull T getState();
    public @Nonnull <B extends ILoadableBuilder<C,T>> B setComponentClass(final Class<C> componentClass);
    public @Nonnull <B extends ILoadableBuilder<C,T>> B setDriver(final WebDriver driver);
    public @Nonnull <B extends ILoadableBuilder<C,T>> B setLoadTimeoutInSeconds(final @Nonnegative int loadTimeoutInSeconds);
    public @Nonnull <B extends ILoadableBuilder<C,T>> B setEnumerator(final IEnumeratorBean<? extends IEnumerable<?>,?> enumerator);
}

and this implementation:

@ParametersAreNonnullByDefault
public class LoadableBuilder<C extends ILoadable,T extends ILoadableBean,B extends ILoadableBuilder<C,T>> implements
        ILoadableBuilder<C,T> {   

    private final @Nonnull T componentBean;
    private @Nullable IEnumeratorBean<? extends IEnumerable<?>,?> enumerator;
    private @Nullable Class<C> componentClass;

    public LoadableBuilder(final T componentBean) {
        this.componentBean = componentBean;
    }

    @Override
    public final @Nonnull T getState() {
        return componentBean;
    }

    @SuppressWarnings("unchecked")
    @Override
    public final @Nonnull B setComponentClass(final Class<C> componentClass) {
        this.componentClass = componentClass;
        return (B)this;
    }

    @SuppressWarnings("unchecked")
    @Override
    public final @Nonnull B setDriver(final WebDriver driver) {
        getState().setDriver(driver);
        return (B)this;
    }

    @SuppressWarnings("unchecked")
    @Override
    public final @Nonnull B setLoadTimeoutInSeconds(final @Nonnegative int loadTimeoutInSeconds) {
        getState().getLoadTimeoutInSeconds();
        return (B)this;
    }

    @SuppressWarnings("unchecked")
    @Override
    public @Nonnull B setEnumerator(final IEnumeratorBean<? extends IEnumerable<?>,?> enumerator) {
        this.enumerator = enumerator;
        return (B)this;
    }

    public @Nullable Class<C> getComponentClass() {
        return componentClass;
    }
}

The compiler complains about the setComponentClass method and the setEnumeratorMethod in the implementation class -- It says that they do not override the methods in the interface, but it does not complain about any of the other methods which are implemented in the same way with the same return value. Why are these two methods a problem for the compiler but the others are not?

Compiler errors:

Error:(16, 8) java: com.someplace.api.ui.LoadableBuilder is not abstract and does not override abstract method <B>setEnumerator(com.someplace.api.ui.IEnumeratorBean<? extends com.someplace.api.dsl.IEnumerable<?>,?>) in com.someplace.api.ui.ILoadableBuilder
Error:(47, 29) java: name clash: setComponentClass(java.lang.Class<C>) in com.someplace.api.ui.LoadableBuilder and <B>setComponentClass(java.lang.Class<C>) in com.someplace.api.ui.ILoadableBuilder have the same erasure, yet neither overrides the other
Error:(46, 5) java: method does not override or implement a method from a supertype
Error:(68, 23) java: name clash: setEnumerator(com.someplace.api.ui.IEnumeratorBean<? extends com.someplace.api.dsl.IEnumerable<?>,?>) in com.someplace.api.ui.LoadableBuilder and <B>setEnumerator(com.someplace.api.ui.IEnumeratorBean<? extends com.someplace.api.dsl.IEnumerable<?>,?>) in com.someplace.api.ui.ILoadableBuilder have the same erasure, yet neither overrides the other
Error:(67, 5) java: method does not override or implement a method from a supertype

来源:https://stackoverflow.com/questions/28662918/java-generic-interface-implementation-compiler-complains-that-one-method-does-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!