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