• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.validation;
18 
19 import com.google.auto.common.MoreTypes;
20 import com.google.auto.common.SuperficialValidation;
21 import com.google.common.base.Equivalence;
22 import dagger.internal.codegen.langmodel.DaggerTypes;
23 import java.util.ArrayDeque;
24 import java.util.HashSet;
25 import java.util.Queue;
26 import java.util.Set;
27 import javax.lang.model.type.TypeMirror;
28 
29 /** Utility methods for validating the type hierarchy of a given type. */
30 final class TypeHierarchyValidator {
TypeHierarchyValidator()31   private TypeHierarchyValidator() {}
32 
33   /**
34    * Validate the type hierarchy of the given type including all super classes, interfaces, and
35    * type parameters.
36    *
37    * @throws TypeNotPresentException if an type in the hierarchy is not valid.
38    */
validateTypeHierarchy(TypeMirror type, DaggerTypes types)39   public static void validateTypeHierarchy(TypeMirror type, DaggerTypes types) {
40     Queue<TypeMirror> queue = new ArrayDeque<>();
41     Set<Equivalence.Wrapper<TypeMirror>> queued = new HashSet<>();
42     queue.add(type);
43     queued.add(MoreTypes.equivalence().wrap(type));
44     while (!queue.isEmpty()) {
45       TypeMirror currType = queue.remove();
46       if (!SuperficialValidation.validateType(currType)) {
47         throw new TypeNotPresentException(currType.toString(), null);
48       }
49       for (TypeMirror superType : types.directSupertypes(currType)) {
50         if (queued.add(MoreTypes.equivalence().wrap(superType))) {
51           queue.add(superType);
52         }
53       }
54     }
55   }
56 }
57