KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Set JavaDoc;
28
29 import oracle.toplink.essentials.exceptions.*;
30 import oracle.toplink.essentials.internal.helper.*;
31 import oracle.toplink.essentials.mappings.AttributeAccessor;
32 import oracle.toplink.essentials.internal.security.*;
33
34 /**
35  * <p><b>Purpose</b>: A wrapper class for handling cases when the domain object attributes are
36  * to be accessed thru the accessor methods. This could happen if the variables are not defined
37  * public in the domain object.
38  *
39  * @author Sati
40  * @since TOPLink/Java 1.0
41  */

42 public class MethodAttributeAccessor extends AttributeAccessor {
43     protected String JavaDoc setMethodName;
44     protected String JavaDoc getMethodName;
45     protected transient Method setMethod;
46     protected transient Method getMethod;
47
48     /**
49      * Return the return type of the method accessor.
50      */

51     public Class JavaDoc getAttributeClass() {
52         if (getGetMethod() == null) {
53             return null;
54         }
55
56         return getGetMethodReturnType();
57     }
58
59     /**
60      * Gets the value of an instance variable in the object.
61      */

62     public Object JavaDoc getAttributeValueFromObject(Object JavaDoc anObject) throws DescriptorException {
63         try {
64             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
65                 try {
66                     return AccessController.doPrivileged(new PrivilegedMethodInvoker(getGetMethod(), anObject, (Object JavaDoc[])null));
67                 } catch (PrivilegedActionException JavaDoc exception) {
68                     Exception JavaDoc throwableException = exception.getException();
69                     if (throwableException instanceof IllegalAccessException JavaDoc) {
70                         throw DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), throwableException);
71                     } else {
72                         throw DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), throwableException);
73                      }
74                 }
75             } else {
76                 return PrivilegedAccessHelper.invokeMethod(getGetMethod(), anObject, (Object JavaDoc[])null);
77             }
78         } catch (IllegalArgumentException JavaDoc exception) {
79             throw DescriptorException.illegalArgumentWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
80         } catch (IllegalAccessException JavaDoc exception) {
81             throw DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
82         } catch (InvocationTargetException exception) {
83             throw DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
84         } catch (NullPointerException JavaDoc exception) {
85             // Some JVM's throw this exception for some very odd reason
86
throw DescriptorException.nullPointerWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
87         }
88     }
89
90     /**
91      * Return the accessor method for the attribute accessor.
92      */

93     protected Method getGetMethod() {
94         return getMethod;
95     }
96
97     /**
98      * Return the name of theh accessor method for the attribute accessor.
99      */

100     public String JavaDoc getGetMethodName() {
101         return getMethodName;
102     }
103
104     public Class JavaDoc getGetMethodReturnType() {
105         if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
106             try {
107                 return (Class JavaDoc)AccessController.doPrivileged(new PrivilegedGetMethodReturnType(getGetMethod()));
108             } catch (PrivilegedActionException JavaDoc exception) {
109                 // we should not get here since this call does not throw any checked exceptions
110
return null;
111             }
112         } else {
113             return PrivilegedAccessHelper.getMethodReturnType(getGetMethod());
114         }
115     }
116
117     /**
118      * Return the set method for the attribute accessor.
119      */

120     protected Method getSetMethod() {
121         return setMethod;
122     }
123
124     /**
125      * Return the name of the set method for the attribute accessor.
126      */

127     public String JavaDoc getSetMethodName() {
128         return setMethodName;
129     }
130
131     public Class JavaDoc getSetMethodParameterType() {
132         if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
133             try {
134                 return ((Class JavaDoc[])AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(getSetMethod())))[0];
135             } catch (PrivilegedActionException JavaDoc exception) {
136                 // we should not get here since this call does not throw any checked exceptions
137
return null;
138             }
139         } else {
140             return PrivilegedAccessHelper.getMethodParameterTypes(getSetMethod())[0];
141         }
142     }
143
144     /**
145      * Set get and set method after creating these methods by using
146      * get and set method names
147      */

148     public void initializeAttributes(Class JavaDoc theJavaClass) throws DescriptorException {
149         if (getAttributeName() == null) {
150             throw DescriptorException.attributeNameNotSpecified();
151         }
152         try {
153             setGetMethod(Helper.getDeclaredMethod(theJavaClass, getGetMethodName(), (Class JavaDoc[])null));
154
155             // The parameter type for the set method must always be the return type of the get method.
156
Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
157             parameterTypes[0] = getGetMethodReturnType();
158             setSetMethod(Helper.getDeclaredMethod(theJavaClass, getSetMethodName(), parameterTypes));
159         } catch (NoSuchMethodException JavaDoc ex) {
160             DescriptorException descriptorException = DescriptorException.noSuchMethodWhileInitializingAttributesInMethodAccessor(getSetMethodName(), getGetMethodName(), theJavaClass.getName());
161             descriptorException.setInternalException(ex);
162             throw descriptorException;
163         } catch (SecurityException JavaDoc exception) {
164             DescriptorException descriptorException = DescriptorException.securityWhileInitializingAttributesInMethodAccessor(getSetMethodName(), getGetMethodName(), theJavaClass.getName());
165             descriptorException.setInternalException(exception);
166             throw descriptorException;
167         }
168     }
169
170     public boolean isMethodAttributeAccessor() {
171         return true;
172     }
173
174     /**
175      * Sets the value of the instance variable in the object to the value.
176      */

177     public void setAttributeValueInObject(Object JavaDoc domainObject, Object JavaDoc attributeValue) throws DescriptorException {
178         Object JavaDoc[] parameters = new Object JavaDoc[1];
179         parameters[0] = attributeValue;
180         try {
181             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
182                 try {
183                     AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters));
184                 } catch (PrivilegedActionException JavaDoc exception) {
185                     Exception JavaDoc throwableException = exception.getException();
186                     if (throwableException instanceof IllegalAccessException JavaDoc) {
187                         throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, throwableException);
188                     } else {
189                         throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, throwableException);
190                      }
191                 }
192             } else {
193                 PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters);
194             }
195         } catch (IllegalAccessException JavaDoc exception) {
196             throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
197         } catch (IllegalArgumentException JavaDoc exception) {
198             // TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
199
// Allow XML change set to merge correctly since new value in XML change set is always String
200
try {
201                 if (attributeValue instanceof String JavaDoc) {
202                     Object JavaDoc newValue = ConversionManager.getDefaultManager().convertObject(attributeValue, getAttributeClass());
203                     Object JavaDoc[] newParameters = new Object JavaDoc[1];
204                     newParameters[0] = newValue;
205                     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
206                         try {
207                             AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, newParameters));
208                         } catch (PrivilegedActionException JavaDoc exc) {
209                             // Do nothing and move on to throw the original exception
210
}
211                     } else {
212                         PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, newParameters);
213                     }
214                     return;
215                 }
216             } catch (Exception JavaDoc e) {
217                 // Do nothing and move on to throw the original exception
218
}
219             throw DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
220         } catch (InvocationTargetException exception) {
221             throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
222         } catch (NullPointerException JavaDoc exception) {
223             try {
224                 // TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
225
// cr 3737 If a null pointer was thrown because toplink attempted to set a null referece into a
226
// primitive create a primitive of value 0 to set in the object.
227
// Is this really the best place for this? is this not why we have null-value and conversion-manager?
228
Class JavaDoc fieldClass = getSetMethodParameterType();
229
230                 //Found when fixing Bug2910086
231
if (fieldClass.isPrimitive() && (attributeValue == null)) {
232                     parameters[0] = ConversionManager.getDefaultManager().convertObject(new Integer JavaDoc(0), fieldClass);
233                     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
234                         try {
235                             AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters));
236                         } catch (PrivilegedActionException JavaDoc exc) {
237                             Exception JavaDoc throwableException = exc.getException();
238                             if (throwableException instanceof IllegalAccessException JavaDoc) {
239                                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, throwableException);
240                             } else {
241                                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, throwableException);
242                              }
243                         }
244                     } else {
245                         PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters);
246                     }
247                 } else {
248                     // Some JVM's throw this exception for some very odd reason
249
throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
250                 }
251             } catch (IllegalAccessException JavaDoc accessException) {
252                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
253             } catch (InvocationTargetException invocationException) {
254                 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
255             }
256         }
257     }
258
259     /**
260      * Set the accessor method for the attribute accessor.
261      */

262     protected void setGetMethod(Method getMethod) {
263         this.getMethod = getMethod;
264     }
265
266     /**
267      * Set the name of the accessor method for the attribute accessor.
268      */

269     public void setGetMethodName(String JavaDoc getMethodName) {
270         this.getMethodName = getMethodName;
271     }
272
273     /**
274      * Set the set method for the attribute accessor.
275      */

276     protected void setSetMethod(Method setMethod) {
277         this.setMethod = setMethod;
278     }
279
280     /**
281      * Set the name of the set method for the attribute accessor.
282      */

283     public void setSetMethodName(String JavaDoc setMethodName) {
284         this.setMethodName = setMethodName;
285     }
286 }
287
Popular Tags