• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.base;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static javax.tools.Diagnostic.Kind.ERROR;
21 
22 import com.squareup.javapoet.ClassName;
23 import java.util.Optional;
24 import javax.annotation.processing.Messager;
25 import javax.lang.model.element.Element;
26 
27 /**
28  * An exception thrown to indicate that a source file could not be generated.
29  *
30  * <p>This exception <b>should not</b> be used to report detectable, logical errors as it may mask
31  * other errors that might have been caught upon further processing. Use a {@link ValidationReport}
32  * for that.
33  */
34 public final class SourceFileGenerationException extends Exception {
35   private final Element associatedElement;
36 
SourceFileGenerationException( Optional<ClassName> generatedClassName, Throwable cause, Element associatedElement)37   SourceFileGenerationException(
38       Optional<ClassName> generatedClassName, Throwable cause, Element associatedElement) {
39     super(createMessage(generatedClassName, cause.getMessage()), cause);
40     this.associatedElement = checkNotNull(associatedElement);
41   }
42 
createMessage(Optional<ClassName> generatedClassName, String message)43   private static String createMessage(Optional<ClassName> generatedClassName, String message) {
44     return String.format("Could not generate %s: %s.",
45         generatedClassName.isPresent()
46             ? generatedClassName.get()
47             : "unknown file",
48         message);
49   }
50 
printMessageTo(Messager messager)51   public void printMessageTo(Messager messager) {
52     messager.printMessage(ERROR, getMessage(), associatedElement);
53   }
54 }
55