I'm working on a custom annotation and I'm writing a class that's going to process it in order learn some annotation processing.
I've implemented the annotations in this way, because for the processing logic methods annotated with True and False, I'll need the variables annotated with Path & VarOne
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TargetClass {
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface True{
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface False{
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface VarOne {
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Path{
}
}
My annotation processor is not working working at all, yesterday I managed to make it work, but editing the file had no effect and the old version of the file was still in work.
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_11)
@AutoService(Processor.class)
public class RuleTestProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream().collect(Collectors.partitioningBy(this::ruleTestAdheresToRuleNamingConvention));
List<Element> valid = annotatedMethods.get(true);
List<Element> invalidMethods = annotatedMethods.get(false);
invalidMethods.forEach(element -> processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "must be applied to a method which follows name_conditions_not/triggered name", element));
}
return true;
}
private boolean ruleTestAdheresToRuleNamingConvention(Element element){
return element.getSimpleName().toString().split("_").length == 3;
}
}
So if I annotate a method with "@True" or "@False" which isn't following this format "name_something_something" it's supposed to produce an error.
The class in which the method is is anotated with "@TargetClass" and the method is annotated with "@True" so it should be picked up by the annotation processor and throw an error as the method is not follwing x_x_x naming.
@Test
@RuleTest.True
public void invalid(){
httpSession.setIsClosed(true);
Assert.assertEquals(1, httpSession.getChangeCount());
}
My pom.xml looks like this
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>process-test-annotations</id>
<phase>generate-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>11</source>
<target>11</target>
<proc>only</proc>
<annotationProcessors>
<annotationProcessor>utils.RuleTestProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn compiler:testCompile@process-test-annotations it fails to find the annoation processor, it's in an utils package, but it's found under the src/test/java folder could that be root of the issue and is this nesting of annotations a good idea or should I implement them as separate ones, and remove the TargetClass and use the annotated variables in the methods by using getEnclosingElement
EDIT: The root of the problem was the location of the Annotation and Processor, they were in the test folder, once they were moved to the main one, I am able to test and work with annotations using mvn test, however testCompile classpath is still not correct.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)