• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Google, Inc.
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 dagger.internal.codegen;
17 
18 import com.google.common.collect.ImmutableSet;
19 import java.util.Set;
20 import javax.inject.Inject;
21 import javax.lang.model.element.AnnotationMirror;
22 import javax.lang.model.element.ExecutableElement;
23 import javax.lang.model.element.Modifier;
24 import javax.lang.model.element.VariableElement;
25 import javax.tools.Diagnostic.Kind;
26 
27 import static dagger.internal.codegen.ErrorMessages.ABSTRACT_INJECT_METHOD;
28 import static dagger.internal.codegen.ErrorMessages.GENERIC_INJECT_METHOD;
29 import static dagger.internal.codegen.ErrorMessages.MULTIPLE_QUALIFIERS;
30 import static dagger.internal.codegen.ErrorMessages.PRIVATE_INJECT_METHOD;
31 import static dagger.internal.codegen.ErrorMessages.STATIC_INJECT_METHOD;
32 import static dagger.internal.codegen.InjectionAnnotations.getQualifiers;
33 import static javax.lang.model.element.Modifier.ABSTRACT;
34 import static javax.lang.model.element.Modifier.PRIVATE;
35 import static javax.lang.model.element.Modifier.STATIC;
36 
37 /**
38  * A {@linkplain ValidationReport validator} for {@link Inject} methods.
39  *
40  * @author Gregory Kick
41  * @since 2.0
42  */
43 final class InjectMethodValidator {
44   private Kind privateMemberValidationKind;
45   private Kind staticMemberValidationKind;
46 
InjectMethodValidator( Kind privateMemberValidationKind, Kind staticMemberValidationKind)47   public InjectMethodValidator(
48       Kind privateMemberValidationKind, Kind staticMemberValidationKind) {
49     this.privateMemberValidationKind = privateMemberValidationKind;
50     this.staticMemberValidationKind = staticMemberValidationKind;
51   }
52 
validate(ExecutableElement methodElement)53   ValidationReport<ExecutableElement> validate(ExecutableElement methodElement) {
54     ValidationReport.Builder<ExecutableElement> builder = ValidationReport.about(methodElement);
55     Set<Modifier> modifiers = methodElement.getModifiers();
56     if (modifiers.contains(ABSTRACT)) {
57       builder.addError(ABSTRACT_INJECT_METHOD, methodElement);
58     }
59 
60     if (modifiers.contains(PRIVATE)) {
61       builder.addItem(PRIVATE_INJECT_METHOD, privateMemberValidationKind, methodElement);
62     }
63 
64     if (modifiers.contains(STATIC)) {
65       builder.addItem(STATIC_INJECT_METHOD, staticMemberValidationKind, methodElement);
66     }
67 
68     if (!methodElement.getTypeParameters().isEmpty()) {
69       builder.addError(GENERIC_INJECT_METHOD, methodElement);
70     }
71 
72     for (VariableElement parameter : methodElement.getParameters()) {
73       ImmutableSet<? extends AnnotationMirror> qualifiers = getQualifiers(parameter);
74       if (qualifiers.size() > 1) {
75         for (AnnotationMirror qualifier : qualifiers) {
76           builder.addError(MULTIPLE_QUALIFIERS, methodElement, qualifier);
77         }
78       }
79     }
80 
81     return builder.build();
82   }
83 }
84