01: package org.vraptor.component;
02:
03: import java.lang.annotation.Annotation;
04: import java.lang.reflect.Method;
05: import java.lang.reflect.Type;
06: import java.util.ArrayList;
07: import java.util.List;
08:
09: import org.vraptor.annotations.Logic;
10: import org.vraptor.annotations.Parameter;
11: import org.vraptor.reflection.StringUtil;
12:
13: /**
14: * The default implementation of a parameter info provider. Looks for its type, Logic and Parameter annotation.
15: * @author Guilherme Silveira
16: * @since 2.5.1
17: */
18: public class DefaultParameterInfoProvider implements
19: ParameterInfoProvider {
20:
21: public List<MethodParameter> provideFor(Method method) {
22: Class<?>[] params = method.getParameterTypes();
23: Type[] generic = method.getGenericParameterTypes();
24: Annotation[][] annotations = method.getParameterAnnotations();
25: List<MethodParameter> list = new ArrayList<MethodParameter>();
26:
27: Logic logic = method.getAnnotation(Logic.class);
28: String[] parameterNames = logic != null ? logic.parameters()
29: : null;
30: if (parameterNames != null
31: && parameterNames.length != 0
32: && parameterNames.length != method.getParameterTypes().length) {
33: throw new IllegalArgumentException(
34: "The number of parameters at @Logic does "
35: + "not match the number of this method arguments in method"
36: + method.getName());
37: }
38:
39: for (int i = 0; i < params.length; i++) {
40: Class<?> param = params[i];
41: Parameter parameterAnnotation = searchParameterAnnotation(
42: annotations, i);
43: String key;
44:
45: if (parameterAnnotation != null
46: && !parameterAnnotation.key().equals("")) {
47: // using @Parameter key name
48: key = parameterAnnotation.key();
49: } else {
50: // using @Logic(parameters={..} value
51: if (parameterNames != null
52: && parameterNames.length != 0) {
53: key = parameterNames[i];
54: } else {
55: key = StringUtil.classNameToInstanceName(param
56: .getSimpleName());
57: }
58: }
59:
60: list.add(new MethodParameter(param, generic[i], i, key));
61: }
62: return list;
63: }
64:
65: private Parameter searchParameterAnnotation(
66: Annotation[][] annotations, int i) {
67: for (Annotation a : annotations[i]) {
68: if (a.annotationType().equals(Parameter.class)) {
69: return (Parameter) a;
70: }
71: }
72: return null;
73: }
74:
75: }
|