2

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 that 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

martin
  • 980
  • 1
  • 13
  • 28

2 Answers2

0

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.

moritz
  • 5,094
  • 1
  • 26
  • 33
  • Thanks for the answer. The approach with XText and a DSL for defining DTO classes sounds nice but i don't think that my collegues are willing to change the entire definition of the dtos by a dsl =) – martin Mar 23 '11 at 07:46
0

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.

Nr9
  • 417
  • 3
  • 10
  • Hmm, the ApsectJ approach sound interesting. You mean to put some annotations to my dto classes and AspectJ will put the constants in the .class files of my DTOs? – martin Mar 22 '11 at 16:06
  • Sounds good. Thats exactly what i was looking for. Can you point a generator plugin for the CarAspect? Asked google but found nothing. – martin Mar 22 '11 at 16:49
  • For the generator plugin, there is nothing (that I know of) that will work out of the box. I can suggest that you look at the Hibernate Metamodel Generator (see link above) and instead of generating a java file you create a aspect file. The Metamodel Generator uses the Java 6 Annotation processing to inspect the code. It should not be that hard to do something similar. – Nr9 Mar 22 '11 at 17:16
  • Well, i think the HMG is not the tool of my choice because i do not want to create the aspect of an entity but of a DTO class. We have a sepparation of the entities and the API dto classes in the backend. The mapping for entity to DTO is done via Apache dozer. – martin Mar 23 '11 at 07:44
  • Yes, I understood that. Your plugin will need to inspect your DTO class to find the bean properties that need to be put into the aspect, and then generate the aspect files. My point is that the HMG implementation can be looked at to get a good idea on how to write your plugin (but not necessarily reusing any of the code). – Nr9 Mar 23 '11 at 13:57