Java reflection mechanism

Source: Internet
Author: User

Importjava.lang.annotation.Annotation;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Modifier;/** Test the reflection mechanism to get information based on the class name*/ Public classClassloadtest { Public Static voidMain (string[] args)throwsillegalargumentexception, InvocationTargetException, ClassNotFoundException, Instantiationexception, illegalaccessexception {String ClassName= "Test.son"; Class GetClass=Class.forName (className); Object GetObject=getclass.newinstance (); /** Get constructors of classes and their formal parameters and exceptions*/System.out.println ("======================================================="); Constructor[] Constructlist=getclass.getdeclaredconstructors ();//constructor[] constructlist = Getclass.getconstructors ();             for(inti = 0; i < constructlist.length; i++) {Constructor<?> constructor =Constructlist[i]; System.out.println ("Construction Method Name" + i + ":" +constructor.getname ()); System.out.println ("Class or interface:" +Constructor.getdeclaringclass ()); Class<?>[] Constructparamlist =constructor.getparametertypes ();  for(intj = 0; J < Constructparamlist.length; i++) {System.out.println ("Construction Method Parameter" + i + "type:" +constructparamlist[i].getname ()); } Class<?>[] Getexceplist =constructor.getexceptiontypes ();  for(intj = 0; J < Getexceplist.length; J + +) {System.out.println ("Construction Method Exception:" + j + "type:" +getexceplist[j].getname ()); } System.out.println ("---------------------------"); }            /** Obtain the corresponding class method and its formal parameters and exceptions (method name, method class or interface, formal parameter type, exception type )*/System.out.println ("======================================================="); /** Getdeclaredmethods obtains all private, default, protected, and common methods represented by class, but does not include the inheritance of local law (* If the Super class and sub classes are compiled in the same Java source file, explicit The results of the show include methods shared by the parent class) * Getmethodshuode obtains all public methods of class and its parent class * Getdeclaredconstructors and Getconstructo RS is the same*/            //method[] methods = C.getdeclaredmethods ();Method[] Methods =Getclass.getmethods ();  for(Method m:methods) {annotation[] annotations=m.getannotations (); System.out.println ("Get Comments" +annotations.tostring ()); System.out.println ("Method Name:" +m.getname ()); System.out.println ("The method is in the same class or interface:" +M.getdeclaringclass ()); Class<?>[] Pramaterlist =m.getparametertypes ();  for(inti = 0; i < pramaterlist.length; i++) {System.out.println (Type of "parameter" + i + ":" +pramaterlist[i].getname ()); } Class<?>[] Getexceplist =m.getexceptiontypes ();  for(inti = 0; i < getexceplist.length; i++) {System.out.println ("Exception:" + i + "type:" +getexceplist[i].getname ()); }//M.invoke (getObject, null);//execution methods where variable parameters are involvedSystem.out.println ("---------------------------"); }            /** Gets the attribute of the corresponding class (name, interface or class, type, permission decoration*/System.out.println ("======================================================="); Field filedlist[]=Getclass.getdeclaredfields ();  for(inti = 0; i < filedlist.length; i++) {Field F=Filedlist[i]; System.out.println ("Properties" + i + ":" +f.getname ()); System.out.println ("Where Interface or class:" +F.getdeclaringclass ()); System.out.println ("Property Type:" +F.gettype ()); System.out.println ("Property decoration is:" +modifier.tostring (F.getmodifiers ())); System.out.println ("---------------------------"); }    }}





 Public classSonextendsfather{Static{System.out.println ("(son) static code"); }             PublicSon () {System.out.println ("(son) constructor"); }                Private intprivateparameter_s;  Public intpublicparameter_s; protected intprotectedparameter_s; intdefaultparameter_s; //Note (son) Call the public method         Public voidpublicmethod_s () {System.out.println ("(son) Call the public method"); }        Private  voidprivatemethod_s () {System.out.println ("(son) call a private method"); }        protected voidprotectedmethod_s () {System.out.println ("(son) call a protected method"); }        voiddesaultmethod_s () {System.out.println ("(son) Call the default method"); }    }        classfather{Private intPrivateparameter_f;  Public intPublicparameter_f; protected intProtectedparameter_f; intDefaultparameter_f;  Publicfather () {Super(); System.out.println ("(father) constructor"); }         Public voidPublicmethod_f () {System.out.println ("(father) Call the public method"); }        Private  voidPrivatemethod_f () {System.out.println ("(father) call a private method"); }        protected voidProtectedmethod_f () {System.out.println ("(father) call a protected method"); }        voidDefaultmethod_f () {System.out.println ("(father) Call the default method"); }    }




Java reflection mechanism

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.