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 com.google.auto.common.MoreElements; 20 import com.google.auto.value.AutoValue; 21 import com.google.common.base.Throwables; 22 import java.util.ArrayList; 23 import java.util.List; 24 import java.util.Optional; 25 import javax.annotation.processing.Messager; 26 import javax.annotation.processing.ProcessingEnvironment; 27 import javax.lang.model.element.Element; 28 import javax.lang.model.util.Elements; 29 import javax.tools.Diagnostic.Kind; 30 31 /** Utility class to handle keeping track of errors during processing. */ 32 final class ProcessorErrorHandler { 33 34 private static final String FAILURE_PREFIX = "[Hilt]\n"; 35 36 // Special characters to make the tag red and bold to draw attention since 37 // this error can get drowned out by other errors resulting from missing 38 // symbols when we can't generate code. 39 private static final String FAILURE_SUFFIX = 40 "\n\033[1;31m[Hilt] Processing did not complete. See error above for details.\033[0m"; 41 42 private final Messager messager; 43 private final Elements elements; 44 private final List<HiltError> hiltErrors; 45 ProcessorErrorHandler(ProcessingEnvironment env)46 ProcessorErrorHandler(ProcessingEnvironment env) { 47 this.messager = env.getMessager(); 48 this.elements = env.getElementUtils(); 49 this.hiltErrors = new ArrayList<>(); 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 (Element 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 Element element = hiltError.element().get(); 88 if (MoreElements.isType(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 elements.getTypeElement( 96 MoreElements.asType(element).getQualifiedName().toString()); 97 } 98 messager.printMessage(Kind.ERROR, hiltError.message(), element); 99 } else { 100 messager.printMessage(Kind.ERROR, hiltError.message()); 101 } 102 }); 103 hiltErrors.clear(); 104 } 105 } 106 107 @AutoValue 108 abstract static class HiltError { of(String message)109 static HiltError of(String message) { 110 return of(message, Optional.empty()); 111 } 112 of(String message, Element element)113 static HiltError of(String message, Element element) { 114 return of(message, Optional.of(element)); 115 } 116 of(String message, Optional<Element> element)117 private static HiltError of(String message, Optional<Element> element) { 118 return new AutoValue_ProcessorErrorHandler_HiltError( 119 FAILURE_PREFIX + message + FAILURE_SUFFIX, element); 120 } 121 message()122 abstract String message(); 123 element()124 abstract Optional<Element> element(); 125 } 126 } 127