• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.annotation.processing.validator;
2 
3 import javax.annotation.processing.Messager;
4 import javax.annotation.processing.ProcessingEnvironment;
5 import javax.lang.model.element.AnnotationMirror;
6 import javax.lang.model.element.AnnotationValue;
7 import javax.lang.model.element.Element;
8 import javax.lang.model.element.ElementVisitor;
9 import javax.lang.model.element.ExecutableElement;
10 import javax.lang.model.element.PackageElement;
11 import javax.lang.model.element.TypeElement;
12 import javax.lang.model.element.TypeParameterElement;
13 import javax.lang.model.element.VariableElement;
14 import javax.lang.model.util.AbstractElementVisitor6;
15 import javax.lang.model.util.Elements;
16 import javax.lang.model.util.Types;
17 import javax.tools.Diagnostic.Kind;
18 import org.robolectric.annotation.processing.Helpers;
19 import org.robolectric.annotation.processing.RobolectricModel;
20 
21 /** Base class for validators. */
22 public abstract class Validator implements ElementVisitor<Void, Element> {
23   protected final RobolectricModel.Builder modelBuilder;
24   protected final Elements elements;
25   protected final Types types;
26   protected final Messager messager;
27   protected final TypeElement annotationType;
28   protected final Helpers helpers;
29   // This is the easiest way to do it because visit() is final in AbstractEV6
30   final ElementVisitor<Void, Element> visitorAdapter =
31       new AbstractElementVisitor6<Void, Element>() {
32 
33         @Override
34         public Void visitPackage(PackageElement e, Element p) {
35           return Validator.this.visitPackage(e, p);
36         }
37 
38         @Override
39         public Void visitType(TypeElement e, Element p) {
40           return Validator.this.visitType(e, p);
41         }
42 
43         @Override
44         public Void visitVariable(VariableElement e, Element p) {
45           return Validator.this.visitVariable(e, p);
46         }
47 
48         @Override
49         public Void visitExecutable(ExecutableElement e, Element p) {
50           return Validator.this.visitExecutable(e, p);
51         }
52 
53         @Override
54         public Void visitTypeParameter(TypeParameterElement e, Element p) {
55           return Validator.this.visitTypeParameter(e, p);
56         }
57       };
58   protected Element currentElement;
59   protected AnnotationMirror currentAnnotation;
60 
Validator( RobolectricModel.Builder modelBuilder, ProcessingEnvironment env, String annotationType)61   public Validator(
62       RobolectricModel.Builder modelBuilder, ProcessingEnvironment env, String annotationType) {
63     this.modelBuilder = modelBuilder;
64     elements = env.getElementUtils();
65     types = env.getTypeUtils();
66     this.helpers = new Helpers(env);
67     messager = env.getMessager();
68     // FIXME: Need to test case where type lookup fails
69     this.annotationType = elements.getTypeElement(annotationType);
70   }
71 
getCurrentAnnotation()72   protected AnnotationMirror getCurrentAnnotation() {
73     if (currentAnnotation == null) {
74       currentAnnotation = Helpers.getAnnotationMirror(types, currentElement, annotationType);
75     }
76     return currentAnnotation;
77   }
78 
message(Kind severity, String msg, AnnotationValue av)79   protected void message(Kind severity, String msg, AnnotationValue av) {
80     final AnnotationMirror am = getCurrentAnnotation();
81     messager.printMessage(severity, msg, currentElement, am, av);
82   }
83 
message(Kind severity, String msg)84   protected void message(Kind severity, String msg) {
85     final AnnotationMirror am = getCurrentAnnotation();
86     messager.printMessage(severity, msg, currentElement, am);
87   }
88 
error(String msg)89   protected void error(String msg) {
90     message(Kind.ERROR, msg);
91   }
92 
error(String msg, AnnotationValue av)93   protected void error(String msg, AnnotationValue av) {
94     message(Kind.ERROR, msg, av);
95   }
96 
init(Element e, Element p)97   public void init(Element e, Element p) {
98     currentElement = e;
99     currentAnnotation = null;
100   }
101 
getAnnotationType()102   public TypeElement getAnnotationType() {
103     return annotationType;
104   }
105 
106   @Override
visit(Element e, Element p)107   public Void visit(Element e, Element p) {
108     init(e, p);
109     return visitorAdapter.visit(e, p);
110   }
111 
112   @Override
visit(Element e)113   public Void visit(Element e) {
114     return visit(e, null);
115   }
116 
117   @Override
visitPackage(PackageElement e, Element p)118   public Void visitPackage(PackageElement e, Element p) {
119     return null;
120   }
121 
122   @Override
visitType(TypeElement e, Element p)123   public Void visitType(TypeElement e, Element p) {
124     return null;
125   }
126 
127   @Override
visitVariable(VariableElement e, Element p)128   public Void visitVariable(VariableElement e, Element p) {
129     return null;
130   }
131 
132   @Override
visitExecutable(ExecutableElement e, Element p)133   public Void visitExecutable(ExecutableElement e, Element p) {
134     return null;
135   }
136 
137   @Override
visitTypeParameter(TypeParameterElement e, Element p)138   public Void visitTypeParameter(TypeParameterElement e, Element p) {
139     return null;
140   }
141 
142   @Override
visitUnknown(Element e, Element p)143   public Void visitUnknown(Element e, Element p) {
144     return null;
145   }
146 }
147