Java reflection (see it at a glance)

Source: Internet
Author: User

Java Reflection Mechanism:

(1) in the running state
(2) For any class, all the properties and methods of the class can be known;
(3) Any object (an instance of a class) can invoke (access, detect and modify) any of its methods and properties;

This dynamic acquisition of information and the ability to dynamically invoke the object's methods is called the reflection mechanism of the Java language.

The effect of the reflection mechanism:

1. decompile:. Class-->.java

2. access to Java Object Properties, methods, construction methods, etc. through the reflection mechanism;


Instance:

First, the reflection mechanism to obtain the class has three kinds of methods
 PackageFirst ;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Modifier; Public classTest {Private intA; PrivateString ID;  Public Static voidMain (string[] args)throwsException {//1Class C = class.forname ("First.test");                System.out.println (c); //2Class C2 = Test.class;                System.out.println (C2); //3Test C3 =NewTest (); Class C4=C3.getclass ();            System.out.println (C4); }}

The output is as follows:
Class First.testclass First.testclass First.test

  

Second, create the object:
Class C = class.forname ("first.test");   /* * Create a new instance of the class represented by this class object * called the employee's parameterless construction method.   */  = C.newinstance ();


Third, get the property
(1) Get the specified property
 PackageFirst ;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Modifier; Public classTest {PrivateString ID;  Public Static voidMain (string[] args)throwsException {//gets the class package name. Class NameClass C = class.forname ("First.test"); //Gets the ID property of the test classField fs = C.getdeclaredfield ("id"); //instantiate this class to assign OObject o =c.newinstance (); //Break the package, enable to modifyFs.setaccessible (true); //The ID property of the O object is assigned a value of "" "when running stateFs.set (O, "110");//SetSystem.out.println (Fs.get (o)); }}

The output is as follows:
110

  

(2) Get all properties

 PackageFirst ;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Modifier; Public classTest { Public Static voidMain (string[] args)throwsException {//get the Integer classClass C = class.forname ("Java.lang.Integer"); //get all the propertiesfield[] fs =C.getdeclaredfields (); //defines a variable-length string used to store propertiesStringBuffer SB =NewStringBuffer (); //stitching each property into this string by appending the methodSb.append (Modifier.tostring (C.getmodifiers ()) + "class" + c.getsimplename () + "{\ n"); //iterate through each of the properties inside         for(Field field:fs) {sb.append ("\ t");//SpaceSb.append (Modifier.tostring (Field.getmodifiers ()) + "");//gets the modifier for the property, such as Public,static, and so onSb.append (Field.gettype (). Getsimplename () + "");//the name of the type of the propertySb.append (Field.getname () + "; \ n");//Name of property + carriage return} sb.append ("}");     System.out.println (SB); }}

The output is as follows:
Public final class Integer{public static final int min_value;public static final int max_value;public static final class T Ype;static final char[] digits;static final char[] digittens;static final char[] digitones;static final int[] Sizetable;pr Ivate Final int value;public static final int size;public static final int bytes;private static final long Serialversionui D;}

  







Reference:
http://blog.csdn.net/liujiahan629629/article/details/18013523


Java reflection (see it at a glance)

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.