public interface Trait extends FromSourceLocation, ToNode, ToShapeId
A trait complements a Shape
by providing additional
information to help correctly interpret any specific representation
of it or to add information about constraints on the logical structure
of the Shape
. For example, one Trait
object might
reflect details about how a Shape
is bound to JSON while
another might reflect details about how that same Shape
is
bound to Ion.
Traits are discovered through Java SPI using the TraitService
interface. All traits that are defined in a Smithy MUST provide a
TraitService in order for the concrete trait type to be created for
the trait in code. Otherwise, the trait is created as a
DynamicTrait
.
Traits may perform as much validation in their constructor; any
exception thrown while creating a trait when assembling a model will
automatically include the name of the trait in the thrown exception
message. Any validation that requires more context than is provided to
the trait constructor should be performed by implementing a
Validator
class for the trait that is automatically registered
each time the model is validated by implementing the
Validator
interface and registering the validator through SPI.
Modifier and Type | Method and Description |
---|---|
static <S extends Shape,T extends Trait> |
flatMapStream(S shape,
java.lang.Class<T> traitClass)
Deprecated.
|
static java.lang.String |
getIdiomaticTraitName(java.lang.String traitName)
Gets the idiomatic name of a prelude trait by stripping off the
smithy.api# prefix.
|
static java.lang.String |
getIdiomaticTraitName(ToShapeId id)
Gets the idiomatic name of a prelude trait by stripping off the
smithy.api# prefix.
|
default boolean |
isSynthetic()
Checks if this trait is persisted with the shape, or if it is a
synthetic, or transient trait, only meant to temporarily aid in
some kind of in-memory model transformation.
|
static java.lang.String |
makeAbsoluteName(java.lang.String traitName)
Makes the given trait name absolute if it is relative.
|
static java.lang.String |
makeAbsoluteName(java.lang.String traitName,
java.lang.String defaultNamespace)
Makes the given trait name absolute if it is relative.
|
ShapeId |
toShapeId()
Gets the shape ID of the trait.
|
getSourceLocation
ShapeId toShapeId()
default boolean isSynthetic()
Because synthetic traits are not persisted with shapes, they also do not need to be defined in Smithy's semantic model before they can be used in the model.
@Deprecated static <S extends Shape,T extends Trait> java.util.stream.Stream<Pair<S,T>> flatMapStream(S shape, java.lang.Class<T> traitClass)
Stream
with a
Pair
of Shape and Trait if the trait is present on the
given shape.
This method is deprecated because it generally results in harder
to read code using unnamed tuples. Use Shape.hasTrait(Class)
and Shape.expectTrait(Class)
instead.
S
- ShapeT
- Traitshape
- Shape to query for the trait.traitClass
- Trait to retrieve.static java.lang.String getIdiomaticTraitName(java.lang.String traitName)
traitName
- Trait name to make idiomatic.static java.lang.String getIdiomaticTraitName(ToShapeId id)
id
- Trait name to make idiomatic.static java.lang.String makeAbsoluteName(java.lang.String traitName)
The namespace used to resolve with the trait name is the prelude namespace, smithy.api.
traitName
- Trait name to make absolute.static java.lang.String makeAbsoluteName(java.lang.String traitName, java.lang.String defaultNamespace)
traitName
- Trait name to make absolute.defaultNamespace
- Namespace to use if the name is relative.