• 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 androidx.room.compiler.processing.XAnnotation;
22 import androidx.room.compiler.processing.XAnnotationValue;
23 import androidx.room.compiler.processing.XType;
24 import dagger.internal.codegen.langmodel.Accessibility;
25 import java.util.List;
26 import java.util.function.Predicate;
27 
28 /** Utility class for checking the visibility of an annotation. */
29 public final class MapKeyAccessibility {
30 
MapKeyAccessibility()31   private MapKeyAccessibility() {}
32 
checkAnnotation( XAnnotation annotation, Predicate<XType> accessibilityChecker)33   private static boolean checkAnnotation(
34       XAnnotation annotation, Predicate<XType> accessibilityChecker) {
35     return checkValues(annotation.getAnnotationValues(), accessibilityChecker);
36   }
37 
checkValues( List<XAnnotationValue> values, Predicate<XType> accessibilityChecker)38   private static boolean checkValues(
39       List<XAnnotationValue> values, Predicate<XType> accessibilityChecker) {
40     return values.stream().allMatch(value -> checkValue(value, accessibilityChecker));
41   }
42 
checkValue(XAnnotationValue value, Predicate<XType> accessibilityChecker)43   private static boolean checkValue(XAnnotationValue value, Predicate<XType> accessibilityChecker) {
44     if (value.hasListValue()) {
45       return checkValues(value.asAnnotationValueList(), accessibilityChecker);
46     } else if (value.hasAnnotationValue()) {
47       return checkAnnotation(value.asAnnotation(), accessibilityChecker);
48     } else if (value.hasEnumValue()) {
49       return accessibilityChecker.test(value.asEnum().getEnclosingElement().getType());
50     } else if (value.hasTypeValue()) {
51       return accessibilityChecker.test(value.asType());
52     } else {
53       return true;
54     }
55   }
56 
isMapKeyAccessibleFrom(XAnnotation annotation, String accessingPackage)57   public static boolean isMapKeyAccessibleFrom(XAnnotation annotation, String accessingPackage) {
58     return checkAnnotation(annotation, type -> isTypeAccessibleFrom(type, accessingPackage));
59   }
60 
isMapKeyPubliclyAccessible(XAnnotation annotation)61   public static boolean isMapKeyPubliclyAccessible(XAnnotation annotation) {
62     return checkAnnotation(annotation, Accessibility::isTypePubliclyAccessible);
63   }
64 }
65