Package nbbrd.service

Annotation Type ServiceId


@Documented @Target(METHOD) @Retention(CLASS) public @interface ServiceId
Specifies that a method can be used to retrieve an identifier on a service. This annotation must be used in conjunction with ServiceDefinition.

The annotated method must follow the following rules:

  1. Id method only applies to methods of a service
  2. Id method does not apply to static methods
  3. Id method must have no-args
  4. Id method must return String, a built-in representable type, or any type with a format method specified via formatMethodName
  5. Id method must be unique
  6. Id method must not throw checked exceptions
  7. Id pattern must be valid
  8. Format method (if specified) must exist on the return type, be no-arg and return String

The following JDK types are recognized as built-in representable types and do not require an explicit formatMethodName:

  • All Enum subtypes — uses name()
  • java.util.UUID — uses toString()
  • java.net.URI — uses toString()
  • java.nio.charset.Charset — uses name()
  • java.util.Locale — uses toLanguageTag()

Additionally, if the return type is annotated with @nbbrd.design.RepresentableAsString, its formatMethodName attribute is used automatically (without requiring that annotation on the processor classpath).

Resolution priority (first match wins):

  1. Explicit @ServiceId(formatMethodName=...) value
  2. @RepresentableAsString(formatMethodName=...) on the return type
  3. Built-in JDK registry listed above
  4. Direct String return type
  • Field Details

  • Element Details

    • pattern

      String pattern
      Specifies the regex pattern that the ID is expected to match.
      If specified, this pattern will be added to the loader as a static final field.
      Returns:
      a regex pattern if required, empty otherwise
      Default:
      ""
    • formatMethodName

      String formatMethodName
      Specifies the name of a no-arg method on the return type of the annotated method that converts it to a unique String representation.
      When specified, the annotated method may return any type (not just String), as long as that type exposes the named method returning String.

      This attribute is optional for types listed as built-in representable types (see class-level documentation). It is only required for types not in that list.

      Example: @ServiceId(formatMethodName = "name") on MyEnum getCategory() will use provider.getCategory().name() as the identifier. However, any Enum subtype already uses name() automatically, so the explicit formatMethodName is not needed in that case.

      Returns:
      the name of the format method, or empty to use the default for built-in types or require a direct String return type
      Default:
      ""