Class SexprToModel

java.lang.Object
org.jfuncmachine.sexprlang.translate.SexprToModel

public class SexprToModel extends Object
A utility to map S-expressions to an object model using the SexprMapper interface
  • Method Details

    • sexprsToModel

      public static Object[] sexprsToModel(SexprList sexprs, SexprMapper mapper, Class targetClass) throws MappingException
      Map a list of S-expressions to model objects
      Parameters:
      sexprs - The list of S-expressions to map
      mapper - The mapper to use
      targetClass - The (super-)class that each expression is supposed to map to, or null if there is no expectation
      Returns:
      An array of the mapped objects
      Throws:
      MappingException - If there is an error performing the mapping
    • sexprToModel

      public static Object sexprToModel(SexprItem item, SexprMapper mapper, Class targetClass) throws MappingException
      Map an S-expression item to a model object
      Parameters:
      item - The S-expression item to map
      mapper - The mapper to use
      targetClass - The (super-)class that the object is supposed to map to, or null if there is no expectation
      Returns:
      The mapped object
      Throws:
      MappingException - If there is an error performing the mapping
    • sexprListToItemList

      public static Object sexprListToItemList(SexprList list, SexprMapper mapper) throws MappingException
      Map an S-expression list to a model item using a mapper
      Parameters:
      list - The S-expression to map
      mapper - The mapper to use
      Returns:
      The object the list maps to
      Throws:
      MappingException - If there is an error performing the mapping
    • sexprListToModel

      public static Object sexprListToModel(Class clazz, SexprList list, SexprMapper mapper) throws MappingException
      Map an S-expression list to an instance of a specific class
      Parameters:
      clazz - The class the list should map to
      list - The list to map
      mapper - The mapper to use
      Returns:
      An instance of the class that the list has been mapped to
      Throws:
      MappingException - If there is an error performing the mapping
    • convertArrayTypes

      public static void convertArrayTypes(Class[] paramTypes, Object[] params)
      Convert any Object arrays in a parameter list to objects of a specific component type. This is used when invoking constructors, which may take a specific type of object, but the parameters have all been generated as arrays of type Object (Object[]).
      Parameters:
      paramTypes - The types of each parameter
      params - The parameters to convert
    • matches

      public static boolean matches(Constructor cons, Object[] params, List<SexprItem> items, SexprMapper mapper) throws MappingException
      Test whether a list of S-expression items can be mapped to the arguments of a constructor, and place the mapped arguments into the params array
      Parameters:
      cons - The constructor to match against
      params - An array of constructor parameters to be populated during the matching process
      items - The list of S-expression items to match to the constructor
      mapper - The mapper to use
      Returns:
      True if the items can be mapped to the constructor params
      Throws:
      MappingException - If there is an error performing the mapping
    • matchesWithLocation

      public static boolean matchesWithLocation(Constructor cons, Object[] params, List<SexprItem> items, SexprMapper mapper) throws MappingException
      Test whether a list of S-expression items can be mapped to the arguments of a constructor, where the constructor's last two parameters are a String and an int, which are expected to be a filename and line number, and place the mapped arguments into the params array
      Parameters:
      cons - The constructor to match against
      params - An array of constructor parameters to be populated during the matching process
      items - The list of S-expression items to match to the constructor
      mapper - The mapper to use
      Returns:
      True if the items can be mapped to the constructor params
      Throws:
      MappingException - If there is an error performing the mapping
    • matches

      public static boolean matches(Constructor cons, Object[] params)
      Tests whether an array of parameters matches the parameters for a constructor
      Parameters:
      cons - The constructor to test
      params - The parameters to test against the constructor parameters
      Returns:
      True if the params match the constructor parameters
    • matchesWithLocation

      public static boolean matchesWithLocation(Constructor cons, Object[] params)
      Tests whether an array of parameters matches the parameters for a constructor, where the last two parameters of the constructor are a string and an int, which are expected to be a filename and line number
      Parameters:
      cons - The constructor to test
      params - The parameters to test against the constructor parameters
      Returns:
      True if the params match the constructor parameters
    • trySimpleConversion

      public static Object trySimpleConversion(Class paramType, SexprItem item)