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.ModelFatalException;
20  import org.apache.jdo.model.java.JavaMethod;
21  import org.apache.jdo.model.java.JavaProperty;
22  import org.apache.jdo.model.java.JavaType;
23  import org.apache.jdo.util.I18NHelper;
24  
25  /***
26   * Default Implementation of the JavaProperty interface. A JavaProperty
27   * instance represents a JavaBeans property.
28   *
29   * @author Michael Bouschen
30   * @since JDO 2.0
31   */
32  public class JavaPropertyImpl
33      extends AbstractJavaMember
34      implements JavaProperty
35  {
36      /*** The method object of the getter method. */
37      private final JavaMethod getter;
38  
39      /*** The method object of the setter method. */
40      private final JavaMethod setter;
41  
42      /*** The type of the property. */
43      private final JavaType type;
44  
45      /*** I18N support */
46      private static I18NHelper msg = 
47          I18NHelper.getInstance(JavaPropertyImpl.class);
48  
49      /*** Constructor setting name, getter, setter, type and declaringClass. */
50      public JavaPropertyImpl(String name, JavaMethod getter, JavaMethod setter,
51                              JavaType type, JavaType declaringClass)
52          throws ModelFatalException
53      {
54          super(name, declaringClass);
55          this.getter = getter;
56          this.setter = setter;
57          this.type = type;
58          if ((getter == null) && (setter == null))
59              throw new ModelFatalException(
60                  msg.msg("EXC_MissingGetterAndSetter", //NOI18N
61                           name, declaringClass.getName()));
62      }
63      
64      // ===== Methods specified in JavaElement =====
65      
66      /*** 
67       * Returns the environment specific instance wrapped by this JavaModel
68       * element.
69       * <p> 
70       * This implementation returns the underlying object of the
71       * getter method if available; otherwise the one from the setter method. 
72       * @return the environment specific instance wrapped by this JavaModel
73       * element.
74       */
75      public Object getUnderlyingObject() 
76      {
77          Object underlyingObject = null;
78          
79          if (getter != null)
80              underlyingObject = getter.getUnderlyingObject();
81          else if (setter != null)
82              underlyingObject = setter.getUnderlyingObject();
83  
84          return underlyingObject;
85      }   
86  
87      // ===== Methods specified in JavaMember =====
88  
89      /***
90       * Returns the Java language modifiers for the field represented by
91       * this JavaMember, as an integer. The java.lang.reflect.Modifier class
92       * should be used to decode the modifiers. 
93       * <p> 
94       * This implementation returns the underlying object of the getter method
95       * if available; otherwise the one from the setter method.  
96       * @return the Java language modifiers for this JavaMember
97       * @see java.lang.reflect.Modifier
98       */
99      public int getModifiers()
100     {
101         int modifiers = 0;
102         
103         if (getter != null)
104             modifiers = getter.getModifiers();
105         else if (setter != null)
106             modifiers = setter.getModifiers();
107 
108         return modifiers;
109     }
110     
111     // ===== Methods specified in JavaProperty =====
112 
113     /***
114      * Returns the JavaMethod representation of the getter method for this
115      * JavaProperty. If there is no getter method for this JavaProperty
116      * (i.e. the property is write-only), then the method returns
117      * <code>null</code>.
118      * @return the getter method if available; or <code>null</code>
119      * otherwise.
120      */
121     public JavaMethod getGetterMethod()
122     {
123         return getter;
124     }
125 
126     /***
127      * Returns the JavaMethod representation of the setter method for this
128      * JavaProperty. If there is no setter method for this JavaProperty
129      * (i.e. the property is read-only), then the method returns
130      * <code>null</code>.
131      * @return the setter method if available; or <code>null</code>
132      * otherwise.
133      */
134     public JavaMethod getSetterMethod()
135     {
136         return setter;
137     }
138 
139     /***
140      * Returns the JavaType representation of the property type.
141      * @return property type
142      */
143     public JavaType getType()
144     {
145         return type;
146     }
147 }