- All Superinterfaces:
JStachioExtension
- All Known Subinterfaces:
JStachioTemplateFinder.SimpleTemplateFinder
,TemplateProvider.GeneratedTemplateProvider
Finds templates based on the model type (class).
The default JStachio
uses a combination of relection and the ServiceLoader to
find templates.
Other implementations may want to use their DI framework like Spring or CDI to find templates.
- Author:
- agentgt
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
An easier to implement template finder based on a sequence of templates. -
Method Summary
Modifier and TypeMethodDescriptionstatic JStachioTemplateFinder
Decorates a template finder with a cache usingClassValue
with the modelType as the key.static JStachioTemplateFinder
defaultTemplateFinder
(JStachioConfig config) The default template finder that uses reflection and or the ServiceLoader.default @Nullable TemplateInfo
findOrNull
(Class<?> modelType) Finds a template or null if no template is found.findTemplate
(Class<?> modelType) Finds aTemplate
if possible otherwise possibly falling back to aTemplateInfo
based on annotation metadata or some other mechanism.static JStachioTemplateFinder
of
(Iterable<? extends TemplateInfo> templates, int order) Creates a template finder from an iterable of templates.default int
order()
Hint on order of template finders.default boolean
supportsType
(Class<?> modelType) Determines if this template finder has a template for the model type (the class annotated by JStache).
-
Method Details
-
findTemplate
Finds aTemplate
if possible otherwise possibly falling back to aTemplateInfo
based on annotation metadata or some other mechanism.- Parameters:
modelType
- the models class (the one annotated withJStache
and not the Templates class)- Returns:
- the template info which might be a
Template
if the generated template was found. - Throws:
Exception
- if any reflection or runtime error happensNoSuchElementException
- if the template is not found and there were no other errorsNullPointerException
- if the modelType is null- API Note
- Callers can do an
instanceof Template t
to see if a generated template was returned instead of the fallbackTemplateInfo
metadata.
-
findOrNull
Finds a template or null if no template is found. Should not throw an exception if a template is not found.- Parameters:
modelType
- the models class (the one annotated withJStache
and not the Templates class)- Returns:
null
if the template is was not found or the template info which might be aTemplate
if the generated template was found.- Throws:
NullPointerException
- if the modelType is null- See Also:
-
supportsType
Determines if this template finder has a template for the model type (the class annotated by JStache).- Parameters:
modelType
- the models class (the one annotated withJStache
and not the Templates class)- Returns:
- true if this finder has template for modelType
- Throws:
NullPointerException
- if the modelType is null
-
order
Hint on order of template finders. The foundJStachioTemplateFinder
s are sorted naturally (lower number comes first) based on the returned number. Thus a template finder with a lower order number thatsupportsType(Class)
the model class will be used.- Returns:
- default returns zero
-
defaultTemplateFinder
The default template finder that uses reflection and or the ServiceLoader.This implementation performs no caching. If you would like caching call
cachedTemplateFinder(JStachioTemplateFinder)
on the returned finder.- Parameters:
config
- used to help find templates as well as logging.- Returns:
- default template finder.
-
cachedTemplateFinder
Decorates a template finder with a cache usingClassValue
with the modelType as the key.While the finder does not provide any eviction the cache will not prevent garbage collection of the model classes.
- Parameters:
finder
- to be decorated unless the finder is already decorated thus it is a noop to repeateadly call this method on already cached template finder.- Returns:
- caching template finder
-
of
Creates a template finder from an iterable of templates. The returned finder will just loop through the templates and callTemplateInfo.supportsType(Class)
. To avoid the looping cost wrap the return withcachedTemplateFinder(JStachioTemplateFinder)
.- Parameters:
templates
- templates to be searched in order of the iterableorder
- order hint seeorder()
.- Returns:
- adapted template finder
-