• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Dagger Authors.
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 
17 package dagger.internal.codegen;
18 
19 import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom;
20 
21 import dagger.internal.codegen.langmodel.Accessibility;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.function.Predicate;
25 import javax.lang.model.element.AnnotationMirror;
26 import javax.lang.model.element.AnnotationValue;
27 import javax.lang.model.element.VariableElement;
28 import javax.lang.model.type.TypeMirror;
29 import javax.lang.model.util.SimpleAnnotationValueVisitor8;
30 
31 final class MapKeyAccessibility extends SimpleAnnotationValueVisitor8<Boolean, Void> {
32   private final Predicate<TypeMirror> accessibilityChecker;
33 
MapKeyAccessibility(Predicate<TypeMirror> accessibilityChecker)34   private MapKeyAccessibility(Predicate<TypeMirror> accessibilityChecker) {
35     this.accessibilityChecker = accessibilityChecker;
36   }
37 
38   @Override
visitAnnotation(AnnotationMirror annotation, Void aVoid)39   public Boolean visitAnnotation(AnnotationMirror annotation, Void aVoid) {
40     // The annotation type is not checked, as the generated code will refer to the @AutoAnnotation
41     // generated type which is always public
42     return visitValues(annotation.getElementValues().values());
43   }
44 
45   @Override
visitArray(List<? extends AnnotationValue> values, Void aVoid)46   public Boolean visitArray(List<? extends AnnotationValue> values, Void aVoid) {
47     return visitValues(values);
48   }
49 
visitValues(Collection<? extends AnnotationValue> values)50   private boolean visitValues(Collection<? extends AnnotationValue> values) {
51     return values.stream().allMatch(value -> value.accept(this, null));
52   }
53 
54   @Override
visitEnumConstant(VariableElement enumConstant, Void aVoid)55   public Boolean visitEnumConstant(VariableElement enumConstant, Void aVoid) {
56     return accessibilityChecker.test(enumConstant.getEnclosingElement().asType());
57   }
58 
59   @Override
visitType(TypeMirror type, Void aVoid)60   public Boolean visitType(TypeMirror type, Void aVoid) {
61     return accessibilityChecker.test(type);
62   }
63 
64   @Override
defaultAction(Object o, Void aVoid)65   protected Boolean defaultAction(Object o, Void aVoid) {
66     return true;
67   }
68 
isMapKeyAccessibleFrom(AnnotationMirror annotation, String accessingPackage)69   static boolean isMapKeyAccessibleFrom(AnnotationMirror annotation, String accessingPackage) {
70     return new MapKeyAccessibility(type -> isTypeAccessibleFrom(type, accessingPackage))
71         .visitAnnotation(annotation, null);
72   }
73 
isMapKeyPubliclyAccessible(AnnotationMirror annotation)74   static boolean isMapKeyPubliclyAccessible(AnnotationMirror annotation) {
75     return new MapKeyAccessibility(Accessibility::isTypePubliclyAccessible)
76         .visitAnnotation(annotation, null);
77   }
78 }
79