gpt4 book ai didi

eclipse-emf - 如何从电动势模型生成 'java code with annotations'

转载 作者:行者123 更新时间:2023-12-04 12:01:43 26 4
gpt4 key购买 nike

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

最佳答案

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

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

  • http://wiki.eclipse.org/index.php/EMF-FAQ#What_are_Dynamic_Templates.3F
  • http://wiki.eclipse.org/index.php/EMF-FAQ#How_do_I_use_Dynamic_Templates.3F

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

    The inserts look like this:


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

    so under your templates folder you'd create files like this:


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

    and whatever you put in the file will be inserted on the getter. Likely you'd include guards like this:


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

    Try to follow the convention of using tabs for the indentation since these will be converted to the formatting preference of the target project.



    一旦您可以提供自己的模板,您有两种选择:
  • 默认情况下将休眠标签添加到您的所有代码
  • 修改模板以读取 ecore 模型中的注释。

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

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

    关于eclipse-emf - 如何从电动势模型生成 'java code with annotations',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4033246/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com