1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package ch.powerunit.impl.validator;
21
22 import javax.annotation.processing.RoundEnvironment;
23 import javax.lang.model.element.Element;
24 import javax.lang.model.element.ElementKind;
25 import javax.lang.model.element.ExecutableElement;
26 import javax.lang.model.element.Modifier;
27 import javax.lang.model.type.TypeKind;
28
29 import ch.powerunit.Test;
30
31 public interface TestProcessorValidator extends ProcessValidator {
32 default void testAnnotationValidation(RoundEnvironment roundEnv) {
33 roundEnv.getElementsAnnotatedWith(Test.class).forEach(
34 this::testOneAnnotationValidation);
35 }
36
37 default void testOneAnnotationValidation(Element element) {
38 if (element.getKind() != ElementKind.METHOD) {
39 error("@Test must prefix a method -- " + element
40 + " is not a method");
41 return;
42 }
43 ExecutableElement ee = (ExecutableElement) element;
44 if (ee.getModifiers().contains(Modifier.STATIC)) {
45 warn("Method "
46 + elementAsString(ee)
47 + "\n\tis prefixed with @Test and is static\n\tA test method can't be static");
48 }
49 if (!ee.getModifiers().contains(Modifier.PUBLIC)) {
50 warn("Method "
51 + elementAsString(ee)
52 + "\n\tis prefixed with @Test and is not public \n\tA test method must be public");
53 }
54 if (!TypeKind.VOID.equals(ee.getReturnType().getKind())) {
55 warn("Method "
56 + elementAsString(ee)
57 + "\n\tis prefixed with @Test and is not void\n\tA test method must be void");
58 }
59 if (!ee.getParameters().isEmpty()) {
60 warn("Method"
61 + elementAsString(ee)
62 + "\n\tis prefixed with @Test and is not 0-args\n\tA test method must be 0-args");
63 }
64 }
65 }