Looking back, Java restudying (11): Java Reflection

Source: Internet
Author: User

Recently continue to review the Java Foundation, to the Java reflection this part, the personal feeling that this part should be considered Java advanced features, in the daily development of the use of not much, the application is mainly tool builders, so this study to understand the main, Java reflection mainly used in the development of tool-based projects , such as spring.

During the run of the program, the JRE creates a type identity for all objects, and the class that holds the information is class, and all functions are implemented around this class, and there are several ways to get it:

1.new Date (). GetClass ();

2.date.class;

3.class.forname (Java.util.Date)

The basic method is as follows (see the API for more methods):

1.class.getname () returns the class name

2.class.newinstance () Creating an instance from a Class object

3.class.getdeclaredfields () Gets an array of properties for the class

4.class.getdeclaredmethods () Gets the method array of the class

5.class.getdeclaredconstructors () Gets the constructor array for the class

6.field/method/constructor.getmodifiers () Gets the modifier for the property/method/constructor

The following is a demo of the parsing class, with the following results:

    

The code is as follows:

  

 PackageCom.demo.reflect;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Modifier;ImportJava.util.Scanner;/*** Java reflection gets class content *@authorLiweitao **/ Public classPrintclass { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); System.out.println ("Enter a class name"); String name=Scanner.nextline (); Try{Class CLS=class.forname (name); String modifier=modifier.tostring (Cls.getmodifiers ()); Class Supercls=Cls.getsuperclass (); Class[] Intercls=cls.getinterfaces (); System.out.print (modifier+ "class" +name); if(Supercls! =NULL&& supercls! = Object.class) {System.out.print ("Extends" +supercls.getname ()); }                         for(inti = 0; i < intercls.length; i++){                if(i = = 0) {System.out.print ("Implements"); }Else if(I > 0) {System.out.print (",");            } System.out.print (Intercls[i].getname ()); } System.out.println ("{");            Printfields (CLS);            Printconstructors (CLS);            Printmethods (CLS); System.out.print ("}"); } Catch(ClassNotFoundException e) {e.printstacktrace (); }                    }        /*** Print field *@paramCLS*/     Public Static voidPrintfields (Class cls) {field[] fields=Cls.getdeclaredfields ();  for(Field field:fields) {stringbuffer result=NewStringBuffer (); Class type=Field.gettype (); String name=Field.getname (); String modifiers=modifier.tostring (Field.getmodifiers ()); if(Modifiers.length () > 0) {result.append ("\ T"). Append (Modifiers). Append (""). Append (Type.getname ()). Append (" "). Append (name);        } System.out.println (Result.tostring ()); }    }    /*** Print Builder *@paramCLS*/     Public Static voidprintconstructors (Class cls) {constructor[] constructors=cls.getdeclaredconstructors ();  for(Constructor constructor:constructors) {stringbuffer result=NewStringBuffer (); String name=Constructor.getname (); String modifiers=modifier.tostring (Constructor.getmodifiers ()); if(Modifiers.length () > 0) {result.append ("\ T"). Append (Modifiers). Append (""). Append (name). Append ("("); } class[] Params=constructor.getparametertypes ();  for(intj = 0; J < Params.length; J + +){                if(J > 0) {result.append (",");            } result.append (Params[j].getname ()); } result.append (")");        System.out.println (Result.tostring ()); }    }         Public Static voidprintmethods (Class cls) {method[] methods=Cls.getdeclaredmethods ();  for(Method method:methods) {stringbuffer result=NewStringBuffer (); String name=Method.getname (); Class ReturnType=Method.getreturntype (); String modifier=modifier.tostring (Method.getmodifiers ()); Class[] Params=method.getparametertypes (); if(Modifier.length () > 0) {result.append ("\ T"). Append (modifier). Append (""). Append (Returntype.getname ()). Append (""). Append (name). Append ("("); }                         for(intj = 0; J < Params.length; J + +){                if(J > 0) {result.append (",");            } result.append (Params[j].getname ()); } result.append (")");        System.out.println (Result.tostring ()); }    }    }

Looking back, Java restudying (11): Java Reflection

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.