• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.annotation.processing.validator;
2 
3 import com.google.common.collect.ImmutableSet;
4 import java.util.Set;
5 import javax.annotation.processing.ProcessingEnvironment;
6 import javax.lang.model.element.ExecutableElement;
7 import javax.lang.model.element.Modifier;
8 import javax.lang.model.element.TypeElement;
9 import javax.tools.Diagnostic.Kind;
10 import org.robolectric.annotation.processing.RobolectricModel.Builder;
11 
12 /**
13  * Validator that checks usages of {@link org.robolectric.annotation.Implementation}.
14  */
15 public class ImplementationValidator extends FoundOnImplementsValidator {
16   public static final ImmutableSet<String> METHODS_ALLOWED_TO_BE_PUBLIC =
17       ImmutableSet.of(
18           "toString",
19           "hashCode",
20           "equals"
21       );
22 
ImplementationValidator(Builder modelBuilder, ProcessingEnvironment env)23   public ImplementationValidator(Builder modelBuilder, ProcessingEnvironment env) {
24     super(modelBuilder, env, "org.robolectric.annotation.Implementation");
25   }
26 
27   @Override
visitExecutable(ExecutableElement elem, TypeElement parent)28   public Void visitExecutable(ExecutableElement elem, TypeElement parent) {
29     Set<Modifier> modifiers = elem.getModifiers();
30     if (!METHODS_ALLOWED_TO_BE_PUBLIC.contains(elem.getSimpleName().toString())) {
31       if (!modifiers.contains(Modifier.PUBLIC) && !modifiers.contains(Modifier.PROTECTED)) {
32         message(Kind.ERROR, "@Implementation methods should be protected (preferred) or public (deprecated)");
33       }
34     }
35 
36     // TODO: Check that it has the right signature
37     return null;
38   }
39 }
40