Java Reflection (Application of field)

Source: Internet
Author: User

//$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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.