Class ClassGenerator

java.lang.Object
nl.pim16aap2.util.codegeneration.ClassGenerator
Direct Known Subclasses:
FakePlayerClassGenerator

public abstract class ClassGenerator extends Object
Represents an abstract super class that can generate new classes and insert them into a specific ClassLoader.
  • Field Details

    • generatedClass

      protected @Nullable Class<?> generatedClass
    • generatedConstructor

      protected @Nullable Constructor<?> generatedConstructor
  • Constructor Details

    • ClassGenerator

      protected ClassGenerator(ClassLoader classLoader)
      Parameters:
      classLoader - The ClassLoader to insert generated classes into.
  • Method Details

    • generate

      protected final void generate() throws Exception
      Generates the class.
      Throws:
      Exception - When any kind of exception occurs during the generation process.
    • generateImpl

      protected abstract void generateImpl() throws Exception
      The implementation of the class generation code. This method is called by generate() when needed.
      Throws:
      Exception - When any kind of exception occurs during the generation process.
    • createBuilder

      protected final net.bytebuddy.dynamic.DynamicType.Builder<?> createBuilder(Class<?> superClass)
      Creates a new ByteBuddy builder.

      The returned builder will be a subclass of the provided superclass, have a properly formatted name, and implement IGeneratedClass.

      Parameters:
      superClass - The Class the builder should be a subclass of. This can also be an interface.
      Returns:
      The new, properly configured, ByteBuddy builder.
    • getFormattedName

      protected String getFormattedName()
      Retrieves the name the generated class should have in the correct format.

      getBaseName() is used as the basis of the formatted name.

      Returns:
      The correctly formatted name.
    • getBaseName

      protected abstract String getBaseName()
      Retrieves the base name of the class to be generated.

      This should be the simple name describing the class that is to be generated.

      For example, the base name of the generated EntityFallingBlock class would be just that: "EntityFallingBlock". Any additional information (such as the fact that it is a generated class) will be added later on by getFormattedName().

      Returns:
      The base name of the class to be generated.
    • getConstructorArgumentTypes

      protected abstract Class<?>[] getConstructorArgumentTypes()
      Retrieves the array of parameter types as the generated constructor takes them.
      Returns:
      The array of parameter types of the generated constructor.
    • finishBuilder

      protected final void finishBuilder(net.bytebuddy.dynamic.DynamicType.Builder<?> builder)
      Finishes the builder.

      This method takes care of loading the generated class, and retrieving the class object and the constructor of the generated class.

      The generated class will not be saved to a file.

      Parameters:
      builder - The builder to finish.
    • finishBuilder

      protected final void finishBuilder(net.bytebuddy.dynamic.DynamicType.Builder<?> builder, @Nullable Path savePath)
      Finishes the builder.

      This method takes care of loading the generated class, and retrieving the class object and the constructor of the generated class.

      Parameters:
      builder - The builder to finish.
      savePath - The path to save the generated class to. If this is null, the class will not be saved.
    • getGeneratedClass

      public final Class<?> getGeneratedClass()
      Gets the class generated by this ClassGenerator.
      Returns:
      The generated class.
    • getGeneratedConstructor

      public final Constructor<?> getGeneratedConstructor()
      Gets the constructor generated by this ClassGenerator.
      Returns:
      The constructor generated by this ClassGenerator.
    • toString

      public String toString()
      Overrides:
      toString in class Object