Class BytecodeClass

  • All Implemented Interfaces:
    Testable

    public final class BytecodeClass
    extends Object
    implements Testable
    Class useful for generating bytecode for testing purposes.
    Since:
    0.1.0
    • Constructor Detail

      • BytecodeClass

        public BytecodeClass()
        Constructor.
      • BytecodeClass

        public BytecodeClass​(String name)
        Constructor.
        Parameters:
        name - Class name.
      • BytecodeClass

        public BytecodeClass​(String name,
                             int access)
        Constructor.
        Parameters:
        name - Class name.
        access - Access modifiers.
      • BytecodeClass

        public BytecodeClass​(String name,
                             BytecodeClassProperties properties)
        Constructor.
        Parameters:
        name - Class name.
        properties - Class properties.
      • BytecodeClass

        public BytecodeClass​(String name,
                             BytecodeClassProperties properties,
                             boolean verify)
        Constructor.
        Parameters:
        name - Class name.
        properties - Class properties.
        verify - Verify bytecode.
      • BytecodeClass

        public BytecodeClass​(String name,
                             int access,
                             CustomClassWriter writer)
        Constructor.
        Parameters:
        name - Class name.
        access - Access modifiers.
        writer - ASM class writer.
    • Method Detail

      • xml

        public com.jcabi.xml.XML xml()
        Converts bytecode into XML.
        Returns:
        XML representation of bytecode.
      • bytecode

        public Bytecode bytecode()
        Generate bytecode.

        In this method we intentionally use the Thread.currentThread().getContextClassLoader() for the class loader of the CheckClassAdapter.verify(ClassReader, ClassLoader, boolean, PrintWriter) instead of default implementation. This is because the default class loader doesn't know about classes compiled on the previous maven step. You can read more about the problem here: PluginStartup.init() ()}

        Returns:
        Bytecode.
      • withConstructor

        public BytecodeMethod withConstructor​(int... modifiers)
        Add constructor.
        Parameters:
        modifiers - Constructor modifiers.
        Returns:
        This object.
      • withConstructor

        public BytecodeMethod withConstructor​(String descriptor,
                                              int... modifiers)
        Add constructor.
        Parameters:
        descriptor - Constructor descriptor.
        modifiers - Constructor modifiers.
        Returns:
        This object.
      • withMethod

        public BytecodeMethod withMethod​(String mname,
                                         String descriptor,
                                         int... modifiers)
        Add method.
        Parameters:
        mname - Method name.
        descriptor - Method descriptor.
        modifiers - Access modifiers.
        Returns:
        This object.
      • withField

        public BytecodeClass withField​(String fname)
        Add field.
        Parameters:
        fname - Field name.
        Returns:
        This object.
      • withField

        public BytecodeField withField​(String fname,
                                       String descriptor,
                                       String signature,
                                       Object value,
                                       int... modifiers)
        Add field.
        Parameters:
        fname - Field name.
        descriptor - Field descriptor.
        signature - Field signature.
        value - Field value.
        modifiers - Access modifiers.
        Returns:
        This object.
      • withAnnotations

        public BytecodeClass withAnnotations​(XmlAnnotations all)
        Add anns.
        Parameters:
        all - Annotations.
        Returns:
        This object.
      • testCode

        public String testCode()
        Description copied from interface: Testable
        Generate test Java code.
        Specified by:
        testCode in interface Testable
        Returns:
        Java code.
      • helloWorldMethod

        public BytecodeClass helloWorldMethod()
        Hello world bytecode.
        Returns:
        The same class with the hello world method.