1 16 17 package org.springframework.core; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.springframework.util.Assert; 25 26 43 public class MethodParameter { 44 45 private Method method; 46 47 private Constructor constructor; 48 49 private final int parameterIndex; 50 51 private int nestingLevel; 52 53 54 private final Map typeIndexesPerLevel = new HashMap (4); 55 56 57 62 public MethodParameter(Method method, int parameterIndex) { 63 this(method, parameterIndex, 1); 64 } 65 66 76 public MethodParameter(Method 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 91 public MethodParameter(Constructor constructor, int parameterIndex) { 92 this(constructor, parameterIndex, 1); 93 } 94 95 103 public MethodParameter(Constructor 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 119 public Method getMethod() { 120 return this.method; 121 } 122 123 128 public Constructor getConstructor() { 129 return this.constructor; 130 } 131 132 136 public int getParameterIndex() { 137 return this.parameterIndex; 138 } 139 140 141 145 public void increaseNestingLevel() { 146 this.nestingLevel++; 147 } 148 149 153 public void decreaseNestingLevel() { 154 this.typeIndexesPerLevel.remove(new Integer (this.nestingLevel)); 155 this.nestingLevel--; 156 } 157 158 163 public int getNestingLevel() { 164 return this.nestingLevel; 165 } 166 167 173 public void setTypeIndexForCurrentLevel(int typeIndex) { 174 this.typeIndexesPerLevel.put(new Integer (this.nestingLevel), new Integer (typeIndex)); 175 } 176 177 183 public Integer getTypeIndexForCurrentLevel() { 184 return getTypeIndexForLevel(this.nestingLevel); 185 } 186 187 193 public Integer getTypeIndexForLevel(int nestingLevel) { 194 return (Integer ) this.typeIndexesPerLevel.get(new Integer (nestingLevel)); 195 } 196 197 198 206 public static MethodParameter forMethodOrConstructor(Object methodOrConstructor, int parameterIndex) { 207 if (methodOrConstructor instanceof Method ) { 208 return new MethodParameter((Method ) methodOrConstructor, parameterIndex); 209 } 210 else if (methodOrConstructor instanceof Constructor ) { 211 return new MethodParameter((Constructor ) methodOrConstructor, parameterIndex); 212 } 213 else { 214 throw new IllegalArgumentException ( 215 "Given object [" + methodOrConstructor + "] is neither a Method nor a Constructor"); 216 } 217 } 218 219 } 220 | Popular Tags |