简体   繁体   English

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

[英]How to generate code dynamically with annotations at build time in Java?

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. 我用谷歌搜索,搜索SO和一些博客,但我找不到一个好的解决方案。

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 : 我正在寻找的解决方案的关键点:

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

For example : 例如 :

@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;
   }
}

EDIT: 编辑:
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). 最后,我将我的第三个要求从MANDATORY转为OPTIONAL并选择了项目Lombok (与Maven和Eclipse轻松集成,几乎没有任何工作可以使用它)。

The annotation processing tool has been integrated in javac since version 1.6 and is part of the JDK . 自1.6版以来, 注释处理工具已集成在javac中,并且是JDK的一部分 So there is no need for external tools when using the Pluggable Annotation API . 因此,使用Pluggable Annotation API时无需外部工具。 You can generate any code by analysing custom annotations or method/parameter/field/class declarations using the Mirror API . 您可以使用Mirror API分析自定义注释或方法/参数/字段/类声明来生成任何代码。

The annotation processor API says you shouldn't change existing classes. 注释处理器API表示您不应更改现有类。 Instead you should generate subclasses of existing classes and create extension methods on those subclasses. 相反,您应该生成现有类的子类并在这些子类上创建扩展方法。

It seems to be possible to change classes anyway (eg by using bytecode manipulation libraries) though that would in contrast to the specification and could lead to issues with other annotation processors and may not work with all compilers in the same way. 似乎有可能改变类(例如通过使用字节码操作库),尽管这与规范形成对比,并且可能导致其他注释处理器出现问题,并且可能无法以相同的方式与所有编译器一起使用。

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. 我知道你没有要求外部工具,但你基本上会重新创建它。

I use XML and XSLT to generate code. 我使用XML和XSLT来生成代码。 It is used for EJB, Logic and the CRUD part of the views. 它用于EJB,Logic和视图的CRUD部分。 It isnt gerated at runtime but instead on the buildserver. 它不是在运行时进行的,而是在构建服务器上。 Developers can generate the code manually for well development purposes. 开发人员可以手动生成代码以进行良好的开发。 This is done with the same command ANT uses on the buildserver. 这是通过ANT在buildserver上使用的相同命令完成的。

Because the generation is with XML and XSLT it is highly customizable. 因为生成是使用XML和XSLT,所以它是高度可定制的。

If you google Java code generation with XSLT you will run into alot of examples. 如果您Java code generation with XSLT您将遇到很多示例。 Please note that this technique dates from ~2000 and thus probably has been preceded by now by easier solutions. 请注意,这种技术可以追溯到〜2000年,因此现在可能已经提供了更简单的解决方案。

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

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