1 /* 2 * Copyright 2019 The Android Open Source Project 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 android.processor.view.inspector; 18 19 import static javax.tools.Diagnostic.Kind.ERROR; 20 21 import androidx.annotation.NonNull; 22 import androidx.annotation.Nullable; 23 24 import javax.annotation.processing.Messager; 25 import javax.lang.model.element.AnnotationMirror; 26 import javax.lang.model.element.AnnotationValue; 27 import javax.lang.model.element.Element; 28 29 /** 30 * Internal exception used to signal an error processing an annotation. 31 */ 32 final class ProcessingException extends RuntimeException { 33 private final @Nullable Element mElement; 34 private final @Nullable AnnotationMirror mAnnotationMirror; 35 private final @Nullable AnnotationValue mAnnotationValue; 36 ProcessingException(@onNull String message)37 ProcessingException(@NonNull String message) { 38 this(message, null, null, null); 39 } 40 ProcessingException(@onNull String message, @NonNull Element element)41 ProcessingException(@NonNull String message, @NonNull Element element) { 42 this(message, element, null, null); 43 } 44 ProcessingException( @onNull String message, @NonNull Element element, @NonNull AnnotationMirror annotationMirror)45 ProcessingException( 46 @NonNull String message, 47 @NonNull Element element, 48 @NonNull AnnotationMirror annotationMirror) { 49 this(message, element, annotationMirror, null); 50 } 51 ProcessingException( @onNull String message, @Nullable Element element, @Nullable AnnotationMirror annotationMirror, @Nullable AnnotationValue annotationValue)52 ProcessingException( 53 @NonNull String message, 54 @Nullable Element element, 55 @Nullable AnnotationMirror annotationMirror, 56 @Nullable AnnotationValue annotationValue) { 57 super(message); 58 mElement = element; 59 mAnnotationMirror = annotationMirror; 60 mAnnotationValue = annotationValue; 61 } 62 63 /** 64 * Prints the exception to a Messager. 65 * 66 * @param messager A Messager to print to 67 */ print(@onNull Messager messager)68 void print(@NonNull Messager messager) { 69 if (mElement != null) { 70 if (mAnnotationMirror != null) { 71 if (mAnnotationValue != null) { 72 messager.printMessage( 73 ERROR, getMessage(), mElement, mAnnotationMirror, mAnnotationValue); 74 } else { 75 messager.printMessage(ERROR, getMessage(), mElement, mAnnotationMirror); 76 } 77 } else { 78 messager.printMessage(ERROR, getMessage(), mElement); 79 } 80 } else { 81 messager.printMessage(ERROR, getMessage()); 82 } 83 } 84 } 85