<bdo id='lvYBt'></bdo><ul id='lvYBt'></ul>
  • <tfoot id='lvYBt'></tfoot>
  • <legend id='lvYBt'><style id='lvYBt'><dir id='lvYBt'><q id='lvYBt'></q></dir></style></legend>
  • <i id='lvYBt'><tr id='lvYBt'><dt id='lvYBt'><q id='lvYBt'><span id='lvYBt'><b id='lvYBt'><form id='lvYBt'><ins id='lvYBt'></ins><ul id='lvYBt'></ul><sub id='lvYBt'></sub></form><legend id='lvYBt'></legend><bdo id='lvYBt'><pre id='lvYBt'><center id='lvYBt'></center></pre></bdo></b><th id='lvYBt'></th></span></q></dt></tr></i><div id='lvYBt'><tfoot id='lvYBt'></tfoot><dl id='lvYBt'><fieldset id='lvYBt'></fieldset></dl></div>
    1. <small id='lvYBt'></small><noframes id='lvYBt'>

        java - 如何在Java中构建时使用注释动态生成代码

        时间:2023-09-27

      1. <tfoot id='zilBa'></tfoot>
        <i id='zilBa'><tr id='zilBa'><dt id='zilBa'><q id='zilBa'><span id='zilBa'><b id='zilBa'><form id='zilBa'><ins id='zilBa'></ins><ul id='zilBa'></ul><sub id='zilBa'></sub></form><legend id='zilBa'></legend><bdo id='zilBa'><pre id='zilBa'><center id='zilBa'></center></pre></bdo></b><th id='zilBa'></th></span></q></dt></tr></i><div id='zilBa'><tfoot id='zilBa'></tfoot><dl id='zilBa'><fieldset id='zilBa'></fieldset></dl></div>
      2. <legend id='zilBa'><style id='zilBa'><dir id='zilBa'><q id='zilBa'></q></dir></style></legend>

              <tbody id='zilBa'></tbody>
              <bdo id='zilBa'></bdo><ul id='zilBa'></ul>

              • <small id='zilBa'></small><noframes id='zilBa'>

                  本文介绍了java - 如何在Java中构建时使用注释动态生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在寻找生成代码的解决方案.我在 SO 和一些博客上搜索过、搜索过,但没有找到好的解决方案.

                  I'm looking for a solution for generating code. I have googled, searched on SO and some blogs but I didn't find a good solution.

                  我想在我的类上放一个注解,在编译时,一些方法和属性会自动添加到类中.

                  I'd like to put an annotation on my class and at compilation time, some methods and properties would be automatically added to the class.

                  我正在寻找的解决方案的关键点:

                  Key points of the solution I'm looking for :

                  • 生成的代码可自定义(强制)
                  • 无需调用像 apt 这样的外部工具(强制)
                  • 仅 JDK,无第三方框架(强制可选)
                  • 可自定义注释名称(可选)
                  • Generated code customizable (MANDATORY)
                  • No external tool like apt have to be called (MANDATORY)
                  • JDK only, no third-party framework (MANDATORY OPTIONAL)
                  • Annotation name customizable (OPTIONAL)

                  例如:

                  @Aliasable
                  public class MyClass {
                  //Some properties
                  
                  // Contructor ...
                  
                  // Some methods
                  }
                  

                  我的班级在编译后会是这样的:

                  My class would look like this after compilation :

                  public class MyClass {
                     //Some properties
                     private String alias;
                  
                     // Contructor ...
                  
                     // Some methods
                     public String getAlias() {
                        return alias;
                     }
                  
                     public void setAlias(String alias) {
                        this.alias=alias;
                     }
                  }
                  


                  最后,我把我的第三个需求从 MANDATORY 变成了 OPTIONAL 并选择了 project Lombok (与 Maven 和 Eclipse 轻松集成,几乎没有工作去做使用它).


                  Finally, I turned my third requirement from MANDATORY to OPTIONAL and choosed project Lombok (easy integration with Maven and Eclipse, virtually no work to do for using it).

                  推荐答案

                  看看 Project Lombok.它会在您编写时生成代码:

                  Have a look at Project Lombok. It generates code as you ask when you write:

                  public class MyClass {
                    @Getter @Setter private String alias;
                  }
                  

                  如果您需要它,它还可以做更多的事情.我知道您没有要求任何外部工具,但您基本上是在重新创建它.

                  It also does a lot more if you need it. I know you asked for no external tools, but you would basically be recreating this.

                  这篇关于java - 如何在Java中构建时使用注释动态生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Java 中使用 @inherited 注解? 下一篇:JSF 如何找到使用@ManagedBean 注释的bean?

                  相关文章

                  最新文章

                • <legend id='lKOdu'><style id='lKOdu'><dir id='lKOdu'><q id='lKOdu'></q></dir></style></legend>
                    <i id='lKOdu'><tr id='lKOdu'><dt id='lKOdu'><q id='lKOdu'><span id='lKOdu'><b id='lKOdu'><form id='lKOdu'><ins id='lKOdu'></ins><ul id='lKOdu'></ul><sub id='lKOdu'></sub></form><legend id='lKOdu'></legend><bdo id='lKOdu'><pre id='lKOdu'><center id='lKOdu'></center></pre></bdo></b><th id='lKOdu'></th></span></q></dt></tr></i><div id='lKOdu'><tfoot id='lKOdu'></tfoot><dl id='lKOdu'><fieldset id='lKOdu'></fieldset></dl></div>
                  1. <tfoot id='lKOdu'></tfoot>

                      • <bdo id='lKOdu'></bdo><ul id='lKOdu'></ul>

                      <small id='lKOdu'></small><noframes id='lKOdu'>