生成常量类与Maven属性? [英] Generate constants for class attributes with maven?

查看:180
本文介绍了生成常量类与Maven属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小code产生问题。
我有一个EJB3后端供应DTO对象至前端。前端使用可配置的绑定过程的DTO的属性映射到它们的形式。
目前,他们被specifing属性作为疗法配置字符串做。结合实现使用反射来存取权限的属性。
听起来不错,但问题是,每次我们在DTO更改属性的名字的时候,因为他们刚刚字符串这不会导致前端编译错误。

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.

我现在正在寻找一种方式来创建一个字符串常量可以由前端使用,以便他们得到的编译错误,如果我做的DTO属性更改属性映射到其表单类的每个属性

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.

示例是怎么一回事:

public class CarDTO {

    private String vendor;
    private String name;


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

以及应如何为:

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() {}
    [..]    
}

我一直在寻找一个Maven插件,它能够这一点,但没有成功。有没有谁NOW的一个工具,它可以做这样的事情,任何一个?

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?

在此先感谢

马丁

推荐答案

修改现有的类是比较困难的,然后创建新的。

Modifying existing class is more difficult then creating new one.

JPA采取了有趣的方法来解决通过创建这个问题CarDTO _ 类。请参见 http://www.hibernate.org/subprojects/jpamodelgen.html 了解更多详情。这种方法容易得多。你可以看一下实现了code一代休眠Maven插件。

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.

如果你真的要修改现有的类,然后我会建议使用AspectJ类似Spring Roo的,那里的方面包含生成code的方法。

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.

编辑(使用AspectJ例)

Edited (Example using AspectJ)

在此情况下,我们使用AspectJ的 inter-type声明的,使您能够修改现有的类。

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. 插件生成 CarAspect

  2. 的AspectJ的Maven的插件来编译(编织)方面

此外,Eclipse有切面,所以你可以在那里使用它也很好的支持。

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

这篇关于生成常量类与Maven属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆