limpbizkit | 916f548 | 2008-04-16 20:51:14 +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 | |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 18 | package com.google.inject; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 19 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 21 | import com.google.inject.internal.ErrorHandler; |
| 22 | import com.google.inject.internal.ErrorMessage; |
| 23 | import com.google.inject.internal.StackTraceElements; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 24 | import java.lang.reflect.Constructor; |
| 25 | |
| 26 | /** |
| 27 | * @author jessewilson@google.com (Jesse Wilson) |
| 28 | */ |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 29 | class RuntimeReflectionFactory implements Reflection.Factory { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 30 | public Reflection create(ErrorHandler errorHandler, |
| 31 | ConstructionProxyFactory constructionProxyFactory) { |
| 32 | return new RuntimeReflection(errorHandler, constructionProxyFactory); |
| 33 | } |
| 34 | |
| 35 | private static class RuntimeReflection implements Reflection { |
| 36 | private final ErrorHandler errorHandler; |
| 37 | private final ConstructionProxyFactory constructionProxyFactory; |
| 38 | |
| 39 | private RuntimeReflection(ErrorHandler errorHandler, |
| 40 | ConstructionProxyFactory constructionProxyFactory) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 41 | this.errorHandler = checkNotNull(errorHandler, "errorHandler"); |
| 42 | this.constructionProxyFactory = checkNotNull(constructionProxyFactory, "constructionProxyFatory"); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public <T> ConstructionProxy<T> getConstructionProxy(Class<T> implementation) { |
| 46 | return constructionProxyFactory.get(findConstructorIn(implementation)); |
| 47 | } |
| 48 | |
| 49 | private <T> Constructor<T> findConstructorIn(Class<T> implementation) { |
| 50 | Constructor<T> found = null; |
| 51 | @SuppressWarnings("unchecked") |
| 52 | Constructor<T>[] constructors |
| 53 | = (Constructor<T>[]) implementation.getDeclaredConstructors(); |
| 54 | for (Constructor<T> constructor : constructors) { |
| 55 | Inject inject = constructor.getAnnotation(Inject.class); |
| 56 | if (inject != null) { |
| 57 | if (inject.optional()) { |
| 58 | errorHandler.handle( |
| 59 | StackTraceElements.forMember(constructor), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 60 | ErrorMessage.optionalConstructor()); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (found != null) { |
| 64 | errorHandler.handle( |
| 65 | StackTraceElements.forMember(found), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 66 | ErrorMessage.tooManyConstructors()); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 67 | return invalidConstructor(); |
| 68 | } |
| 69 | found = constructor; |
| 70 | } |
| 71 | } |
| 72 | if (found != null) { |
| 73 | return found; |
| 74 | } |
| 75 | |
| 76 | // If no annotated constructor is found, look for a no-arg constructor |
| 77 | // instead. |
| 78 | try { |
| 79 | return implementation.getDeclaredConstructor(); |
| 80 | } |
| 81 | catch (NoSuchMethodException e) { |
| 82 | errorHandler.handle( |
| 83 | StackTraceElements.forMember( |
| 84 | implementation.getDeclaredConstructors()[0]), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 85 | ErrorMessage.missingConstructor(implementation)); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 86 | return invalidConstructor(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * A placeholder. This enables us to continue processing and gather more |
| 92 | * errors but blows up if you actually try to use it. |
| 93 | */ |
| 94 | static class InvalidConstructor { |
| 95 | InvalidConstructor() { |
| 96 | throw new AssertionError(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @SuppressWarnings("unchecked") |
| 101 | static <T> Constructor<T> invalidConstructor() { |
| 102 | try { |
| 103 | return (Constructor<T>) InvalidConstructor.class.getDeclaredConstructor(); |
| 104 | } |
| 105 | catch (NoSuchMethodException e) { |
| 106 | throw new AssertionError(e); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |