简体   繁体   English

与给定方法签名不兼容的泛型类型?

[英]Generic Types incompatible with given method signature?

Given the following method: 给出以下方法:

public <E> void bindContentBidirectional(final String fieldPath,
        final String itemFieldPath, final Class<?> itemFieldPathType,
        final ObservableList<E> list, final Class<E> listValueType,
        final SelectionModel<E> selectionModel,
        final String selectionModelItemMasterPath)

Am I correct in my understanding that, if I have an ObservableList<PrintablePredicate<SomeClass>> as the type of the: 我是否理解正确,如果我将ObservableList<PrintablePredicate<SomeClass>>作为其类型:

final ObservableList<E> list

(that is, E = PrintablePredicate<SomeClass> that this can never work because for the next argument: (即E = PrintablePredicate<SomeClass> ,由于下一个参数,它永远无法工作:

final Class<E> listValueType) 

I can only write PrintablePredicate.class and not PrintablePredicate.class as generic types are not reified. 我只能写PrintablePredicate.class,而不能写PrintablePredicate.class,因为泛型类型没有得到验证。 In other words, the given method signature for bindContentBidirectional is incompatible with all E such that E has generic type arguments. 换句话说,bindContentBidirectional的给定方法签名与所有E不兼容,因此E具有泛型类型参数。

Putting it all together in a concrete code scenario, say we have: 将它们放到一个具体的代码场景中,说我们有:

@FXML private CheckListView<PrintablePredicate<Miner>> profileConditions;      
private MinerMonitorProfile minerMonitorProfile;

private void initialize() {
    BeanPathAdapter<MinerMonitorProfile> minerMonitorProfileBPA = new BeanPathAdapter<>    (this.minerMonitorProfile);
    minerMonitorProfileBPA.bindContentBidirectional("conditions", null, String.class, this.profileConditions.getItems(), PrintablePredicate.class, null, null);
}

the compiler says: 编译器说:

The method bindContentBidirectional(String, String, Class<?>, ObservableList<E>, Class<E>, SelectionModel<E>, String) in the type BeanPathAdapter<MinerMonitorProfile> is not applicable for the arguments ( String , null , Class<String> , ObservableList<PrintablePredicate<Miner>> , Class<PrintablePredicate> , null , null ) BeanPathAdapter<MinerMonitorProfile>类型的方法bindContentBidirectional(String, String, Class<?>, ObservableList<E>, Class<E>, SelectionModel<E>, String)不适用于参数( StringnullClass<String>ObservableList<PrintablePredicate<Miner>>Class<PrintablePredicate>nullnull

Is there anyway around this? 有没有办法解决? Thanks! 谢谢!

Note: this.profileConditions.getItems() returns type: ObservableList<PrintablePredicate<Miner>> 注意: this.profileConditions.getItems()返回类型: ObservableList<PrintablePredicate<Miner>>

Further note that parameterizing the method call as in: 还要注意,参数化方法调用的方式如下:

minerMonitorProfileBPA.<PrintablePredicate<Miner>>bindContentBidirectional("conditions", null, String.class, this.profileConditions.getItems(), PrintablePredicate.class, null, null);

does not alleviate the issue. 不能缓解问题。

When I have that kind of issues (casting a plain class to a parameterized one?) I use double casting, in order to bypass the compiler complain. 当我遇到此类问题(将普通类投射到参数化类吗?)时,我会使用双重强制转换,以绕过编译器抱怨。

So in your case I guess you could write 所以我想你可以写

(Class<PrintablePredicate<Miner>>)(Class<?>) PrintablePredicate.class

when passing the PrintablePredicate.class parameter to the bindContentBidirectional function. PrintablePredicate.class参数传递给bindContentBidirectional函数时。


Edit: I found out that simply casting PrintablePredicate.class to Class (I don't undestand why though, it seems kinda unnecessary on my opinion since *.class are already of type Class) or assigning it to a variable before passing it to the method works for this case (using javac 1.8.0_25). 编辑:我发现只是将PrintablePredicate.class强制转换为Class(我不明白为什么会这样,因为* .class已经是Class类型,所以我认为这似乎是不必要的)或在将其传递给变量之前将其分配给变量方法适用于这种情况(使用javac 1.8.0_25)。 Therefor maybe you should just use this instead of the code above: 因此,也许您应该只使用它而不是上面的代码:

(Class)PrintablePredicate.class

So your error might be due to a compiler bug similar to this one from OpenJDK (yeah, which doesn't make sense because it's a bug): https://bugs.openjdk.java.net/browse/JDK-8028682 因此,您的错误可能是由于类似于OpenJDK的编译器错误引起的(是的,因为它是一个错误,所以没有任何意义): https : //bugs.openjdk.java.net/browse/JDK-8028682


As a summary then after my findings, this "double casting" trick stuff (or just assigning an object to a variable) can help you "make it easier" for the compiler to "understand" your code when it has a bug such as this one seems (lol, it's kind of funny having to do that I guess, but it just shows you that sometimes you cannot even trust the compiler). 作为总结,然后根据我的发现,这种“双重转换”技巧(或只是将一个对象分配给变量)可以帮助您“轻松”使编译器在有此类错误时“理解”您的代码一个似乎(大笑,我猜这很有趣,但这只是向您表明有时候您甚至不能信任编译器)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM