blob: ff170b57cf8d64142a940c344728bcaf480e52a2 [file] [log] [blame]
crazyboblee07e41822006-11-21 01:27:08 +00001/**
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
17package com.google.inject;
18
19import java.lang.reflect.Member;
20
21/**
22 * Thrown when a constant type conversion error occurs.
23 *
24 * @author crazybob@google.com (Bob Lee)
25 */
crazyboblee4727ee22007-01-30 03:13:38 +000026class ConstantConversionException extends Exception {
crazyboblee07e41822006-11-21 01:27:08 +000027
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
crazyboblee4727ee22007-01-30 03:13:38 +000038 static String createMessage(String value, Key<?> key, Member member,
crazyboblee07e41822006-11-21 01:27:08 +000039 String reason) {
40 return member == null
41 ? "Error converting '" + value + "' to "
crazybobleeed8825f2006-12-06 01:28:10 +000042 + key.getRawType().getSimpleName()
crazyboblee07e41822006-11-21 01:27:08 +000043 + " while getting dependency named '" + key.getName()
44 + "'. Reason: " + reason
45 : "Error converting '" + value + "' to "
crazybobleeed8825f2006-12-06 01:28:10 +000046 + key.getRawType().getSimpleName() + " while injecting "
crazyboblee07e41822006-11-21 01:27:08 +000047 + member.getName() + " with dependency named '" + key.getName()
48 + "' in " + member.getDeclaringClass().getSimpleName()
49 + ". Reason: " + reason;
50 }
crazyboblee07e41822006-11-21 01:27:08 +000051}