Java reflection example

1. Modify the value of a constant through reflection

package com.blueStarWei.invoke;

import java.lang.reflect.Field;

public class ModifyFinalField {
    
    private final Integer KEY_EXIT = 1024;
    
    private static void invoke() throws Exception{
        ModifyFinalField mff = new ModifyFinalField();
        
        System.out.println( "Before modifying : "+ mff.KEY_EXIT); //1024
         
//Get attribute [private final java.lang.Integer com.blueStarWei.invoke.ModifyFinalField.KEY_EXIT] Field field
= mff.getClass() .getDeclaredField("KEY_EXIT" );
//Ignore the attribute's access permission field.setAccessible(
true );
//Set the new value field.set(mff,
512 ); System.out.println("After modifying : "+mff.KEY_EXIT);//512 } public static void main(String[] args) { try { invoke(); } catch (Exception e) { e.printStackTrace (); } } }

 

2. Modify the value of static constants through reflection

package com.blueStarWei.invoke;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ModifyFinalField {
    
    private static final Integer KEY_EXIT = 1024;
    
    private static void invoke() throws Exception{
        System.out.println( "Before modifying : "+ ModifyFinalField.KEY_EXIT); //1024
         
//Get attribute [private final java.lang.Integer com.blueStarWei.invoke.ModifyFinalField.KEY_EXIT] Field field
= ModifyFinalField.class .getDeclaredField ("KEY_EXIT" ); //Ignore access permissions
field.setAccessible(
true ); //Ignore static modifiers [I don't understand the principle] Field modifiers = Field.class .getDeclaredField("modifiers" ); modifiers.setAccessible(true); modifiers.setInt(field, field.getModifiers() &~ Modifier.FINAL);
//set new value field.set(
null , 512 ); System.out.println("After modifying : "+ModifyFinalField.KEY_EXIT);//512 } public static void main(String[] args) { try { invoke(); } catch (Exception e) { e.printStackTrace (); } } }

 

3. Precautions

    3.1 When compiling final constants of basic data types and String types, the compiler will automatically replace the place where the constant is used with the actual value (regardless of whether it is static or not); this phenomenon does not exist for encapsulated types.

static final int A = 23;
if(i > A){
  System.out.println(A);
}

// automatically compiled into

static final int A = 23;
if(i > 23){
  System.out.println(23);
}

    3.2 Problems caused: Even if the value of the final constant of the basic data type and String type is modified through reflection, the value is still the original value when the constant is used.

package com.blueStarWei.invoke;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public  class SpecialCase {

    private static final int NUM = 1024;
    
    private static void invok1() throws Exception {
        System.out.println("Before modify : "+SpecialCase.NUM);//Before modify : 1024
        Field field = SpecialCase.class.getDeclaredField("NUM");
        field.setAccessible(true);
        Field modifiers = Field.class.getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(field, field.getModifiers()&~Modifier.FINAL);
        field.set( null , 512 );
         // Break the point on the next line, you will find that the value of NUM has become 512, but the output is still 1024 
        System.out.println("After modify : "+SpecialCase.NUM); // After modify : 1024 
    }
    
    public static void main(String[] args) {
        try {
            invok1 ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}

 

    For more content, please visit: http://www.cnblogs.com/BlueStarWei/

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324976200&siteId=291194637