• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.processingstep;
18 
19 import static dagger.internal.codegen.xprocessing.XElements.closestEnclosingTypeElement;
20 
21 import androidx.room.compiler.processing.XElement;
22 import androidx.room.compiler.processing.XTypeElement;
23 import dagger.internal.codegen.base.ClearableCache;
24 import dagger.internal.codegen.base.DaggerSuperficialValidation;
25 import dagger.internal.codegen.base.DaggerSuperficialValidation.ValidationException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Optional;
29 import javax.inject.Inject;
30 import javax.inject.Singleton;
31 
32 /** Validates enclosing type elements in a round. */
33 @Singleton
34 final class SuperficialValidator implements ClearableCache {
35 
36   private final DaggerSuperficialValidation superficialValidation;
37   private final Map<XTypeElement, Optional<ValidationException>> validationExceptions =
38       new HashMap<>();
39 
40   @Inject
SuperficialValidator(DaggerSuperficialValidation superficialValidation)41   SuperficialValidator(DaggerSuperficialValidation superficialValidation) {
42     this.superficialValidation = superficialValidation;
43   }
44 
throwIfNearestEnclosingTypeNotValid(XElement element)45   void throwIfNearestEnclosingTypeNotValid(XElement element) {
46     Optional<ValidationException> validationException =
47         validationExceptions.computeIfAbsent(
48             closestEnclosingTypeElement(element),
49             this::validationExceptionsUncached);
50 
51     if (validationException.isPresent()) {
52       throw validationException.get();
53     }
54   }
55 
validationExceptionsUncached(XTypeElement element)56   private Optional<ValidationException> validationExceptionsUncached(XTypeElement element) {
57     try {
58       superficialValidation.validateElement(element);
59     } catch (ValidationException validationException) {
60       return Optional.of(validationException);
61     }
62     return Optional.empty();
63   }
64 
65   @Override
clearCache()66   public void clearCache() {
67     validationExceptions.clear();
68   }
69 }
70