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