Interface ConstantBackedEnum

All Known Subinterfaces:
EnumDefinition
All Known Implementing Classes:
ReleasePolicy, TypedMarker

public interface ConstantBackedEnum
Pairs each constant of an enum with a matched public static final String mirror field and verifies the one-to-one correspondence at class-load time.

Annotation element values in Java must be constant expressions (JLS §15.28). MyEnum.FOO.toString() is not — method calls never are; MyEnum.FOO itself is not, even when the annotation element is String-typed. The only constant handle on an enum-shaped name is a sibling public static final String NAME_FOO = "foo"; on the enum class. This interface formalises that pattern: each constant passes its mirror to the constructor by qualified name (Goal.NAME_FOO) — a simple-name forward reference to a field declared later is illegal under JLS §8.3.3, but a qualified reference is exempt from that restriction. The interface contributes the literalName() contract, and verify(Class) guarantees the constants and their NAME_* mirrors stay in lockstep.

Usage — the consuming enum runs verify(Class) from its static initializer, so a structural mismatch makes the class permanently unloadable rather than silently miscompiling whatever the mirror feeds (e.g. a plugin.xml goal name):

public enum Goal implements ConstantBackedEnum {
    CREATE(Goal.NAME_CREATE),
    ALIGN (Goal.NAME_ALIGN);

    public static final String NAME_CREATE = "create";
    public static final String NAME_ALIGN  = "align";

    public final String mojoName;
    Goal(String n) { this.mojoName = n; }

    @Override public String literalName() { return mojoName; }

    static { ConstantBackedEnum.verify(Goal.class); }
}
Annotation site: @Mojo(name = Goal.NAME_CREATE).
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Field-name prefix for the mirror constants.
    static <E extends Enum<E> & ConstantBackedEnum>
    Map<String,E>
    index(Class<E> enumClass)
    Build an immutable literalName -> constant lookup index, rejecting duplicate literals.
    The String literal carried by this constant.
    static <E extends Enum<E> & ConstantBackedEnum>
    void
    verify(Class<E> enumClass)
    Verify mirror correspondence with the defaultPrefix() prefix and no value-shape check.
    static <E extends Enum<E> & ConstantBackedEnum>
    void
    verify(Class<E> enumClass, String prefix, BiPredicate<E,String> valueShape)
    Verify that every constant of enumClass has a matching public static final String mirror field named prefix + constantName whose value equals the constant's literalName(), that no mirror field is orphaned, and — when valueShape is supplied — that each value satisfies the shape predicate.
  • Method Details

    • literalName

      String literalName()
      The String literal carried by this constant.
      Returns:
      the literal, equal to the corresponding NAME_* mirror constant on the implementing enum
    • defaultPrefix

      static String defaultPrefix()
      Field-name prefix for the mirror constants.
      Returns:
      the default prefix, "NAME_"
    • verify

      static <E extends Enum<E> & ConstantBackedEnum> void verify(Class<E> enumClass)
      Verify mirror correspondence with the defaultPrefix() prefix and no value-shape check.
      Type Parameters:
      E - the enum type
      Parameters:
      enumClass - the enum class to verify
    • verify

      static <E extends Enum<E> & ConstantBackedEnum> void verify(Class<E> enumClass, String prefix, BiPredicate<E,String> valueShape)
      Verify that every constant of enumClass has a matching public static final String mirror field named prefix + constantName whose value equals the constant's literalName(), that no mirror field is orphaned, and — when valueShape is supplied — that each value satisfies the shape predicate.
      Type Parameters:
      E - the enum type
      Parameters:
      enumClass - the enum class to verify
      prefix - the mirror-field name prefix
      valueShape - optional per-constant value-shape check, or null to skip it
      Throws:
      AssertionError - on any mismatch — intentionally, so the violation surfaces as ExceptionInInitializerError and renders the class unusable
    • index

      static <E extends Enum<E> & ConstantBackedEnum> Map<String,E> index(Class<E> enumClass)
      Build an immutable literalName -> constant lookup index, rejecting duplicate literals.
      Type Parameters:
      E - the enum type
      Parameters:
      enumClass - the enum class to index
      Returns:
      an immutable map keyed by literalName()
      Throws:
      AssertionError - if two constants share a literal name