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 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 40 public class InstanceVariableAttributeAccessor extends AttributeAccessor { 41 42 43 protected transient Field attributeField; 44 45 48 public Class getAttributeClass() { 49 if (getAttributeField() == null) { 50 return null; 51 } 52 53 return getAttributeType(); 54 } 55 56 59 protected Field getAttributeField() { 60 return attributeField; 61 } 62 63 66 public Class getAttributeType() { 67 return attributeField.getType(); 68 } 69 70 73 public Object getAttributeValueFromObject(Object anObject) throws DescriptorException { 74 try { 75 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 77 try { 78 return AccessController.doPrivileged(new PrivilegedGetValueFromField(this.attributeField, anObject)); 79 } catch (PrivilegedActionException 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 exception) { 86 throw DescriptorException.illegalArgumentWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), getAttributeType().getName(), anObject.getClass().getName(), exception); 87 } catch (IllegalAccessException exception) { 88 throw DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor(getAttributeName(), anObject.getClass().getName(), exception); 89 } catch (NullPointerException exception) { 90 String className = null; 91 if (anObject != null) { 92 className = anObject.getClass().getName(); 94 } 95 throw DescriptorException.nullPointerWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), className, exception); 96 } 97 } 98 99 102 public void initializeAttributes(Class theJavaClass) throws DescriptorException { 103 if (getAttributeName() == null) { 104 throw DescriptorException.attributeNameNotSpecified(); 105 } 106 try { 107 setAttributeField(Helper.getField(theJavaClass, getAttributeName())); 108 } catch (NoSuchFieldException exception) { 109 throw DescriptorException.noSuchFieldWhileInitializingAttributesInInstanceVariableAccessor(getAttributeName(), theJavaClass.getName(), exception); 110 } catch (SecurityException exception) { 111 throw DescriptorException.securityWhileInitializingAttributesInInstanceVariableAccessor(getAttributeName(), theJavaClass.getName(), exception); 112 } 113 } 114 115 118 protected void setAttributeField(Field field) { 119 attributeField = field; 120 } 121 122 125 public void setAttributeValueInObject(Object anObject, Object value) throws DescriptorException { 126 DescriptorException descriptorException; 127 128 try { 129 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 131 try { 132 AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, value)); 133 } catch (PrivilegedActionException 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 exception) { 140 try { 142 if (value == null) { 146 Class 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 (0), fieldClass))); 153 } catch (PrivilegedActionException 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 (0), fieldClass)); 158 } 159 } 160 return; 161 } 162 } catch (IllegalAccessException accessException) { 163 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception); 164 } 165 166 try { 169 if (value instanceof String ) { 170 Object newValue = ConversionManager.getDefaultManager().convertObject(value, getAttributeClass()); 171 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 172 try { 173 AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, newValue)); 174 } catch (PrivilegedActionException exc) { 175 } 176 } else { 177 oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, newValue); 178 } 179 return; 180 } 181 } catch (Exception e) { 182 } 184 throw DescriptorException.illegalArgumentWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), getAttributeType().getName(), value, exception); 185 } catch (IllegalAccessException exception) { 186 if (value == null) { 187 return; 188 } 189 throw DescriptorException.illegalAccessWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), anObject.getClass().getName(), value, exception); 190 } catch (NullPointerException exception) { 191 try { 192 if (anObject != null) { 195 Class 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 (0), fieldClass))); 203 } catch (PrivilegedActionException 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 (0), fieldClass)); 208 } 209 } 210 } else { 211 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception); 212 } 213 } else { 214 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception); 216 } 217 } catch (IllegalAccessException accessException) { 218 throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception); 219 } 220 } 221 } 222 } 223 | Popular Tags |