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;
18  
19  import java.lang.reflect.Field;
20  
21  import org.apache.jdo.model.ModelFatalException;
22  import org.apache.jdo.model.java.JavaField;
23  import org.apache.jdo.model.java.JavaProperty;
24  import org.apache.jdo.model.java.JavaType;
25  import org.apache.jdo.util.I18NHelper;
26  
27  
28  
29  /***
30   * This class provides a basic JavaType implementation using a reflection
31   * Class instance.
32   * <p> 
33   * Note, BaseReflectionJavaType must not be used for array types, since it inherits
34   * the default implemention of methods isArray and getArrayComponentType
35   * from its superclass AbstractJavaType.
36   * 
37   * @author Michael Bouschen
38   * @since JDO 1.1
39   */
40  public class BaseReflectionJavaType
41      extends AbstractJavaType
42  {
43      /*** The java.lang.Class instance for this BaseReflectionJavaType. */
44      protected Class clazz;
45  
46      /*** The superclass JavaType. */
47      protected JavaType superclass;
48  
49      /*** I18N support */
50      private static I18NHelper msg = 
51          I18NHelper.getInstance(BaseReflectionJavaType.class);
52  
53      /***
54       * Constructor. The specified java.lang.Class instance must not be
55       * <code>null</code>. The 
56       * @param clazz the Class instance representing the type
57       * @param superclass JavaType instance representing the superclass.
58       */
59      public BaseReflectionJavaType(Class clazz, JavaType superclass)
60      {
61          if (clazz == null)
62              throw new ModelFatalException(msg.msg(
63                  "ERR_InvalidNullClassInstance", "BaseReflectionJavaType.<init>")); //NOI18N
64          this.clazz = clazz;
65          this.superclass = superclass;
66      }
67      
68      // ===== Methods specified in JavaElement =====
69  
70      /***
71       * Returns the environment specific instance wrapped by this JavaModel
72       * element. This implementation returns the
73       * <code>java.lang.Class</code> instance for this JavaType.
74       * @return the environment specific instance wrapped by this JavaModel
75       * element.
76       */
77      public Object getUnderlyingObject() 
78      {
79          return getJavaClass();
80      }
81  
82      // ===== Methods specified in JavaType =====
83  
84      /*** 
85       * Determines if this JavaType object represents an interface type.
86       * @return <code>true</code> if this object represents an interface type; 
87       * <code>false</code> otherwise.
88       */
89      public boolean isInterface()
90      {
91          return clazz.isInterface();
92      }
93      
94      /*** 
95       * Returns true if this JavaType is compatible with the specified
96       * JavaType. 
97       * @param javaType the type this JavaType is checked with.
98       * @return <code>true</code> if this is compatible with the specified
99       * type; <code>false</code> otherwise.
100      */
101     public boolean isCompatibleWith(JavaType javaType)
102     {
103         if (javaType == null)
104             return false;
105         
106         if (javaType instanceof BaseReflectionJavaType) {
107             BaseReflectionJavaType otherType = (BaseReflectionJavaType)javaType;
108             return otherType.getJavaClass().isAssignableFrom(clazz);
109         }
110         
111         return false;
112     }
113 
114     /***
115      * Returns the name of the type. If this type represents a class or
116      * interface, the name is fully qualified.
117      * @return type name
118      */
119     public String getName()
120     {
121         return clazz.getName();
122     }
123 
124     /***
125      * Returns the Java language modifiers for the field represented by
126      * this JavaType, as an integer. The java.lang.reflect.Modifier class
127      * should be used to decode the modifiers. 
128      * @return the Java language modifiers for this JavaType
129      */
130     public int getModifiers() 
131     { 
132         return clazz.getModifiers(); 
133     }
134 
135     /*** 
136      * Returns the JavaType representing the superclass of the entity
137      * represented by this JavaType. If this JavaType represents either the 
138      * Object class, an interface, a primitive type, or <code>void</code>, 
139      * then <code>null</code> is returned. If this object represents an
140      * array class then the JavaType instance representing the Object class
141      * is returned.  
142      * @return the superclass of the class represented by this JavaType.
143      */
144     public JavaType getSuperclass()
145     {
146         return superclass;
147     }
148 
149     /***
150      * Returns a JavaField instance that reflects the field with the
151      * specified name of the class or interface represented by this
152      * JavaType instance. The method returns <code>null</code>, if the
153      * class or interface (or one of its superclasses) does not have a
154      * field with that name.
155      * @param fieldName the name of the field 
156      * @return the JavaField instance for the specified field in this class
157      * or <code>null</code> if there is no such field.
158      */
159     public JavaField getJavaField(String fieldName) 
160     { 
161         Field field = 
162             BaseReflectionJavaField.getDeclaredFieldPrivileged(clazz, fieldName);
163         if (field != null) {
164             return new BaseReflectionJavaField(field, this);
165         }
166         
167         // check superclass, if available and other than Object
168         JavaType superclass = getSuperclass();
169         if ((superclass != null) && (superclass != PredefinedType.objectType)) {
170             return superclass.getJavaField(fieldName);
171         }
172         
173         return null;
174     }
175 
176     /*** */
177     public JavaField[] getDeclaredJavaFields()
178     {
179         throw new UnsupportedOperationException();
180     }
181     
182     /*** */
183     public JavaProperty getJavaProperty(String name)
184     {
185         throw new UnsupportedOperationException();
186     }
187 
188     /*** */
189     public JavaProperty[] getDeclaredJavaProperties()
190     {
191         throw new UnsupportedOperationException();
192     }
193 
194     // ===== Methods not defined in JavaType =====
195 
196     /***
197      * Returns the java.lang.Class instance wrapped by this JavaType.
198      * @return the Class instance for this JavaType.
199      */
200     public Class getJavaClass()
201     {
202         return clazz;
203     }
204 
205 }