blob: b43329e5747a988cef1e1230ed9b974eb635b510 [file] [log] [blame]
limpbizkit916f5482008-04-16 20:51:14 +00001/**
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
18package com.google.inject;
19
limpbizkit009bb092008-05-07 06:36:02 +000020import com.google.inject.internal.ErrorHandler;
21import com.google.inject.internal.Keys;
22import net.sf.cglib.reflect.FastConstructor;
23
24import java.lang.annotation.Annotation;
limpbizkit916f5482008-04-16 20:51:14 +000025import java.lang.reflect.Constructor;
limpbizkit009bb092008-05-07 06:36:02 +000026import java.lang.reflect.Member;
limpbizkit916f5482008-04-16 20:51:14 +000027import java.lang.reflect.Method;
28import java.lang.reflect.Type;
limpbizkit916f5482008-04-16 20:51:14 +000029import java.util.ArrayList;
limpbizkit009bb092008-05-07 06:36:02 +000030import java.util.Arrays;
31import java.util.Iterator;
32import java.util.List;
limpbizkit916f5482008-04-16 20:51:14 +000033
34/**
35 * A method or constructor parameter, plus Guice metadata.
36 *
37 * @author jessewilson@google.com (Jesse Wilson)
38 */
limpbizkit009bb092008-05-07 06:36:02 +000039class Parameter<T> {
limpbizkit916f5482008-04-16 20:51:14 +000040 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}