1 /* 2 * Copyright (C) 2014 Google, Inc. 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 package dagger.internal.codegen; 17 18 import com.google.common.base.Optional; 19 import com.google.common.collect.ImmutableSet; 20 import com.google.common.collect.Iterables; 21 import dagger.internal.codegen.writer.ClassName; 22 import javax.annotation.processing.Messager; 23 import javax.lang.model.element.Element; 24 25 import static com.google.common.base.Preconditions.checkNotNull; 26 import static javax.tools.Diagnostic.Kind.ERROR; 27 28 /** 29 * An exception thrown to indicate that a source file could not be generated. 30 * 31 * <p>This exception <b>should not</b> be used to report detectable, logical errors as it may mask 32 * other errors that might have been caught upon further processing. Use a {@link ValidationReport} 33 * for that. 34 * 35 * @author Gregory Kick 36 * @since 2.0 37 */ 38 final class SourceFileGenerationException extends Exception { 39 private final ImmutableSet<ClassName> generatedClassNames; 40 private final Optional<? extends Element> associatedElement; 41 SourceFileGenerationException(Iterable<ClassName> generatedClassNames, Throwable cause, Optional<? extends Element> associatedElement)42 SourceFileGenerationException(Iterable<ClassName> generatedClassNames, Throwable cause, 43 Optional<? extends Element> associatedElement) { 44 super(createMessage(generatedClassNames, cause.getMessage()), cause); 45 this.generatedClassNames = ImmutableSet.copyOf(generatedClassNames); 46 this.associatedElement = checkNotNull(associatedElement); 47 } 48 SourceFileGenerationException(Iterable<ClassName> generatedClassNames, Throwable cause)49 SourceFileGenerationException(Iterable<ClassName> generatedClassNames, Throwable cause) { 50 this(generatedClassNames, cause, Optional.<Element>absent()); 51 } 52 SourceFileGenerationException(Iterable<ClassName> generatedClassNames, Throwable cause, Element associatedElement)53 SourceFileGenerationException(Iterable<ClassName> generatedClassNames, Throwable cause, 54 Element associatedElement) { 55 this(generatedClassNames, cause, Optional.of(associatedElement)); 56 } 57 generatedClassNames()58 public ImmutableSet<ClassName> generatedClassNames() { 59 return generatedClassNames; 60 } 61 associatedElement()62 public Optional<? extends Element> associatedElement() { 63 return associatedElement; 64 } 65 createMessage(Iterable<ClassName> generatedClassNames, String message)66 private static String createMessage(Iterable<ClassName> generatedClassNames, String message) { 67 return String.format("Could not generate %s: %s.", 68 Iterables.isEmpty(generatedClassNames) 69 ? "unknown files" 70 : Iterables.toString(generatedClassNames), 71 message); 72 } 73 printMessageTo(Messager messager)74 void printMessageTo(Messager messager) { 75 if (associatedElement.isPresent()) { 76 messager.printMessage(ERROR, getMessage(), associatedElement.get()); 77 } else { 78 messager.printMessage(ERROR, getMessage()); 79 } 80 } 81 } 82