Reputation: 136
i've written thi annotation
@Retention(RetentionPolicy.SOURCE)
public @interface Encrypt {
}
and its processor...
@SupportedAnnotationTypes("it.trecube.annotation.Encrypt")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class EncryptProcessor extends AbstractProcessor{
public EncryptProcessor(){
super();
}
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
String className = null;
String packageName = null;
String fqClassName = null;
for (Element elem : roundEnv.getElementsAnnotatedWith(Encrypt.class)) {
if (elem.getKind() == ElementKind.CLASS) {
// Encrypt encrypt = elem.getAnnotation(Encrypt.class);
// String message = "annotation found in " + elem.getSimpleName();
// processingEnv.getMessager().printMessage(Kind.NOTE, message);
TypeElement classElement = (TypeElement) elem;
PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
processingEnv.getMessager().printMessage(
Diagnostic.Kind.NOTE,
"annotated class: @Encrypt" , elem);
className = classElement.getSimpleName().toString();
packageName = packageElement.getQualifiedName().toString();
fqClassName = classElement.getQualifiedName().toString();
if (fqClassName != null) {
processingEnv.getMessager().printMessage(
Diagnostic.Kind.NOTE,
"fqClassName: "+fqClassName , elem);
Properties props = new Properties();
URL url = this.getClass().getClassLoader().getResource("velocity.properties");
try {
props.load(url.openStream());
} catch (IOException e) {
processingEnv.getMessager().printMessage(
Diagnostic.Kind.ERROR,
"annotated class: " + classElement.getQualifiedName()+"->\n"+
e.getMessage(), elem);
e.printStackTrace();
return true;
}
VelocityEngine ve = new VelocityEngine(props);
ve.init();
VelocityContext vc = new VelocityContext();
vc.put("className", className);
vc.put("packageName", packageName);
Template vt = ve.getTemplate("encrypt.vm");
File file = new File("src/main/java/"+fqClassName.replace(".", "/")+"_Encrypt.aj");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
processingEnv.getMessager().printMessage(
Diagnostic.Kind.NOTE,
"creating source file: " + file.getAbsolutePath());
processingEnv.getMessager().printMessage(
Diagnostic.Kind.NOTE,
"applying velocity template: " + vt.getName());
vt.merge(vc, bw);
bw.close();
} catch (IOException e) {
processingEnv.getMessager().printMessage(
Diagnostic.Kind.ERROR,
"applying velocity error: " + vt.getName()+"->\n"+e.getMessage());
e.printStackTrace();
}
}
}
}
return true; // no further processing of this annotation type
}
I've tested all into a maven client project and all work, but when i try to use it into an existing spring (full maven build) project not work... Can someone help me?? Tnx
Upvotes: 3
Views: 6621
Reputation: 136
Solved adding this...
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
Upvotes: 1
Reputation: 13821
Have you created the META-INF/services/javax.annotation.processing.Processor
file in the project where you have your annotation processor? This file should contain the fully qualified name of your processor. Also, with maven, you specify which annotation processors to use in the build by configuring the maven-compiler-plugin
:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessors>
<annotationProcessor>com.mycompany.MyAnnotationProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
Also, having the annotation processor in the same project as you're using it might cause some problems with maven, so if you have not done it already, I suggest that you extract your annotation processor and annotations into a separate maven project.
Upvotes: 2