📌  相关文章
📜  Java.lang.instrument.ClassDefinition Java的类

📅  最后修改于: 2022-05-13 01:55:36.223000             🧑  作者: Mango

Java.lang.instrument.ClassDefinition Java的类

此类用于将提供的类和类文件字节绑定到单个 ClassDefinition 对象中。这些类提供了提取有关类类型和类文件字节信息的方法。这个类是Java.lang.Object 类的子类。

类声明:

public final class ClassDefinition
extends Object

构造函数:

ConstructorDescription 
ClassDefinition(Class theClass, byte[] theClassFile)This constructor creates a new instance of  ClassDefinition class by binding the supplied class and class file bytes.

方法:

MethodDescription
getDefinitionClass()This method is used to get the class type of this class
getDefinitionClassFile()This method Is used to get the array of bytes that contains the new class file.

示例 1: Java程序创建新的 ClassDefinition 对象



Java
// Java program to create new ClassDefinition object
import java.lang.instrument.ClassDefinition;
  
// driver class
public class GFG {
    // demoClass
    static class demoClass {
        void msg() { System.out.println("GeeksForGeeks"); }
    }
    // main method
    public static void main(String[] args)
    {
        try {
  
            // creating demoClass object
            demoClass cls = new demoClass();
            
            // creating object of supplied class
            Class theClass = cls.getClass();
            
            // creating array of class files
            byte[] classFiles = { 0 };
            
            // creating a new ClassDefinition object
            ClassDefinition classdefinition
                = new ClassDefinition(theClass, classFiles);
            System.out.println(
                "ClassDefinition object successfully created");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Java
// Java program to illustrate ClassDefinition class methods
import java.lang.instrument.ClassDefinition;
  
// driver class
public class GFG {
    
    // demoClass
    static class demoClass {
        void msg() { System.out.println("GeeksForGeeks"); }
    }
    
    // main method
    public static void main(String[] args)
    {
        try {
  
            // creating demoClass object
            demoClass cls = new demoClass();
            
            // creating object of supplied class
            Class theClass = cls.getClass();
            
            // creating array of class files
            byte[] classFiles = { 0 };
            
            // creating a new ClassDefinition object
            ClassDefinition classdefinition
                = new ClassDefinition(theClass, classFiles);
            
            // printing the class
            System.out.println(
                classdefinition.getDefinitionClass());
            
            // printing array of bytes that contain class
            // files
            System.out.println(
                classdefinition.getDefinitionClassFile());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


输出
ClassDefinition object successfully created

示例 2: Java程序来说明 ClassDefinition 类方法

Java

// Java program to illustrate ClassDefinition class methods
import java.lang.instrument.ClassDefinition;
  
// driver class
public class GFG {
    
    // demoClass
    static class demoClass {
        void msg() { System.out.println("GeeksForGeeks"); }
    }
    
    // main method
    public static void main(String[] args)
    {
        try {
  
            // creating demoClass object
            demoClass cls = new demoClass();
            
            // creating object of supplied class
            Class theClass = cls.getClass();
            
            // creating array of class files
            byte[] classFiles = { 0 };
            
            // creating a new ClassDefinition object
            ClassDefinition classdefinition
                = new ClassDefinition(theClass, classFiles);
            
            // printing the class
            System.out.println(
                classdefinition.getDefinitionClass());
            
            // printing array of bytes that contain class
            // files
            System.out.println(
                classdefinition.getDefinitionClassFile());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
输出
class GFG$demoClass
[B@448139f0