How do I get the contents of a bytecode file in java

  • 2020-06-19 10:19:00
  • OfStack

How do I get the contents of a bytecode file in java

Reflection means that all properties and methods of any class (class file) can be known at runtime. Methods and properties of any object can be called. This ability to dynamically retrieve information and dynamically invoke methods on objects is called the reflection mechanism of the Java language.

Simply put, the dynamic retrieval of information from a class is the reflection mechanism of Java.

In Java's reflection mechanism, we can use the configuration file information and then use the class name to get the details contained in the class, such as constructors, member variables, and member functions. In the following sections, the authors demonstrate how to retrieve information contained in a class by its name.

1. Get the constructor for the bytecode file


import java.lang.reflect.Constructor;

public class GetClassConstructor {
  public static void main(String[] args) throws Exception {
    createNewObject_2();
  }

  public static void createNewObject_2() throws Exception {
    /**
     *  Gets the object represented in the class with the specified name, and the initialization of the object does not apply to the constructor of the null parameter 
     *  You can first get the constructor for the null parameter through the bytecode file object of the class 
     *  The method is as follows: getConstructor(parameterTypes)
     */

  //  The package name 1 Make sure you write it all, or it will be reported "java.lang.ClassNotFoundException" abnormal 
  String name = "src.Person";
    //  Find the name class file and add it to memory to generate Class object 
    Class clazz = Class.forName(name);
    //  Gets the specified constructor object 
    Constructor constructor = clazz.getConstructor(String.class, int.class);
    //  Through the constructor object newInstance Initialize the object 
    constructor.newInstance(" Xiao Ming ", 12);
  }

  public static void createNewObject() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    String name = "src.Person";
    //  Find the name class file and add it to memory to generate Class object 
    Class clazz = Class.forName(name);
    //  The instance object that produces the class (empty argument) 
    Object obj = clazz.newInstance();
  }
}

2. Gets the member variable of the bytecode file


import java.lang.reflect.Field;

public class GetClassField {
  public static void main(String[] args) throws Exception {
    getField();
  }

  /**
   *  Gets a member variable in a bytecode file 
   */
  public static void getField() throws Exception {
    Class clazz = Class.forName("src.Person");
    Field field = null;

    //  Gets the field of this class, containing the private 
    field = clazz.getDeclaredField("age");

    //  Access to a private field is cancelled by permission checks, which can be called violent access 
    field.setAccessible(true);

    Object obj = clazz.newInstance();

    field.set(obj, Integer.valueOf(89));

    Object o = field.get(obj);
    System.out.println(o);
  }
}

3. Get the member function of the bytecode file


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class GetClassMethod {
  public static void main(String[] args) throws Exception {
    getMethod_3();
  }

  public static void getMethod_3() throws Exception {
    Class clazz = Class.forName("src.Person");
    Method method = clazz.getMethod("walkInfo", new Class[]{String.class, Integer.TYPE});
    Object obj = clazz.newInstance();
    method.invoke(obj, new Object[]{" Jack Bauer ", Integer.valueOf(20)});
  }

  public static void getMethod_2() throws Exception {
    Class clazz = Class.forName("src.Person");

    //  Method to get an empty parameter 
    Method method = clazz.getMethod("show", (Class[]) null);
    Constructor constructor = clazz.getConstructor(new Class[]{String.class, Integer.TYPE});
    Object obj = constructor.newInstance(new Object[]{" Xiao Ming ", Integer.valueOf(12)});
    method.invoke(obj, (Object[]) null);
  }

  /**
   *  Access to specify Class Public function in 
   */
  public static void getMethod() throws Exception {
    Class clazz = Class.forName("src.Person");

    //  Get the public methods in the class 
    Method[] methods = clazz.getMethods();

    //  Gets all the methods in this class 
    methods = clazz.getDeclaredMethods();
    Method[] var5 = methods;
    int var4 = methods.length;

    for (int var3 = 0; var3 < var4; ++var3) {
      Method method = var5[var3];
      System.out.println(method);
    }
  }
}

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: