java.lang.Object
io.jstach.jstachio.spi.Templates
Locates generated templates by their model via reflection.
This utility class is useful if you plan on implementing your own JStachio
and
or other integrations.
- Author:
- agentgt
- API Note
- In order to use reflection in a modular setup one must
open
packages to theio.jstach.jstachio
module.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Strategy to load templates dynamically. -
Method Summary
Modifier and TypeMethodDescriptionstatic TemplateInfo
findTemplate
(Class<?> modelType, JStachioConfig config) Finds aTemplate
if possible otherwise falling back to aTemplateInfo
based on annotation metadata.static @Nullable TemplateInfo
findTemplateOrNull
(Class<?> modelType, JStachioConfig config) Finds aTemplate
if possible otherwise falling back to aTemplateInfo
based on annotation metadata.findTemplates
(ServiceLoader<TemplateProvider> serviceLoader, TemplateConfig templateConfig, Consumer<ServiceConfigurationError> errorHandler) Find templates by the given service loader.static String
generatedClassName
(Class<?> modelClass) Gets the canonical class name of the generated template code regardless of whether or not code has actually been generated.static TemplateInfo
getInfoByReflection
(Class<?> modelType) Finds template info by accessing JStache annotations through reflective lookup.static <T> Template<T>
getTemplate
(Class<T> clazz) Finds a template by reflection or an exception is thrown.static <T> Template<T>
getTemplate
(Class<T> modelType, Iterable<Templates.TemplateLoadStrategy> strategies, Iterable<ClassLoader> classLoaders, System.Logger logger) Finds a template by reflection or an exception is thrown.static @Nullable JStachePath
resolvePath
(Class<?> model) Resolve path lookup information reflectively from a model class by doing config resolution at runtime.static void
validateEncoding
(TemplateInfo template, Output.EncodedOutput<?> output) A utility method that will check if the templates encoding matches the outputs encoding.
-
Method Details
-
validateEncoding
A utility method that will check if the templates encoding matches the outputs encoding.- Parameters:
template
- template charset to checkoutput
- an encoded output expecting the template charset to be the same.- Throws:
UnsupportedCharsetException
- if the charsets do not match
-
findTemplate
Finds aTemplate
if possible otherwise falling back to aTemplateInfo
based on annotation metadata. A call first resolves the type that is actually annotated with JStache and then effectively callsgetTemplate(Class)
first and if that fails possibly triesgetInfoByReflection(Class)
based on config.- Parameters:
modelType
- the models class (the one annotated withJStache
and not the Templates class)config
- config used to determine whether or not to fallback- Returns:
- the template info which might be a
Template
if the generated template was found. - Throws:
Exception
- if any reflection error happes or the template is not found- API Note
- Callers can do an
instanceof Template t
to see if a generated template was returned instead of the fallback.
-
findTemplateOrNull
Finds aTemplate
if possible otherwise falling back to aTemplateInfo
based on annotation metadata. A call first resolves the type that is actually annotated with JStache and then effectively callsgetTemplate(Class)
first and if that fails possibly triesgetInfoByReflection(Class)
based on config. UnlikefindTemplate(Class, JStachioConfig)
this call will not produce any logging and will not throw an exception if it fails.- Parameters:
modelType
- the models class (the one annotated withJStache
and not the Templates class)config
- config used to determine whether or not to fallback- Returns:
- the template info which might be a
Template
if the generated template was found ornull
if not found. - API Note
- Callers can do an
instanceof Template t
to see if a generated template was returned instead of the fallback.
-
getInfoByReflection
Finds template info by accessing JStache annotations through reflective lookup.This allows you to lookup template meta data regardless of whether or not the annotation processor has generated code. This method is mainly used for fallback mechanisms and extensions.
Why might you need the reflective data instead of the static generated meta data? Well often times the annotation processor in a hot reload environment such as JRebel, JBoss modules, or Spring Reload has not generated the code from a JStache model and or it is not desired. This allows reflection based engines like JMustache to keep working even if code is not generated.
-
getTemplate
Finds a template by reflection or an exception is thrown.- Type Parameters:
T
- the model type- Parameters:
clazz
- the model type- Returns:
- the template never
null
. - Throws:
NoSuchElementException
- if the template is not foundException
- if the template is not found or any reflective access errors
-
getTemplate
public static <T> Template<T> getTemplate(Class<T> modelType, Iterable<Templates.TemplateLoadStrategy> strategies, Iterable<ClassLoader> classLoaders, System.Logger logger) throws Exception Finds a template by reflection or an exception is thrown. Because the return template is parameterized a template matching the exact type is returned and inheritance either via interfaces or super class is not checked!- Type Parameters:
T
- the model type- Parameters:
modelType
- the model typestrategies
- load strategiesclassLoaders
- class loaders to try loadinglogger
- used to log attempted strategies. If you do not want to log useJStachioConfig.noopLogger()
.- Returns:
- the template never
null
. - Throws:
NoSuchElementException
- if the template is not foundException
- if the template is not found or any reflective access errors
-
generatedClassName
Gets the canonical class name of the generated template code regardless of whether or not code has actually been generated. Because the class may not have been generated the return is a String.- Parameters:
modelClass
- the exact model class that contains theJStache
annotation.- Returns:
- the FQN class name of the would be generated template code
- Throws:
NoSuchElementException
- if the model class is not annotated withJStache
.
-
findTemplates
public static Stream<Template<?>> findTemplates(ServiceLoader<TemplateProvider> serviceLoader, TemplateConfig templateConfig, Consumer<ServiceConfigurationError> errorHandler) Find templates by the given service loader.- Parameters:
serviceLoader
- a prepared service loadertemplateConfig
- template config to use for instantiating templateserrorHandler
- handleServiceConfigurationError
errors.- Returns:
- lazy stream of templates
-
resolvePath
Resolve path lookup information reflectively from a model class by doing config resolution at runtime.- Parameters:
model
- a class annotated with JStache- Returns:
- the resolved path annotation
- API Note
- This method is an implementation detail for reflection rendering engines such as JMustache and JStachio's future reflection based engine. It is recommended you do not rely on it as it is subject to change in the future.
-