1 21 package oracle.toplink.essentials.internal.descriptors; 23 24 import java.lang.reflect.*; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 import java.util.Set ; 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 42 public class MethodAttributeAccessor extends AttributeAccessor { 43 protected String setMethodName; 44 protected String getMethodName; 45 protected transient Method setMethod; 46 protected transient Method getMethod; 47 48 51 public Class getAttributeClass() { 52 if (getGetMethod() == null) { 53 return null; 54 } 55 56 return getGetMethodReturnType(); 57 } 58 59 62 public Object getAttributeValueFromObject(Object anObject) throws DescriptorException { 63 try { 64 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 65 try { 66 return AccessController.doPrivileged(new PrivilegedMethodInvoker(getGetMethod(), anObject, (Object [])null)); 67 } catch (PrivilegedActionException exception) { 68 Exception throwableException = exception.getException(); 69 if (throwableException instanceof IllegalAccessException ) { 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 [])null); 77 } 78 } catch (IllegalArgumentException exception) { 79 throw DescriptorException.illegalArgumentWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception); 80 } catch (IllegalAccessException 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 exception) { 85 throw DescriptorException.nullPointerWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception); 87 } 88 } 89 90 93 protected Method getGetMethod() { 94 return getMethod; 95 } 96 97 100 public String getGetMethodName() { 101 return getMethodName; 102 } 103 104 public Class getGetMethodReturnType() { 105 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 106 try { 107 return (Class )AccessController.doPrivileged(new PrivilegedGetMethodReturnType(getGetMethod())); 108 } catch (PrivilegedActionException exception) { 109 return null; 111 } 112 } else { 113 return PrivilegedAccessHelper.getMethodReturnType(getGetMethod()); 114 } 115 } 116 117 120 protected Method getSetMethod() { 121 return setMethod; 122 } 123 124 127 public String getSetMethodName() { 128 return setMethodName; 129 } 130 131 public Class getSetMethodParameterType() { 132 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 133 try { 134 return ((Class [])AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(getSetMethod())))[0]; 135 } catch (PrivilegedActionException exception) { 136 return null; 138 } 139 } else { 140 return PrivilegedAccessHelper.getMethodParameterTypes(getSetMethod())[0]; 141 } 142 } 143 144 148 public void initializeAttributes(Class theJavaClass) throws DescriptorException { 149 if (getAttributeName() == null) { 150 throw DescriptorException.attributeNameNotSpecified(); 151 } 152 try { 153 setGetMethod(Helper.getDeclaredMethod(theJavaClass, getGetMethodName(), (Class [])null)); 154 155 Class [] parameterTypes = new Class [1]; 157 parameterTypes[0] = getGetMethodReturnType(); 158 setSetMethod(Helper.getDeclaredMethod(theJavaClass, getSetMethodName(), parameterTypes)); 159 } catch (NoSuchMethodException ex) { 160 DescriptorException descriptorException = DescriptorException.noSuchMethodWhileInitializingAttributesInMethodAccessor(getSetMethodName(), getGetMethodName(), theJavaClass.getName()); 161 descriptorException.setInternalException(ex); 162 throw descriptorException; 163 } catch (SecurityException 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 177 public void setAttributeValueInObject(Object domainObject, Object attributeValue) throws DescriptorException { 178 Object [] parameters = new Object [1]; 179 parameters[0] = attributeValue; 180 try { 181 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 182 try { 183 AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters)); 184 } catch (PrivilegedActionException exception) { 185 Exception throwableException = exception.getException(); 186 if (throwableException instanceof IllegalAccessException ) { 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 exception) { 196 throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception); 197 } catch (IllegalArgumentException exception) { 198 try { 201 if (attributeValue instanceof String ) { 202 Object newValue = ConversionManager.getDefaultManager().convertObject(attributeValue, getAttributeClass()); 203 Object [] newParameters = new Object [1]; 204 newParameters[0] = newValue; 205 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 206 try { 207 AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, newParameters)); 208 } catch (PrivilegedActionException exc) { 209 } 211 } else { 212 PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, newParameters); 213 } 214 return; 215 } 216 } catch (Exception e) { 217 } 219 throw DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception); 220 } catch (InvocationTargetException exception) { 221 throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception); 222 } catch (NullPointerException exception) { 223 try { 224 Class fieldClass = getSetMethodParameterType(); 229 230 if (fieldClass.isPrimitive() && (attributeValue == null)) { 232 parameters[0] = ConversionManager.getDefaultManager().convertObject(new Integer (0), fieldClass); 233 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 234 try { 235 AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters)); 236 } catch (PrivilegedActionException exc) { 237 Exception throwableException = exc.getException(); 238 if (throwableException instanceof IllegalAccessException ) { 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 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception); 250 } 251 } catch (IllegalAccessException accessException) { 252 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception); 253 } catch (InvocationTargetException invocationException) { 254 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception); 255 } 256 } 257 } 258 259 262 protected void setGetMethod(Method getMethod) { 263 this.getMethod = getMethod; 264 } 265 266 269 public void setGetMethodName(String getMethodName) { 270 this.getMethodName = getMethodName; 271 } 272 273 276 protected void setSetMethod(Method setMethod) { 277 this.setMethod = setMethod; 278 } 279 280 283 public void setSetMethodName(String setMethodName) { 284 this.setMethodName = setMethodName; 285 } 286 } 287 | Popular Tags |