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