1 /* 2 * Copyright (C) 2019 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.hilt.processor.internal; 18 19 import static androidx.room.compiler.processing.XElementKt.isTypeElement; 20 import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; 21 22 import androidx.room.compiler.processing.XElement; 23 import androidx.room.compiler.processing.XMessager; 24 import androidx.room.compiler.processing.XProcessingEnv; 25 import com.google.auto.value.AutoValue; 26 import com.google.common.base.Throwables; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Optional; 30 import javax.tools.Diagnostic.Kind; 31 32 /** Utility class to handle keeping track of errors during processing. */ 33 final class ProcessorErrorHandler { 34 35 private static final String FAILURE_PREFIX = "[Hilt] "; 36 37 // Special characters to make the tag red and bold to draw attention since 38 // this error can get drowned out by other errors resulting from missing 39 // symbols when we can't generate code. 40 private static final String FAILURE_SUFFIX = 41 "\n\033[1;31m[Hilt] Processing did not complete. See error above for details.\033[0m"; 42 43 private final XProcessingEnv processingEnv; 44 private final XMessager messager; 45 private final List<HiltError> hiltErrors = new ArrayList<>(); 46 ProcessorErrorHandler(XProcessingEnv processingEnv)47 ProcessorErrorHandler(XProcessingEnv processingEnv) { 48 this.processingEnv = processingEnv; 49 this.messager = processingEnv.getMessager(); 50 } 51 52 /** 53 * Records an error message for some exception to the messager. This can be used to handle 54 * exceptions gracefully that would otherwise be propagated out of the {@code process} method. The 55 * message is stored in order to allow the build to continue as far as it can. The build will be 56 * failed with a {@link Kind#ERROR} in {@link #checkErrors} if an error was recorded with this 57 * method. 58 */ recordError(Throwable t)59 void recordError(Throwable t) { 60 // Store messages to allow the build to continue as far as it can. The build will 61 // be failed in checkErrors when processing is over. 62 63 if (t instanceof BadInputException) { 64 BadInputException badInput = (BadInputException) t; 65 if (badInput.getBadElements().isEmpty()) { 66 hiltErrors.add(HiltError.of(badInput.getMessage())); 67 } 68 for (XElement element : badInput.getBadElements()) { 69 hiltErrors.add(HiltError.of(badInput.getMessage(), element)); 70 } 71 } else if (t instanceof ErrorTypeException) { 72 ErrorTypeException badInput = (ErrorTypeException) t; 73 hiltErrors.add(HiltError.of(badInput.getMessage(), badInput.getBadElement())); 74 } else if (t.getMessage() != null) { 75 hiltErrors.add(HiltError.of(t.getMessage() + ": " + Throwables.getStackTraceAsString(t))); 76 } else { 77 hiltErrors.add(HiltError.of(t.getClass() + ": " + Throwables.getStackTraceAsString(t))); 78 } 79 } 80 81 /** Checks for any recorded errors. This should be called at the end of process every round. */ checkErrors()82 void checkErrors() { 83 if (!hiltErrors.isEmpty()) { 84 hiltErrors.forEach( 85 hiltError -> { 86 if (hiltError.element().isPresent()) { 87 XElement element = hiltError.element().get(); 88 if (isTypeElement(element)) { 89 // If the error type is a TypeElement, get a new one just in case it was thrown in a 90 // previous round we can report the correct instance. Otherwise, this leads to 91 // issues in AndroidStudio when linking an error to the proper element. 92 // TODO(bcorso): Consider only allowing TypeElement errors when delaying errors, 93 // or maybe even removing delayed errors altogether. 94 element = 95 processingEnv.requireTypeElement(asTypeElement(element).getQualifiedName()); 96 } 97 messager.printMessage(Kind.ERROR, hiltError.message(), element); 98 } else { 99 messager.printMessage(Kind.ERROR, hiltError.message()); 100 } 101 }); 102 hiltErrors.clear(); 103 } 104 } 105 isEmpty()106 public boolean isEmpty() { 107 return hiltErrors.isEmpty(); 108 } 109 110 @AutoValue 111 abstract static class HiltError { of(String message)112 static HiltError of(String message) { 113 return of(message, Optional.empty()); 114 } 115 of(String message, XElement element)116 static HiltError of(String message, XElement element) { 117 return of(message, Optional.of(element)); 118 } 119 of(String message, Optional<XElement> element)120 private static HiltError of(String message, Optional<XElement> element) { 121 return new AutoValue_ProcessorErrorHandler_HiltError( 122 FAILURE_PREFIX + message + FAILURE_SUFFIX, element); 123 } 124 message()125 abstract String message(); 126 element()127 abstract Optional<XElement> element(); 128 } 129 } 130