// $ ID: directpropertyaccessor. java 11405 12: 50: 34z max.andersen@jboss.com $ <br/> package Org. hibernate. property; </P> <p> Import Java. lang. reflect. field; <br/> Import Java. lang. reflect. method; <br/> Import Java. util. map; </P> <p> Import Org. hibernate. hibernateexception; <br/> Import Org. hibernate. propertyaccessexception; <br/> Import Org. hibernate. propertynotfoundexception; <br/> Import Org. hiberna Te. engine. sessionfactoryimplementor; <br/> Import Org. hibernate. engine. sessionimplementor; <br/> Import Org. hibernate. util. reflecthelper; </P> <p>/** <br/> * accesses fields directly. <br/> * @ author Gavin King <br/> */<br/> public class directpropertyaccessor implements propertyaccessor {</P> <p> Public static final class directgetter implements getter {<br/> private final transient field; <br/> P Rivate final class clazz; <br/> private final string name; <br/> directgetter (field, class clazz, string name) {<br/> This. field = field; <br/> This. clazz = clazz; <br/> This. name = Name; <br/>}< br/> Public object get (object target) throws hibernateexception {<br/> try {<br/> return field. get (target); // return the value of the field on the specified object. <Br/>}< br/> catch (exception e) {<br/> throw new propertyaccessexception (E, "cocould not get a field value by reflection", false, clazz, name); <br/>}</P> <p> Public object getforinsert (object target, map mergemap, sessionimplementor session) {<br/> return get (target); <br/>}</P> <p> Public method getmethod () {<br/> return NULL; <br/>}< br/> Public String getmethodname () {<br/> return NULL; <br/ >}< Br/> public class getreturntype () {<br/> return field. getType (); <br/>}</P> <p> Object readresolve () {<br/> return New directgetter (getfield (clazz, name), clazz, name); <br/>}</P> <p> Public String tostring () {<br/> return "directgetter (" + clazz. getname () + '. '+ name + ')'; <br/>}</P> <p> Public static final class directsetter implements setter {<br/> private final transient field; <B R/> private final class clazz; <br/> private final string name; <br/> directsetter (field, class clazz, string name) {<br/> This. field = field; <br/> This. clazz = clazz; <br/> This. name = Name; <br/>}< br/> Public method getmethod () {<br/> return NULL; <br/>}< br/> Public String getmethodname () {<br/> return NULL; <br/>}< br/> Public void set (object target, object value, sessionfactoryimplementor fa Ctory) throws hibernateexception {<br/> try {<br/> field. set (target, value); // set the field represented by this field object on the specified object variable to the specified new value. <Br/>}< br/> catch (exception e) {<br/> If (value = NULL & field. getType (). isprimitive () {<br/> throw new propertyaccessexception (<br/> E, <br/> "null value was assigned to a property of primitive type ", <br/> true, <br/> clazz, <br/> name <br/>); <br/>}else {<br/> throw new propertyaccessexception (E, "cocould not set a field value by reflection", true, clazz, name ); <br/>}</P> <p> Public String tostring () {<br/> return "directsetter (" + clazz. getname () + '. '+ name +') '; <br/>}</P> <p> Object readresolve () {<br/> return New directsetter (getfield (clazz, name ), clazz, name); <br/>}</P> <p> Private Static Field getfield (class clazz, string name) throws propertynotfoundexception {<br/> If (clazz = NULL | clazz = object. class) {<br/> throw new propertynotfoundexception ("field no T found: "+ name); <br/>}< br/> field; <br/> try {<br/> Field = clazz. getdeclaredfield (name); // returns a Field object that reflects the specified declared fields of the class object or interface. <Br/>}< br/> catch (nosuchfieldexception nsfe) {<br/> Field = getfield (clazz, clazz. getsuperclass (), name); <br/>}< br/> If (! Reflecthelper. ispublic (clazz, field) field. setaccessible (true); <br/> return field; <br/>}</P> <p> Private Static Field getfield (class root, class clazz, string name) throws propertynotfoundexception {<br/> If (clazz = NULL | clazz = object. class) {<br/> throw new propertynotfoundexception ("field [" + name + "] Not found on" + root. getname (); <br/>}< br/> Field field; <br/> try {<br/> Field = clazz. Getdeclaredfield (name); <br/>}< br/> catch (nosuchfieldexception nsfe) {<br/> Field = getfield (root, clazz. getsuperclass (), name); <br/>}< br/> If (! Reflecthelper. ispublic (clazz, field) field. setaccessible (true); <br/> return field; <br/>}</P> <p> Public getter getgetter (class theclass, string propertyname) <br/> throws propertynotfoundexception {<br/> return New directgetter (getfield (theclass, propertyname), theclass, propertyname ); <br/>}</P> <p> Public setter getsetter (class theclass, string propertyname) <br/> throws propertynotfoundexception {<br/> return New directsetter (getfield (theclass, propertyname), theclass, propertyname ); <br/>}</P> <p >}< br/>