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