KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > MethodParameter


1 /*
2  * Copyright 2002-2007 the original author or authors.
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 org.springframework.core;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.springframework.util.Assert;
25
26 /**
27  * Helper class that encapsulates the specification of a method parameter, i.e.
28  * a Method or Constructor plus a parameter index and a nested type index for
29  * a declared generic type. Useful as a specification object to pass along.
30  *
31  * <p>Used by {@link GenericCollectionTypeResolver},
32  * {@link org.springframework.beans.BeanWrapperImpl} and
33  * {@link org.springframework.beans.factory.support.AbstractBeanFactory}.
34  *
35  * <p>Note that this class does not depend on JDK 1.5 API artifacts,
36  * to remain compatible with JDK 1.3/1.4. Concrete generic type resolution
37  * via JDK 1.5 API happens in {@link GenericCollectionTypeResolver} only.
38  *
39  * @author Juergen Hoeller
40  * @since 2.0
41  * @see GenericCollectionTypeResolver
42  */

43 public class MethodParameter {
44
45     private Method JavaDoc method;
46
47     private Constructor JavaDoc constructor;
48
49     private final int parameterIndex;
50
51     private int nestingLevel;
52
53     /** Map from Integer level to Integer type index */
54     private final Map JavaDoc typeIndexesPerLevel = new HashMap JavaDoc(4);
55
56
57     /**
58      * Create a new MethodParameter for the given method, with nesting level 1.
59      * @param method the Method to specify a parameter for
60      * @param parameterIndex the index of the parameter
61      */

62     public MethodParameter(Method JavaDoc method, int parameterIndex) {
63         this(method, parameterIndex, 1);
64     }
65
66     /**
67      * Create a new MethodParameter for the given method.
68      * @param method the Method to specify a parameter for
69      * @param parameterIndex the index of the parameter
70      * (-1 for the method return type; 0 for the first method parameter,
71      * 1 for the second method parameter, etc)
72      * @param nestingLevel the nesting level of the target type
73      * (typically 1; e.g. in case of a List of Lists, 1 would indicate the
74      * nested List, whereas 2 would indicate the element of the nested List)
75      */

76     public MethodParameter(Method JavaDoc method, int parameterIndex, int nestingLevel) {
77         Assert.notNull(method, "Method must not be null");
78         Assert.isTrue(parameterIndex >= -1, "Parameter index must not be -1 or higher");
79         Assert.isTrue(parameterIndex < method.getParameterTypes().length,
80                 "Parameter index must not exceed " + (method.getParameterTypes().length - 1));
81         this.method = method;
82         this.parameterIndex = parameterIndex;
83         this.nestingLevel = nestingLevel;
84     }
85
86     /**
87      * Create a new MethodParameter for the given constructor, with nesting level 1.
88      * @param constructor the Constructor to specify a parameter for
89      * @param parameterIndex the index of the parameter
90      */

91     public MethodParameter(Constructor JavaDoc constructor, int parameterIndex) {
92         this(constructor, parameterIndex, 1);
93     }
94
95     /**
96      * Create a new MethodParameter for the given constructor.
97      * @param constructor the Constructor to specify a parameter for
98      * @param parameterIndex the index of the parameter
99      * @param nestingLevel the nesting level of the target type
100      * (typically 1; e.g. in case of a List of Lists, 1 would indicate the
101      * nested List, whereas 2 would indicate the element of the nested List)
102      */

103     public MethodParameter(Constructor JavaDoc constructor, int parameterIndex, int nestingLevel) {
104         Assert.notNull(constructor, "Constructor must not be null");
105         Assert.isTrue(parameterIndex >= 0, "Parameter index must not be negative");
106         Assert.isTrue(parameterIndex < constructor.getParameterTypes().length,
107                 "Parameter index must not exceed " + (constructor.getParameterTypes().length - 1));
108         this.constructor = constructor;
109         this.parameterIndex = parameterIndex;
110         this.nestingLevel = nestingLevel;
111     }
112
113
114     /**
115      * Return the Method held, if any.
116      * <p>Note: Either Method or Constructor is available.
117      * @return the Method, or <code>null</code> if none
118      */

119     public Method JavaDoc getMethod() {
120         return this.method;
121     }
122
123     /**
124      * Return the Constructor held, if any.
125      * <p>Note: Either Method or Constructor is available.
126      * @return the Constructor, or <code>null</code> if none
127      */

128     public Constructor JavaDoc getConstructor() {
129         return this.constructor;
130     }
131
132     /**
133      * Return the index of the method/constructor parameter.
134      * @return the parameter index (never negative)
135      */

136     public int getParameterIndex() {
137         return this.parameterIndex;
138     }
139
140
141     /**
142      * Increase this parameter's nesting level.
143      * @see #getNestingLevel()
144      */

145     public void increaseNestingLevel() {
146         this.nestingLevel++;
147     }
148
149     /**
150      * Decrease this parameter's nesting level.
151      * @see #getNestingLevel()
152      */

153     public void decreaseNestingLevel() {
154         this.typeIndexesPerLevel.remove(new Integer JavaDoc(this.nestingLevel));
155         this.nestingLevel--;
156     }
157
158     /**
159      * Return the nesting level of the target type
160      * (typically 1; e.g. in case of a List of Lists, 1 would indicate the
161      * nested List, whereas 2 would indicate the element of the nested List).
162      */

163     public int getNestingLevel() {
164         return this.nestingLevel;
165     }
166
167     /**
168      * Set the type index for the current nesting level.
169      * @param typeIndex the corresponding type index
170      * (or <code>null</code> for the default type index)
171      * @see #getNestingLevel()
172      */

173     public void setTypeIndexForCurrentLevel(int typeIndex) {
174         this.typeIndexesPerLevel.put(new Integer JavaDoc(this.nestingLevel), new Integer JavaDoc(typeIndex));
175     }
176
177     /**
178      * Return the type index for the current nesting level.
179      * @return the corresponding type index, or <code>null</code>
180      * if none specified (indicating the default type index)
181      * @see #getNestingLevel()
182      */

183     public Integer JavaDoc getTypeIndexForCurrentLevel() {
184         return getTypeIndexForLevel(this.nestingLevel);
185     }
186
187     /**
188      * Return the type index for the specified nesting level.
189      * @param nestingLevel the nesting level to check
190      * @return the corresponding type index, or <code>null</code>
191      * if none specified (indicating the default type index)
192      */

193     public Integer JavaDoc getTypeIndexForLevel(int nestingLevel) {
194         return (Integer JavaDoc) this.typeIndexesPerLevel.get(new Integer JavaDoc(nestingLevel));
195     }
196
197
198     /**
199      * Create a new MethodParameter for the given method or constructor.
200      * <p>This is a convenience constructor for scenarios where a
201      * Method or Constructor reference is treated in a generic fashion.
202      * @param methodOrConstructor the Method or Constructor to specify a parameter for
203      * @param parameterIndex the index of the parameter
204      * @return the corresponding MethodParameter instance
205      */

206     public static MethodParameter forMethodOrConstructor(Object JavaDoc methodOrConstructor, int parameterIndex) {
207         if (methodOrConstructor instanceof Method JavaDoc) {
208             return new MethodParameter((Method JavaDoc) methodOrConstructor, parameterIndex);
209         }
210         else if (methodOrConstructor instanceof Constructor JavaDoc) {
211             return new MethodParameter((Constructor JavaDoc) methodOrConstructor, parameterIndex);
212         }
213         else {
214             throw new IllegalArgumentException JavaDoc(
215                     "Given object [" + methodOrConstructor + "] is neither a Method nor a Constructor");
216         }
217     }
218
219 }
220
Popular Tags