简体   繁体   English

通用问题:T扩展接口类型。 无法调用方法返回扩展类型的对象?

[英]Generic issues: T extends interface type. Cannot call method to return an object which extends the type?

I have this method declared in a parent class: 我在父类中声明了此方法:

protected <ConfigT extends LoadableConfig> ConfigT getConfig(final String configId) {
    return (ConfigT)getConfigService().getConfig(configId, getDriver());
}

The configuration service method is defined as follows: 配置服务方法定义如下:

@SuppressWarnings("unchecked")
@Override
public <ConfigT extends LoadableConfig> ConfigT getConfig(String configId, WebDriver driver) {

    //noinspection unchecked
    Map<String,LoadableConfig> profile = profiles.get(getProfileName(driver));

    if(profile != null) {
        return (ConfigT) profile.get(configId);
    }

    return null;
}

The configuration type I want is here: 我想要的配置类型在这里:

public interface AccessibleConfig extends LoadableConfig, PolleableConfig {
 ....
}

This line of code is throwing an incompatible type error: 这行代码引发了不兼容的类型错误:

 AccessibleConfig config = getConfig(ValidationPane.class.getCanonicalName());

How is this possible? 这怎么可能? The AccessibleConfig extends LoadableConfig . AccessibleConfig扩展了LoadableConfig The method called declared that that return type is some type which extends LoadableConfig . 称为的方法声明该返回类型是扩展LoadableConfig某种类型。 I am running jdk1.8.0_102.jdk. 我正在运行jdk1.8.0_102.jdk。 This failure happens in IntelliJ as well as when compiling with Maven from the command line. 在IntelliJ中以及从命令行使用Maven进行编译时都会发生此故障。 I am able to do this with other configuration types which extends the LoadableConfig type. 我可以使用扩展了LoadableConfig类型的其他配置类型来执行此操作。

EDIT: 编辑:

Parent class: 家长班:

public abstract class AbstractLoadable<T extends AbstractLoadable<T>> {

    private Map<String,LoadableConfig> profiles = new HashMap<>();

    protected <T extends LoadableConfig> T getConfig(final String configId) {
        return (T) profiles.get(configId);
    }
}

Concrete class: 具体课程:

public class ConcreteLoadable extends AbstractLoadable {

    public ConcreteLoadable(final String profileName) {

        AccessibleConfig config = getConfig(ConcreteLoadable.class.getCanonicalName());
    }

}

And the interface types: 以及接口类型:

public interface LoadableConfig {
    Integer getLoadTimeoutInSeconds();
    void setLoadTimeoutInSeconds(final Integer loadTimeoutInSeconds);
}

public interface AccessibleConfig extends LoadableConfig {
    Boolean getHoverOverAccessorWithJavascript();
    void setHoverOverAccessorWithJavascript(final Boolean hoverOverAccessorWithJavascript);
    Boolean getClickAccessorWithJavascript();
    void setClickAccessorWithJavascript(final Boolean clickAccessorWithJavascript);
}

So, the exercise of producing this minimal example actually made it really easy to identify the source of the compiler error. 因此,产生此最小示例的练习实际上使识别编译器错误的根源变得非常容易。 I have posted an answer to this question below. 我在下面发布了这个问题的答案。 I humbly accept that I did not in fact originally post a complete example. 我谦卑地接受我实际上并没有最初发表一个完整的例子。

I have identified the problem. 我发现了问题。 The concrete class needs to pass itself to the parent because the parent is reflexively generic. 具体类需要将自身传递给父类,因为父类是自反通用的。 The error message wasn't super helpful so I got tied up in thinking about the generic parameters of the parent class method and not of the generic parameters applied to the parent class itself. 该错误消息并不是超级有用,因此我被束缚在考虑父类方法的通用参数而不是应用于父类本身的通用参数上。

The class declaration for ConcreteLoadable needs to changed to this to get rid of the compile error: 需要将ConcreteLoadable的类声明更改为此,以摆脱编译错误:

public class ConcreteLoadable extends AbstractLoadable<ConcreteLoadable> {

    public ConcreteLoadable(final String profileName) {

        AccessibleConfig config = getConfig(ConcreteLoadable.class.getCanonicalName());
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java如何返回扩展接口的泛型类型 - Java how to return a generic type that extends an interface 具有扩展 Iterable 类型的通用方法不起作用 - Generic method with type which extends Iterable doesn't work 带有泛型的mock方法,并以返回类型扩展 - mock method with generic and extends in return type 为什么编译器不对扩展接口的泛型强制执行返回类型值? - Why does the compiler not enforce the return type value for a generic that extends an interface? 模拟具有泛型(?extends Collection)返回类型的方法存在问题 - Having an issue with mocking a method which has a generic (? extends Collection) return type Java返回扩展接口的泛型类型 - Java returning generic type that extends an interface 如何返回扩展某些东西的泛型类型 T 的列表 - How to return a list of generic type T that extends something 通用方法定义包含 <T extends Class> 尽管返回类型已经定义? - Generic Method Definition contains <T extends Class> in-spite of return type is already Defined? 如何覆盖Iterable <interface> 将类型方法转换为Iterable <? extends interface> 返回类型方法 - How do I Override a Iterable<interface> return type method into Iterable<? extends interface> return type method 在通用类型“ T”的对象和通用类型“ <T extends Comparable<T> &gt;” - Casting between an object of generic type “T” and an object of generic type “<T extends Comparable<T>>”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM