Class TypeOps
- java.lang.Object
-
- net.sourceforge.pmd.lang.java.types.TypeOps
-
public final class TypeOps extends Object
Common operations on types.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TypeOps.Convertibility
A result for a convertibility check.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Predicate<JMethodSymbol>
accessibleMethodFilter(String name, @NonNull JClassSymbol symbol)
static boolean
allArgsAreUnboundedWildcards(List<JTypeMirror> sargs)
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.static boolean
areRelated(@NonNull JTypeMirror t, JTypeMirror s)
Returns true if both types have a common supertype that is not Object.static boolean
areSameTypes(List<JTypeMirror> ts, List<JTypeMirror> ss)
static boolean
areSameTypesInInference(List<JTypeMirror> ts, List<JTypeMirror> ss)
static @Nullable JClassType
asClassType(@Nullable JTypeMirror t)
Returns t if it is a class or interface type.static List<JTypeMirror>
asList(JTypeMirror t)
Returns the components of t if it is an intersection type, otherwise returns t.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.static @Nullable JTypeMirror
asSuper(@NonNull JTypeMirror t, @NonNull JClassSymbol s)
static List<JTypeMirror>
erase(Collection<? extends JTypeMirror> ts)
Returns a list with the erasures of the given types, may be unmodifiable.static List<JMethodSig>
filterAccessible(List<JMethodSig> visible, @NonNull JClassSymbol accessSite)
static @Nullable JMethodSig
findFunctionalInterfaceMethod(@Nullable JTypeMirror type)
Finds the method of the given type that can be overridden as a lambda expression.static @Nullable JTypeMirror
getArrayComponent(@Nullable JTypeMirror t)
static NameResolver<JVariableSig.FieldSig>
getMemberFieldResolver(JTypeMirror c, @NonNull String accessPackageName, @Nullable JClassSymbol access, String name)
static List<JMethodSig>
getMethodsOf(JTypeMirror type, String name, boolean staticOnly, @NonNull JClassSymbol enclosing)
static Set<JTypeMirror>
getSuperTypeSet(@NonNull JTypeMirror t)
Returns the set of all supertypes of the given type.static boolean
hasUnresolvedSymbol(@Nullable JTypeMirror t)
Return true if the argument is aJClassType
with an unresolved symbol or aJArrayType
whose element type matches the first criterion.static boolean
haveSameTypeParams(JMethodSig m1, JMethodSig m2)
static TypeOps.Convertibility
isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
static TypeOps.Convertibility
isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s, boolean capture)
Returns whether ifT <: S
, ie T is a subtype of S.static TypeOps.Convertibility
isConvertibleNoCapture(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
static boolean
isOverridableIn(JExecutableSymbol m, JTypeDeclSymbol origin)
Returns true if the given method can be overridden in the origin class.static boolean
isReturnTypeSubstitutable(JMethodSig m1, JMethodSig m2)
Returns true if m1 is return-type substitutable with m2.static boolean
isSameType(JMethodSig t, JMethodSig s)
Return true if t and s are the same method type.static boolean
isSameType(JTypeMirror t, JTypeMirror s)
Return true if t and s are the same type, ignoring any type annotations appearing within them.static boolean
isSameTypeInInference(JTypeMirror t, JTypeMirror s)
Return true if t and s are the same type.static boolean
isSameTypeWithSameAnnotations(JTypeMirror t, JTypeMirror s)
Return true if t and s are the same type, considering any type annotations appearing within them.static boolean
isSpecialUnresolved(@NonNull JTypeMirror t)
static boolean
isStrictSubtype(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
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.static boolean
isUnresolved(@NonNull JTypeMirror t)
Returns true if the type isTypeSystem.UNKNOWN
,TypeSystem.ERROR
, or its symbol is unresolved.static boolean
isUnresolvedOrNull(@Nullable JTypeMirror t)
static Iterable<JMethodSig>
lazyFilterAccessible(List<JMethodSig> visible, @NonNull JClassSymbol accessSite)
static boolean
mentions(@NonNull JTypeVisitable type, @NonNull InferenceVar parent)
static boolean
mentionsAny(JTypeVisitable t, Collection<? extends SubstVar> vars)
static Set<JTypeMirror>
mostSpecific(Collection<? extends JTypeMirror> set)
Returns a subset S of the parameter, whose components have no strict supertype in S.static @Nullable JClassType
nonWildcardParameterization(@NonNull JClassType type)
Returns the non-wildcard parameterization of the given functional interface type.static boolean
overrides(JMethodSig m1, JMethodSig m2, JTypeMirror origin)
Returns true if m1 overrides m2, when both are view as members of class origin.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.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.static List<JTypeMirror>
subst(List<? extends JTypeMirror> ts, Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
Substitute on a list of types.static List<JClassType>
substClasses(List<JClassType> ts, Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
static List<JTypeVar>
substInBoundsOnly(List<JTypeVar> ts, Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
-
-
-
Method Detail
-
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.
-
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 ofJTypeMirror
.
-
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.
-
isSameTypeInInference
@InternalApi public static boolean isSameTypeInInference(JTypeMirror t, JTypeMirror s)
Return true if t and s are the same type. This may perform side effects on inference variables. Annotations are ignored.
-
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:
JTypeMirror.getSuperTypeSet()
-
isConvertible
public static TypeOps.Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
-
isConvertibleNoCapture
public static TypeOps.Convertibility isConvertibleNoCapture(@NonNull JTypeMirror t, @NonNull JTypeMirror s)
-
isConvertible
public static TypeOps.Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s, boolean capture)
Returns whether ifT <: S
, ie T is a subtype of S.Note that
TypeSystem.ERROR
andTypeSystem.UNKNOWN
are considered subtypes of anything.- Parameters:
t
- A type Ts
- A type 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 substitutesubst
- Substitution function, eg aSubstitution
-
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 testorigin
- 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.9If 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:
JTypeMirror.getAsSuper(JClassSymbol)
-
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.
-
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 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)
-
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 types
- Non-null type- Throws:
NullPointerException
- if a parameter is null
-
isUnresolved
public static boolean isUnresolved(@NonNull JTypeMirror t)
Returns true if the type isTypeSystem.UNKNOWN
,TypeSystem.ERROR
, or its symbol is unresolved.- Parameters:
t
- Non-null type- Throws:
NullPointerException
- if the parameter is null
-
isSpecialUnresolved
public static boolean isSpecialUnresolved(@NonNull JTypeMirror t)
-
hasUnresolvedSymbol
public static boolean hasUnresolvedSymbol(@Nullable JTypeMirror t)
Return true if the argument is aJClassType
with an unresolved symbol or aJArrayType
whose element type matches the first criterion.
-
isUnresolvedOrNull
public static boolean isUnresolvedOrNull(@Nullable JTypeMirror t)
-
getArrayComponent
public static @Nullable JTypeMirror getArrayComponent(@Nullable JTypeMirror t)
-
-