GWT生成器和Java注释处理器之间的区别? [英] Difference between a GWT Generator and a Java Annotation Processor?

查看:93
本文介绍了GWT生成器和Java注释处理器之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GWT使用Generator创建代码,然后将所有内容都转换为JavaScript。另一方面,Java有一个注释处理器,它在所有内容都被转换为字节代码之前生成代码。

GWT生成器和Java注释处理器之间有什么区别?



生成器和处理器的调用方式也非常不同。对于每个置换,GWT为每个需要处理的类型实例化并运行一个生成器(这源于上述差异:生成器告诉GWT实际实例化给定类型的类作为输入),而注释处理器每次实例化一次编译,然后为每个处理循环重复调用,每个循环处理一组类型。



GWT支持 / em>,其中生成的类可以根据绑定属性值来实现延迟绑定(GWT生成器只是延迟绑定的一个方面)。请注意,GWT生成器实际上没有生成任何东西,它们也可以只选择一个现有的类。您只能通过生成代码来在运行时选择给定的实现来近似此注释处理程序。

换言之,如果为语言环境生成特定实现 en-US fr-FR ;使用GWT生成器,当生成器被调用时,它知道该排列是关于哪个区域,因此它可以告诉GWT使用该特定实现。最终结果就好像代码中的 GWT.create()被替换为 new TheGeneratedClass(),并且该类可以根据排列而不同(例如 MyMessagesImpl_en_US MyMessagesImpl_fr_FR )。使用注释处理器,您必须生成两个类,然后动态地(在运行时)选择它们,具体取决于上下文(您可以生成一个工厂来帮助完成此操作,但是您仍然必须以某种方式为工厂与当前的 context ,例如当前的locale)。



最后,你触发它们的方式是不同的。 GWT的延期绑定只能由代码中的 GWT.create()触发,即在写作时尚未确定的实例类;而注释处理器仅仅是通过元素(包,类型,字段,方法,参数,甚至Java 8中的类型参数或使用类型的任何位置)上的注释来触发的。 p>

GWT生成器和注释处理器是不同的东西,它们有不同的目标。在许多情况下,您可以几乎可以互换地使用其中一种(例如,AutoBeans,RequestFactory,编辑器框架,GWT中的PlaceHistoryMappers可以使用注释处理器完成),但并非总是如此。


GWT uses Generator to create code before everything is translated into JavaScript.

Java on the other hand has an annotation processor which generates code before everything is translated into byte code.

What is the difference between a GWT Generator and a Java Annotation Processor?

解决方案

GWT generators not only generate code, they also tell GWT to use it; whereas annotation processors only generate code that then needs to be used by some other code (either referenced directly in the code, or loaded through reflection – something that's not possible in a GWT environment though).

The way generators and processors are invoked is very different too. GWT instantiates and runs a generator for each type that needs processing, for each permutation (this stems from the above difference: the generator tells GWT which class to actually instantiate for the given type passed as input), whereas an annotation processor is instantiated once per compilation and then invoked repeatedly for each processing round, where each round processes a set of types.

GWT has support for permutations, where the generated class can be different depending on the binding properties' values to achieve deferred binding (GWT generators are just one facet of deferred binding). Note that GWT generators don't actually have to generate anything, they can also just select an existing class. You can only approximate this with annotation processors by generating code that will select the given implementation at runtime.
Put differently, if you generate a specific implementation for locales en-US and fr-FR; with a GWT generator, when the generator is invoked, it knows which locale the permutation is about, so it can tell GWT to use that particular implementation. The net result is as if the GWT.create() in the code was replaced with a new TheGeneratedClass(), and that class can be different depending on the permutation (e.g. MyMessagesImpl_en_US vs. MyMessagesImpl_fr_FR). With an annotation processor, you'd have to generate both classes and then dynamically (at runtime) select among them, depending on the context (you could generate a factory to help doing that, but you'd still have to somehow feed the factory with the current context, e.g. the current locale).

Finally, the way you trigger them is different. GWT's deferred binding is only triggered by GWT.create() in the code, i.e. instantiation of a yet-to-be-determined-at-the-time-of-writing class; whereas annotation processors are triggered by the mere presence of an annotation on an element (a package, a type, a field, a method, a parameter, or even in Java 8 a type-parameter or anywhere a type is used).

GWT generators and annotation processors are different things, made with different goals. In many cases you could use one or the other almost interchangeably (e.g. AutoBeans, RequestFactory, the Editor framework, PlaceHistoryMappers in GWT could be done using annotation processors), but not always.

这篇关于GWT生成器和Java注释处理器之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆