• 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.base;
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 /** Utility class for checking the visibility of an annotation.  */
32 public final class MapKeyAccessibility extends SimpleAnnotationValueVisitor8<Boolean, Void> {
33   private final Predicate<TypeMirror> accessibilityChecker;
34 
MapKeyAccessibility(Predicate<TypeMirror> accessibilityChecker)35   private MapKeyAccessibility(Predicate<TypeMirror> accessibilityChecker) {
36     this.accessibilityChecker = accessibilityChecker;
37   }
38 
39   @Override
visitAnnotation(AnnotationMirror annotation, Void aVoid)40   public Boolean visitAnnotation(AnnotationMirror annotation, Void aVoid) {
41     // The annotation type is not checked, as the generated code will refer to the @AutoAnnotation
42     // generated type which is always public
43     return visitValues(annotation.getElementValues().values());
44   }
45 
46   @Override
visitArray(List<? extends AnnotationValue> values, Void aVoid)47   public Boolean visitArray(List<? extends AnnotationValue> values, Void aVoid) {
48     return visitValues(values);
49   }
50 
visitValues(Collection<? extends AnnotationValue> values)51   private boolean visitValues(Collection<? extends AnnotationValue> values) {
52     return values.stream().allMatch(value -> value.accept(this, null));
53   }
54 
55   @Override
visitEnumConstant(VariableElement enumConstant, Void aVoid)56   public Boolean visitEnumConstant(VariableElement enumConstant, Void aVoid) {
57     return accessibilityChecker.test(enumConstant.getEnclosingElement().asType());
58   }
59 
60   @Override
visitType(TypeMirror type, Void aVoid)61   public Boolean visitType(TypeMirror type, Void aVoid) {
62     return accessibilityChecker.test(type);
63   }
64 
65   @Override
defaultAction(Object o, Void aVoid)66   protected Boolean defaultAction(Object o, Void aVoid) {
67     return true;
68   }
69 
isMapKeyAccessibleFrom( AnnotationMirror annotation, String accessingPackage)70   public static boolean isMapKeyAccessibleFrom(
71       AnnotationMirror annotation, String accessingPackage) {
72     return new MapKeyAccessibility(type -> isTypeAccessibleFrom(type, accessingPackage))
73         .visitAnnotation(annotation, null);
74   }
75 
isMapKeyPubliclyAccessible(AnnotationMirror annotation)76   public static boolean isMapKeyPubliclyAccessible(AnnotationMirror annotation) {
77     return new MapKeyAccessibility(Accessibility::isTypePubliclyAccessible)
78         .visitAnnotation(annotation, null);
79   }
80 }
81