micronaut-projects / micronaut-sourcegen

Compilation time source code generators
Apache License 2.0
7 stars 4 forks source link

Improving enum capabilities #170

Closed elifKurtay closed 3 weeks ago

elifKurtay commented 1 month ago

Implementation of this issue.

Generated enum class example:

package io.micronaut.sourcegen.example;

import java.lang.String;

public enum MyEnum2 {

  A(0),
  B(1),
  C(2);

  public int myValue;

  private MyEnum2(int myValue) {
    this.myValue = myValue;
  }

  public String myName() {
    return this.toString();
  }
}

From the following definition:

EnumDef enumDef = EnumDef.builder(enumClassName)
            .addModifiers(Modifier.PUBLIC)
            .addEnumConstant("A", ExpressionDef.constant(0))
            .addEnumConstant("B", ExpressionDef.constant(1))
            .addEnumConstant("C", ExpressionDef.constant(2))
            .addField(FieldDef.builder("myValue").ofType(TypeDef.Primitive.INT).addModifiers(Modifier.PUBLIC).build())
            .addMethod(MethodDef.builder("myName")
                .addModifiers(Modifier.PUBLIC)
                .returns(TypeDef.STRING)
                .build((aThis, parameters) ->
                    aThis.invoke("toString", TypeDef.STRING, List.of()).returning()))
            .addAllFieldsConstructor(Modifier.PRIVATE)
            .build();
dstepanov commented 1 month ago

That example doesn't make sense. It should be:

     .addEnumConstant(constant("active"))
dstepanov commented 1 month ago

For enums we should add the concept of fields, properties and constructors we already have (which we already have because of methods)