简体   繁体   English

为什么GWT编辑器框架中的子编辑器必须是程序包私有的?

[英]Why do sub-editors in GWT's editor framework need to be package-private?

I've just tried out GWT's editor framework and got it working for a small example. 我刚刚试用了GWT的编辑器框架,并使其成为一个小例子。 But I was wondering why sub-editors need to be package-private? 但是我想知道为什么子编辑器必须是程序包私有的?

There is a comment in an example in the linked page that specifically mentions package-protected fields, but I cannot find the reason why. 在链接页面的一个示例中有一个注释,其中特别提到了受程序包保护的字段,但我找不到原因。

// Sub-editors are retrieved from package-protected fields, usually initialized with UiBinder.
// Many Editors have no interesting logic in them
public class PersonEditor extends Dialog implements Editor<Person> {
    Label nameEditor;
    AddressEditor addressEditor;
    ...
}

In the example I tried myself, I only got it working if my sub-editors are package-private, if i make them private, the binding does no longer work. 在我自己尝试的示例中,只有当我的子编辑器是私有程序包时,它才起作用;如果我将它们设为私有,则绑定不再起作用。

Can anyone explain to me why this restriction exists? 谁能向我解释为什么存在此限制? It makes my coding style appear a bit inconsistently. 这使我的编码样式显得有点不一致。 Thanks! 谢谢!

Similar to UiBinder, the generator for an EditorDriver generates classes along-side Editor classes. 与UiBinder相似, EditorDriver生成Editor类的EditorDriver生成类。 These classes need access to the editors to be able to work with them. 这些类需要访问编辑器才能使用它们。

Put differently, the editor framework won't modify you classes ( there's no magic ), so you have to somehow expose your sub-editors: package-private is enough, but public would of course work too. 换句话说,编辑器框架不会修改您的类( 没有魔术 ),因此您必须以某种方式公开您的子编辑器:package-private足够了,但是public当然也可以工作。
The best way to understand what's going on is to pass the -gen option (followed by a directory path) to the GWT compiler of dev mode, so that it outputs all the generated classes to disk. 了解正在发生的事情的最好方法是将-gen选项(后跟目录路径)传递给dev模式的GWT编译器,以便将所有生成的类输出到磁盘。 Be warned though: the editor framework is really hard to comprehend! 但是要警告:编辑器框架真的很难理解!

Also, if your editor extends some other class in another package that contains a sub-editor (that you want to inherit), that subeditor must be visible from the package of the child class, so it has to be public in the parent class, or be explicitly exposed by the child class (using an accessor method that's not private ). 另外,如果您的编辑器在另一个包含子编辑器(要继承)的包中扩展了其他类,则该子编辑器必须在子类的包中可见,因此它必须在父类中是public的,或由子类显式公开(使用不是private的访问器方法)。

GWT generates an editor delegate (and a context) for each editor that will be used by the driver. GWT为驱动程序将使用的每个编辑器生成一个编辑器委托(和上下文)。 This delegate (and context) require access to the subeditors they will manipulate as part of passing data in and out of the editors. 该委托(和上下文)需要访问子编辑器,这些子编辑器将作为将数据传入和传出编辑器的一部分进行操作。 As these are written in Java, and not using JSNI to access private fields, your editors must be accessible to other classes in the same package. 由于这些都是用Java编写的,并且不使用JSNI访问私有字段,因此同一程序包中的其他类必须可以访问您的编辑器。

That said, there are several options for you. 也就是说,有几种选择适合您。 The first is to make them explicitly public , or protected , which may or may not fit your code style. 首先是使它们显式为publicprotected ,它们可能适合您的代码样式,也可能不适合您的代码样式。 The second is to expose them via methods, with the method named the same as the property to be edited, optionally suffixed with 'Editor'. 第二种方法是通过方法公开它们,方法的名称与要编辑的属性相同,并且可以选择在其后缀“ Editor”。 See http://code.google.com/webtoolkit/doc/latest/DevGuideUiEditors.html#Editor_contract for more details on this. 有关更多详细信息,请参见http://code.google.com/webtoolkit/doc/latest/DevGuideUiEditors.html#Editor_contract

This restriction is the same as the uibinder restriction - the field must be accessible to read/write it from generated classes. 此限制与uibinder限制相同-必须访问该字段才能从生成的类读取/写入它。 If you just have a private Label nameEditor; 如果您只有private Label nameEditor; , then your IDE will likely complain at you that this is unused, as it can't see the code which hasnt been generated which could access this fields. ,那么您的IDE可能会向您抱怨它没有被使用,因为它看不到尚未生成的代码可以访问此字段。 Making it more public than private makes it clear that it will be used outside that single class. 使它比private更公开,可以清楚地表明它将在单个类之外使用。

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

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