那么, 最后我得到了一个解决方案,但仍然卡住在一个点。 与apt的提示是成功的。
我设法创建了一个AnnotationProcessor,它以String的形式生成一个方面。这是问题。创建一个新的File对象并将其粘贴到其中以创建每个注释类的方面文件是不好的? 那是我目前可以成像的唯一方法。
感谢
马丁
解决方案:
我创造了我的创建方面的AnnotationProcessor(JDK1.6)。 generateAspect方法在每个方面的默认源输出文件夹中创建一个文件。
@SupportedAnnotationTypes({ "my.own.annotation.GenerateDTOConstants" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class DTOConstantAnnotationProcessor extends AbstractProcessor {
private static final Logger LOG = LoggerFactory
.getLogger(DTOConstantAnnotationProcessor.class);
private static final String ASPECT_POSTFIX = ".aj";
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
DTOConstantElementVisitor visitor = new DTOConstantElementVisitor();
for(TypeElement element : annotations) {
Set<? extends Element> annotatedClasses = roundEnv.getElementsAnnotatedWith(element);
for(Element dto : annotatedClasses) {
generateAspect(visitor, dto);
}
}
return true;
}
/**
* @param visitor
* @param dto
*/
private void generateAspect(DTOConstantElementVisitor visitor, Element dto) {
dto.accept(visitor, null);
LOG.info("Generating aspect for " + dto.getSimpleName());
Filer filer = this.processingEnv.getFiler();
try {
String fileName = visitor.getFileName() + ASPECT_POSTFIX;
String pkg = visitor.getPkg();
FileObject aspectFile = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg,
fileName);
Writer writer = aspectFile.openWriter();
LOG.info("writing aspect content into file");
writer.write(visitor.getFileContent());
writer.close();
LOG.info("Aspect generated for " + visitor.getFileName());
}
catch(IOException e) {
e.printStackTrace();
throw new java.lang.RuntimeException(e);
}
}
}
答案这里是我使用的访问者(只是一个片段):
public class DTOConstantElementVisitor extends AbstractElementVisitor6<Void, String> {
private static final String FIELD_PREFIX = "public static final String ";
private String fileName = null;
private String clazzName;
private String pkg;
private StringBuffer fileContentBuff;
@Override
public Void visitPackage(PackageElement e, String p) {
System.out.println("visitPackage" + e);
return null;
}
@Override
public Void visitType(TypeElement e, String p) {
System.out.println("visitTypeElement" + e);
try {
Class<?> clazz = Class.forName(e.getQualifiedName().toString());
this.clazzName = clazz.getSimpleName();
createFileName(clazz);
this.pkg = clazz.getPackage().getName();
this.fileContentBuff = new StringBuffer();
fileContentBuff.append("package " + this.pkg + ";\n");
fileContentBuff.append("public aspect " + this.fileName + " {\n");
for(Field field : clazz.getDeclaredFields()) {
if(Modifier.isPrivate(field.getModifiers())) {
String fieldName = field.getName();
if(shouldGenerateField(fieldName)) {
fileContentBuff.append(FIELD_PREFIX + clazzName + "."
+ fieldName.toUpperCase() + " = \"" + fieldName + "\";\n");
}
}
}
fileContentBuff.append("}\n");
System.out.println(fileContentBuff.toString());
}
catch(ClassNotFoundException e1) {
throw new java.lang.RuntimeException(e1);
}
return null;
}
private boolean shouldGenerateField(String fieldName) {
if("serialVersionUID".equals(fieldName)) {
return false;
}
return true;
}
private void createFileName(Class clazz) {
this.fileName = clazzName + "Aspect";
}
}
Additonally你必须建立在
META-INF/services
配置文件名为
javax.annotation.processing.Processor
包含包和n在AnnotationProcessor
my.package.annotation.processor.DTOConstantAnnotationProcessor
最后,包括在Maven构建过程的AME:
<build>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>aspectprocessing</id>
<phase>compile</phase>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
为奋斗目标,单呼使用
mvn processor:process
这就是所有=)
,那岂不是更好地你的getter和setter移入方面呢? – 2011-03-23 19:57:23
好吧,没有。我们在后端代码中使用getter和setter。因此它更容易直接在DTO中。防止编译错误=) – martin 2011-03-30 07:18:25