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