1 /* 2 * Copyright (C) 2006 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.checkArgument; 20 21 import com.google.common.collect.ImmutableSet; 22 import com.google.inject.internal.Messages; 23 import com.google.inject.spi.Message; 24 import java.util.Collection; 25 26 /** 27 * Thrown when errors occur while creating a {@link Injector}. Includes a list of encountered 28 * errors. Clients should catch this exception, log it, and stop execution. 29 * 30 * @author crazybob@google.com (Bob Lee) 31 */ 32 public class CreationException extends RuntimeException { 33 34 private final ImmutableSet<Message> messages; 35 36 /** Creates a CreationException containing {@code messages}. */ CreationException(Collection<Message> messages)37 public CreationException(Collection<Message> messages) { 38 this.messages = ImmutableSet.copyOf(messages); 39 checkArgument(!this.messages.isEmpty()); 40 initCause(Messages.getOnlyCause(this.messages)); 41 } 42 43 /** Returns messages for the errors that caused this exception. */ getErrorMessages()44 public Collection<Message> getErrorMessages() { 45 return messages; 46 } 47 48 @Override getMessage()49 public String getMessage() { 50 return Messages.formatMessages("Unable to create injector, see the following errors", messages); 51 } 52 53 private static final long serialVersionUID = 0; 54 } 55