Class TypeOps


  • public final class TypeOps
    extends Object
    Common operations on types.
    • Method Detail

      • isSameType

        public static boolean isSameType​(JTypeMirror t,
                                         JTypeMirror s)
        Return true if t and s are the same type, ignoring any type annotations appearing within them. This is the implementation of the equals method of JTypeMirror.
      • isSameTypeWithSameAnnotations

        public static boolean isSameTypeWithSameAnnotations​(JTypeMirror t,
                                                            JTypeMirror s)
        Return true if t and s are the same type, considering any type annotations appearing within them.
      • allArgsAreUnboundedWildcards

        public static boolean allArgsAreUnboundedWildcards​(List<JTypeMirror> sargs)
      • isStrictSubtype

        public static boolean isStrictSubtype​(@NonNull JTypeMirror t,
                                              @NonNull JTypeMirror s)
      • subst

        public static JTypeMirror subst​(@Nullable JTypeMirror type,
                                        Function<? super SubstVar,​? extends @NonNull JTypeMirror> subst)
        Replace the type variables occurring in the given type to their image by the given function. Substitutions are not applied recursively.
        Parameters:
        type - Type to substitute
        subst - Substitution function, eg a Substitution
      • projectUpwards

        public static JTypeMirror projectUpwards​(JTypeMirror t)
        Returns the upwards projection of the given type, with respect to the set of capture variables that are found in it. This is some supertype of T which does not mention those capture variables. This is used for local variable type inference. https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.10.5
      • isReturnTypeSubstitutable

        public static boolean isReturnTypeSubstitutable​(JMethodSig m1,
                                                        JMethodSig m2)
        Returns true if m1 is return-type substitutable with m2. The notion of return-type-substitutability supports covariant returns, that is, the specialization of the return type to a subtype. https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.5
      • areOverrideEquivalent

        public static boolean areOverrideEquivalent​(JMethodSig m1,
                                                    JMethodSig m2)
        Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1. This does not look at the origin of the methods (their declaring class).

        This is a prerequisite for one method to override the other, but not the only condition. See overrides(JMethodSig, JMethodSig, JTypeMirror). See JLS§8

      • isSubSignature

        public static boolean isSubSignature​(JMethodSig m1,
                                             JMethodSig m2)
        The signature of a method m1 is a subsignature of the signature of a method m2 if either: - m2 has the same signature as m1, or - the signature of m1 is the same as the erasure (§4.6) of the signature of m2.
      • overrides

        public static boolean overrides​(JMethodSig m1,
                                        JMethodSig m2,
                                        JTypeMirror origin)
        Returns true if m1 overrides m2, when both are view as members of class origin. m1 and m2 may be declared in supertypes of origin, possibly unrelated (default methods), which is why we need that third parameter. By convention a method overrides itself.

        This method ignores the static modifier. If both methods are static, then this method tests for hiding. Otherwise, this method properly tests for overriding. Note that it is an error for a static method to override an instance method, or the reverse.

      • isOverridableIn

        public static boolean isOverridableIn​(JExecutableSymbol m,
                                              JTypeDeclSymbol origin)
        Returns true if the given method can be overridden in the origin class. This only checks access modifiers and not eg whether the method is final or static. Regardless of whether the method is final it is overridden - whether this is a compile error or not is another matter.

        Like overrides(JMethodSig, JMethodSig, JTypeMirror), this does not check the static modifier, and tests for hiding if the method is static.

        Parameters:
        m - Method to test
        origin - Site of the potential override
      • nonWildcardParameterization

        public static @Nullable JClassType nonWildcardParameterization​(@NonNull JClassType type)
        Returns the non-wildcard parameterization of the given functional interface type. Returns null if such a parameterization does not exist.

        This is used to remove wildcards from the type of a functional interface. https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.9

        Parameters:
        type - A parameterized functional interface type
      • findFunctionalInterfaceMethod

        public static @Nullable JMethodSig findFunctionalInterfaceMethod​(@Nullable JTypeMirror type)
        Finds the method of the given type that can be overridden as a lambda expression. That is more complicated than "the unique abstract method", it's actually a function type which can override all abstract methods of the SAM at once. https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.9

        If the parameter is not mappable to a class type with asClassType(JTypeMirror), or if the functional method does not exist, returns null.

      • asClassType

        public static @Nullable JClassType asClassType​(@Nullable JTypeMirror t)
        Returns t if it is a class or interface type. If it is an intersection type, returns the induced class or interface type. Returns null otherwise, including if the parameter is null.
      • asOuterSuper

        public static JClassType asOuterSuper​(JTypeMirror t,
                                              JClassSymbol sym)
        Return the base type of t or any of its outer types that starts with the given type. If none exists, return null.
      • mostSpecific

        public static Set<JTypeMirror> mostSpecific​(Collection<? extends JTypeMirror> set)
        Returns a subset S of the parameter, whose components have no strict supertype in S.
        
         S = { V | V in set, and for all W ≠ V in set, it is not the case that W <: V }
         
      • asList

        public static List<JTypeMirror> asList​(JTypeMirror t)
        Returns the components of t if it is an intersection type, otherwise returns t.
      • mentions

        public static boolean mentions​(@NonNull JTypeVisitable type,
                                       @NonNull net.sourceforge.pmd.lang.java.types.internal.infer.InferenceVar parent)
      • areRelated

        public static boolean areRelated​(@NonNull JTypeMirror t,
                                         JTypeMirror s)
        Returns true if both types have a common supertype that is not Object. Primitive types are only related to themselves.
        Parameters:
        t - Non-null type
        s - Non-null type
        Throws:
        NullPointerException - if a parameter is null
      • isSpecialUnresolved

        public static boolean isSpecialUnresolved​(@NonNull JTypeMirror t)
      • isUnresolvedOrNull

        public static boolean isUnresolvedOrNull​(@Nullable JTypeMirror t)