Class FactoryHelper

java.lang.Object
groovy.lang.GroovyObjectSupport
com.blackbuild.klum.ast.runtime.internal.FactoryHelper
All Implemented Interfaces:
groovy.lang.GroovyObject

public class FactoryHelper extends groovy.lang.GroovyObjectSupport
Helper methods fro use in convenience factories. This will eventually take a lot of code from the AST generated methods.
  • Field Details

  • Method Details

    • withTemplateDefinition

      public static <T> T withTemplateDefinition(Supplier<T> action)
      Internal scope for Template-definition import paths that need nested Builder composition.
    • createBuilder

      public static <T> InternalKlumBuilder<T> createBuilder(Class<T> type, String key)
    • createBuilder

      public static <T> InternalKlumBuilder<T> createBuilder(Class<T> type, String key, String breadcrumbPathExtension)
    • createTemplateBuilderForImport

      public static <T> KlumBuilder<T> createTemplateBuilderForImport(Class<T> type)
      Internal adapter hook for importing a value-only Template without a Groovy configuration source.
    • createTemplateBuilderForImport

      public static <T> KlumBuilder<T> createTemplateBuilderForImport(Class<T> type, String key)
      Internal adapter hook for a nested Template value imported without a Groovy configuration source.
    • createNestedBuilder

      public static <T> InternalKlumBuilder<T> createNestedBuilder(Class<T> type, Map<String,?> values, String key)
      Creates and configures a composition child without entering another phase lifecycle. The root Builder traversal will process and materialize it together with its owner.
    • createFromClasspath

      public static <T> T createFromClasspath(Class<T> type)
      Creates an instance of the given class by reading the model script class from the classpath.

      The model script is determined by reading the properties file META-INF/klum-model/<type>.properties, which must contain the key 'model-class' with the fully qualified class name of the model script.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      Returns:
      The created instance
    • createFromClasspath

      public static <T> T createFromClasspath(Class<T> type, ClassLoader loader)
      Creates an instance of the given class by reading the model script class from the classpath.

      The model script is determined by reading the properties file META-INF/klum-model/<type>.properties, which must contain the key 'model-class' with the fully qualified class name of the model script. The properties and the script class is loaded using the given class loader.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      loader - The class loader to load the script name and class from
      Returns:
      The created instance
    • createFrom

      public static <T> T createFrom(Class<T> type, Class<? extends groovy.lang.Script> scriptType)
      Creates an instance of the given type by running the given script.

      The script can either be a regular script or a delegating script. A regular script will be instantiated and executed, the result will be returned. If the script returns the wrong type, an error will be thrown. For a delegating script, a new instance of the given type will be created and the script run as a closure on the RW instance.

      Basically, a regular script must start with Type.Create..., while a delegating script should only contain the body of the configuration closure.

      For a keyed type with a delegating script, the simple name of the script is used as the key, for a regular script, the script itself is responsible to provide the key.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      scriptType - The script to run
      Returns:
      The created instance
    • create

      public static <T> T create(Class<T> type, @Nullable @Nullable Map<String,?> values, @Nullable @Nullable String key, @Nullable @Nullable groovy.lang.Closure<?> body)
      Creates a new instance of the given type using the provided values, key and config closure.

      First the class is instantiated using the key as constructor argument if keyed. Then the values are applied by using the keys as method names of the RW instance and the values as the single argument of that method. Finally the config closure is applied to the RW instance.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      values - The value map to apply
      key - The key to use for instantiation, ignored if the type is not keyed
      body - The config closure to apply
      Returns:
      The created instance
    • createAsBuilder

      public static <T> InternalKlumBuilder<T> createAsBuilder(Class<T> type, @Nullable @Nullable Map<String,?> values, @Nullable @Nullable String key, @Nullable @Nullable groovy.lang.Closure<?> body, String operation)
    • createFrom

      public static <T> T createFrom(Class<T> type, URL src, @Nullable @Nullable Function<URL,String> keyProvider, @Nullable @Nullable ClassLoader loader)
      Creates an instance of the given type by reading the given URL, compiling it into a delegating script and applying it to a newly created instance.
      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      src - The URL to read
      Returns:
      The created instance
    • createFrom

      public static <T> T createFrom(Class<T> type, @Nullable @Nullable String name, String text, @Nullable @Nullable ClassLoader loader)
      Creates an instance of the given type by compiling the given text into a delegating script and applying it to a newly created instance.
      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      name - The name of the script, only relevant for keyed types
      Returns:
      The created instance
    • createFrom

      public static <T> T createFrom(Class<T> type, File file, @Nullable @Nullable Function<File,String> keyProvider, @Nullable @Nullable ClassLoader loader)
      Creates an instance of the given type by reading the given file, compiling it into a delegating script and applying it to a newly created instance.
      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      file - The file to read
      keyProvider - The function to provide the key from the file, usually some kind of name extraction
      loader - The classloader to use for creating the script
      Returns:
      The created instance
    • createAsTemplate

      public static <T> T createAsTemplate(Class<T> type, File scriptFile, ClassLoader loader)
      Creates a template of the given type by reading the given resource, compiling it into a delegating script and applying it to a newly created instance.

      Template instance don't run lifecycle phases/methods.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      scriptFile - The resource to read
      Returns:
      The created instance
    • createAsTemplate

      public static <T> T createAsTemplate(Class<T> type, String text, ClassLoader loader)
      Creates a template of the given type by reading the given resource, compiling it into a delegating script and applying it to a newly created instance.

      Template instance don't run lifecycle phases/methods.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      text - The script text
      Returns:
      The created instance
    • createAsTemplate

      public static <T> T createAsTemplate(Class<T> type, URL script, ClassLoader loader)
      Creates a template of the given type by reading the given resource, compiling it into a delegating script and applying it to a newly created instance.

      Template instance don't run lifecycle phases/methods.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      script - The resource to read
      Returns:
      The created instance
    • createAsTemplate

      public static <T> T createAsTemplate(Class<T> type, Map<String,?> values, @Nullable @Nullable groovy.lang.Closure<?> closure)
      Creates a new template instance of the given type using the provided values and config closure.

      The values are applied by using the keys as method names of the RW instance and the values as the single argument of that method. Finally the config closure is applied to the RW instance. Template instance don't run lifecycle phases/methods.

      Type Parameters:
      T - The type to create
      Parameters:
      type - The type to create
      values - The value map to apply
      closure - The optional config closure to apply
      Returns:
      The created instance
    • createFromMap

      public static <T> T createFromMap(Class<T> type, Map<String,Object> configMap)
    • createFromMapAsBuilder

      public static <T> InternalKlumBuilder<T> createFromMapAsBuilder(Class<T> type, Map<String,Object> configMap)
    • createFromAsBuilder

      public static <T> InternalKlumBuilder<T> createFromAsBuilder(Class<T> type, Class<? extends groovy.lang.Script> scriptType)
    • createFromSerializedState

      public static <T> T createFromSerializedState(Class<T> type, Map<String,Object> serializedState)
      Restores serialized field state into a Builder and then runs the complete lifecycle.

      This ordering is the explicitly provisional Jackson policy from issue #428: mutating callbacks run again after restoration and may therefore be non-idempotent.

    • getTypeOrDefaultType

      public static <T> Class<T> getTypeOrDefaultType(Class<T> type)