• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.auto.factory.processor;
17 
18 import static com.google.auto.common.MoreElements.isAnnotationPresent;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static javax.tools.Diagnostic.Kind.ERROR;
21 
22 import com.google.auto.factory.AutoFactory;
23 import com.google.auto.factory.Provided;
24 import javax.annotation.processing.Messager;
25 import javax.lang.model.element.Element;
26 import javax.lang.model.element.ExecutableElement;
27 import javax.lang.model.element.VariableElement;
28 import javax.lang.model.util.ElementKindVisitor6;
29 import org.checkerframework.checker.nullness.qual.Nullable;
30 
31 final class ProvidedChecker {
32   private final Messager messager;
33 
ProvidedChecker(Messager messager)34   ProvidedChecker(Messager messager) {
35     this.messager = messager;
36   }
37 
checkProvidedParameter(Element element)38   void checkProvidedParameter(Element element) {
39     checkArgument(
40         isAnnotationPresent(element, Provided.class), "%s not annoated with @Provided", element);
41     element.accept(
42         new ElementKindVisitor6<@Nullable Void, @Nullable Void>() {
43           @Override
44           protected @Nullable Void defaultAction(Element e, @Nullable Void p) {
45             throw new AssertionError("Provided can only be applied to parameters");
46           }
47 
48           @Override
49           public @Nullable Void visitVariableAsParameter(
50               VariableElement providedParameter, @Nullable Void p) {
51             providedParameter
52                 .getEnclosingElement()
53                 .accept(
54                     new ElementKindVisitor6<@Nullable Void, @Nullable Void>() {
55                       @Override
56                       protected @Nullable Void defaultAction(Element e, @Nullable Void p) {
57                         raiseError(
58                             providedParameter, "@%s may only be applied to constructor parameters");
59                         return null;
60                       }
61 
62                       @Override
63                       public @Nullable Void visitExecutableAsConstructor(
64                           ExecutableElement constructor, @Nullable Void p) {
65                         if (!(annotatedWithAutoFactory(constructor)
66                             || annotatedWithAutoFactory(constructor.getEnclosingElement()))) {
67                           raiseError(
68                               providedParameter,
69                               "@%s may only be applied to constructors requesting an auto-factory");
70                         }
71                         return null;
72                       }
73                     },
74                     p);
75             return null;
76           }
77         },
78         null);
79   }
80 
raiseError(VariableElement providedParameter, String messageFormat)81   private void raiseError(VariableElement providedParameter, String messageFormat) {
82     messager.printMessage(
83         ERROR,
84         String.format(messageFormat, Provided.class.getSimpleName()),
85         providedParameter,
86         Mirrors.getAnnotationMirror(providedParameter, Provided.class).get());
87   }
88 
annotatedWithAutoFactory(Element e)89   private static boolean annotatedWithAutoFactory(Element e) {
90     return isAnnotationPresent(e, AutoFactory.class);
91   }
92 }
93