limpbizkit | 564053f | 2008-06-15 11:21:18 +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.internal; |
| 18 | |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Lists; |
Sam Berlin | c00df28 | 2014-07-01 16:53:41 -0400 | [diff] [blame] | 22 | import com.google.common.collect.Ordering; |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 23 | import com.google.inject.ConfigurationException; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 24 | import com.google.inject.CreationException; |
sameb | 9867f9c | 2015-02-02 12:45:25 -0800 | [diff] [blame^] | 25 | import com.google.inject.Guice; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 26 | import com.google.inject.Key; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 27 | import com.google.inject.MembersInjector; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 28 | import com.google.inject.Provider; |
sameb | 9867f9c | 2015-02-02 12:45:25 -0800 | [diff] [blame^] | 29 | import com.google.inject.Provides; |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 30 | import com.google.inject.ProvisionException; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 31 | import com.google.inject.Scope; |
| 32 | import com.google.inject.TypeLiteral; |
sberlin | dfee9fd | 2010-10-30 14:15:06 +0000 | [diff] [blame] | 33 | import com.google.inject.internal.util.Classes; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 34 | import com.google.inject.internal.util.SourceProvider; |
| 35 | import com.google.inject.internal.util.StackTraceElements; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 36 | import com.google.inject.spi.Dependency; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 37 | import com.google.inject.spi.ElementSource; |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 38 | import com.google.inject.spi.InjectionListener; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 39 | import com.google.inject.spi.InjectionPoint; |
| 40 | import com.google.inject.spi.Message; |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 41 | import com.google.inject.spi.ScopeBinding; |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 42 | import com.google.inject.spi.TypeConverterBinding; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 43 | import com.google.inject.spi.TypeListenerBinding; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 44 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 45 | import java.io.PrintWriter; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 46 | import java.io.Serializable; |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 47 | import java.io.StringWriter; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 48 | import java.lang.annotation.Annotation; |
| 49 | import java.lang.reflect.Constructor; |
| 50 | import java.lang.reflect.Field; |
| 51 | import java.lang.reflect.Member; |
limpbizkit | b47d75a | 2009-09-08 00:17:45 +0000 | [diff] [blame] | 52 | import java.lang.reflect.Method; |
limpbizkit | b0f3c9b | 2008-11-15 09:19:28 +0000 | [diff] [blame] | 53 | import java.lang.reflect.Type; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 54 | import java.util.Collection; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 55 | import java.util.Formatter; |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 56 | import java.util.List; |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 57 | import java.util.Set; |
sameb | 9867f9c | 2015-02-02 12:45:25 -0800 | [diff] [blame^] | 58 | import java.util.logging.Level; |
| 59 | import java.util.logging.Logger; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 62 | * A collection of error messages. If this type is passed as a method parameter, the method is |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 63 | * considered to have executed successfully only if new errors were not added to this collection. |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 64 | * |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 65 | * <p>Errors can be chained to provide additional context. To add context, call {@link #withSource} |
| 66 | * to create a new Errors instance that contains additional context. All messages added to the |
| 67 | * returned instance will contain full context. |
| 68 | * |
| 69 | * <p>To avoid messages with redundant context, {@link #withSource} should be added sparingly. A |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 70 | * good rule of thumb is to assume a method's caller has already specified enough context to |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 71 | * identify that method. When calling a method that's defined in a different context, call that |
| 72 | * method with an errors object that includes its context. |
| 73 | * |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 74 | * @author jessewilson@google.com (Jesse Wilson) |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 75 | */ |
| 76 | public final class Errors implements Serializable { |
| 77 | |
sameb | 9867f9c | 2015-02-02 12:45:25 -0800 | [diff] [blame^] | 78 | private static final Logger logger = Logger.getLogger(Guice.class.getName()); |
| 79 | |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 80 | |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 81 | /** |
| 82 | * The root errors object. Used to access the list of error messages. |
| 83 | */ |
| 84 | private final Errors root; |
| 85 | |
| 86 | /** |
| 87 | * The parent errors object. Used to obtain the chain of source objects. |
| 88 | */ |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 89 | private final Errors parent; |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * The leaf source for errors added here. |
| 93 | */ |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 94 | private final Object source; |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 95 | |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 96 | /** |
| 97 | * null unless (root == this) and error messages exist. Never an empty list. |
| 98 | */ |
| 99 | private List<Message> errors; // lazy, use getErrorsForAdd() |
| 100 | |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 101 | public Errors() { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 102 | this.root = this; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 103 | this.parent = null; |
| 104 | this.source = SourceProvider.UNKNOWN_SOURCE; |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public Errors(Object source) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 108 | this.root = this; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 109 | this.parent = null; |
| 110 | this.source = source; |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 113 | private Errors(Errors parent, Object source) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 114 | this.root = parent.root; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 115 | this.parent = parent; |
| 116 | this.source = source; |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns an instance that uses {@code source} as a reference point for newly added errors. |
| 121 | */ |
| 122 | public Errors withSource(Object source) { |
Sam Berlin | c5848c0 | 2013-12-06 17:05:44 -0500 | [diff] [blame] | 123 | return source == this.source || source == SourceProvider.UNKNOWN_SOURCE |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 124 | ? this |
| 125 | : new Errors(this, source); |
| 126 | } |
| 127 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 128 | /** |
| 129 | * We use a fairly generic error message here. The motivation is to share the |
| 130 | * same message for both bind time errors: |
| 131 | * <pre><code>Guice.createInjector(new AbstractModule() { |
| 132 | * public void configure() { |
| 133 | * bind(Runnable.class); |
| 134 | * } |
| 135 | * }</code></pre> |
| 136 | * ...and at provide-time errors: |
| 137 | * <pre><code>Guice.createInjector().getInstance(Runnable.class);</code></pre> |
| 138 | * Otherwise we need to know who's calling when resolving a just-in-time |
| 139 | * binding, which makes things unnecessarily complex. |
| 140 | */ |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 141 | public Errors missingImplementation(Key key) { |
| 142 | return addMessage("No implementation for %s was bound.", key); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 143 | } |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 144 | |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 145 | public Errors jitDisabled(Key key) { |
| 146 | return addMessage("Explicit bindings are required and %s is not explicitly bound.", key); |
| 147 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 148 | |
Tavian Barnes | 1dd0a91 | 2014-12-19 22:38:50 -0500 | [diff] [blame] | 149 | public Errors jitDisabledInParent(Key<?> key) { |
Tavian Barnes | 6c9726f | 2015-01-07 10:37:17 -0500 | [diff] [blame] | 150 | return addMessage( |
| 151 | "Explicit bindings are required and %s would be bound in a parent injector.%n" |
| 152 | + "Please add an explicit binding for it, either in the child or the parent.", |
| 153 | key); |
Tavian Barnes | 1dd0a91 | 2014-12-19 22:38:50 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 156 | public Errors atInjectRequired(Class clazz) { |
| 157 | return addMessage( |
| 158 | "Explicit @Inject annotations are required on constructors," |
| 159 | + " but %s has no constructors annotated with @Inject.", |
| 160 | clazz); |
| 161 | } |
| 162 | |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 163 | public Errors converterReturnedNull(String stringValue, Object source, |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 164 | TypeLiteral<?> type, TypeConverterBinding typeConverterBinding) { |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 165 | return addMessage("Received null converting '%s' (bound at %s) to %s%n" |
| 166 | + " using %s.", |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 167 | stringValue, convert(source), type, typeConverterBinding); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 168 | } |
| 169 | |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 170 | public Errors conversionTypeError(String stringValue, Object source, TypeLiteral<?> type, |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 171 | TypeConverterBinding typeConverterBinding, Object converted) { |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 172 | return addMessage("Type mismatch converting '%s' (bound at %s) to %s%n" |
| 173 | + " using %s.%n" |
| 174 | + " Converter returned %s.", |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 175 | stringValue, convert(source), type, typeConverterBinding, converted); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | public Errors conversionError(String stringValue, Object source, |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 179 | TypeLiteral<?> type, TypeConverterBinding typeConverterBinding, RuntimeException cause) { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 180 | return errorInUserCode(cause, "Error converting '%s' (bound at %s) to %s%n" |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 181 | + " using %s.%n" |
| 182 | + " Reason: %s", |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 183 | stringValue, convert(source), type, typeConverterBinding, cause); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 184 | } |
| 185 | |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 186 | public Errors ambiguousTypeConversion(String stringValue, Object source, TypeLiteral<?> type, |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 187 | TypeConverterBinding a, TypeConverterBinding b) { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 188 | return addMessage("Multiple converters can convert '%s' (bound at %s) to %s:%n" |
| 189 | + " %s and%n" |
| 190 | + " %s.%n" |
| 191 | + " Please adjust your type converter configuration to avoid overlapping matches.", |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 192 | stringValue, convert(source), type, a, b); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | public Errors bindingToProvider() { |
| 196 | return addMessage("Binding to Provider is not allowed."); |
| 197 | } |
| 198 | |
| 199 | public Errors subtypeNotProvided(Class<? extends Provider<?>> providerType, |
| 200 | Class<?> type) { |
| 201 | return addMessage("%s doesn't provide instances of %s.", providerType, type); |
| 202 | } |
| 203 | |
| 204 | public Errors notASubtype(Class<?> implementationType, Class<?> type) { |
| 205 | return addMessage("%s doesn't extend %s.", implementationType, type); |
| 206 | } |
| 207 | |
| 208 | public Errors recursiveImplementationType() { |
| 209 | return addMessage("@ImplementedBy points to the same class it annotates."); |
| 210 | } |
| 211 | |
| 212 | public Errors recursiveProviderType() { |
| 213 | return addMessage("@ProvidedBy points to the same class it annotates."); |
| 214 | } |
| 215 | |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 216 | public Errors missingRuntimeRetention(Class<? extends Annotation> annotation) { |
| 217 | return addMessage(format("Please annotate %s with @Retention(RUNTIME).", annotation)); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 220 | public Errors missingScopeAnnotation(Class<? extends Annotation> annotation) { |
| 221 | return addMessage(format("Please annotate %s with @ScopeAnnotation.", annotation)); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | public Errors optionalConstructor(Constructor constructor) { |
| 225 | return addMessage("%s is annotated @Inject(optional=true), " |
| 226 | + "but constructors cannot be optional.", constructor); |
| 227 | } |
| 228 | |
| 229 | public Errors cannotBindToGuiceType(String simpleName) { |
| 230 | return addMessage("Binding to core guice framework type is not allowed: %s.", simpleName); |
| 231 | } |
| 232 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 233 | public Errors scopeNotFound(Class<? extends Annotation> scopeAnnotation) { |
| 234 | return addMessage("No scope is bound to %s.", scopeAnnotation); |
| 235 | } |
| 236 | |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 237 | public Errors scopeAnnotationOnAbstractType( |
| 238 | Class<? extends Annotation> scopeAnnotation, Class<?> type, Object source) { |
| 239 | return addMessage("%s is annotated with %s, but scope annotations are not supported " |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 240 | + "for abstract types.%n Bound at %s.", type, scopeAnnotation, convert(source)); |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | public Errors misplacedBindingAnnotation(Member member, Annotation bindingAnnotation) { |
| 244 | return addMessage("%s is annotated with %s, but binding annotations should be applied " |
| 245 | + "to its parameters instead.", member, bindingAnnotation); |
| 246 | } |
| 247 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 248 | private static final String CONSTRUCTOR_RULES = |
| 249 | "Classes must have either one (and only one) constructor " |
limpbizkit | d37c58b | 2008-06-29 23:00:47 +0000 | [diff] [blame] | 250 | + "annotated with @Inject or a zero-argument constructor that is not private."; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 251 | |
| 252 | public Errors missingConstructor(Class<?> implementation) { |
| 253 | return addMessage("Could not find a suitable constructor in %s. " + CONSTRUCTOR_RULES, |
| 254 | implementation); |
| 255 | } |
| 256 | |
| 257 | public Errors tooManyConstructors(Class<?> implementation) { |
| 258 | return addMessage("%s has more than one constructor annotated with @Inject. " |
| 259 | + CONSTRUCTOR_RULES, implementation); |
| 260 | } |
| 261 | |
limpbizkit | 9b8bdc7 | 2009-06-07 19:37:28 +0000 | [diff] [blame] | 262 | public Errors constructorNotDefinedByType(Constructor<?> constructor, TypeLiteral<?> type) { |
| 263 | return addMessage("%s does not define %s", type, constructor); |
| 264 | } |
| 265 | |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 266 | public Errors duplicateScopes(ScopeBinding existing, |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 267 | Class<? extends Annotation> annotationType, Scope scope) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 268 | return addMessage("Scope %s is already bound to %s at %s.%n Cannot bind %s.", |
| 269 | existing.getScope(), annotationType, existing.getSource(), scope); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 270 | } |
| 271 | |
limpbizkit | 759662b | 2009-04-26 21:00:24 +0000 | [diff] [blame] | 272 | public Errors voidProviderMethod() { |
| 273 | return addMessage("Provider methods must return a value. Do not return void."); |
| 274 | } |
| 275 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 276 | public Errors missingConstantValues() { |
| 277 | return addMessage("Missing constant value. Please call to(...)."); |
| 278 | } |
| 279 | |
| 280 | public Errors cannotInjectInnerClass(Class<?> type) { |
| 281 | return addMessage("Injecting into inner classes is not supported. " |
| 282 | + "Please use a 'static' class (top-level or nested) instead of %s.", type); |
| 283 | } |
| 284 | |
| 285 | public Errors duplicateBindingAnnotations(Member member, |
| 286 | Class<? extends Annotation> a, Class<? extends Annotation> b) { |
| 287 | return addMessage("%s has more than one annotation annotated with @BindingAnnotation: " |
| 288 | + "%s and %s", member, a, b); |
| 289 | } |
| 290 | |
guice.mirrorbot@gmail.com | ee443bc | 2011-09-09 21:19:11 +0000 | [diff] [blame] | 291 | public Errors staticInjectionOnInterface(Class<?> clazz) { |
| 292 | return addMessage("%s is an interface, but interfaces have no static injection points.", clazz); |
| 293 | } |
| 294 | |
limpbizkit | b47d75a | 2009-09-08 00:17:45 +0000 | [diff] [blame] | 295 | public Errors cannotInjectFinalField(Field field) { |
| 296 | return addMessage("Injected field %s cannot be final.", field); |
| 297 | } |
| 298 | |
| 299 | public Errors cannotInjectAbstractMethod(Method method) { |
| 300 | return addMessage("Injected method %s cannot be abstract.", method); |
| 301 | } |
| 302 | |
| 303 | public Errors cannotInjectNonVoidMethod(Method method) { |
| 304 | return addMessage("Injected method %s must return void.", method); |
| 305 | } |
| 306 | |
| 307 | public Errors cannotInjectMethodWithTypeParameters(Method method) { |
| 308 | return addMessage("Injected method %s cannot declare type parameters of its own.", method); |
| 309 | } |
| 310 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 311 | public Errors duplicateScopeAnnotations( |
| 312 | Class<? extends Annotation> a, Class<? extends Annotation> b) { |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 313 | return addMessage("More than one scope annotation was found: %s and %s.", a, b); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | public Errors recursiveBinding() { |
| 317 | return addMessage("Binding points to itself."); |
| 318 | } |
| 319 | |
| 320 | public Errors bindingAlreadySet(Key<?> key, Object source) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 321 | return addMessage("A binding to %s was already configured at %s.", key, convert(source)); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 322 | } |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 323 | |
sberlin | 0d8b52f | 2011-02-21 21:06:40 +0000 | [diff] [blame] | 324 | public Errors jitBindingAlreadySet(Key<?> key) { |
| 325 | return addMessage("A just-in-time binding to %s was already configured on a parent injector.", key); |
| 326 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 327 | |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 328 | public Errors childBindingAlreadySet(Key<?> key, Set<Object> sources) { |
| 329 | Formatter allSources = new Formatter(); |
| 330 | for (Object source : sources) { |
| 331 | if (source == null) { |
| 332 | allSources.format("%n (bound by a just-in-time binding)"); |
| 333 | } else { |
| 334 | allSources.format("%n bound at %s", source); |
| 335 | } |
| 336 | } |
| 337 | Errors errors = addMessage( |
| 338 | "Unable to create binding for %s." |
| 339 | + " It was already configured on one or more child injectors or private modules" |
| 340 | + "%s%n" |
| 341 | + " If it was in a PrivateModule, did you forget to expose the binding?", |
| 342 | key, allSources.out()); |
| 343 | return errors; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 344 | } |
| 345 | |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 346 | public Errors errorCheckingDuplicateBinding(Key<?> key, Object source, Throwable t) { |
| 347 | return addMessage( |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 348 | "A binding to %s was already configured at %s and an error was thrown " |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 349 | + "while checking duplicate bindings. Error: %s", |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 350 | key, convert(source), t); |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 351 | } |
| 352 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 353 | public Errors errorInjectingMethod(Throwable cause) { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 354 | return errorInUserCode(cause, "Error injecting method, %s", cause); |
| 355 | } |
| 356 | |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 357 | public Errors errorNotifyingTypeListener(TypeListenerBinding listener, |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 358 | TypeLiteral<?> type, Throwable cause) { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 359 | return errorInUserCode(cause, |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 360 | "Error notifying TypeListener %s (bound at %s) of %s.%n" |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 361 | + " Reason: %s", |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 362 | listener.getListener(), convert(listener.getSource()), type, cause); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | public Errors errorInjectingConstructor(Throwable cause) { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 366 | return errorInUserCode(cause, "Error injecting constructor, %s", cause); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 367 | } |
| 368 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 369 | public Errors errorInProvider(RuntimeException runtimeException) { |
sberlin | 890b9ef | 2010-07-31 15:06:36 +0000 | [diff] [blame] | 370 | Throwable unwrapped = unwrap(runtimeException); |
| 371 | return errorInUserCode(unwrapped, "Error in custom provider, %s", unwrapped); |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 372 | } |
| 373 | |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 374 | public Errors errorInUserInjector( |
| 375 | MembersInjector<?> listener, TypeLiteral<?> type, RuntimeException cause) { |
| 376 | return errorInUserCode(cause, "Error injecting %s using %s.%n" |
| 377 | + " Reason: %s", type, listener, cause); |
| 378 | } |
| 379 | |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 380 | public Errors errorNotifyingInjectionListener( |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 381 | InjectionListener<?> listener, TypeLiteral<?> type, RuntimeException cause) { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 382 | return errorInUserCode(cause, "Error notifying InjectionListener %s of %s.%n" |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 383 | + " Reason: %s", listener, type, cause); |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 384 | } |
| 385 | |
sberlin | 3748e4a | 2010-05-23 13:40:06 +0000 | [diff] [blame] | 386 | public Errors exposedButNotBound(Key<?> key) { |
| 387 | return addMessage("Could not expose() %s, it must be explicitly bound.", key); |
| 388 | } |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 389 | |
sberlin | 3748e4a | 2010-05-23 13:40:06 +0000 | [diff] [blame] | 390 | public Errors keyNotFullySpecified(TypeLiteral<?> typeLiteral) { |
| 391 | return addMessage("%s cannot be used as a key; It is not fully specified.", typeLiteral); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 392 | } |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 393 | |
sberlin | b02627d | 2010-10-25 04:56:00 +0000 | [diff] [blame] | 394 | public Errors errorEnhancingClass(Class<?> clazz, Throwable cause) { |
| 395 | return errorInUserCode(cause, "Unable to method intercept: %s", clazz); |
| 396 | } |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 397 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 398 | public static Collection<Message> getMessagesFromThrowable(Throwable throwable) { |
| 399 | if (throwable instanceof ProvisionException) { |
| 400 | return ((ProvisionException) throwable).getErrorMessages(); |
| 401 | } else if (throwable instanceof ConfigurationException) { |
| 402 | return ((ConfigurationException) throwable).getErrorMessages(); |
| 403 | } else if (throwable instanceof CreationException) { |
| 404 | return ((CreationException) throwable).getErrorMessages(); |
| 405 | } else { |
| 406 | return ImmutableSet.of(); |
| 407 | } |
| 408 | } |
| 409 | |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 410 | public Errors errorInUserCode(Throwable cause, String messageFormat, Object... arguments) { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 411 | Collection<Message> messages = getMessagesFromThrowable(cause); |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 412 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 413 | if (!messages.isEmpty()) { |
| 414 | return merge(messages); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 415 | } else { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 416 | return addMessage(cause, messageFormat, arguments); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 419 | |
sberlin | 890b9ef | 2010-07-31 15:06:36 +0000 | [diff] [blame] | 420 | private Throwable unwrap(RuntimeException runtimeException) { |
| 421 | if(runtimeException instanceof Exceptions.UnhandledCheckedUserException) { |
| 422 | return runtimeException.getCause(); |
| 423 | } else { |
| 424 | return runtimeException; |
| 425 | } |
| 426 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 427 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 428 | public Errors cannotInjectRawProvider() { |
| 429 | return addMessage("Cannot inject a Provider that has no type parameter"); |
| 430 | } |
| 431 | |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 432 | public Errors cannotInjectRawMembersInjector() { |
| 433 | return addMessage("Cannot inject a MembersInjector that has no type parameter"); |
| 434 | } |
| 435 | |
limpbizkit | b0f3c9b | 2008-11-15 09:19:28 +0000 | [diff] [blame] | 436 | public Errors cannotInjectTypeLiteralOf(Type unsupportedType) { |
| 437 | return addMessage("Cannot inject a TypeLiteral of %s", unsupportedType); |
| 438 | } |
| 439 | |
| 440 | public Errors cannotInjectRawTypeLiteral() { |
| 441 | return addMessage("Cannot inject a TypeLiteral that has no type parameter"); |
| 442 | } |
| 443 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 444 | public Errors cannotSatisfyCircularDependency(Class<?> expectedType) { |
| 445 | return addMessage( |
| 446 | "Tried proxying %s to support a circular dependency, but it is not an interface.", |
| 447 | expectedType); |
| 448 | } |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 449 | |
sberlin | b997821 | 2010-04-24 21:42:40 +0000 | [diff] [blame] | 450 | public Errors circularProxiesDisabled(Class<?> expectedType) { |
| 451 | return addMessage( |
| 452 | "Tried proxying %s to support a circular dependency, but circular proxies are disabled.", |
| 453 | expectedType); |
| 454 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 455 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 456 | public void throwCreationExceptionIfErrorsExist() { |
| 457 | if (!hasErrors()) { |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | throw new CreationException(getMessages()); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 462 | } |
| 463 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 464 | public void throwConfigurationExceptionIfErrorsExist() { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 465 | if (!hasErrors()) { |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | throw new ConfigurationException(getMessages()); |
| 470 | } |
| 471 | |
| 472 | public void throwProvisionExceptionIfErrorsExist() { |
| 473 | if (!hasErrors()) { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | throw new ProvisionException(getMessages()); |
| 478 | } |
| 479 | |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 480 | private Message merge(Message message) { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 481 | List<Object> sources = Lists.newArrayList(); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 482 | sources.addAll(getSources()); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 483 | sources.addAll(message.getSources()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 484 | return new Message(sources, message.getMessage(), message.getCause()); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 485 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 486 | |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 487 | public Errors merge(Collection<Message> messages) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 488 | for (Message message : messages) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 489 | addMessage(merge(message)); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 490 | } |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 491 | return this; |
| 492 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 493 | |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 494 | public Errors merge(Errors moreErrors) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 495 | if (moreErrors.root == root || moreErrors.root.errors == null) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 496 | return this; |
| 497 | } |
| 498 | |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 499 | merge(moreErrors.root.errors); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 500 | return this; |
| 501 | } |
| 502 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 503 | public List<Object> getSources() { |
| 504 | List<Object> sources = Lists.newArrayList(); |
| 505 | for (Errors e = this; e != null; e = e.parent) { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 506 | if (e.source != SourceProvider.UNKNOWN_SOURCE) { |
| 507 | sources.add(0, e.source); |
| 508 | } |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 509 | } |
| 510 | return sources; |
| 511 | } |
| 512 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 513 | public void throwIfNewErrors(int expectedSize) throws ErrorsException { |
| 514 | if (size() == expectedSize) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | |
| 518 | throw toException(); |
| 519 | } |
| 520 | |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 521 | public ErrorsException toException() { |
| 522 | return new ErrorsException(this); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | public boolean hasErrors() { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 526 | return root.errors != null; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 527 | } |
| 528 | |
limpbizkit | 03c8d26 | 2008-12-02 06:15:57 +0000 | [diff] [blame] | 529 | public Errors addMessage(String messageFormat, Object... arguments) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 530 | return addMessage(null, messageFormat, arguments); |
| 531 | } |
| 532 | |
| 533 | private Errors addMessage(Throwable cause, String messageFormat, Object... arguments) { |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 534 | String message = format(messageFormat, arguments); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 535 | addMessage(new Message(getSources(), message, cause)); |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 536 | return this; |
| 537 | } |
| 538 | |
| 539 | public Errors addMessage(Message message) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 540 | if (root.errors == null) { |
| 541 | root.errors = Lists.newArrayList(); |
| 542 | } |
| 543 | root.errors.add(message); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 544 | return this; |
| 545 | } |
| 546 | |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 547 | public static String format(String messageFormat, Object... arguments) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 548 | for (int i = 0; i < arguments.length; i++) { |
| 549 | arguments[i] = Errors.convert(arguments[i]); |
| 550 | } |
| 551 | return String.format(messageFormat, arguments); |
| 552 | } |
| 553 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 554 | public List<Message> getMessages() { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 555 | if (root.errors == null) { |
| 556 | return ImmutableList.of(); |
| 557 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 558 | |
Sam Berlin | c00df28 | 2014-07-01 16:53:41 -0400 | [diff] [blame] | 559 | return new Ordering<Message>() { |
| 560 | @Override |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 561 | public int compare(Message a, Message b) { |
| 562 | return a.getSource().compareTo(b.getSource()); |
| 563 | } |
Sam Berlin | c00df28 | 2014-07-01 16:53:41 -0400 | [diff] [blame] | 564 | }.sortedCopy(root.errors); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 565 | } |
| 566 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 567 | /** Returns the formatted message for an exception with the specified messages. */ |
| 568 | public static String format(String heading, Collection<Message> errorMessages) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 569 | Formatter fmt = new Formatter().format(heading).format(":%n%n"); |
| 570 | int index = 1; |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 571 | boolean displayCauses = getOnlyCause(errorMessages) == null; |
| 572 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 573 | for (Message errorMessage : errorMessages) { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 574 | fmt.format("%s) %s%n", index++, errorMessage.getMessage()); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 575 | |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 576 | List<Object> dependencies = errorMessage.getSources(); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 577 | for (int i = dependencies.size() - 1; i >= 0; i--) { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 578 | Object source = dependencies.get(i); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 579 | formatSource(fmt, source); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 580 | } |
| 581 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 582 | Throwable cause = errorMessage.getCause(); |
| 583 | if (displayCauses && cause != null) { |
| 584 | StringWriter writer = new StringWriter(); |
| 585 | cause.printStackTrace(new PrintWriter(writer)); |
| 586 | fmt.format("Caused by: %s", writer.getBuffer()); |
| 587 | } |
| 588 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 589 | fmt.format("%n"); |
| 590 | } |
| 591 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 592 | if (errorMessages.size() == 1) { |
| 593 | fmt.format("1 error"); |
| 594 | } else { |
| 595 | fmt.format("%s errors", errorMessages.size()); |
| 596 | } |
| 597 | |
| 598 | return fmt.toString(); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 599 | } |
| 600 | |
limpbizkit | 918dff8 | 2008-08-03 01:29:46 +0000 | [diff] [blame] | 601 | /** |
| 602 | * Returns {@code value} if it is non-null allowed to be null. Otherwise a message is added and |
| 603 | * an {@code ErrorsException} is thrown. |
| 604 | */ |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 605 | public <T> T checkForNull(T value, Object source, Dependency<?> dependency) |
limpbizkit | 918dff8 | 2008-08-03 01:29:46 +0000 | [diff] [blame] | 606 | throws ErrorsException { |
sberlin | a23937e | 2011-06-29 22:02:50 +0000 | [diff] [blame] | 607 | if (value != null || dependency.isNullable() ) { |
limpbizkit | 918dff8 | 2008-08-03 01:29:46 +0000 | [diff] [blame] | 608 | return value; |
| 609 | } |
| 610 | |
sameb | 9867f9c | 2015-02-02 12:45:25 -0800 | [diff] [blame^] | 611 | // Hack to allow null parameters to @Provides methods, for backwards compatibility. |
| 612 | if (dependency.getInjectionPoint().getMember() instanceof Method) { |
| 613 | Method annotated = (Method) dependency.getInjectionPoint().getMember(); |
| 614 | if (annotated.isAnnotationPresent(Provides.class)) { |
| 615 | switch (InternalFlags.getNullableProvidesOption()) { |
| 616 | case ERROR: |
| 617 | break; // break out & let the below exception happen |
| 618 | case IGNORE: |
| 619 | return value; // user doesn't care about injecting nulls to non-@Nullables. |
| 620 | case WARN: |
| 621 | logger.log(Level.WARNING, |
| 622 | "Guice injected null into parameter {0} of {1} (a {2}), please mark it @Nullable." |
| 623 | + " Use -Dguice_check_nullable_provides_params=ERROR to turn this into an" |
| 624 | + " error.", |
| 625 | new Object[] { |
| 626 | dependency.getParameterIndex(), |
| 627 | convert(dependency.getInjectionPoint().getMember()), |
| 628 | convert(dependency.getKey())}); |
| 629 | return null; // log & exit. |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 634 | int parameterIndex = dependency.getParameterIndex(); |
limpbizkit | 918dff8 | 2008-08-03 01:29:46 +0000 | [diff] [blame] | 635 | String parameterName = (parameterIndex != -1) |
| 636 | ? "parameter " + parameterIndex + " of " |
| 637 | : ""; |
| 638 | addMessage("null returned by binding at %s%n but %s%s is not @Nullable", |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 639 | source, parameterName, dependency.getInjectionPoint().getMember()); |
limpbizkit | 918dff8 | 2008-08-03 01:29:46 +0000 | [diff] [blame] | 640 | |
| 641 | throw toException(); |
| 642 | } |
| 643 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 644 | /** |
| 645 | * Returns the cause throwable if there is exactly one cause in {@code messages}. If there are |
| 646 | * zero or multiple messages with causes, null is returned. |
| 647 | */ |
| 648 | public static Throwable getOnlyCause(Collection<Message> messages) { |
| 649 | Throwable onlyCause = null; |
| 650 | for (Message message : messages) { |
| 651 | Throwable messageCause = message.getCause(); |
| 652 | if (messageCause == null) { |
| 653 | continue; |
| 654 | } |
| 655 | |
| 656 | if (onlyCause != null) { |
| 657 | return null; |
| 658 | } |
| 659 | |
| 660 | onlyCause = messageCause; |
| 661 | } |
| 662 | |
| 663 | return onlyCause; |
| 664 | } |
| 665 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 666 | public int size() { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 667 | return root.errors == null ? 0 : root.errors.size(); |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 668 | } |
| 669 | |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 670 | private static abstract class Converter<T> { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 671 | |
| 672 | final Class<T> type; |
| 673 | |
| 674 | Converter(Class<T> type) { |
| 675 | this.type = type; |
| 676 | } |
| 677 | |
| 678 | boolean appliesTo(Object o) { |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 679 | return o != null && type.isAssignableFrom(o.getClass()); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | String convert(Object o) { |
| 683 | return toString(type.cast(o)); |
| 684 | } |
| 685 | |
| 686 | abstract String toString(T t); |
| 687 | } |
| 688 | |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 689 | private static final Collection<Converter<?>> converters = ImmutableList.of( |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 690 | new Converter<Class>(Class.class) { |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 691 | @Override public String toString(Class c) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 692 | return c.getName(); |
| 693 | } |
| 694 | }, |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 695 | new Converter<Member>(Member.class) { |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 696 | @Override public String toString(Member member) { |
sberlin | dfee9fd | 2010-10-30 14:15:06 +0000 | [diff] [blame] | 697 | return Classes.toString(member); |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 698 | } |
| 699 | }, |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 700 | new Converter<Key>(Key.class) { |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 701 | @Override public String toString(Key key) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 702 | if (key.getAnnotationType() != null) { |
| 703 | return key.getTypeLiteral() + " annotated with " |
| 704 | + (key.getAnnotation() != null ? key.getAnnotation() : key.getAnnotationType()); |
| 705 | } else { |
| 706 | return key.getTypeLiteral().toString(); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 707 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 708 | } |
| 709 | }); |
| 710 | |
| 711 | public static Object convert(Object o) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 712 | ElementSource source = null; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 713 | if (o instanceof ElementSource) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 714 | source = (ElementSource)o; |
| 715 | o = source.getDeclaringSource(); |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 716 | } |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 717 | return convert(o, source); |
| 718 | } |
| 719 | |
| 720 | public static Object convert(Object o, ElementSource source) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 721 | for (Converter<?> converter : converters) { |
| 722 | if (converter.appliesTo(o)) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 723 | return appendModules(converter.convert(o), source); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 726 | return appendModules(o, source); |
| 727 | } |
| 728 | |
| 729 | private static Object appendModules(Object source, ElementSource elementSource) { |
| 730 | String modules = moduleSourceString(elementSource); |
| 731 | if (modules.length() == 0) { |
| 732 | return source; |
| 733 | } else { |
| 734 | return source + modules; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | private static String moduleSourceString(ElementSource elementSource) { |
| 739 | // if we only have one module (or don't know what they are), then don't bother |
| 740 | // reporting it, because the source already is going to report exactly that module. |
| 741 | if (elementSource == null) { |
| 742 | return ""; |
| 743 | } |
| 744 | List<String> modules = Lists.newArrayList(elementSource.getModuleClassNames()); |
| 745 | // Insert any original element sources w/ module info into the path. |
| 746 | while(elementSource.getOriginalElementSource() != null) { |
| 747 | elementSource = elementSource.getOriginalElementSource(); |
| 748 | modules.addAll(0, elementSource.getModuleClassNames()); |
| 749 | } |
| 750 | if (modules.size() <= 1) { |
| 751 | return ""; |
| 752 | } |
| 753 | |
| 754 | // Ideally we'd do: |
| 755 | // return Joiner.on(" -> ") |
| 756 | // .appendTo(new StringBuilder(" (via modules: "), Lists.reverse(modules)) |
| 757 | // .append(")").toString(); |
| 758 | // ... but for some reason we can't find Lists.reverse, so do it the boring way. |
| 759 | StringBuilder builder = new StringBuilder(" (via modules: "); |
| 760 | for (int i = modules.size() - 1; i >= 0; i--) { |
| 761 | builder.append(modules.get(i)); |
| 762 | if (i != 0) { |
| 763 | builder.append(" -> "); |
| 764 | } |
| 765 | } |
| 766 | builder.append(")"); |
| 767 | return builder.toString(); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 768 | } |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 769 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 770 | public static void formatSource(Formatter formatter, Object source) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 771 | ElementSource elementSource = null; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 772 | if (source instanceof ElementSource) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 773 | elementSource = (ElementSource)source; |
| 774 | source = elementSource.getDeclaringSource(); |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 775 | } |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 776 | formatSource(formatter, source, elementSource); |
| 777 | } |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 778 | |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 779 | public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) { |
| 780 | String modules = moduleSourceString(elementSource); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 781 | if (source instanceof Dependency) { |
| 782 | Dependency<?> dependency = (Dependency<?>) source; |
| 783 | InjectionPoint injectionPoint = dependency.getInjectionPoint(); |
| 784 | if (injectionPoint != null) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 785 | formatInjectionPoint(formatter, dependency, injectionPoint, elementSource); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 786 | } else { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 787 | formatSource(formatter, dependency.getKey(), elementSource); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 788 | } |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 789 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 790 | } else if (source instanceof InjectionPoint) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 791 | formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 792 | |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 793 | } else if (source instanceof Class) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 794 | formatter.format(" at %s%s%n", StackTraceElements.forType((Class<?>) source), modules); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 795 | |
| 796 | } else if (source instanceof Member) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 797 | formatter.format(" at %s%s%n", StackTraceElements.forMember((Member) source), modules); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 798 | |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 799 | } else if (source instanceof TypeLiteral) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 800 | formatter.format(" while locating %s%s%n", source, modules); |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 801 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 802 | } else if (source instanceof Key) { |
| 803 | Key<?> key = (Key<?>) source; |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 804 | formatter.format(" while locating %s%n", convert(key, elementSource)); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 805 | |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 806 | } else { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 807 | formatter.format(" at %s%s%n", source, modules); |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 810 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 811 | public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency, |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 812 | InjectionPoint injectionPoint, ElementSource elementSource) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 813 | Member member = injectionPoint.getMember(); |
sberlin | dfee9fd | 2010-10-30 14:15:06 +0000 | [diff] [blame] | 814 | Class<? extends Member> memberType = Classes.memberType(member); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 815 | |
| 816 | if (memberType == Field.class) { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 817 | dependency = injectionPoint.getDependencies().get(0); |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 818 | formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 819 | formatter.format(" for field at %s%n", StackTraceElements.forMember(member)); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 820 | |
| 821 | } else if (dependency != null) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 822 | formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 823 | formatter.format(" for parameter %s at %s%n", |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 824 | dependency.getParameterIndex(), StackTraceElements.forMember(member)); |
| 825 | |
| 826 | } else { |
| 827 | formatSource(formatter, injectionPoint.getMember()); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 828 | } |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 829 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 830 | } |