1 package org.apache.ojb.broker.metadata.fieldaccess; 2 3 17 18 import org.apache.ojb.broker.core.proxy.ProxyHelper; 19 import org.apache.ojb.broker.metadata.MetadataException; 20 21 import java.lang.reflect.Field ; 22 23 35 public class PersistentFieldDirectAccessImpl extends AbstractPersistentField 36 { 37 private static final long serialVersionUID = -5458024240998909205L; 38 39 public PersistentFieldDirectAccessImpl() 40 { 41 } 42 43 public PersistentFieldDirectAccessImpl(Class type, String fieldname) 44 { 45 super(type, fieldname); 46 } 47 48 57 public void doSet(Object obj, Object value) throws MetadataException 58 { 59 Field fld = getField(); 60 Class type = fld.getType(); 61 try 62 { 63 68 if ((value != null) || !type.isPrimitive()) 70 { 71 fld.set(ProxyHelper.getRealObject(obj), value); 72 } 73 } 74 catch (NullPointerException ignored) 75 { 76 getLog().info("Value for field " + (fld != null ? fld.getName() : null) + 77 " of type " + type.getName() + " is null. Can't write into null.", ignored); 78 } 79 catch (IllegalAccessException e) 80 { 81 getLog().error("while set field: " + buildMessageString(obj, value, fld)); 82 throw new MetadataException("IllegalAccess error setting field:" + 83 (fld != null ? fld.getName() : null) + " in object:" + obj.getClass().getName(), e); 84 } 85 catch (Exception e) 86 { 87 getLog().error("while set field: " + buildMessageString(obj, value, fld), e); 88 throw new MetadataException("Error setting field:" + (fld != null ? fld.getName() : null) + 89 " in object:" + obj.getClass().getName(), e); 90 } 91 } 92 93 101 public Object doGet(Object obj) throws MetadataException 102 { 103 if(obj == null) return null; 104 Field fld = getField(); 105 try 106 { 107 Object result = fld.get(ProxyHelper.getRealObject(obj)); 108 return result; 109 } 110 catch (IllegalAccessException e) 111 { 112 throw new MetadataException( 113 "IllegalAccess error getting field:" + 114 (fld != null ? fld.getName() : null) + " in object:" 115 + obj != null ? obj.getClass().getName() : null, e); 116 } 117 catch (Throwable e) 118 { 119 throw new MetadataException("Error getting field:" + 120 (fld != null ? fld.getName() : null) + " in object:" + 121 (obj != null ? obj.getClass().getName() : null) , e); 122 } 123 } 124 125 129 public boolean makeAccessible() 130 { 131 return true; 132 } 133 134 138 public boolean usesAccessorsAndMutators() 139 { 140 return false; 141 } 142 } 143 | Popular Tags |