A registry of accessors. Accessors are used by the
insert()
and
populate()
methods of the
RenderSession
class to extract values from data provided by the data access
layer. This is how an AccessorRegistry
decides which accessor to use for a
particular type of object:
- If you have registered your
own
Accessor
for that particular type of object, then that is theAccessor
that is going to be used. - Otherwise an internally defined, non-exposed
Accessor
implementation will be used. ThisAccessor
implementation is very versatile and can read almost any type of object. It is internally backed by aPathWalker
.
Note that the internally defined Accessor
mentioned above does not use
reflection to read bean properties, but it does use reflection to figure
out what those properties are in the first place. Thus, if the JavaBeans are
inside a Java 9+ module, you must open
the module to Klojang
Templates. Irrespective of whether the JavaBeans are inside a Java 9+
module, both the bean class and the bean properties must be public
.
Alternatively, you could write your own Accessor
:
Accessor<Person> personAccessor = (person, property) -> switch(property) { case "id" : return person.getId(); case "firstName" : return person.getFirstName(); case "lastName" : return person.getLastName(); case "birthDate" : return person.getBirthDate(); default : return Accessor.UNDEFINED; }; AccessorRegistry accessors = AccessorRegistry .configure() .register(Person.class, new PersonAccessor()) .freeze(); RenderSession session = template.newRenderSession(accessors);
A slightly less verbose, but still fully reflection-free alternative is to use
a BeanReaderBuilder
:
// forClass returns a BeanReaderBuilder BeanReader beanReader = BeanReader.forClass(Person.class) .withInt("id") .withString("firstName", "lastName") .with(LocalDate.class, "birthDate")) .build(); AccessorRegistry accessors = AccessorRegistry .configure() .register(beanReader) .freeze(); RenderSession session = template.newRenderSession(accessors);
In practice, you would likely create just a single AccessorRegistry
instance for your entire application, when it starts up, and pass that instance
to all calls to
Template.newRenderSession()
.
- Author:
- Ayco Holleman
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A builder class forAccessorRegistry
instances. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic AccessorRegistry.Builder
Returns aBuilder
object that lets you configure anAccessorRegistry
.static AccessorRegistry
standard
(boolean nullEqualsUndefined) Returns anAccessorRegistry
that should be sufficient for most use cases.static AccessorRegistry
standard
(NameMapper nameMapper) Returns anAccessorRegistry
that should be sufficient for most use cases.static AccessorRegistry
standard
(NameMapper nameMapper, boolean nullEqualsUndefined) Returns anAccessorRegistry
that should be sufficient for most use cases.
-
Field Details
-
STANDARD_ACCESSORS
The defaultAccessorRegistry
. It assumes that template variables map as-is to names used in source data objects.
-
-
Method Details
-
standard
Returns anAccessorRegistry
that should be sufficient for most use cases. It allows you to specify one globalNameMapper
for mapping the template variables to the names used in source data objects.- Parameters:
nameMapper
- theNameMapper
to be used to map template variables to bean properties and/or map keys.- Returns:
- an
AccessorRegistry
the should sufficient for most use cases
-
standard
Returns anAccessorRegistry
that should be sufficient for most use cases.- Parameters:
nullEqualsUndefined
- whethernull
values should be treated the same way asAccessor.UNDEFINED
- Returns:
- an
AccessorRegistry
the should sufficient for most use cases
-
standard
Returns anAccessorRegistry
that should be sufficient for most use cases. It allows you to specify one globalNameMapper
for mapping the template variables to the names used in source data objects.- Parameters:
nameMapper
- theNameMapper
to be used to map template variables to bean properties and/or map keys.nullEqualsUndefined
- whethernull
values should be treated the same way asAccessor.UNDEFINED
- Returns:
- an
AccessorRegistry
the should sufficient for most use cases
-
configure
Returns aBuilder
object that lets you configure anAccessorRegistry
.- Returns:
- a
Builder
object that lets you configure anAccessorRegistry
-