开发者

Generate constants for class attributes with maven?

i have a small code generation question. I have a EJB3 backend that serves DTO objects to a frontend. The frontend uses a configurable binding procedure to map the attributes of the DTO to their forms. At the moment they are doing it by specifing the attributes as strings in ther configuration. The binding implementation uses reflection to acces the attributes. Sounds nice but the problem is, each time we change an attribute name in a DTO, this will not lead to a compile error in the frontend because they have just strings.

I'm now looking for a way to create a string constant for each attribute of the class that can be used by the frontend to map the attributes to their forms so that they get compile errors if i made changes in the dto attributes.

Example how it is:

public class CarDTO {

    private String vendor;
    private String name;


    public String getVendor() {}
    public String getName() {}
    [..]    
}

And how it should be:

public class CarDTO {

    public static final String VENDOR = "vendor";
    public static final String NAME = "name";

    private String vendor;
    private String name;


    public String getVendor() {}
    public String getName() {}
    [..]    
}

I was looking for a maven plugin t开发者_开发知识库hat is capable of this but without success. Is there any one who nows a tool which can do things like that?

Thanks in advance

martin


Modifying existing class is more difficult then creating new one.

JPA took a interesting approach to solve this problem by creating CarDTO_ class. See http://www.hibernate.org/subprojects/jpamodelgen.html for more details. This approach is much easier. You can look at the hibernate maven plugin that implement the code generation.

If you really want to modify the existing class, then I would recommend using AspectJ with an approach similar to Spring Roo, where the aspect contains the generated code.

Edited (Example using AspectJ)

In this case, we are using AspectJ inter-type declarations that enables you to modify an existing class.

aspect CarAspect 
{
     public static final String CarDTO.VENDOR = "vendor";
     public static final String CarDTO.NAME = "name";
}

Do implement this in maven, you need

  1. a plugin to generate the CarAspect
  2. the aspectj-maven-plugin to compile (weave) the aspect

Also, Eclipse has good support for AspectJ so you can use it there too.


I have a app with a similar frontend approach to access the domain classes, but I have my domain is entirely created via a DSL implemented via Eclipse Xtext, who can be used in a maven build also. There is a sample Java domain DSL project in the xtext distribution, its easy to start from there.

This sure is not a fast "just use a maven plugin" solution but once you get into Xtext it will pay off, especially if you have a lot domain classes, or a lot similar projects.

From my domain DSL I create via code templates and a xtext generator project three classes:

  • target/generated/mydsl (generated always):
    • AbstractDomainClass (in this file i have my static string's)
  • src/main/java (generated once):
    • ConcreteDomainClass
  • src/test/java (generated once):
    • ConcreteDomainClassTest

In the abstract domain class i have all getters and setters and simple persistence stuff in it, in the concrete domain class is the more complex stuff and the test class stands for it self.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜