KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > ErrorMessages


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 com.google.inject.util.StackTraceElements;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.Field JavaDoc;
22 import java.lang.reflect.Member JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Error message templates.
30  *
31  * @author crazybob@google.com (Bob Lee)
32  */

33 class ErrorMessages {
34
35   private static final String JavaDoc MISSING_BINDING =
36       "Binding to %s not found. No bindings to that"
37           + " type were found.";
38
39   private static final String JavaDoc MISSING_BINDING_BUT_OTHERS_EXIST =
40       "Binding to %s not found. Annotations on other"
41           + " bindings to that type include: %s";
42
43   static void handleMissingBinding(ErrorHandler errorHandler, Member JavaDoc member,
44       Key<?> key, List JavaDoc<String JavaDoc> otherNames) {
45     if (otherNames.isEmpty()) {
46       errorHandler.handle(StackTraceElements.forMember(member),
47           MISSING_BINDING, key);
48     }
49     else {
50       errorHandler.handle(StackTraceElements.forMember(member),
51           MISSING_BINDING_BUT_OTHERS_EXIST, key, otherNames);
52     }
53   }
54
55   static final String JavaDoc SUBTYPE_NOT_PROVIDED
56       = "%s doesn't provide instances of %s.";
57
58   static final String JavaDoc NOT_A_SUBTYPE = "%s doesn't extend %s.";
59
60   static final String JavaDoc RECURSIVE_IMPLEMENTATION_TYPE = "@DefaultImplementation"
61       + " points to the same class it annotates.";
62
63   static final String JavaDoc RECURSIVE_PROVIDER_TYPE = "@DefaultProvider"
64       + " points to the same class it annotates.";
65
66   static final String JavaDoc ERROR_INJECTING_MEMBERS = "An error occurred"
67       + " while injecting members of %s. Error message: %s";
68
69   static final String JavaDoc ERROR_INJECTING_MEMBERS_SEE_LOG = "An error of type %s"
70       + " occurred while injecting members of %s. See log for details. Error"
71       + " message: %s";
72
73   static final String JavaDoc EXCEPTION_REPORTED_BY_MODULE = "An exception was"
74       + " caught and reported. Message: %s";
75
76   static final String JavaDoc EXCEPTION_REPORTED_BY_MODULE_SEE_LOG = "An exception"
77       + " was caught and reported. See log for details. Message: %s";
78
79   static final String JavaDoc MISSING_BINDING_ANNOTATION = "Please annotate with"
80       + " @BindingAnnotation. Bound at %s.";
81
82   static final String JavaDoc MISSING_RUNTIME_RETENTION = "Please annotate with"
83       + " @Retention(RUNTIME). Bound at %s.";
84
85   static final String JavaDoc MISSING_SCOPE_ANNOTATION = "Please annotate with"
86       + " @ScopeAnnotation.";
87
88   static final String JavaDoc OPTIONAL_CONSTRUCTOR = "@Inject(optional=true) is"
89       + " not allowed on constructors.";
90
91   static final String JavaDoc CONSTANT_CONVERSION_ERROR = "Error converting String"
92       + " constant bound at %s to %s: %s";
93
94   static final String JavaDoc CANNOT_BIND_TO_GUICE_TYPE = "Binding to core guice"
95       + " framework type is not allowed: %s.";
96
97   static final String JavaDoc SCOPE_NOT_FOUND = "No scope is bound to %s.";
98
99   static final String JavaDoc SINGLE_INSTANCE_AND_SCOPE = "Setting the scope is not"
100       + " permitted when binding to a single instance.";
101
102   static final String JavaDoc CONSTRUCTOR_RULES = "Classes must have either one (and"
103       + " only one) constructor annotated with @Inject or a zero-argument"
104       + " constructor.";
105
106   static final String JavaDoc MISSING_CONSTRUCTOR = "Could not find a suitable"
107       + " constructor in %s. " + CONSTRUCTOR_RULES;
108
109   static final String JavaDoc TOO_MANY_CONSTRUCTORS = "Found more than one constructor"
110       + " annotated with @Inject. " + CONSTRUCTOR_RULES;
111
112   static final String JavaDoc DUPLICATE_SCOPES = "Scope %s is already bound to %s."
113       + " Cannot bind %s.";
114
115   static final String JavaDoc MISSING_CONSTANT_VALUE = "Missing constant value. Please"
116       + " call to(...).";
117
118   static final String JavaDoc CANNOT_INJECT_ABSTRACT_TYPE =
119       "Injecting into abstract types is not supported. Please use a concrete"
120           + " type instead of %s.";
121
122   static final String JavaDoc ANNOTATION_ALREADY_SPECIFIED =
123       "More than one annotation is specified for this binding.";
124
125   static final String JavaDoc IMPLEMENTATION_ALREADY_SET = "Implementation is set more"
126       + " than once.";
127
128   static final String JavaDoc SCOPE_ALREADY_SET = "Scope is set more than once.";
129
130   static final String JavaDoc DUPLICATE_ANNOTATIONS = "Found more than one annotation"
131       + " annotated with @BindingAnnotation: %s and %s";
132
133   static final String JavaDoc DUPLICATE_SCOPE_ANNOTATIONS = "More than one scope"
134       + " annotation was found: %s and %s";
135
136   static final String JavaDoc CONSTANT_VALUE_ALREADY_SET = "Constant value is set more"
137       + " than once.";
138
139   static final String JavaDoc RECURSIVE_BINDING = "Binding points to itself.";
140
141   static final String JavaDoc BINDING_ALREADY_SET = "A binding to %s was already"
142       + " configured at %s.";
143
144   static final String JavaDoc PRELOAD_NOT_ALLOWED = "Preloading is only supported for"
145       + " singleton-scoped bindings.";
146
147   static final String JavaDoc EXCEPTION_WHILE_CREATING = "Error while locating"
148       + " instance%n bound to %s%n for member at %s";
149   
150   static final String JavaDoc NULL_PROVIDED = "Null value returned by custom provider"
151       + " bound at %s";
152
153   static String JavaDoc getRootMessage(Throwable JavaDoc t) {
154     Throwable JavaDoc cause = t.getCause();
155     return cause == null
156         ? t.toString()
157         : getRootMessage(cause);
158   }
159
160   static Object JavaDoc convert(Object JavaDoc o) {
161     for (Converter<?> converter : converters) {
162       if (converter.appliesTo(o)) {
163         return converter.convert(o);
164       }
165     }
166     return o;
167   }
168
169   @SuppressWarnings JavaDoc("unchecked")
170   static final Collection JavaDoc<Converter<?>> converters = Arrays.asList(
171       new Converter<Method JavaDoc>(Method JavaDoc.class) {
172         public String JavaDoc toString(Method JavaDoc m) {
173           return "method " + m.getDeclaringClass().getName() + "."
174               + m.getName() + "()";
175         }
176       },
177       new Converter<Constructor JavaDoc>(Constructor JavaDoc.class) {
178         public String JavaDoc toString(Constructor JavaDoc c) {
179           return "constructor " + c.getDeclaringClass().getName() + "()";
180         }
181       },
182       new Converter<Field JavaDoc>(Field JavaDoc.class) {
183         public String JavaDoc toString(Field JavaDoc f) {
184           return "field " + f.getDeclaringClass().getName() + "." + f.getName();
185         }
186       },
187       new Converter<Class JavaDoc>(Class JavaDoc.class) {
188         public String JavaDoc toString(Class JavaDoc c) {
189           return c.getName();
190         }
191       },
192       new Converter<Key>(Key.class) {
193         public String JavaDoc toString(Key k) {
194           return k.hasAnnotationType()
195               ? k.getTypeLiteral() + " annotated with " + k.getAnnotationName()
196               : k.getTypeLiteral().toString();
197         }
198       }
199   );
200
201   static abstract class Converter<T> {
202
203     final Class JavaDoc<T> type;
204
205     Converter(Class JavaDoc<T> type) {
206       this.type = type;
207     }
208
209     boolean appliesTo(Object JavaDoc o) {
210       return type.isAssignableFrom(o.getClass());
211     }
212
213     String JavaDoc convert(Object JavaDoc o) {
214       return toString(type.cast(o));
215     }
216
217     abstract String JavaDoc toString(T t);
218   }
219 }
220
Popular Tags