limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 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 | |
| 18 | package com.google.inject; |
| 19 | |
limpbizkit | 64c7bfa | 2008-04-27 08:16:37 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.ErrorHandler; |
| 21 | import com.google.inject.internal.ErrorMessages; |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.Message; |
| 23 | |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 24 | import java.util.ArrayList; |
limpbizkit | 64c7bfa | 2008-04-27 08:16:37 +0000 | [diff] [blame] | 25 | import java.util.Collection; |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * A stateful error handler that can be used at both configuration time and |
| 29 | * at runtime. By using the same error handler in both situations, a reference |
| 30 | * to this error handler will work in both situations. |
| 31 | * |
| 32 | * @author crazybob@google.com (Bob Lee) |
| 33 | * @author jessewilson@google.com (Jesse Wilson) |
| 34 | */ |
limpbizkit | 64c7bfa | 2008-04-27 08:16:37 +0000 | [diff] [blame] | 35 | class DefaultErrorHandler implements ErrorHandler { |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 36 | State state = State.CONFIGURATION_TIME; |
| 37 | final Collection<Message> errorMessages = new ArrayList<Message>(); |
| 38 | |
| 39 | public void handle(Object source, String message) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 40 | source = ErrorMessages.convert(source); |
| 41 | |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 42 | if (state == State.RUNTIME) { |
| 43 | throw new ConfigurationException("Error at " + source + " " + message); |
| 44 | |
| 45 | } else if (state == State.CONFIGURATION_TIME) { |
| 46 | errorMessages.add(new Message(source, message)); |
| 47 | |
| 48 | } else { |
| 49 | throw new AssertionError(); |
| 50 | } |
| 51 | } |
| 52 | |
limpbizkit | 64c7bfa | 2008-04-27 08:16:37 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Implements formatting. Converts known types to readable strings. |
| 55 | */ |
| 56 | public final void handle(Object source, String message, Object... arguments) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 57 | handle(source, ErrorMessages.format(message, arguments)); |
limpbizkit | 64c7bfa | 2008-04-27 08:16:37 +0000 | [diff] [blame] | 58 | } |
| 59 | |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 60 | void blowUpIfErrorsExist() { |
| 61 | if (!errorMessages.isEmpty()) { |
| 62 | throw new CreationException(errorMessages); |
| 63 | } |
| 64 | } |
| 65 | |
limpbizkit | f5b13b3 | 2008-04-27 08:59:10 +0000 | [diff] [blame] | 66 | void switchToRuntime() { |
| 67 | state = State.RUNTIME; |
| 68 | errorMessages.clear(); |
| 69 | } |
| 70 | |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 71 | enum State { |
| 72 | CONFIGURATION_TIME, RUNTIME |
| 73 | } |
| 74 | } |