Class TypeTestUtil


  • public final class TypeTestUtil
    extends Object
    Public utilities to test the type of nodes.
    See Also:
    InvocationMatcher
    • Method Detail

      • isA

        public static boolean isA​(@NonNull Class<?> clazz,
                                  @Nullable 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
         

        If either type is unresolved, the types are tested for equality, thus giving more useful results than JTypeMirror.isSubtypeOf(JTypeMirror).

        Note that primitives are NOT considered subtypes of one another by this method, even though JTypeMirror.isSubtypeOf(JTypeMirror) does.

        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​(@NonNull Class<?> clazz,
                                  @Nullable JTypeMirror type)
        Checks whether the given type of the node is a subtype of the first argument. See isA(Class, TypeNode) for examples and more info.
        Parameters:
        clazz - a class or array type (without whitespace)
        type - the type node to check
        Returns:
        true if the second argument is not null and the type test matches
        Throws:
        NullPointerException - if the class parameter is null
        See Also:
        isA(Class, TypeNode)
      • isA

        public static boolean isA​(@NonNull String canonicalName,
                                  @Nullable TypeNode node)
        Checks whether the static type of the node is a subtype of the class identified by the given name. See isA(Class, TypeNode) for examples and more info.
        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
        IllegalArgumentException - if the class name parameter is not a valid java binary name, eg it has type arguments
        See Also:
        isA(Class, TypeNode)
      • isA

        public static boolean isA​(@NonNull String canonicalName,
                                  @Nullable JTypeMirror thisType)
      • isA

        public static boolean isA​(@Nullable JTypeMirror t1,
                                  @NonNull JTypeMirror t2)
        Checks whether the second type is a subtype of the first. This removes some behavior of isSubtypeOf that we don't want (eg, that unresolved types are subtypes of everything).
        Parameters:
        t1 - A supertype
        t2 - A type
        Returns:
        Whether t1 is a subtype of t2
      • isExactlyA

        public static boolean isExactlyA​(@NonNull Class<?> clazz,
                                         @Nullable 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​(@NonNull Class<?> klass,
                                         @Nullable JTypeMirror type)
      • isExactlyA

        public static boolean isExactlyA​(@NonNull Class<?> klass,
                                         @Nullable JTypeDeclSymbol type)
      • isDeclaredInClass

        public static boolean isDeclaredInClass​(@NonNull Class<?> klass,
                                                @NonNull JMethodSig sig)
        Returns true if the signature is that of a method declared in the given class.
        Parameters:
        klass - Class
        sig - Method signature to test
        Throws:
        NullPointerException - If any argument is null
      • isExactlyA

        public static boolean isExactlyA​(@NonNull String canonicalName,
                                         @Nullable TypeNode node)
        Checks whether the static type of the node is exactly the type given by the name. See isExactlyA(Class, TypeNode) for examples and more info.
        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
        IllegalArgumentException - if the class name parameter is not a valid java binary name, eg it has type arguments
        See Also:
        isExactlyA(Class, TypeNode)