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.Field;
20  
21  import org.apache.jdo.impl.model.java.BaseReflectionJavaField;
22  import org.apache.jdo.model.ModelFatalException;
23  import org.apache.jdo.model.java.JavaField;
24  import org.apache.jdo.model.java.JavaType;
25  import org.apache.jdo.model.jdo.JDOField;
26  import org.apache.jdo.util.I18NHelper;
27  
28  /***
29   * A reflection based JavaField implementation used at runtime.  
30   * The implementation takes <code>java.lang.reflect.Field</code> instances
31   * to get Java related metadata about fields. 
32   * 
33   * @author Michael Bouschen
34   * @since JDO 1.1
35   * @version JDO 2.0
36   */
37  public class ReflectionJavaField
38      extends BaseReflectionJavaField
39  {
40      /*** I18N support */
41      private final static I18NHelper msg =  
42          I18NHelper.getInstance("org.apache.jdo.impl.model.java.Bundle"); //NOI18N
43  
44      /*** 
45       * Constructor for fields w/o JDO metadata. 
46       * @param field the reflection field representation.
47       * @param declaringClass the JavaType of the class that declares the field.
48       */
49      public ReflectionJavaField(Field field, JavaType declaringClass)
50      {
51          super(field, declaringClass);
52          this.type = getJavaTypeForClass(field.getType());
53      }
54      
55      /*** 
56       * Constructor for fields having JDO metadata.
57       * @param fieldName the name of the field.
58       * @param type the field type.
59       * @param declaringClass the JavaType of the class that declares the field.
60       */
61      public ReflectionJavaField(String fieldName, JavaType type, 
62                                 JavaType declaringClass)
63      {
64          super(fieldName, declaringClass);
65          this.type = type;
66      }
67      
68      /***
69       * Returns the JavaType representation of the field type.
70       * @return field type
71       */
72      public JavaType getType()
73      {
74          if (type == null) {
75              type = getJavaTypeForClass(getField().getType());
76          }
77          return type;
78      }
79  
80      // ===== Methods not defined in JavaField =====
81  
82      /*** 
83       * Returns a JavaType instance for the specified Class object. 
84       * This method provides a hook such that ReflectionJavaField subclasses can
85       * implement their own mapping of Class objects to JavaType instances. 
86       */
87      public JavaType getJavaTypeForClass(Class clazz)
88      {
89          return ((ReflectionJavaType)getDeclaringClass()).getJavaTypeForClass(clazz);
90      }
91  }