KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > descriptors > InstanceVariableAttributeAccessor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.descriptors;
23
24 import java.lang.reflect.*;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.internal.helper.*;
30 import oracle.toplink.essentials.mappings.AttributeAccessor;
31 import oracle.toplink.essentials.internal.security.*;
32
33 /**
34  * <p><b>Purpose</b>: A wrapper class for handling cases when the domain object has instance varible
35  * to map to the database field.
36  *
37  * @author Sati
38  * @since TOPLink/Java 1.0
39  */

40 public class InstanceVariableAttributeAccessor extends AttributeAccessor {
41
42     /** The attribute name of an object is converted to Field type to access it reflectively */
43     protected transient Field attributeField;
44
45     /**
46      * Returns the class type of the attribute.
47      */

48     public Class JavaDoc getAttributeClass() {
49         if (getAttributeField() == null) {
50             return null;
51         }
52
53         return getAttributeType();
54     }
55
56     /**
57      * Returns the value of attributeField.
58      */

59     protected Field getAttributeField() {
60         return attributeField;
61     }
62
63     /**
64      * Returns the declared type of attributeField.
65      */

66     public Class JavaDoc getAttributeType() {
67         return attributeField.getType();
68     }
69
70     /**
71      * Returns the value of the attribute on the specified object.
72      */

73     public Object JavaDoc getAttributeValueFromObject(Object JavaDoc anObject) throws DescriptorException {
74         try {
75             // PERF: Direct variable access.
76
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
77                 try {
78                     return AccessController.doPrivileged(new PrivilegedGetValueFromField(this.attributeField, anObject));
79                 } catch (PrivilegedActionException JavaDoc exception) {
80                     throw DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor(getAttributeName(), anObject.getClass().getName(), exception.getException());
81                 }
82             } else {
83                 return oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getValueFromField(this.attributeField, anObject);
84             }
85         } catch (IllegalArgumentException JavaDoc exception) {
86             throw DescriptorException.illegalArgumentWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), getAttributeType().getName(), anObject.getClass().getName(), exception);
87         } catch (IllegalAccessException JavaDoc exception) {
88             throw DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor(getAttributeName(), anObject.getClass().getName(), exception);
89         } catch (NullPointerException JavaDoc exception) {
90             String JavaDoc className = null;
91             if (anObject != null) {
92                 // Some JVM's throw this exception for some very odd reason
93
className = anObject.getClass().getName();
94             }
95             throw DescriptorException.nullPointerWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), className, exception);
96         }
97     }
98
99     /**
100      * instanceVariableName is converted to Field type.
101      */

102     public void initializeAttributes(Class JavaDoc theJavaClass) throws DescriptorException {
103         if (getAttributeName() == null) {
104             throw DescriptorException.attributeNameNotSpecified();
105         }
106         try {
107             setAttributeField(Helper.getField(theJavaClass, getAttributeName()));
108         } catch (NoSuchFieldException JavaDoc exception) {
109             throw DescriptorException.noSuchFieldWhileInitializingAttributesInInstanceVariableAccessor(getAttributeName(), theJavaClass.getName(), exception);
110         } catch (SecurityException JavaDoc exception) {
111             throw DescriptorException.securityWhileInitializingAttributesInInstanceVariableAccessor(getAttributeName(), theJavaClass.getName(), exception);
112         }
113     }
114
115     /**
116      * Sets the value of the attributeField.
117      */

118     protected void setAttributeField(Field field) {
119         attributeField = field;
120     }
121
122     /**
123      * Sets the value of the instance variable in the object to the value.
124      */

125     public void setAttributeValueInObject(Object JavaDoc anObject, Object JavaDoc value) throws DescriptorException {
126         DescriptorException descriptorException;
127
128         try {
129             // PERF: Direct variable access.
130
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
131                 try {
132                     AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, value));
133                 } catch (PrivilegedActionException JavaDoc exception) {
134                     throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception.getException());
135                 }
136             } else {
137                 oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, value);
138             }
139         } catch (IllegalArgumentException JavaDoc exception) {
140             // This is done to overcome VA Java bug because VA Java does not allow null to be set reflectively.
141
try {
142                 // This is done to overcome VA Java bug because VA Java does not allow null to be set reflectively.
143
// Bug2910086 In JDK1.4, IllegalArgumentException is thrown if value is null.
144
// TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
145
if (value == null) {
146                     // cr 3737 If a null pointer was thrown because toplink attempted to set a null referece into a
147
// primitive create a primitive of value 0 to set in the object.
148
Class JavaDoc fieldClass = getAttributeClass();
149                     if (oracle.toplink.essentials.internal.helper.Helper.isPrimitiveWrapper(fieldClass)) {
150                         if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
151                             try {
152                                 AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(new Integer JavaDoc(0), fieldClass)));
153                             } catch (PrivilegedActionException JavaDoc exc) {
154                                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exc.getException());
155                                                         }
156                         } else {
157                             oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(new Integer JavaDoc(0), fieldClass));
158                         }
159                     }
160                     return;
161                 }
162             } catch (IllegalAccessException JavaDoc accessException) {
163                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
164             }
165
166             // TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
167
// Allow XML change set to merge correctly since new value in XML change set is always String
168
try {
169                 if (value instanceof String JavaDoc) {
170                     Object JavaDoc newValue = ConversionManager.getDefaultManager().convertObject(value, getAttributeClass());
171                     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
172                         try {
173                             AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, newValue));
174                         } catch (PrivilegedActionException JavaDoc exc) {
175                         }
176                     } else {
177                         oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, newValue);
178                     }
179                     return;
180                 }
181             } catch (Exception JavaDoc e) {
182                 // Do nothing and move on to throw the original exception
183
}
184             throw DescriptorException.illegalArgumentWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), getAttributeType().getName(), value, exception);
185         } catch (IllegalAccessException JavaDoc exception) {
186             if (value == null) {
187                 return;
188             }
189             throw DescriptorException.illegalAccessWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), anObject.getClass().getName(), value, exception);
190         } catch (NullPointerException JavaDoc exception) {
191             try {
192                 // TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
193
//Bug2910086 In JDK1.3, NullPointerException is thrown if value is null. Add a null pointer check so that the TopLink exception is thrown if anObject is null.
194
if (anObject != null) {
195                     // cr 3737 If a null pointer was thrown because toplink attempted to set a null referece into a
196
// primitive create a primitive of value 0 to set in the object.
197
Class JavaDoc fieldClass = getAttributeClass();
198                     if (oracle.toplink.essentials.internal.helper.Helper.isPrimitiveWrapper(fieldClass) && (value == null)) {
199                         if (oracle.toplink.essentials.internal.helper.Helper.isPrimitiveWrapper(fieldClass)) {
200                             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
201                                 try {
202                                     AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(new Integer JavaDoc(0), fieldClass)));
203                                 } catch (PrivilegedActionException JavaDoc exc) {
204                                     throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exc.getException());
205                                 }
206                             } else {
207                                 oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(new Integer JavaDoc(0), fieldClass));
208                             }
209                         }
210                     } else {
211                         throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
212                     }
213                 } else {
214                     // Some JVM's throw this exception for some very odd reason
215
throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
216                 }
217             } catch (IllegalAccessException JavaDoc accessException) {
218                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
219             }
220         }
221     }
222 }
223
Popular Tags