1

给定以下方法:

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)

我的理解是否正确,如果我有ObservableList<PrintablePredicate<SomeClass>>以下类型:

final ObservableList<E> list

(也就是说,E = PrintablePredicate<SomeClass>这永远不会起作用,因为对于下一个参数:

final Class<E> listValueType) 

我只能写 PrintablePredicate.class 而不是 PrintablePredicate.class 因为泛型类型没有被具体化。换句话说,bindContentBidirectional 的给定方法签名与所有 E 不兼容,因此 E 具有泛型类型参数。

将它们放在一个具体的代码场景中,假设我们有:

@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);
}

编译器说:

bindContentBidirectional(String, String, Class<?>, ObservableList<E>, Class<E>, SelectionModel<E>, String)类型中的方法BeanPathAdapter<MinerMonitorProfile>不适用于参数(String, null, Class<String>, ObservableList<PrintablePredicate<Miner>>, Class<PrintablePredicate>, null, null

有没有办法解决?谢谢!

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

进一步注意,参数化方法调用如下:

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

不能缓解问题。

4

1 回答 1

1

当我遇到这类问题(将普通类转换为参数化类?)时,我使用双重转换,以绕过编译器抱怨。

所以在你的情况下,我想你可以写

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

PrintablePredicate.class参数传递给 bindContentBidirectional 函数时。


编辑:我发现只是将 PrintablePredicate.class 转换为 Class (我不明白为什么,我认为这似乎有点不必要,因为 *.class 已经是 Class 类型)或者在将其传递给变量之前将其分配给方法适用于这种情况(使用 javac 1.8.0_25)。因此,也许您应该只使用它而不是上面的代码:

(Class)PrintablePredicate.class

因此,您的错误可能是由于类似于来自 OpenJDK 的编译器错误(是的,这没有意义,因为它是一个错误):https ://bugs.openjdk.java.net/browse/JDK-8028682


作为一个总结,然后在我的发现之后,这种“双重转换”技巧(或只是将一个对象分配给一个变量)可以帮助您“更容易”让编译器在它有这样的错误时“理解”你的代码一个似乎(大声笑,我想这样做很有趣,但这只是向您表明,有时您甚至不能信任编译器)。

于 2014-11-29T21:40:47.787 回答