当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。