COMMUNITY

I need a work-around for build macro limitation

Here’s the situation. My goal is to create a build macro that adds a static variable to Classes that extend a given Interface. This static variable stores an array of instances of its class.

However, according to the documentation:

They [build macros] are executed per type, after the type has been set up (including its relation to other types, such as inheritance for classes) but before its fields are typed

After a few failed attempts I’ve found that its not possible for a macro to create fields/properties of the type its operating on. Which makes sense given the following diagram that describes at what point the macro processor is executed during compilation:

Is there a possible work-around that would allow me to use macros to add static properties of the same type as the Class the macro is operating on? Maybe using multiple build macros in sequence or some such?

I’m not sure I understand. Build macros generate untyped AST, so you can generate whatever you want and nothing stops you from specifying the current class in a TPath of the field’s type. And you can get the information about the the class being built with Context.getLocalClass().