• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2008 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 
17 package com.google.inject;
18 
19 import static com.google.common.base.Preconditions.checkState;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.inject.internal.Errors;
23 import com.google.inject.spi.Message;
24 
25 import java.util.Collection;
26 
27 /**
28  * Thrown when a programming error such as a misplaced annotation, illegal binding, or unsupported
29  * scope is found. Clients should catch this exception, log it, and stop execution.
30  *
31  * @author jessewilson@google.com (Jesse Wilson)
32  * @since 2.0
33  */
34 public final class ConfigurationException extends RuntimeException {
35 
36   private final ImmutableSet<Message> messages;
37   private Object partialValue = null;
38 
39   /** Creates a ConfigurationException containing {@code messages}. */
ConfigurationException(Iterable<Message> messages)40   public ConfigurationException(Iterable<Message> messages) {
41     this.messages = ImmutableSet.copyOf(messages);
42     initCause(Errors.getOnlyCause(this.messages));
43   }
44 
45   /** Returns a copy of this configuration exception with the specified partial value. */
withPartialValue(Object partialValue)46   public ConfigurationException withPartialValue(Object partialValue) {
47     checkState(this.partialValue == null,
48         "Can't clobber existing partial value %s with %s", this.partialValue, partialValue);
49     ConfigurationException result = new ConfigurationException(messages);
50     result.partialValue = partialValue;
51     return result;
52   }
53 
54   /** Returns messages for the errors that caused this exception. */
getErrorMessages()55   public Collection<Message> getErrorMessages() {
56     return messages;
57   }
58 
59   /**
60    * Returns a value that was only partially computed due to this exception. The caller can use
61    * this while collecting additional configuration problems.
62    *
63    * @return the partial value, or {@code null} if none was set. The type of the partial value is
64    *      specified by the throwing method.
65    */
66   @SuppressWarnings("unchecked") // this is *extremely* unsafe. We trust the caller here.
getPartialValue()67   public <E> E getPartialValue() {
68     return (E) partialValue;
69   }
70 
getMessage()71   @Override public String getMessage() {
72     return Errors.format("Guice configuration errors", messages);
73   }
74 
75   private static final long serialVersionUID = 0;
76 }