sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 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.throwingproviders; |
| 18 | |
| 19 | import java.lang.annotation.Annotation; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.util.List; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import com.google.inject.Binder; |
| 26 | import com.google.inject.Exposed; |
| 27 | import com.google.inject.Key; |
| 28 | import com.google.inject.PrivateBinder; |
| 29 | import com.google.inject.Provider; |
| 30 | import com.google.inject.TypeLiteral; |
| 31 | import com.google.inject.binder.ScopedBindingBuilder; |
| 32 | import com.google.inject.internal.util.ImmutableSet; |
| 33 | import com.google.inject.internal.util.StackTraceElements; |
| 34 | import com.google.inject.spi.Dependency; |
| 35 | import com.google.inject.spi.HasDependencies; |
| 36 | import com.google.inject.throwingproviders.ThrowingProviderBinder.SecondaryBinder; |
| 37 | |
| 38 | /** |
| 39 | * A provider that invokes a method and returns its result. |
| 40 | * |
| 41 | * @author sameb@google.com (Sam Berlin) |
| 42 | */ |
sberlin@gmail.com | ba8a4cd | 2010-11-27 01:56:53 +0000 | [diff] [blame] | 43 | class CheckedProviderMethod<T> implements CheckedProvider<T>, HasDependencies { |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 44 | private final Key<T> key; |
| 45 | private final Class<? extends Annotation> scopeAnnotation; |
| 46 | private final Object instance; |
| 47 | private final Method method; |
| 48 | private final ImmutableSet<Dependency<?>> dependencies; |
| 49 | private final List<Provider<?>> parameterProviders; |
| 50 | private final boolean exposed; |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 51 | private final Class<? extends CheckedProvider> checkedProvider; |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 52 | private final List<TypeLiteral<?>> exceptionTypes; |
| 53 | |
sberlin@gmail.com | ba8a4cd | 2010-11-27 01:56:53 +0000 | [diff] [blame] | 54 | CheckedProviderMethod( |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 55 | Key<T> key, |
| 56 | Method method, |
| 57 | Object instance, |
| 58 | ImmutableSet<Dependency<?>> dependencies, |
| 59 | List<Provider<?>> parameterProviders, |
| 60 | Class<? extends Annotation> scopeAnnotation, |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 61 | Class<? extends CheckedProvider> checkedProvider, |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 62 | List<TypeLiteral<?>> exceptionTypes) { |
| 63 | this.key = key; |
| 64 | this.scopeAnnotation = scopeAnnotation; |
| 65 | this.instance = instance; |
| 66 | this.dependencies = dependencies; |
| 67 | this.method = method; |
| 68 | this.parameterProviders = parameterProviders; |
| 69 | this.exposed = method.isAnnotationPresent(Exposed.class); |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 70 | this.checkedProvider = checkedProvider; |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 71 | this.exceptionTypes = exceptionTypes; |
| 72 | |
| 73 | method.setAccessible(true); |
| 74 | } |
| 75 | |
sberlin@gmail.com | ba8a4cd | 2010-11-27 01:56:53 +0000 | [diff] [blame] | 76 | void configure(Binder binder) { |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 77 | binder = binder.withSource(method); |
| 78 | |
| 79 | SecondaryBinder<?> sbinder = |
| 80 | ThrowingProviderBinder.create(binder) |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 81 | .bind(checkedProvider, key.getTypeLiteral().getType()); |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 82 | if(key.getAnnotation() != null) { |
| 83 | sbinder = sbinder.annotatedWith(key.getAnnotation()); |
| 84 | } else if(key.getAnnotationType() != null) { |
| 85 | sbinder = sbinder.annotatedWith(key.getAnnotationType()); |
| 86 | } |
| 87 | ScopedBindingBuilder sbbuilder = sbinder.toProviderMethod(this); |
| 88 | if(scopeAnnotation != null) { |
| 89 | sbbuilder.in(scopeAnnotation); |
| 90 | } |
| 91 | |
| 92 | if (exposed) { |
| 93 | // the cast is safe 'cause the only binder we have implements PrivateBinder. If there's a |
| 94 | // misplaced @Exposed, calling this will add an error to the binder's error queue |
sberlin@gmail.com | ba8a4cd | 2010-11-27 01:56:53 +0000 | [diff] [blame] | 95 | ((PrivateBinder) binder).expose(sbinder.getKey()); |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Validate the exceptions in the method match the exceptions |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 99 | // in the CheckedProvider. |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 100 | for(TypeLiteral<?> exType : exceptionTypes) { |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 101 | Class<?> exActual = exType.getRawType(); |
| 102 | // Ignore runtime exceptions & errors. |
| 103 | if(RuntimeException.class.isAssignableFrom(exActual) || Error.class.isAssignableFrom(exActual)) { |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 104 | continue; |
| 105 | } |
| 106 | |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 107 | boolean notAssignable = true; |
| 108 | for(Class<? extends Throwable> exExpected : sbinder.getExceptionTypes()) { |
| 109 | if (exExpected.isAssignableFrom(exActual)) { |
| 110 | notAssignable = false; |
| 111 | break; |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 114 | if(notAssignable) { |
| 115 | binder.addError( |
| 116 | "%s is not compatible with the exceptions (%s) declared in the CheckedProvider interface (%s)", |
| 117 | exActual, sbinder.getExceptionTypes(), checkedProvider); |
| 118 | } |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | public T get() throws Exception { |
| 123 | Object[] parameters = new Object[parameterProviders.size()]; |
| 124 | for (int i = 0; i < parameters.length; i++) { |
| 125 | parameters[i] = parameterProviders.get(i).get(); |
| 126 | } |
| 127 | |
| 128 | try { |
| 129 | // We know this cast is safe becase T is the method's return type. |
| 130 | @SuppressWarnings({ "unchecked", "UnnecessaryLocalVariable" }) |
| 131 | T result = (T) method.invoke(instance, parameters); |
| 132 | return result; |
| 133 | } catch (IllegalAccessException e) { |
| 134 | throw new AssertionError(e); |
| 135 | } catch (InvocationTargetException e) { |
| 136 | Throwable t = e.getCause(); |
| 137 | if(t instanceof Exception) { |
| 138 | throw (Exception)t; |
| 139 | } else if(t instanceof Error) { |
| 140 | throw (Error)t; |
| 141 | } else { |
| 142 | throw new IllegalStateException(t); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public Set<Dependency<?>> getDependencies() { |
| 148 | return dependencies; |
| 149 | } |
| 150 | |
| 151 | @Override public String toString() { |
sberlin | 488284d | 2010-11-17 14:27:41 +0000 | [diff] [blame] | 152 | return "@CheckedProvides " + StackTraceElements.forMember(method).toString(); |
sberlin | ee446ca | 2010-10-24 21:36:39 +0000 | [diff] [blame] | 153 | } |
| 154 | } |