Home | History | Annotate | Download | only in validator
      1 package org.robolectric.annotation.processing.validator;
      2 
      3 import javax.annotation.processing.ProcessingEnvironment;
      4 import javax.lang.model.element.AnnotationMirror;
      5 import javax.lang.model.element.Element;
      6 import javax.lang.model.element.ExecutableElement;
      7 import javax.lang.model.element.TypeElement;
      8 import javax.lang.model.element.VariableElement;
      9 import javax.lang.model.type.TypeMirror;
     10 import org.robolectric.annotation.processing.Helpers;
     11 import org.robolectric.annotation.processing.RobolectricModel;
     12 
     13 /**
     14  * Validator that checks usages of {@link org.robolectric.annotation.Implements}.
     15  */
     16 public abstract class FoundOnImplementsValidator extends Validator {
     17 
     18   private final TypeElement implementsType =
     19       elements.getTypeElement(ImplementsValidator.IMPLEMENTS_CLASS);
     20 
     21   protected AnnotationMirror imp;
     22 
     23   public FoundOnImplementsValidator(RobolectricModel.Builder modelBuilder,
     24       ProcessingEnvironment env,
     25       String annotationType) {
     26     super(modelBuilder, env, annotationType);
     27   }
     28 
     29   @Override
     30   public void init(Element elem, Element p) {
     31     super.init(elem, p);
     32 
     33     do {
     34       imp = Helpers.getImplementsMirror(p, types, implementsType);
     35 
     36       // if not found, search on superclasses too...
     37       if (imp == null) {
     38         TypeMirror superclass = ((TypeElement) p).getSuperclass();
     39         p = superclass == null ? null : types.asElement(superclass);
     40       } else {
     41         break;
     42       }
     43     } while (p != null);
     44 
     45     if (imp == null) {
     46       error('@' + annotationType.getSimpleName().toString() + " without @Implements");
     47     }
     48   }
     49 
     50   @Override
     51   final public Void visitVariable(VariableElement elem, Element parent) {
     52     return visitVariable(elem, Helpers.getAnnotationTypeMirrorValue(parent));
     53   }
     54 
     55   public Void visitVariable(VariableElement elem, TypeElement parent) {
     56     return null;
     57   }
     58 
     59   @Override
     60   final public Void visitExecutable(ExecutableElement elem, Element parent) {
     61     return visitExecutable(elem, Helpers.getAnnotationTypeMirrorValue(parent));
     62   }
     63 
     64   public Void visitExecutable(ExecutableElement elem, TypeElement parent) {
     65     return null;
     66   }
     67 }
     68