Enum Class ClassFormat
- All Implemented Interfaces:
Serializable,Comparable<ClassFormat>,Constable
Class values.
Used by MarshallingContext.Builder.classFormat(ClassFormat),
Marshalled.classFormat(),
MarshalledProp.classFormat(), and
MarshalledConfig.classFormat() to control how Class values
are written to text-based wire formats.
The default is FQCN which mirrors the historical ClassSwap behavior (canonical dotted
name with sensible fallback to the JVM binary name).
FQCN vs BINARY_NAME for nested classes
For nested-type encoding the two forms diverge:
FQCNusesClass.getCanonicalName()when available —"java.util.Map.Entry"— and falls back toClass.getName()for local / anonymous classes (wheregetCanonicalName()returnsnull).BINARY_NAMEalways usesClass.getName()—"java.util.Map$Entry".
For arrays the difference is more dramatic:
FQCNproduces"int[]"/"java.lang.String[]".BINARY_NAMEproduces"[I"/"[Ljava.lang.String;"(JVM descriptor form).
SIMPLE_NAME is serialize-only
SIMPLE_NAME discards package and outer-class context and is not round-trip safe. The
parser path for this constant throws UnsupportedOperationException; use FQCN or
BINARY_NAME for round-trippable wires.
Precedence (highest to lowest)
@MarshalledProp(classFormat=…)on the bean property.@Marshalled(classFormat=…)on the bean class.@MarshalledConfig(classFormat=…)on-annotated classes / methods.@Rest - Programmatic
MarshallingContext.Builder.classFormat(ClassFormat). - Environment variable
MarshallingContext.classFormat . - The default constant (
FQCN).
Parser leniency
Parsers SHALL try Class.forName(String, boolean, ClassLoader) first regardless of the
parser-side ClassFormat setting — this handles BINARY_NAME and most FQCN
inputs unambiguously. For nested-type FQCN input ("java.util.Map.Entry"), the parser
walks back through dotted segments from right to left, replacing each uppercase-prefixed dot with
$ and retrying — this resolves multi-level nesting like
"com.example.Outer.Inner.Deepest" to com.example.Outer$Inner$Deepest. For
FQCN array input the parser strips trailing [] pairs, resolves the leaf component
type (including primitives like "int"), and rebuilds the array via Class.arrayType()
— so "int[]" resolves to int[].class and "java.lang.String[][]" resolves to
String[][].class.
Binary serializers
Binary serializers (BSON / CBOR / MsgPack / Prototext / Parquet) emit a UTF-8 string regardless of this
setting — there is no native Class wire type in any supported binary format.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionJVM binary name perClass.getName().Canonical fully-qualified name with dotted nested-type separators (the default).Sentinel meaning "no value configured" — falls through to the next-higher precedence level.Unqualified simple name perClass.getSimpleName(). -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringformat(Class<?> value, ClassFormat format) Formats the specifiedClassusing this format.booleanReturnstrue if this format emits a numeric wire value.static Class<?>parse(String value, ClassFormat format, ClassLoader classLoader) Parses the specified wire value into aClass.static voidsetClassNameGuard(Predicate<String> value) Installs a global guard that vets class names before the parser path resolves them to aClass.static ClassFormatReturns the enum constant of this class with the specified name.static ClassFormat[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
NOT_SET
Sentinel meaning "no value configured" — falls through to the next-higher precedence level. -
FQCN
Canonical fully-qualified name with dotted nested-type separators (the default).Uses
Class.getCanonicalName()when available; falls back toClass.getName()for local / anonymous classes wheregetCanonicalName()returnsnull.Example:
java.lang.String java.util.Map.Entry int[]
-
BINARY_NAME
JVM binary name perClass.getName().Example:
java.lang.String java.util.Map$Entry [Ljava.lang.String; [I
Identical to
FQCNfor top-level non-array reference types; diverges for nested types ($vs.) and arrays (JVM descriptor vsType[]form). -
SIMPLE_NAME
Unqualified simple name perClass.getSimpleName().Example:
java.lang.String Entry int
Serialize-only. The parser path for this constant throws
UnsupportedOperationException— there is no defensible way to resolve"Map"back to a uniqueClasswithout a registry hint, and adding such a hint is out of scope.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum class has no constant with the specified nameNullPointerException- if the argument is null
-
setClassNameGuard
Installs a global guard that vets class names before the parser path resolves them to aClass.The guard is consulted for every candidate name (including the nested-type
$ -rewritten forms) before it is passed toClass.forName(String, boolean, ClassLoader). Names the guard rejects are treated as unresolvable. This lets a deployment restrict which types may be materialized from parsed input to an explicit allow-list.- Parameters:
value- The guard predicate returningtrue for permitted class names, ornull to allow all names (the default).
-
format
- Parameters:
value- The value to format. Can benull (returnsnull ).format- The configured format. Can benull (treated asFQCN).- Returns:
- The formatted wire representation, or
null ifvalueisnull .
-
parse
Parses the specified wire value into aClass.Lenient parsing — handles every shape produced by
format(Class, ClassFormat)for the round-trippable formats (FQCNandBINARY_NAME), and accepts cross-form input as well (anFQCNparser still resolvesBINARY_NAMEinput and vice versa).Resolution steps:
- Strip trailing
[]pairs (FQCNarray form), tracking dimension count. Empty brackets at the leading edge (JVM descriptor form like"[I"/"[Ljava.lang.String;") are left toClass.forName(String, boolean, ClassLoader)because that method already handles them. - Resolve the leaf component:
- If the leaf names a primitive type (
"int","boolean", etc. — emitted byFQCNfor primitive arrays), return the correspondingTYPEclass. - Otherwise try
Class.forName(String, boolean, ClassLoader)directly. - On failure, walk dots from right to left, replacing each uppercase-prefixed dot with
$(cumulatively — already-replaced dots stay replaced) and retrying. This resolves multi-level nesting like"com.example.Outer.Inner.Deepest"tocom.example.Outer$Inner$Deepest. Dots whose trailing segment starts with a lowercase letter are treated as package boundaries and skipped.
- If the leaf names a primitive type (
- Apply
Class.arrayType()once per stripped dimension to rebuild the array class.
SIMPLE_NAMEis not supported on the parser side — throwsUnsupportedOperationException.- Parameters:
value- The wire value. Can benull or blank (returnsnull ).format- The configured format hint. Can benull (treated asFQCN).classLoader- The class loader. Can benull (usescontext class loader).- Returns:
- The parsed
Class, ornull ifvalueisnull or blank. - Throws:
IllegalArgumentException- If the value cannot be resolved.UnsupportedOperationException- IfformatisSIMPLE_NAME.
- Strip trailing
-
isNumeric
Returnstrue if this format emits a numeric wire value.Always
false — every constant emits a textual representation.- Returns:
false .
-