6

更准确地说,我想知道如何将注释建模到 ecore 模型定义中。这样生成的 java 代码就会包含它们。(例如:休眠持久化标签)

4

2 回答 2

1

EMF 论坛上的这篇文章讨论了如何使用自定义模板进行代码生成:https ://www.eclipse.org/forums/index.php/t/131673/ 。

简而言之,您可以为代码生成动态提供不同的模板,从而可以插入所需的注释。在论坛帖子中,Ed Merks(EMF 负责人)建议阅读两条信息:

以及如何使用它们的一个小例子:

插入看起来像这样:

<%@ include file="Class/getGenFeature.annotations.insert.javajetinc" fail="silent" %>

因此,在您的模板文件夹下,您将创建如下文件:

<someproject>/templates/model/Class/getGenFeature.annotations.insert.java jetinc

并且您放入文件中的任何内容都将插入到 getter 上。可能你会包括这样的警卫:

<%if (isImplementation) {%>
@Something
<%}%>

尝试遵循使用制表符进行缩进的约定,因为这些将转换为目标项目的格式首选项。

一旦您可以提供自己的模板,您就有两种选择:

  1. 默认情况下将休眠标签添加到所有代码中
  2. 修改模板以读取 ecore 模型中的注释。

对于 2,您需要定义自己的注释源(基本上是一个 url),https://myproject/emf/hibernate然后将 EAnnotations 添加到使用您的自定义 url 并提供key:value设置的 EClasses 中(例如要添加的休眠注释)。然后,您的自定义模板可以从 EClass 读取注释,查询是否使用了您的源,然后使用提供的值添加 Java 注释。

该帖子还提到了Teneo项目,该项目为 EMF 提供 JPA 支持。最近没有进行任何开发(显然),但它可以足够成熟以供使用。

于 2020-06-11T09:19:50.060 回答
0

I don't think you can to this out of the box. However, you could look into the parameters of the ".genmodel" file to see if you can tweak how annotations (EAnnotations) are being output to the files. The problem with code generation templates is that they are fixed, but maybe through some option in the genmodel you can control how annotations get written to files.

于 2010-10-29T12:46:52.807 回答