當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java java.lang.instrument.ClassDefinition用法及代碼示例


該類用於將提供的類和class file bytes綁定在一起,形成一個ClassDefinition對象。這些類提供了提取有關 object 的類類型和類文件字節的信息的方法。該類是java.lang.Object類的子類。

類聲明:

public final class ClassDefinition
extends Object

構造函數:

構造函數 說明
ClassDefinition(Class<?> theClass, byte[] theClassFile) 此構造函數通過綁定提供的類和類文件字節來創建 ClassDefinition 類的新實例。

方法:

方法 說明
getDefinitionClass() 該方法用於獲取該類的類類型
getDefinitionClassFile() 此方法用於獲取包含新類文件的字節數組。

示例 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(); 
        } 
    } 
}
輸出
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


相關用法


注:本文由純淨天空篩選整理自abhinavjain194大神的英文原創作品 java.lang.instrument.ClassDefinition Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。