Java reflection (Reflect)

Source: Internet
Author: User

The cornerstone of reflection is the Class. The Class represents the bytecode obtained after the java source file is compiled,

Several methods to obtain the Class type:

1. class Name. class

2. Object Name. getClass ()

3. Class. forName ("Class path ")


Constructor reflection:


Package test;

Import java. lang. reflect. Constructor;
/**
*
* @ Describe reflection Basics
* @ Author fancy
* @ Mailbox fancydeepin@yeah.net
* @ 2012-8-24 <p>
*
*/
Public class ReflectApp {

Public static void main (String [] args ){

Try {
// Obtain the Class
Class <?> Clazz = Class. forName ("java. lang. Integer ");
// Get the constructor whose parameters are String type
Constructor <?> Constructor = clazz. getConstructor (String. class );
// Use the constructor to create an Instance Object
Object obj = constructor. newInstance ("10 ");
// Print the result
System. out. println (obj );
} Catch (Exception e ){
E. printStackTrace ();
}
}

}

Output result in the background:


10


Reflection of member variables:


Package test;

Import java. lang. reflect. Field;

/**
*-----------------------------------------
* @ Describe reflection Basics
* @ Author fancy
* @ Mailbox fancydeepin@yeah.net
* @ 2012-8-24 <p>
*-----------------------------------------
*/
Public class ReflectApp {

Public static void main (String [] args ){

Try {
// Create a Person Instance Object
Person person = new ReflectApp (). new Person ();
// Obtain the Class
Class <?> Clazz = person. getClass ();
/******** The attribute variable for public permission access through reflection ********/
// Obtain the attribute name of the field
Field age = clazz. getDeclaredField ("age ");
// Set the value of this attribute
Age. set (person, 22 );

/******* The property variable used to access the protected permission through reflection *******/
Field mail = clazz. getDeclaredField ("mail ");
Mail. set (person, "fancydeepin@yeah.net ");

/******** The attribute variable used to access the private permission through reflection ********/
Field name = clazz. getDeclaredField ("name ");
// Private-modified variables cannot be accessed directly. If you need to access these variables, you must set the setAccessible value to true, that is, force access or brute-force access.
Name. setAccessible (true );
Name. set (person, "fancy ");
System. out. println (person );
} Catch (Exception e ){
E. printStackTrace ();
}
}

// Internal class
Public class Person {

Public int age;
Private String name;
Protected String mail;

Public int getAge (){
Return age;
}

Public String getName (){
Return name;
}

Public void setAge (int age ){
This. age = age;
}

Public void setName (String name ){
This. name = name;
}

Public String getMail (){
Return mail;
}

Public void setMail (String mail ){
This. mail = mail;
}

@ Override
Public String toString (){

Return "Name is:" + name + ", \ t Age is:" + age + ", \ t Mail is:" + mail;
}

}
}

Output result in the background:


Name is: fancy, Age is: 22, Mail is: fancydeepin@yeah.net


Reflection of member methods:


Package test;

Import java. lang. reflect. Method;

/**
*-----------------------------------------
* @ Describe reflection Basics
* @ Author fancy
* @ Mailbox fancydeepin@yeah.net
* @ 2012-8-24 <p>
*-----------------------------------------
*/
Public class ReflectApp {

Public static void main (String [] args ){

Try {
// Obtain the Class
Class <?> Clazz = Class. forName ("test. ReflectApp ");
// Obtain Method
Method method = clazz. getDeclaredMethod ("print", String. class );
// Call this method
Method. invoke (clazz. newInstance (), "fancy ");
} Catch (Exception e ){
E. printStackTrace ();
}
}

Public void print (int arg ){
System. out. println ("Arg is int, value is" + arg );
}

Public void print (String arg ){
System. out. println ("Arg is String, value is" + arg );
}
}

Output result in the background:


Arg is String, value is fancy

 

For a simple example:

Question: ArrayList <Integer> list = new ArrayList <Integer> (); stores a String-type object in the ArrayList of Integer type.

Personal analysis: Generic Type check only exists during compilation and does not exist during runtime. reflection can be used to implement Question Setting requirements.


Package examination. topic_04;

Import java. lang. reflect. Method;
Import java. util. ArrayList;

/**
*-----------------------------------------
* @ Description test class
* @ Author fancy
* @ Mailbox fancydeepin@yeah.net
* @ 2012-8-24 <p>
*-----------------------------------------
*/
/**
* TOPIC: ArrayList <Integer> list = new ArrayList <Integer> (); stores a String-type object in the ArrayList of Integer type.
*/
Public class TestApp {

Public static void main (String [] args ){

/**
* Generic type check only exists during compilation and does not exist during runtime. reflection can be used to implement Question Setting requirements.
*/
Try {
ArrayList <Integer> list = new ArrayList <Integer> ();
Method add = ArrayList. class. getDeclaredMethod ("add", Object. class );
Add. invoke (list, "fancy ");
System. out. println (list. get (0 ));
} Catch (Exception e ){
E. printStackTrace ();
}

}

}

Output result in the background:


Fancy

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.