Class TypeOps

java.lang.Object
net.sourceforge.pmd.lang.java.types.TypeOps

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

    • isSameType

      public static boolean isSameType(JMethodSig t, JMethodSig s)
      Return true if t and s are the same method type. This compares their declaring type, and then their signature.
      See Also:
      • haveSameSignature(JMethodSig, JMethodSig)
    • 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.
    • areSameTypes

      public static boolean areSameTypes(List<JTypeMirror> ts, List<JTypeMirror> ss)
    • areSameTypesInInference

      public static boolean areSameTypesInInference(List<JTypeMirror> ts, List<JTypeMirror> ss)
    • getSuperTypeSet

      public static Set<JTypeMirror> getSuperTypeSet(@NonNull JTypeMirror t)
      Returns the set of all supertypes of the given type.
      See Also:
    • isConvertible

      public static TypeOps.Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
    • isConvertible

      @Deprecated public static TypeOps.Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s, boolean capture)
      Deprecated.
    • isConvertibleNoCapture

      public static TypeOps.Convertibility isConvertibleNoCapture(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
    • isConvertiblePure

      public static TypeOps.Convertibility isConvertiblePure(JTypeMirror t, JTypeMirror s)
    • 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
    • subst

      public static List<JTypeMirror> subst(List<? extends JTypeMirror> ts, Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
      Substitute on a list of types.
    • substClasses

      public static List<JClassType> substClasses(List<JClassType> ts, Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
    • substInBoundsOnly

      public static List<JTypeVar> substInBoundsOnly(List<JTypeVar> ts, Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
    • 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
    • haveSameTypeParams

      public static boolean haveSameTypeParams(JMethodSig m1, JMethodSig m2)
    • 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.
    • asSuper

      public static @Nullable JTypeMirror asSuper(@NonNull JTypeMirror t, @NonNull JClassSymbol s)
      See Also:
    • asOuterSuper

      public static @Nullable 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.
    • getReceiverType

      public static @Nullable JClassType getReceiverType(@NonNull JClassType containerType, JClassSymbol sym)
      Return the first enclosing type of the container type that has the given symbol in its supertypes. Return null if this is not found.
    • 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.
    • erase

      public static List<JTypeMirror> erase(Collection<? extends JTypeMirror> ts)
      Returns a list with the erasures of the given types, may be unmodifiable.
    • mentions

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

      public static boolean mentionsAny(JTypeVisitable t, Collection<? extends SubstVar> vars)
    • accessibleMethodFilter

      public static Predicate<JMethodSymbol> accessibleMethodFilter(String name, @NonNull JClassSymbol symbol)
    • lazyFilterAccessible

      public static Iterable<JMethodSig> lazyFilterAccessible(List<JMethodSig> visible, @NonNull JClassSymbol accessSite)
    • filterAccessible

      public static List<JMethodSig> filterAccessible(List<JMethodSig> visible, @NonNull JClassSymbol accessSite)
    • getMemberSource

      public static JTypeMirror getMemberSource(JTypeMirror t)
      Methods and fields of a type variable come from its upper bound, which must be captured. Capturing a type var does NOT capture its upper bound, so we must treat this case here.
    • getMethodsOf

      public static List<JMethodSig> getMethodsOf(JTypeMirror type, String name, boolean staticOnly, @NonNull JClassSymbol enclosing)
    • getMemberFieldResolver

      public static NameResolver<JVariableSig.FieldSig> getMemberFieldResolver(JTypeMirror c, @NonNull String accessPackageName, @Nullable JClassSymbol access, String name)
    • 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
    • isUnresolved

      public static boolean isUnresolved(@NonNull JTypeMirror t)
      Returns true if the type is TypeSystem.UNKNOWN, TypeSystem.ERROR, or a class type with unresolved symbol.
      Parameters:
      t - Non-null type
      Throws:
      NullPointerException - if the parameter is null
    • isUnresolvedOrArray

      public static boolean isUnresolvedOrArray(@NonNull JTypeMirror t)
      Returns true if the type is TypeSystem.UNKNOWN, or TypeSystem.ERROR, or a class type with unresolved symbol, or an array of such types.
      Parameters:
      t - Non-null type
      Throws:
      NullPointerException - if the parameter is null
    • isSpecialUnresolved

      public static boolean isSpecialUnresolved(@NonNull JTypeMirror t)
      Returns true if the type is TypeSystem.UNKNOWN, or TypeSystem.ERROR.
      Parameters:
      t - Non-null type
      Throws:
      NullPointerException - if the parameter is null
    • isSpecialUnresolvedOrArray

      public static boolean isSpecialUnresolvedOrArray(@Nullable JTypeMirror t)
      Returns true if the type is TypeSystem.UNKNOWN, or TypeSystem.ERROR, or an array of such types.
      Parameters:
      t - Non-null type
      Throws:
      NullPointerException - if the parameter is null
    • hasUnresolvedSymbol

      public static boolean hasUnresolvedSymbol(@Nullable JTypeMirror t)
      Return true if the argument is a JClassType with an unresolved symbol.
    • hasUnresolvedSymbolOrArray

      public static boolean hasUnresolvedSymbolOrArray(@Nullable JTypeMirror t)
      Return true if the argument is a JClassType with an unresolved symbol, or an array whose element type has an unresolved symbol.
    • isUnresolvedOrNull

      public static boolean isUnresolvedOrNull(@Nullable JTypeMirror t)
    • getArrayComponent

      public static @Nullable JTypeMirror getArrayComponent(@Nullable JTypeMirror t)
    • isContextDependent

      @Deprecated public static boolean isContextDependent(JMethodSig sig)
      Deprecated.
      Since 7.11.0. Use isContextDependent(JExecutableSymbol) instead which is more flexible.
      Return true if the method is context dependent. That means its return type is influenced by the surrounding context during type inference. Generic constructors are always context dependent.
    • isContextDependent

      public static boolean isContextDependent(JExecutableSymbol symbol)
      Return true if the method is context dependent. That means its return type is influenced by the surrounding context during type inference. Generic constructors are always context dependent.