View Javadoc

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