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 | |
| 18 | package com.google.inject; |
| 19 | |
limpbizkit | 009bb09 | 2008-05-07 06:36:02 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.ErrorHandler; |
| 21 | import com.google.inject.internal.Keys; |
| 22 | import net.sf.cglib.reflect.FastConstructor; |
| 23 | |
| 24 | import java.lang.annotation.Annotation; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 25 | import java.lang.reflect.Constructor; |
limpbizkit | 009bb09 | 2008-05-07 06:36:02 +0000 | [diff] [blame] | 26 | import java.lang.reflect.Member; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 27 | import java.lang.reflect.Method; |
| 28 | import java.lang.reflect.Type; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 29 | import java.util.ArrayList; |
limpbizkit | 009bb09 | 2008-05-07 06:36:02 +0000 | [diff] [blame] | 30 | import java.util.Arrays; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.List; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A method or constructor parameter, plus Guice metadata. |
| 36 | * |
| 37 | * @author jessewilson@google.com (Jesse Wilson) |
| 38 | */ |
limpbizkit | 009bb09 | 2008-05-07 06:36:02 +0000 | [diff] [blame] | 39 | class Parameter<T> { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 40 | private final int index; |
| 41 | private final Key<T> key; |
| 42 | private final Nullability nullability; |
| 43 | |
| 44 | private Parameter(int index, Key<T> key, Nullability nullability) { |
| 45 | this.index = index; |
| 46 | this.key = key; |
| 47 | this.nullability = nullability; |
| 48 | } |
| 49 | |
| 50 | public static <T> Parameter<T> create(int index, Key<T> key, Nullability nullability) { |
| 51 | return new Parameter<T>(index, key, nullability); |
| 52 | } |
| 53 | |
| 54 | public Key<T> getKey() { |
| 55 | return key; |
| 56 | } |
| 57 | |
| 58 | public Nullability getNullability() { |
| 59 | return nullability; |
| 60 | } |
| 61 | |
| 62 | public int getIndex() { |
| 63 | return index; |
| 64 | } |
| 65 | |
| 66 | public static List<Parameter<?>> forMethod(ErrorHandler errorHandler, Method method) { |
| 67 | return forMember(errorHandler, method, method.getGenericParameterTypes(), |
| 68 | method.getParameterAnnotations()); |
| 69 | } |
| 70 | |
| 71 | public static List<Parameter<?>> forConstructor( |
| 72 | ErrorHandler errorHandler, Constructor constructor) { |
| 73 | return forMember(errorHandler, constructor, constructor.getGenericParameterTypes(), |
| 74 | constructor.getParameterAnnotations()); |
| 75 | } |
| 76 | |
| 77 | public static List<Parameter<?>> forConstructor( |
| 78 | ErrorHandler errorHandler, FastConstructor constructor) { |
| 79 | return forConstructor(errorHandler, constructor.getJavaConstructor()); |
| 80 | } |
| 81 | |
| 82 | private static List<Parameter<?>> forMember(ErrorHandler errorHandler, Member member, |
| 83 | Type[] genericParameterTypes, Annotation[][] annotations) { |
| 84 | Iterator<Annotation[]> annotationsIterator = Arrays.asList(annotations).iterator(); |
| 85 | |
| 86 | List<Parameter<?>> parameters = new ArrayList<Parameter<?>>(); |
| 87 | int index = 0; |
| 88 | for (Type parameterType : genericParameterTypes) { |
| 89 | Annotation[] parameterAnnotations = annotationsIterator.next(); |
| 90 | Key<?> key = Keys.get(parameterType, member, parameterAnnotations, errorHandler); |
| 91 | parameters.add(create(index, key, Nullability.forAnnotations(parameterAnnotations))); |
| 92 | index++; |
| 93 | } |
| 94 | |
| 95 | return parameters; |
| 96 | } |
| 97 | } |