Class AttributeSourceGenerator


  • public class AttributeSourceGenerator
    extends Object
    Automatically generates source code defining CQEngine attributes for accessing the fields and methods (aka members) of a given target class.

    Generates CQEngine SimpleAttributes or SimpleNullableAttributes for members which return singular values, and generates CQEngine MultiValueNullableAttributes for members which return Iterables or arrays.

    Note that by default this code generator is cautious and generates "Nullable" attributes by default, for all members except those which return primitive types. Nullable attributes check for and handle nulls automatically, however checking for nulls incurs a performance penalty at runtime. So for non-primitive fields and accessor methods which will not actually contain or return null, it is recommended to replace those attributes with non-nullable variants as discussed in the comments in the generated source code. This is optional but can remove the overhead of unnecessary null checks at runtime.

    Methods are provided both to generate source code for attributes which can be copy-pasted into the target class, and to generate entirely separate attributes classes. In the latter case, for example if class "Car" is given as the target class, source code for a companion class "CQCar" will be generated containing attributes for accessing the fields and accessor methods in class "Car".

    Author:
    Niall Gallagher
    • Method Detail

      • generateAttributesForPastingIntoTargetClass

        public static String generateAttributesForPastingIntoTargetClass​(Class<?> targetClass)
        Generates source code which defines attributes for accessing all fields in the given target class, for the purpose of copy-pasting directly into the target class.
        Parameters:
        targetClass - The POJO class containing fields for which attributes are to be generated
        Returns:
        Source code defining attributes for accessing each of the fields in the target class
      • generateAttributesForPastingIntoTargetClass

        public static String generateAttributesForPastingIntoTargetClass​(Class<?> targetClass,
                                                                         MemberFilter memberFilter)
        Generates source code which defines attributes for accessing all members (fields and methods) in the given target class which are acceptable to the given filter, for the purpose of copy-pasting directly into the target class.
        Parameters:
        targetClass - The POJO class containing members for which attributes are to be generated
        memberFilter - A filter which determines the subset of the members of a class (fields and accessor methods) for which attributes should be generated
        Returns:
        Source code defining attributes for accessing each of the members in the target class
      • generateSeparateAttributesClass

        public static String generateSeparateAttributesClass​(Class<?> targetClass,
                                                             String packageOfAttributesClass)
        Generates source code of a complete separate class, containing attributes for accessing all members (fields and methods) in the given target class.
        Parameters:
        targetClass - The POJO class containing members for which attributes are to be generated
        packageOfAttributesClass - The desired package name of the attributes class. Note this will be used to determine which members will be visible to the generated class, i.e. attributes for package-private members will only be generated if the generated class will be in the same package as the target class
        Returns:
        Source code of a complete separate class, containing attributes for accessing members in the given target class
      • generateSeparateAttributesClass

        public static String generateSeparateAttributesClass​(Class<?> targetClass,
                                                             String packageOfAttributesClass,
                                                             MemberFilter memberFilter)
        Generates source code of a complete separate class, containing attributes for accessing all members (fields and methods) in the given target class which are acceptable to the given filter.
        Parameters:
        targetClass - The POJO class containing members for which attributes are to be generated
        packageOfAttributesClass - The desired package name of the attributes class. Note this will be used to determine which members will be visible to the generated class, i.e. attributes for package-private members will only be generated if the generated class will be in the same package as the target class
        memberFilter - A filter which determines the subset of the members of a class (fields and accessor methods) for which attributes should be generated
        Returns:
        Source code of a complete separate class, containing attributes for accessing members in the given target class
      • generateSeparateAttributesClass

        public static String generateSeparateAttributesClass​(Class<?> targetClass,
                                                             Package packageOfAttributesClass)
        Generates source code of a complete separate class, containing attributes for accessing all members (fields and methods) in the given target class.
        Parameters:
        targetClass - The POJO class containing members for which attributes are to be generated
        packageOfAttributesClass - The desired package name of the attributes class. Note this will be used to determine which members will be visible to the generated class, i.e. attributes for package-private members will only be generated if the generated class will be in the same package as the target class
        Returns:
        Source code of a complete separate class, containing attributes for accessing members in the given target class
      • generateSeparateAttributesClass

        public static String generateSeparateAttributesClass​(Class<?> targetClass,
                                                             Package packageOfAttributesClass,
                                                             MemberFilter memberFilter)
        Generates source code of a complete separate class, containing attributes for accessing all members (fields and methods) in the given target class which are acceptable to the given filter.
        Parameters:
        targetClass - The POJO class containing members for which attributes are to be generated
        packageOfAttributesClass - The desired package name of the attributes class. Note this will be used to determine which members will be visible to the generated class, i.e. attributes for package-private members will only be generated if the generated class will be in the same package as the target class
        memberFilter - A filter which determines the subset of the members of a class (fields and accessor methods) for which attributes should be generated
        Returns:
        Source code of a complete separate class, containing attributes for accessing members in the given target class