Skip to content

Typed annotation member values instead of raw Java source strings in AnnotationDto #447

Done

Description

@igel-devin-ai

Problem

AnnotationDto.members is a Map<String, String>, and each value is a Java source expression. The Roaster backend writes it out as-is: setLiteralValue in RoasterCodeGenerator and RoasterMapper.mapAnnotationMember for type-use annotations. Callers therefore have to do the quoting themselves:

annotation
    .annotationType(SuppressWarnings.class)
    .members(Map.of("value", "\"unused\""));            // String member
annotation.addMember("since", "\"1\"");                   // String member
annotation.addMember("value", "Foo.class");              // Class member
annotation.addMember("mode", "Mode.STRICT");             // enum member
annotation.addMember("value", "{\"a\", \"b\"}");         // array member

The builders added in #415 / #429 make this visible in the public API, including the usage example ShowcaseGenerationTest.

Drawbacks:

Proposal

Add typed member helpers that do the quoting and keep type information, and keep the raw form for advanced expressions:

annotation
    .annotationType(SuppressWarnings.class)
    .add2StringMember("value", "unused")   // -> value = "unused" (escaped)
    .add2ClassMember("value", Foo.class)  // -> value = Foo.class (import collected)
    .add2EnumMember("mode", Mode.STRICT)  // -> mode = Mode.STRICT (import collected)
    .add2RawMember("value", "{1, 2}");    // -> as today

To decide:

  • Model: a small AnnotationMemberValue type (string / class / enum / primitive / array / raw) instead of String in the map, or only helpers on top of Map<String, String>. Only the first lets backends collect imports.
  • Compatibility: keep addMember(String, String) / setMembers(Map<String, String>) as the raw form.
  • Backends: update the Roaster backend and the conformance suite (Shared generator conformance suite for codegen backends #420).

Notes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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