//$Id: Directpropertyaccessor.java 11405 2007-04-15 12:50:34z [email protected] $ PackageOrg.hibernate.property;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportJava.util.Map;Importorg.hibernate.HibernateException;Importorg.hibernate.PropertyAccessException;Importorg.hibernate.PropertyNotFoundException;ImportOrg.hibernate.engine.SessionFactoryImplementor;ImportOrg.hibernate.engine.SessionImplementor;ImportOrg.hibernate.util.ReflectHelper;/*** accesses fields directly. *@authorGavin King*/ Public classDirectpropertyaccessorImplementsPropertyAccessor { Public Static Final classDirectgetterImplementsGetter {Private Final transientField field; Private FinalClass Clazz; Private FinalString name; Directgetter (Field field, Class clazz, String name) { This. field =field; This. Clazz =Clazz; This. Name =name; } PublicObject get (Object target)throwshibernateexception {Try { returnField.get (target);//returns the value of the field represented by this field on the specified object. } Catch(Exception e) {Throw NewPropertyaccessexception (E, "Could not get a field value by reflection",false, Clazz, name); } } Publicobject Getforinsert (object target, Map Mergemap, Sessionimplementor session) {returnget (target); } PublicMethod GetMethod () {return NULL; } PublicString Getmethodname () {return NULL; } PublicClass Getreturntype () {returnField.gettype (); } Object Readresolve () {return NewDirectgetter (GetField (clazz, name), Clazz, name); } PublicString toString () {return"Directgetter (" + clazz.getname () + ') ' + ' + ') '; } } Public Static Final classDirectsetterImplementsSetter {Private Final transientField field; Private FinalClass Clazz; Private FinalString name; Directsetter (Field field, Class clazz, String name) { This. field =field; This. Clazz =Clazz; This. Name =name; } PublicMethod GetMethod () {return NULL; } PublicString Getmethodname () {return NULL; } Public voidSet (object target, object value, Sessionfactoryimplementor factory)throwshibernateexception {Try{field.set (target, value);//sets the field that is represented by this field object on the specified object variable to the specified new value. } Catch(Exception e) {if(Value = =NULL&&Field.gettype (). Isprimitive ()) { Throw Newpropertyaccessexception (E,"Null value is assigned to a property of primitive type", true, Clazz, name); } Else { Throw NewPropertyaccessexception (E, "Could not set a field value by reflection",true, Clazz, name); } } } PublicString toString () {return"Directsetter (" + clazz.getname () + ') ' + ' + ') '; } Object Readresolve () {return NewDirectsetter (GetField (clazz, name), Clazz, name); } } Private StaticField GetField (Class clazz, String name)throwspropertynotfoundexception {if(clazz==NULL|| Clazz==object.class ) { Throw NewPropertynotfoundexception ("Field not found:" +name); } Field field; Try{field= Clazz.getdeclaredfield (name);//returns a Field object that reflects the specified declared field of the class or interface represented by this class object. } Catch(nosuchfieldexception nsfe) {field=GetField (Clazz, Clazz.getsuperclass (), name); } if( ! Reflecthelper.ispublic (clazz, field)) Field.setaccessible (true); returnfield; } Private StaticField GetField (class root, Class Clazz, String name)throwspropertynotfoundexception {if(clazz==NULL|| Clazz==object.class ) { Throw NewPropertynotfoundexception ("field [" + name + "] not found on" +root.getname ()); } Field field; Try{field=Clazz.getdeclaredfield (name); } Catch(nosuchfieldexception nsfe) {field=GetField (Root, Clazz.getsuperclass (), name); } if( ! Reflecthelper.ispublic (clazz, field)) Field.setaccessible (true); returnfield; } PublicGetter getgetter (Class theclass, String PropertyName)throwspropertynotfoundexception {return NewDirectgetter (GetField (Theclass, PropertyName), Theclass, PropertyName); } PublicSetter getsetter (Class theclass, String PropertyName)throwspropertynotfoundexception {return NewDirectsetter (GetField (Theclass, PropertyName), Theclass, PropertyName); }}
Java Reflection (Application of field)