1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jdo.impl.model.java.reflection;
19
20 import java.lang.reflect.Method;
21
22 import org.apache.jdo.model.java.JavaMethod;
23 import org.apache.jdo.model.java.JavaType;
24 import org.apache.jdo.impl.model.java.AbstractJavaMember;
25
26 /***
27 * A reflection based JavaMethod implementation.
28 * The implementation takes <code>java.lang.reflect.Method</code> instances
29 * to get Java related metadata about methods.
30 *
31 * @author Michael Bouschen
32 * @since JDO 2.0
33 */
34 public class ReflectionJavaMethod
35 extends AbstractJavaMember
36 implements JavaMethod
37 {
38 /*** The wrapped java.lang.reflect.Method instance. */
39 private final Method method;
40
41 /***
42 * Constructor.
43 * @param method the reflection method representation.
44 * @param declaringClass the JavaType of the class that declares the field.
45 */
46 public ReflectionJavaMethod(Method method, JavaType declaringClass)
47 {
48 super(method.getName(), declaringClass);
49 this.method = method;
50 }
51
52
53
54 /***
55 * Returns the environment specific instance wrapped by this JavaModel
56 * element. This implementation returns the
57 * <code>java.lang.reflect.Method</code> instance for this JavaMethod.
58 * @return the environment specific instance wrapped by this JavaModel
59 * element.
60 */
61 public Object getUnderlyingObject()
62 {
63 return method;
64 }
65
66
67
68 /***
69 * Returns the Java language modifiers for the field represented by
70 * this JavaMember, as an integer. The java.lang.reflect.Modifier class
71 * should be used to decode the modifiers.
72 * @return the Java language modifiers for this JavaMember
73 * @see java.lang.reflect.Modifier
74 */
75 public int getModifiers()
76 {
77 return method.getModifiers();
78 }
79
80 /*** */
81 public JavaType getType()
82 {
83 return getReturnType();
84 }
85
86
87
88 /***
89 * Returns the JavaType representation of the method return type.
90 * @return method return type.
91 */
92 public JavaType getReturnType()
93 {
94 return getJavaTypeForClass(method.getReturnType());
95 }
96
97 /***
98 * Returns an array of JavaType instances that represent the formal
99 * parameter types, in declaration order, of the method represented by
100 * this JavaMethod instance.
101 * @return the types of teh formal parameters.
102 */
103 public JavaType[] getParameterTypes()
104 {
105 Class[] params = method.getParameterTypes();
106 JavaType[] paramTypes = new JavaType[params.length];
107 for (int i = 0; i < params.length; i++) {
108 paramTypes[i] = getJavaTypeForClass(params[i]);
109 }
110 return paramTypes;
111 }
112
113
114
115 /***
116 * Returns a JavaType instance for the specified Class object.
117 * This method provides a hook such that ReflectionJavaField subclasses can
118 * implement their own mapping of Class objects to JavaType instances.
119 */
120 public JavaType getJavaTypeForClass(Class clazz)
121 {
122 return ((ReflectionJavaType) getDeclaringClass()).
123 getJavaTypeForClass(clazz);
124 }
125 }