RuleProcessorValidator.java

  1. /**
  2.  * Powerunit - A JDK1.8 test framework
  3.  * Copyright (C) 2014 Mathieu Boretti.
  4.  *
  5.  * This file is part of Powerunit
  6.  *
  7.  * Powerunit is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * Powerunit is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package ch.powerunit.impl.validator;

  21. import java.util.HashSet;
  22. import java.util.Set;

  23. import javax.annotation.processing.ProcessingEnvironment;
  24. import javax.annotation.processing.RoundEnvironment;
  25. import javax.lang.model.element.Element;
  26. import javax.lang.model.element.ElementKind;
  27. import javax.lang.model.element.Modifier;
  28. import javax.lang.model.element.Name;
  29. import javax.lang.model.element.TypeElement;
  30. import javax.lang.model.type.DeclaredType;
  31. import javax.lang.model.type.TypeKind;
  32. import javax.lang.model.type.TypeMirror;

  33. import ch.powerunit.Rule;

  34. public interface RuleProcessorValidator extends ProcessValidator {
  35.     default void ruleAnnotationValidation(ProcessingEnvironment processingEnv,
  36.             RoundEnvironment roundEnv) {
  37.         Set<Name> exists = new HashSet<>();
  38.         Set<? extends Element> elements = roundEnv
  39.                 .getElementsAnnotatedWith(Rule.class);
  40.         TypeElement testRule = processingEnv.getElementUtils().getTypeElement(
  41.                 "ch.powerunit.TestRule");
  42.         for (Element element : elements) {
  43.             if (element.getKind() != ElementKind.FIELD) {
  44.                 error("@Rule must prefix a field -- " + element
  45.                         + " is not a field");
  46.                 continue;
  47.             }
  48.             Element parent = element.getEnclosingElement();
  49.             if (exists.contains(parent.getSimpleName())) {
  50.                 warn("Class "
  51.                         + elementAsString(parent)
  52.                         + "\n\t contains more than one @Rule field\n\tOnly one @Run method is field for a test class");
  53.             }
  54.             exists.add(parent.getSimpleName());
  55.             if (element.getModifiers().contains(Modifier.STATIC)) {
  56.                 warn("Field "
  57.                         + elementAsString(element)
  58.                         + "\n\tis prefixed with @Rule and is static\n\tA rule field can't be static");
  59.             }
  60.             if (!element.getModifiers().contains(Modifier.PUBLIC)) {
  61.                 warn("Field "
  62.                         + elementAsString(element)
  63.                         + "\n\tis prefixed with @Rule and is not public\n\tA rule field must be public");
  64.             }
  65.             if (!element.getModifiers().contains(Modifier.FINAL)) {
  66.                 warn("Field "
  67.                         + elementAsString(element)
  68.                         + "\n\tis prefixed with @Rule and is not fianl\n\tA rule field must be final");
  69.             }
  70.             TypeMirror rt = element.asType();
  71.             if (rt.getKind() != TypeKind.DECLARED) {
  72.                 warn("Field " + elementAsString(element)
  73.                         + "\n\tis prefixed with @Rule and is " + rt
  74.                         + "\n\tA rule field must be " + testRule);
  75.             } else {
  76.                 DeclaredType dt = (DeclaredType) rt;
  77.                 if (!processingEnv.getTypeUtils().isSubtype(
  78.                         dt.asElement().asType(), testRule.asType())) {
  79.                     warn("Field " + elementAsString(element)
  80.                             + "\n\tis prefixed with @Rule and is "
  81.                             + dt.asElement().asType()
  82.                             + "\n\tA rule field must be " + testRule);
  83.                 }
  84.             }

  85.         }
  86.     }
  87. }