Open In App

java.lang.instrument.ClassDefinition Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

This class is used to bind together the supplied class and class file bytes in a single ClassDefinition object. These class provide methods to extract information about the type of class and class file bytes of an object. This class is a subclass of java.lang.Object class.

Class declaration:

public final class ClassDefinition
extends Object

Constructor:

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

Methods:

Method Description
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.

Example 1: Java program to create new ClassDefinition object

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();
        }
    }
}


Output

ClassDefinition object successfully created

Example 2: Java program to illustrate ClassDefinition class methods

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();
        }
    }
}


Output

class GFG$demoClass
[B@448139f0


Last Updated : 29 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads