1 16 17 package com.google.inject; 18 19 import com.google.inject.util.StackTraceElements; 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.Field ; 22 import java.lang.reflect.Member ; 23 import java.lang.reflect.Method ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.List ; 27 28 33 class ErrorMessages { 34 35 private static final String MISSING_BINDING = 36 "Binding to %s not found. No bindings to that" 37 + " type were found."; 38 39 private static final String 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 member, 44 Key<?> key, List <String > 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 SUBTYPE_NOT_PROVIDED 56 = "%s doesn't provide instances of %s."; 57 58 static final String NOT_A_SUBTYPE = "%s doesn't extend %s."; 59 60 static final String RECURSIVE_IMPLEMENTATION_TYPE = "@DefaultImplementation" 61 + " points to the same class it annotates."; 62 63 static final String RECURSIVE_PROVIDER_TYPE = "@DefaultProvider" 64 + " points to the same class it annotates."; 65 66 static final String ERROR_INJECTING_MEMBERS = "An error occurred" 67 + " while injecting members of %s. Error message: %s"; 68 69 static final String 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 EXCEPTION_REPORTED_BY_MODULE = "An exception was" 74 + " caught and reported. Message: %s"; 75 76 static final String EXCEPTION_REPORTED_BY_MODULE_SEE_LOG = "An exception" 77 + " was caught and reported. See log for details. Message: %s"; 78 79 static final String MISSING_BINDING_ANNOTATION = "Please annotate with" 80 + " @BindingAnnotation. Bound at %s."; 81 82 static final String MISSING_RUNTIME_RETENTION = "Please annotate with" 83 + " @Retention(RUNTIME). Bound at %s."; 84 85 static final String MISSING_SCOPE_ANNOTATION = "Please annotate with" 86 + " @ScopeAnnotation."; 87 88 static final String OPTIONAL_CONSTRUCTOR = "@Inject(optional=true) is" 89 + " not allowed on constructors."; 90 91 static final String CONSTANT_CONVERSION_ERROR = "Error converting String" 92 + " constant bound at %s to %s: %s"; 93 94 static final String CANNOT_BIND_TO_GUICE_TYPE = "Binding to core guice" 95 + " framework type is not allowed: %s."; 96 97 static final String SCOPE_NOT_FOUND = "No scope is bound to %s."; 98 99 static final String SINGLE_INSTANCE_AND_SCOPE = "Setting the scope is not" 100 + " permitted when binding to a single instance."; 101 102 static final String 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 MISSING_CONSTRUCTOR = "Could not find a suitable" 107 + " constructor in %s. " + CONSTRUCTOR_RULES; 108 109 static final String TOO_MANY_CONSTRUCTORS = "Found more than one constructor" 110 + " annotated with @Inject. " + CONSTRUCTOR_RULES; 111 112 static final String DUPLICATE_SCOPES = "Scope %s is already bound to %s." 113 + " Cannot bind %s."; 114 115 static final String MISSING_CONSTANT_VALUE = "Missing constant value. Please" 116 + " call to(...)."; 117 118 static final String 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 ANNOTATION_ALREADY_SPECIFIED = 123 "More than one annotation is specified for this binding."; 124 125 static final String IMPLEMENTATION_ALREADY_SET = "Implementation is set more" 126 + " than once."; 127 128 static final String SCOPE_ALREADY_SET = "Scope is set more than once."; 129 130 static final String DUPLICATE_ANNOTATIONS = "Found more than one annotation" 131 + " annotated with @BindingAnnotation: %s and %s"; 132 133 static final String DUPLICATE_SCOPE_ANNOTATIONS = "More than one scope" 134 + " annotation was found: %s and %s"; 135 136 static final String CONSTANT_VALUE_ALREADY_SET = "Constant value is set more" 137 + " than once."; 138 139 static final String RECURSIVE_BINDING = "Binding points to itself."; 140 141 static final String BINDING_ALREADY_SET = "A binding to %s was already" 142 + " configured at %s."; 143 144 static final String PRELOAD_NOT_ALLOWED = "Preloading is only supported for" 145 + " singleton-scoped bindings."; 146 147 static final String EXCEPTION_WHILE_CREATING = "Error while locating" 148 + " instance%n bound to %s%n for member at %s"; 149 150 static final String NULL_PROVIDED = "Null value returned by custom provider" 151 + " bound at %s"; 152 153 static String getRootMessage(Throwable t) { 154 Throwable cause = t.getCause(); 155 return cause == null 156 ? t.toString() 157 : getRootMessage(cause); 158 } 159 160 static Object convert(Object 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 ("unchecked") 170 static final Collection <Converter<?>> converters = Arrays.asList( 171 new Converter<Method >(Method .class) { 172 public String toString(Method m) { 173 return "method " + m.getDeclaringClass().getName() + "." 174 + m.getName() + "()"; 175 } 176 }, 177 new Converter<Constructor >(Constructor .class) { 178 public String toString(Constructor c) { 179 return "constructor " + c.getDeclaringClass().getName() + "()"; 180 } 181 }, 182 new Converter<Field >(Field .class) { 183 public String toString(Field f) { 184 return "field " + f.getDeclaringClass().getName() + "." + f.getName(); 185 } 186 }, 187 new Converter<Class >(Class .class) { 188 public String toString(Class c) { 189 return c.getName(); 190 } 191 }, 192 new Converter<Key>(Key.class) { 193 public String 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 <T> type; 204 205 Converter(Class <T> type) { 206 this.type = type; 207 } 208 209 boolean appliesTo(Object o) { 210 return type.isAssignableFrom(o.getClass()); 211 } 212 213 String convert(Object o) { 214 return toString(type.cast(o)); 215 } 216 217 abstract String toString(T t); 218 } 219 } 220 | Popular Tags |