Codegen module allows to build classes and methods in runtime without the overhead of reflection.
You can add Codegen module to your project by inserting dependency in
ClassBuilder allows you to dynamically create classes in a few simple steps:
Use ClassBuilder.create(DefiningClassLoader classLoader, Class<? super T> type) to start constructing your class. You should define DefiningClassLoader (represents a loader for defining dynamically generated classes) and also the type of your class.
Use ClassBuilder.withField(String field, Class<?> fieldClass) to set fields of your class, define String field which is the name of the field and Class <?> fieldClass which is the class of the field.
ClassBuilder.withMethod(String methodName, Expression expression) allows you to setup methods of your class, you’ll need to define String methodName which is the name of the method and Expression expression which represents logic of the method. Expressions class defines list of possibilities for creating dynamic objects.
Finally, use ClassBuilder.build() which will return the new class.