Class Templates

java.lang.Object
io.jstach.jstachio.spi.Templates

public final class Templates extends Object
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 the io.jstach.jstachio module.
  • Method Details

    • findTemplate

      public static TemplateInfo findTemplate(Class<?> modelType, JStachioConfig config) throws Exception
      Finds a Template if possible otherwise falling back to a TemplateInfo based on annotation metadata. This method is effectively calls getTemplate(Class) first and if that fails possibly tries getInfoByReflection(Class) based on config.
      Parameters:
      modelType - the models class (the one annotated with JStache 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.
    • getInfoByReflection

      public static TemplateInfo getInfoByReflection(Class<?> modelType) throws Exception
      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.

      Parameters:
      modelType - the class that is annotated with JStache
      Returns:
      template info meta data
      Throws:
      Exception - if any reflection error happes or the template is not found
    • getTemplate

      public static <T> Template<T> getTemplate(Class<T> clazz) throws Exception
      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:
      ClassNotFoundException - if the template is not found
      Exception - if the template is not found or any reflective access errors