Class TypeTestUtil


  • public final class TypeTestUtil
    extends Object
    Public utilities to test the type of nodes.

    This replaces TypeHelper. Note that in contrast to methods in TypeHelper, these methods:

    • Take the node as the second parameter
    • Systematically return false if the node argument is null
    • Systematically throw if the other argument is null
    • Method Detail

      • isA

        public static boolean isA​(Class<?> clazz,
                                  TypeNode node)
        Checks whether the static type of the node is a subtype of the class identified by the given name. This ignores type arguments, if the type of the node is parameterized. Examples:
        
         isA(List.class, <new ArrayList<String>()>)      = true
         isA(ArrayList.class, <new ArrayList<String>()>) = true
         isA(int[].class, <new int[0]>)                  = true
         isA(Object[].class, <new String[0]>)            = true
         isA(_, null) = false
         isA(null, _) = NullPointerException
         
        Parameters:
        clazz - a class (non-null)
        node - the type node to check
        Returns:
        true if the type test matches
        Throws:
        NullPointerException - if the class parameter is null
      • isA

        public static boolean isA​(String canonicalName,
                                  TypeNode node)
        Checks whether the static type of the node is a subtype of the class identified by the given name. This ignores type arguments, if the type of the node is parameterized. Examples:
        
         isA("java.util.List", <new ArrayList<String>()>)      = true
         isA("java.util.ArrayList", <new ArrayList<String>()>) = true
         isA("int[]", <new int[0]>)                            = true
         isA("java.lang.Object[]", <new String[0]>)            = true
         isA(_, null) = false
         isA(null, _) = NullPointerException
         
        Parameters:
        canonicalName - the canonical name of a class or array type (without whitespace)
        node - the type node to check
        Returns:
        true if the type test matches
        Throws:
        NullPointerException - if the class name parameter is null
      • isExactlyA

        public static boolean isExactlyA​(Class<?> clazz,
                                         TypeNode node)
        Checks whether the static type of the node is exactly the type of the class. This ignores strict supertypes, and type arguments, if the type of the node is parameterized.
        
         isExactlyA(List.class, <new ArrayList<String>()>)      = false
         isExactlyA(ArrayList.class, <new ArrayList<String>()>) = true
         isExactlyA(int[].class, <new int[0]>)                  = true
         isExactlyA(Object[].class, <new String[0]>)            = false
         isExactlyA(_, null) = false
         isExactlyA(null, _) = NullPointerException
         
        Parameters:
        clazz - a class (non-null)
        node - the type node to check
        Returns:
        true if the node is non-null and has the given type
        Throws:
        NullPointerException - if the class parameter is null
      • isExactlyA

        public static boolean isExactlyA​(String canonicalName,
                                         TypeNode node)
        Checks whether the static type of the node is exactly the type given by the name. This ignores strict supertypes, and type arguments if the type of the node is parameterized.
        
         isExactlyA(List.class, <new ArrayList<String>()>)      = false
         isExactlyA(ArrayList.class, <new ArrayList<String>()>) = true
         isExactlyA(int[].class, <new int[0]>)                  = true
         isExactlyA(Object[].class, <new String[0]>)            = false
         isExactlyA(_, null) = false
         isExactlyA(null, _) = NullPointerException
         
        Parameters:
        canonicalName - a canonical name of a class or array type
        node - the type node to check
        Returns:
        true if the node is non-null and has the given type
        Throws:
        NullPointerException - if the class name parameter is null