添加程序注释Java类 [英] Adding programmatic annotations to a Java class
问题描述
用法示例:搜索
我想要把类字段的自定义注释@MyContainer,然后在所有这些领域相关的Hibernate注解(取决于字段类型和属性)自动添加。结果
在另外我需要JAXB XmlType将注释添加到类和立足于类名的类型名称。结果
我想额外地添加注解基于thier类型等领域
所有添加的注释应该可以在运行时(因为Hibernate / JAXB可以找到它们)。结果
我知道以下选项:
Usage example:
I want to put on class fields a custom annotation @MyContainer and then add automatically on all such fields relevant Hibernate annotations (depending on field type and properties).
In additional I need to add JAXB XmlType annotation to the class and base the type name on the class name.
I would want additionally to add annotations to fields based on thier types, etc.
All added annotations should be available at run time (So hibernate / JAXB can find them).
I'm aware of the following options:
- pre-加工类源(坏的选择)
- 与javax.annotation.processing中的API在编译期间处理
- 后编译操纵辅助
- 类加载与java.lang.instrument中的API操作过程中
- 使用AspectJ(没有足够强大)做这件事
使用工具如Java
我的主要目标是:
- 保留类和源极之间的同步进行调试
- 支持来自Maven和IDE(Eclipse中/的IntelliJ)工作
我将AP preciate如果谁已经做了这样的事情的人可以推荐这样一个任务的最佳方法(也许是潜在的陷阱)。
I'll appreciate if people who already done such things can recommend the best approach for such a task (and perhaps potential pitfalls).
推荐答案
下面是定义定制标注一个code例子。这@TesterInfo被应用在一流水平,存储测试的详细信息。这说明不同的使用返回类型 - 枚举,数组和字符串
Here is a code example for defining custom annotation. This @TesterInfo is applied on class level, store the tester details. This shows the different use of return types – enum, array and string.
package com.mkyong.test.core;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) //on class level
public @interface TesterInfo {
public enum Priority {
LOW, MEDIUM, HIGH
}
Priority priority() default Priority.MEDIUM;
String[] tags() default "";
String createdBy() default "Mkyong";
String lastModified() default "03/01/2014";
}
这篇关于添加程序注释Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!