Thursday, July 4, 2024
HomeLanguagesJavajava.lang.instrument.ClassDefinition Class in Java

java.lang.instrument.ClassDefinition Class in Java

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments