Skip to content

Generic Definitions on Static Methods #17

Description

@MorningSage

Thank you for creating this library. I have found it immensely helpful!

One thing I haven't wrapped my head around is how to declare a generic type in a static method. For instance, I would like to generate this code:

// Goal is to define <X> as follows
public static <X> Builder<X> builder() {
    return new Builder<>(); 
} 

I can successfully create the method, but <X> is undefined because this is a static function.

// Actual code generated where <X> is not defined
public static Builder<X> builder() {
    return new Builder<>(); 
} 

Is there a way to define <X> as is the first example? Can I pass a GenericSourceGenerator somewhere or set a flag I'm missing? The code I'm using is fairly dynamic, but a simplified version is:

FunctionSourceGenerator.create("builder")
    .addModifier(Modifier.PUBLIC | Modifier.STATIC)
    .setReturnType(
        TypeDeclarationSourceGenerator.create("Builder").addGeneric(GenericSourceGenerator.create("X"))
    )
    .addBodyCodeLine("return new Builder<>();")

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions