crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 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 java.lang.reflect.Member; |
| 20 | |
| 21 | /** |
| 22 | * Thrown when a constant type conversion error occurs. |
| 23 | * |
| 24 | * @author crazybob@google.com (Bob Lee) |
| 25 | */ |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 26 | class ConstantConversionException extends Exception { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 27 | |
| 28 | ConstantConversionException(Member member, Key<?> key, String value, |
| 29 | String reason) { |
| 30 | super(createMessage(value, key, member, reason)); |
| 31 | } |
| 32 | |
| 33 | ConstantConversionException(Member member, Key<?> key, String value, |
| 34 | Throwable reason) { |
| 35 | this(member, key, value, reason.toString()); |
| 36 | } |
| 37 | |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 38 | static String createMessage(String value, Key<?> key, Member member, |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 39 | String reason) { |
| 40 | return member == null |
| 41 | ? "Error converting '" + value + "' to " |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 42 | + key.getRawType().getSimpleName() |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 43 | + " while getting dependency named '" + key.getName() |
| 44 | + "'. Reason: " + reason |
| 45 | : "Error converting '" + value + "' to " |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 46 | + key.getRawType().getSimpleName() + " while injecting " |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 47 | + member.getName() + " with dependency named '" + key.getName() |
| 48 | + "' in " + member.getDeclaringClass().getSimpleName() |
| 49 | + ". Reason: " + reason; |
| 50 | } |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 51 | } |