Interface ConstantBackedEnum
- All Known Subinterfaces:
EnumDefinition
- All Known Implementing Classes:
ReleasePolicy, TypedMarker
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 TypeMethodDescriptionstatic StringField-name prefix for the mirror constants.static <E extends Enum<E> & ConstantBackedEnum>
Map<String, E> Build an immutableliteralName -> constantlookup index, rejecting duplicate literals.The String literal carried by this constant.static <E extends Enum<E> & ConstantBackedEnum>
voidVerify mirror correspondence with thedefaultPrefix()prefix and no value-shape check.static <E extends Enum<E> & ConstantBackedEnum>
voidverify(Class<E> enumClass, String prefix, BiPredicate<E, String> valueShape) Verify that every constant ofenumClasshas a matchingpublic static final Stringmirror field namedprefix + constantNamewhose value equals the constant'sliteralName(), that no mirror field is orphaned, and — whenvalueShapeis 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
Field-name prefix for the mirror constants.- Returns:
- the default prefix,
"NAME_"
-
verify
Verify mirror correspondence with thedefaultPrefix()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 ofenumClasshas a matchingpublic static final Stringmirror field namedprefix + constantNamewhose value equals the constant'sliteralName(), that no mirror field is orphaned, and — whenvalueShapeis supplied — that each value satisfies the shape predicate.- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum class to verifyprefix- the mirror-field name prefixvalueShape- optional per-constant value-shape check, ornullto skip it- Throws:
AssertionError- on any mismatch — intentionally, so the violation surfaces asExceptionInInitializerErrorand renders the class unusable
-
index
Build an immutableliteralName -> constantlookup 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
-