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 org.apache.jdo.model.java.JavaMember;
20  import org.apache.jdo.model.java.JavaType;
21  
22  /***
23   * Abstract super class for JavaMember implementations. 
24   * It provides getters for the name and declaringClass properties which are
25   * initialized in the constructor. 
26   * <p>
27   * A non-abstract subclass must implement methods 
28   * {@link #getModifiers()} and {@link #getType()}.
29   *
30   * @author Michael Bouschen
31   * @since JDO 2.0
32   */
33  abstract public class AbstractJavaMember
34      implements JavaMember
35  {
36      /*** The Field name. */
37      private String name;
38  
39      /*** The declaring class. */
40      private JavaType declaringClass;
41  
42      /*** 
43       * Constructor setting the name and declaringClass property.
44       * @param name field name
45       * @param declaringClass the JavaType of the class or interface that
46       * declares this JavaMember.
47       */
48      public AbstractJavaMember(String name, JavaType declaringClass)
49      {
50          this.name = name;
51          this.declaringClass = declaringClass;
52      }
53  
54      // ===== Methods specified in JavaMember =====
55  
56      /***
57       * Returns the name of the field. 
58       * @return field name
59       */
60      public String getName()
61      {
62          return name;
63      }
64      
65      /***
66       * Returns the JavaType instance representing the class or interface
67       * that declares the field represented by this JavaMember instance.
68       * @return the JavaType instance of the declaring class.
69       */
70      public JavaType getDeclaringClass()
71      {
72          return declaringClass;
73      }
74      
75      /***
76       * Returns the Java language modifiers for the member represented by
77       * this JavaMember, as an integer. The java.lang.reflect.Modifier class
78       * should be used to decode the modifiers. 
79       * @return the Java language modifiers for this JavaMember
80       * @see java.lang.reflect.Modifier
81       */
82      abstract public int getModifiers();
83  
84      /***
85       * Returns the JavaType representation of the type of the memeber.
86       * @return type of the member
87       */
88      abstract public JavaType getType();
89      
90      /***
91       * Returns the JavaType representation of the component type of the type
92       * of the property, if the property type is an array or collection. The
93       * method returns <code>null</code>, if the property type is not an array
94       * or collection.
95       * @return the component type of the property type in case of an array or
96       * collection.
97       */
98      public JavaType getComponentType()
99      {
100         JavaType componentType = null;
101         JavaType type = getType();
102         if (type.isArray())
103             componentType = type.getArrayComponentType();
104         else if (type.isJDOSupportedCollection())
105             componentType = PredefinedType.objectType;
106         return componentType;
107     }
108 
109     // ===== Methods not specified in JavaMember =====
110 
111     /***
112      * Indicates whether some other object is "equal to" this one.
113      * @param obj the reference object with which to compare. 
114      * <p>
115      * This implementation matches the declaring class and the name of the
116      * specified object to the declaring class and the name of this
117      * JavaMember. 
118      * @return <code>true</code> if this object is the same as the obj
119      * argument; <code>false</code> otherwise. 
120      */
121     public boolean equals(Object obj)
122     {
123         // return true if obj is this
124         if (obj == this) return  true;
125         // return false if obj does not have the correct type
126         if ((obj == null) || !(obj instanceof JavaMember)) return false;
127 
128         JavaMember other = (JavaMember)obj;
129         // compare declaringClass and field names
130         return (getDeclaringClass() == other.getDeclaringClass())
131             && (getName().equals(other.getName()));
132     }
133     
134     /***
135      * Returns a hash code value for the object. 
136      * <p>
137      * This is computed as the exclusive-or of the hashcodes for the
138      * underlying field's declaring class name and its name.
139      * @return a hash code value for this object.
140      */
141     public int hashCode()
142     {
143         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
144     }
145     
146     /***
147      * Returns a string representation of the object. 
148      * @return a string representation of the object.
149      */
150     public String toString()
151     {
152         return getDeclaringClass().getName() + "." + getName(); //NOI18N
153     }
154 }