Advertisement
VirKato

TestProcessor

Mar 29th, 2024 (edited)
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | Software | 0 0
  1. public class TestProcessor extends AbstractProcessor {
  2.     private boolean allMethodsAreTest = false;
  3.  
  4.     @Override
  5.     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  6. //        print("Annotations: " + annotations.toString());
  7.         if (!annotations.isEmpty()) {
  8.             Set<? extends Element> annotatedHandlers = roundEnv.getElementsAnnotatedWith(Test.class);
  9.             annotatedHandlers.forEach(element -> {
  10.                 if (element.getKind().isClass()) {
  11.                     allMethodsAreTest = true;
  12.                 }
  13.                 Test annotation = element.getAnnotation(Test.class);
  14.                 if (annotation.priority() < 1 || annotation.priority() > 10) {
  15.                     print("priority must be in range from 1 to 10");
  16.                 }
  17.             });
  18.             annotatedHandlers = roundEnv.getElementsAnnotatedWith(Before.class);
  19.             annotatedHandlers.forEach(element -> {
  20.                 if (element.getAnnotation(Test.class) == null && !allMethodsAreTest) {
  21.                     print("@Before must be used with @Test");
  22.                 }
  23.             });
  24.             annotatedHandlers = roundEnv.getElementsAnnotatedWith(After.class);
  25.             annotatedHandlers.forEach(element -> {
  26.                 if (element.getAnnotation(Test.class) == null && !allMethodsAreTest) {
  27.                     print("@After must be used with @Test");
  28.                 }
  29.             });
  30.             if (roundEnv.getElementsAnnotatedWith(BeforeSuite.class).size() > 1) {
  31.                 print("@BeforeSuite can't be used more than once");
  32.             }
  33.             if (roundEnv.getElementsAnnotatedWith(AfterSuite.class).size() > 1) {
  34.                 print("@AfterSuite can't be used more than once");
  35.             }
  36.         }
  37.         return false;
  38.     }
  39.  
  40.     private void print(String string) {
  41.         this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, string);
  42.     }
  43. }
Tags: processing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement