Reflect reflection mechanism of Java __java

Source: Internet
Author: User
 
Package Com.jiaocaigen; 
Import Java.lang.reflect.Constructor; 
Import Java.lang.reflect.Field; 
Import java.lang.reflect.InvocationTargetException; 
  

  

Import Java.lang.reflect.Method; public class Reflecttest {public static void main (string[] args) throws exception{/** * Get byte code 
       The corresponding instance objects are 3 ways to take string as an example/string teststr = "Goodleiwei"; 
       Class getClassWay1 = Teststr.getclass (); Class getClassWay2 = String. 
       class; 
       Class getClassWay3 = Class.forName ("java.lang.String"); System. 
       Out. println (getClassWay1 = = GetClassWay2); System. 
       
       
       
       Out. println (getClassWay2 = = getClassWay3); Reflection Application of/** * Construction Method: The construction method is the fundamental way to generate objects * get all the construction methods of a class * constructor[] GetConstructor = Class.forName ("java.la Ng. 
         String "). GetConstructors (); */String str1 = (string) class.forname ("java.lang.String"). newinstance (); Default construction method gets byte code generation instance Object String str2 = (String) class.forname ("java.lang.String")//Get byte code that is the case object. GetConstructor (StringBuffer. C LASS)//Get the specific constructor in this object. newinstance (New StringBuffer ("abc")); Generates an object instance System based on the constructor. 
       
       
       
       Out. println (Str2.charat (2)); 
       /** * Member Variable Reflection Field */User user2 = new User (3,5); 
       Field fieldx = User2.getclass (). GetField ("X"); What's the value of the FIELDX? is 3, the wrong!FIELDX is not a variable on the object, but a variable on the class. To use FIELDX to an object to fetch values such as: System. 
       
       Out. println (Fieldx.get (user2)); A field that is not visible can only be obtained and output by the following way, otherwise it is reported java.lang.NoSuchFieldException java.lang.IllegalAccessException field fie 
       Ldy = User2.getclass (). Getdeclaredfield ("Y"); 
       Fieldy.setaccessible (TRUE); System. 
 Out. println (Fieldy.get (user2)); /** * Member Variable Reflection application field * Change all s in the String attribute field in the user class to H/user user = (User) class.forname ("Com.jiAocaigen. 
       User "). newinstance (); 
       Changstringvalue (user); System. 
       
       
       
       Out. println (user); /** * Member Method Reflection application method invokes the user's main way///new User (). Main (New string[]{"111", "222", "333"}); Traditional dry/* * Do not know the name of the Access class to start---you can configure COM in run As-->run configuration-->arguments. Jiaocaigen . 
       User * Otherwise will report Java.lang.ArrayIndexOutOfBoundsException/String startingclassname = args[0]; 
       Reflection gets to Main method methods = Class.forName (Startingclassname). GetMethod ("main", string[]. Class);  * * Call static method The first parameter does not need to be passed a value * The second parameter must be written as follows or new object[]{new string[]{"111", "222", "333"}} *  Otherwise the report will be Java.lang.IllegalArgumentException:wrong number of arguments/Method.invoke (null, (Object) New 
string[]{"111", "222", "333"}); The private static void Changstringvalue (Object obj) throws IllegalArgumentException, Illegalaccessexception {//Get all the property fields in the Obj class field[] fields = Obj.getclass (). GetFields (); 
              for (field Field:fields) {if (field.gettype () = = String. Class) {//Get the value of the filed field on the Obj class 
              String oldstrvalue = (string) field.get (obj); 
              Replaces the value of the filed field on the obj class String newstrvalue = Oldstrvalue.replace (' s ', ' H '); 
           Field.set (obj, newstrvalue); 
    Class user{public String name1 = "Zhangsan"; 
    Public String name2 = "Lisi"; 
    
    Public String Name3 = "Wangwu"; 
    public int x; 
    
    private int y; 
       Public user () {} public user (int x, int y) {super (); this. 
       x = x; this. 
    y = y; 
    @Override public String toString () {return name1 + ":" + name2 + ":" + name3; 
   public static void Main (string[] args) {for (String String:args) {        System. 
       Out. println (string); 
 } 
    } 
    
}

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.