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 | |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.*; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 21 | import static com.google.inject.internal.Objects.nonNull; |
| 22 | |
| 23 | import java.lang.reflect.Constructor; |
| 24 | |
| 25 | /** |
| 26 | * @author jessewilson@google.com (Jesse Wilson) |
| 27 | */ |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 28 | class RuntimeReflectionFactory implements Reflection.Factory { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 29 | public Reflection create(ErrorHandler errorHandler, |
| 30 | ConstructionProxyFactory constructionProxyFactory) { |
| 31 | return new RuntimeReflection(errorHandler, constructionProxyFactory); |
| 32 | } |
| 33 | |
| 34 | private static class RuntimeReflection implements Reflection { |
| 35 | private final ErrorHandler errorHandler; |
| 36 | private final ConstructionProxyFactory constructionProxyFactory; |
| 37 | |
| 38 | private RuntimeReflection(ErrorHandler errorHandler, |
| 39 | ConstructionProxyFactory constructionProxyFactory) { |
| 40 | this.errorHandler = nonNull(errorHandler, "errorHandler"); |
| 41 | this.constructionProxyFactory = nonNull(constructionProxyFactory, "constructionProxyFatory"); |
| 42 | } |
| 43 | |
| 44 | public <T> ConstructionProxy<T> getConstructionProxy(Class<T> implementation) { |
| 45 | return constructionProxyFactory.get(findConstructorIn(implementation)); |
| 46 | } |
| 47 | |
| 48 | private <T> Constructor<T> findConstructorIn(Class<T> implementation) { |
| 49 | Constructor<T> found = null; |
| 50 | @SuppressWarnings("unchecked") |
| 51 | Constructor<T>[] constructors |
| 52 | = (Constructor<T>[]) implementation.getDeclaredConstructors(); |
| 53 | for (Constructor<T> constructor : constructors) { |
| 54 | Inject inject = constructor.getAnnotation(Inject.class); |
| 55 | if (inject != null) { |
| 56 | if (inject.optional()) { |
| 57 | errorHandler.handle( |
| 58 | StackTraceElements.forMember(constructor), |
| 59 | ErrorMessages.OPTIONAL_CONSTRUCTOR); |
| 60 | } |
| 61 | |
| 62 | if (found != null) { |
| 63 | errorHandler.handle( |
| 64 | StackTraceElements.forMember(found), |
| 65 | ErrorMessages.TOO_MANY_CONSTRUCTORS); |
| 66 | return invalidConstructor(); |
| 67 | } |
| 68 | found = constructor; |
| 69 | } |
| 70 | } |
| 71 | if (found != null) { |
| 72 | return found; |
| 73 | } |
| 74 | |
| 75 | // If no annotated constructor is found, look for a no-arg constructor |
| 76 | // instead. |
| 77 | try { |
| 78 | return implementation.getDeclaredConstructor(); |
| 79 | } |
| 80 | catch (NoSuchMethodException e) { |
| 81 | errorHandler.handle( |
| 82 | StackTraceElements.forMember( |
| 83 | implementation.getDeclaredConstructors()[0]), |
| 84 | ErrorMessages.MISSING_CONSTRUCTOR, |
| 85 | implementation); |
| 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 | } |