Interface JStachioTemplateFinder

All Superinterfaces:
JStachioExtension
All Known Subinterfaces:
JStachioTemplateFinder.SimpleTemplateFinder, TemplateProvider.GeneratedTemplateProvider
All Known Implementing Classes:
TemplateCatalog

public non-sealed interface JStachioTemplateFinder extends JStachioExtension
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
  • Method Details

    • findTemplate

      TemplateInfo findTemplate(Class<?> modelType) throws Exception
      Finds a Template if possible otherwise possibly falling back to a TemplateInfo based on annotation metadata or some other mechanism.
      Parameters:
      modelType - the models class (the one annotated with JStache 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 happens
      NoSuchElementException - if the template is not found and there were no other errors
      NullPointerException - 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 fallback TemplateInfo metadata.
    • findOrNull

      default @Nullable TemplateInfo findOrNull(Class<?> modelType)
      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 with JStache and not the Templates class)
      Returns:
      null if the template is was not found or the template info which might be a Template if the generated template was found.
      Throws:
      NullPointerException - if the modelType is null
      See Also:
    • supportsType

      default boolean supportsType(Class<?> modelType)
      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 with JStache and not the Templates class)
      Returns:
      true if this finder has template for modelType
      Throws:
      NullPointerException - if the modelType is null
    • order

      default int order()
      Hint on order of template finders. The found JStachioTemplateFinders are sorted naturally (lower number comes first) based on the returned number. Thus a template finder with a lower order number that supportsType(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 using ClassValue with the modelType as the key.

      The returned finder will only call findTemplate(Class) on the passed in delegate finder to resolve supportsType(Class) and findOrNull(Class)!

      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

      static JStachioTemplateFinder of(Iterable<? extends TemplateInfo> templates, int order)
      Creates a template finder from an iterable of templates. The returned finder will just loop through the templates and call TemplateInfo.supportsType(Class). To avoid the looping cost wrap the return with cachedTemplateFinder(JStachioTemplateFinder).
      Parameters:
      templates - templates to be searched in order of the iterable
      order - order hint see order().
      Returns:
      adapted template finder
    • of

      static JStachioTemplateFinder of(List<? extends JStachioTemplateFinder> templateFinders)
      Creates a composite template finder from a list. If the list only has a single element then it is returned without being wrapped.
      Parameters:
      templateFinders - list of template finders.
      Returns:
      templateFinder searching in order of order() then the list order