View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
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      // ===== Methods specified in JavaElement =====
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      // ===== Methods specified in JavaMember =====
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      // ===== Methods specified in JavaMethod =====
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     // ===== Methods not defined in JavaMethod =====
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 }