Interface JTypeMirror
-
- All Superinterfaces:
JTypeVisitable
- All Known Subinterfaces:
JClassType
,JTypeVar
,JWildcardType
,SubstVar
- All Known Implementing Classes:
JArrayType
,JIntersectionType
,JPrimitiveType
public interface JTypeMirror extends JTypeVisitable
Type mirrors represent Java types. They are created by aTypeSystem
from symbols, a layer of abstraction above reflection classes.Type mirrors can be obtained from reflected types, directly from nodes, or from arbitrary symbols (see
TypeSystem
).Type mirrors are primarily divided between primitive types and reference types. Reference types can be of one of those kinds:
Wildcard types implement this interface, but are not really types, they can only occur as type arguments of a class type.
A few other special types do not implement one of these public interfaces:
Lastly, types may be inference variables, which should not ever occur outside of a type inference run and should be ignored when querying the AST. If you find an ivar, report a bug.
Note that implementing this type hierarchy outside of this package is not a supported usage of this API. The API is built to work with any symbol implementation, and that is its extension point.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default JTypeMirror
addAnnotation(@NonNull SymbolicValue.SymAnnot newAnnot)
Returns a type mirror that is equal to this instance but has one more type annotation.default JTypeMirror
box()
Returns the primitive wrapper type of this type, if this is a primitive type.boolean
equals(Object o)
Returns true if the object is a type equivalent to this one.default @Nullable JTypeMirror
getAsSuper(@NonNull JClassSymbol symbol)
Returns the most specific declared supertype of this type whose erasure is the same as that of the parameter.default List<JMethodSig>
getConstructors()
Returns a list of all the declared constructors for this type.default JTypeMirror
getErasure()
Returns the erasure of this type.default Set<JTypeMirror>
getSuperTypeSet()
Returns the set of (nominal) supertypes of this type.default @Nullable JTypeDeclSymbol
getSymbol()
Returns the symbol declaring this type.org.pcollections.PSet<SymbolicValue.SymAnnot>
getTypeAnnotations()
Return a list of annotations on this type.TypeSystem
getTypeSystem()
Returns the type system that built this type.default boolean
isArray()
Returns true if this is an array type.default boolean
isBottom()
Returns true if this isTypeSystem.NULL_TYPE
.default boolean
isBoxedPrimitive()
Returns true if this type is a boxed primitive type.default boolean
isClassOrInterface()
Returns true if this is a class or interface type.default TypeOps.Convertibility
isConvertibleTo(@NonNull JTypeMirror other)
Tests this type's convertibility to the other type.default boolean
isFloatingPoint()
Returns true if this type is a primitive type of a floating point type.default boolean
isGeneric()
Returns true if this type is a generic class type.default boolean
isGenericTypeDeclaration()
Returns true if this represents the *declaration* of a generic class or interface and not some parameterization.default boolean
isIntegral()
Returns true if this type is a primitive type of an integral type.default boolean
isInterface()
Returns true if this is an interface type.default boolean
isNumeric()
Returns true if this is a primitive numeric type.default boolean
isParameterizedType()
Returns true if this type is generic, and it it neither raw, nor a generic type declaration.default boolean
isPrimitive()
Returns true if this type is a primitive type.default boolean
isPrimitive(JPrimitiveType.PrimitiveTypeKind kind)
Returns true if this type is the primitive type of the given kind in its type system.default boolean
isRaw()
Returns true if this is a raw type.default boolean
isReifiable()
Returns true if this type is reifiable.default boolean
isSubtypeOf(@NonNull JTypeMirror other)
Returns true if this type is the same type or a subtype of the given type.default boolean
isTop()
Returns true if this isTypeSystem.OBJECT
.default boolean
isTypeVariable()
Returns true if this type is a type variable.default boolean
isVoid()
Returns true if this isTypeSystem.NO_TYPE
, ievoid
.default Stream<JMethodSig>
streamDeclaredMethods(Predicate<? super JMethodSymbol> prefilter)
LikestreamMethods
, but does not recurse into supertypes.default Stream<JMethodSig>
streamMethods(Predicate<? super JMethodSymbol> prefilter)
Returns a stream of method signatures declared in and inherited by this type.JTypeMirror
subst(Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
Replace the type variables occurring in the given type by their image by the given function.String
toString()
The toString of type mirrors prints useful debug information, but shouldn't be relied on anywhere, as it may change anytime.default JTypeMirror
unbox()
Returns the unboxed version of this type.JTypeMirror
withAnnotations(org.pcollections.PSet<SymbolicValue.SymAnnot> newTypeAnnots)
Returns a type mirror that is equal to this instance but has different type annotations.-
Methods inherited from interface net.sourceforge.pmd.lang.java.types.JTypeVisitable
acceptVisitor
-
-
-
-
Method Detail
-
getTypeSystem
TypeSystem getTypeSystem()
Returns the type system that built this type.
-
getTypeAnnotations
org.pcollections.PSet<SymbolicValue.SymAnnot> getTypeAnnotations()
Return a list of annotations on this type. Annotations can be written on nearly any type (eg@A Out.@B In<@C T>
,@A ? extends @B Up
).For
JTypeVar
, this includes both the annotations defined on the type var and those defined at use site. For instance
The T type var will have annotation<@A T> void accept(@B T t);
@A
in the symbol (AnnotableSymbol.getDeclaredAnnotations()
) and in the type var that is in theJMethodSig.getTypeParameters()
. In the formal parameter, the type var will have annotations@B @A
.
-
isSubtypeOf
default boolean isSubtypeOf(@NonNull JTypeMirror other)
Returns true if this type is the same type or a subtype of the given type. Note that for convenience, this returns true if both types are primitive, and this type is convertible to the other through primitive widening. SeeTypeOps.Convertibility.bySubtyping()
.- Throws:
NullPointerException
- If the argument is null
-
isConvertibleTo
default TypeOps.Convertibility isConvertibleTo(@NonNull JTypeMirror other)
Tests this type's convertibility to the other type. SeeTypeOps.Convertibility
for a description of the results.Note that this does not check for boxing/unboxing conversions.
- Throws:
NullPointerException
- If the argument is null
-
getSuperTypeSet
default Set<JTypeMirror> getSuperTypeSet()
Returns the set of (nominal) supertypes of this type. If this is a primitive type, returns the set of other primitives to which this type is convertible by widening conversion (eg forlong
returns{long, float, double}
).The returned set always contains this type, so is never empty. Ordering is stable, though unspecified.
Note that this set contains
TypeSystem.OBJECT
for interfaces too.Note that for types having type annotations, the supertypes do not bear the same annotations. Eg the supertypes of
@A String
containObject
but not@A Object
. The supertypes may be annotated though, eg if a class declaresextends @A Foo
, the supertypes contain@A Foo
.- Throws:
UnsupportedOperationException
- If this is the null type
-
getErasure
default JTypeMirror getErasure()
Returns the erasure of this type. Erasure is defined by JLS§4.6, an adapted definition follows:- The erasure of a parameterized type (§4.5) G<T1,...,Tn> is |G|.
- The erasure of a nested type T.C is |T|.C.
- The erasure of an array type T[] is |T|[].
- The erasure of a type variable (§4.4) is the erasure of its upper bound.
- The erasure of an intersection type is the erasure of its leftmost component.
- The erasure of every other type is the type itself.
The JVM representation of a type is in general the symbol of its erasure. So to get a
Class
instance for the runtime representation of a type, you should dot.getErasure().getSymbol()
. The erasure procedure gets rid of types that have no symbol (except ift
is a wildcard type, or theTypeSystem.NULL_TYPE
)
-
getSymbol
default @Nullable JTypeDeclSymbol getSymbol()
Returns the symbol declaring this type. Reifiable types present a symbol, and some other types too. This method's return value depends on this type:JClassType
: aJClassSymbol
, always (even if not reifiable)JPrimitiveType
: aJClassSymbol
, alwaysJArrayType
: aJClassSymbol
, if the element type does present a symbol.JTypeVar
: aJTypeParameterSymbol
, or null if this is a capture variable. Note that the erasure yields a different symbol (eg Object for unbounded tvars).JIntersectionType
: null, though their erasure always presents a symbol.JWildcardType
,the null type
: null, always
Note that type annotations are not reflected on the symbol, but only on the type.
-
box
default JTypeMirror box()
Returns the primitive wrapper type of this type, if this is a primitive type. Otherwise returns this type unchanged.
-
unbox
default JTypeMirror unbox()
Returns the unboxed version of this type. Returns this type unchanged if this is not a primitive wrapper type.
-
getAsSuper
default @Nullable JTypeMirror getAsSuper(@NonNull JClassSymbol symbol)
Returns the most specific declared supertype of this type whose erasure is the same as that of the parameter. E.g. forEnum<E>, Comparable
, returnsComparable<E>
.Returns null if that can't be found, meaning that the given type is not a supertype of this type.
-
isReifiable
default boolean isReifiable()
-
isPrimitive
default boolean isPrimitive()
Returns true if this type is a primitive type.
-
isPrimitive
default boolean isPrimitive(JPrimitiveType.PrimitiveTypeKind kind)
Returns true if this type is the primitive type of the given kind in its type system.
-
isFloatingPoint
default boolean isFloatingPoint()
Returns true if this type is a primitive type of a floating point type.
-
isIntegral
default boolean isIntegral()
Returns true if this type is a primitive type of an integral type.
-
isTypeVariable
default boolean isTypeVariable()
Returns true if this type is a type variable.
-
isBoxedPrimitive
default boolean isBoxedPrimitive()
Returns true if this type is a boxed primitive type. This is aJClassType
, whoseunbox()
method returns aJPrimitiveType
.
-
isNumeric
default boolean isNumeric()
Returns true if this is a primitive numeric type. The only non-numeric primitive type isTypeSystem.BOOLEAN
.
-
isClassOrInterface
default boolean isClassOrInterface()
Returns true if this is a class or interface type.
-
isTop
default boolean isTop()
Returns true if this isTypeSystem.OBJECT
.
-
isBottom
default boolean isBottom()
Returns true if this isTypeSystem.NULL_TYPE
.
-
isVoid
default boolean isVoid()
Returns true if this isTypeSystem.NO_TYPE
, ievoid
.
-
isArray
default boolean isArray()
Returns true if this is an array type.
-
isGenericTypeDeclaration
default boolean isGenericTypeDeclaration()
Returns true if this represents the *declaration* of a generic class or interface and not some parameterization. This is the "canonical" form of a parameterized type.In that case, the
JClassType.getTypeArgs()
is the same asJClassType.getFormalTypeParams()
.The generic type declaration of a generic type may be obtained with
JClassType.getGenericTypeDeclaration()
.
-
isGeneric
default boolean isGeneric()
Returns true if this type is a generic class type. This means, the symbol declares some type parameters, which is also true for erased types, including raw types.For example,
List
,List<T>
, andList<String>
are generic, butString
is not.- See Also:
JClassType.isGeneric()
-
isParameterizedType
default boolean isParameterizedType()
Returns true if this type is generic, and it it neither raw, nor a generic type declaration.E.g. returns true for
List<String>
orEnum<KeyCode>
, but not forList
(raw type),List<T>
(generic type declaration), orKeyCode
(not a generic type).
-
isRaw
default boolean isRaw()
Returns true if this is a raw type. This may be- A generic class or interface type for which no type arguments were provided
- An array type whose element type is raw
- A non-static member type of a raw type
https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.8
- See Also:
JClassType.isRaw()
-
isInterface
default boolean isInterface()
Returns true if this is an interface type. Annotations are also interface types.
-
streamMethods
default Stream<JMethodSig> streamMethods(Predicate<? super JMethodSymbol> prefilter)
Returns a stream of method signatures declared in and inherited by this type. Method signatures are created on-demand by this method, they're not reused between calls. This stream does not include constructors.Note: streams are a bit impractical when it comes to configuring the filter. Possibly a specialized API should be introduced. We need to support the use cases of the symbol table, ie filter by name + accessibility + staticity, and also possibly use cases for rules, like getting a method from a known signature. See also
JClassType.getDeclaredMethod(JExecutableSymbol)
, which looks like this. Unifying this API would be nice.- Parameters:
prefilter
- Filter selecting symbols for which a signature should be created and yielded by the stream
-
streamDeclaredMethods
default Stream<JMethodSig> streamDeclaredMethods(Predicate<? super JMethodSymbol> prefilter)
LikestreamMethods
, but does not recurse into supertypes. Note that only class and array types declare methods themselves.See also note in
streamMethods(Predicate)
.- See Also:
streamMethods(Predicate)
-
getConstructors
default List<JMethodSig> getConstructors()
Returns a list of all the declared constructors for this type. Abstract types like type variables and interfaces have no constructors.
-
subst
JTypeMirror subst(Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
Description copied from interface:JTypeVisitable
Replace the type variables occurring in the given type by their image by the given function. Substitutions are not applied recursively (ie, is not applied on the result of a substitution).- Specified by:
subst
in interfaceJTypeVisitable
- Parameters:
subst
- Substitution function, eg aSubstitution
-
withAnnotations
JTypeMirror withAnnotations(org.pcollections.PSet<SymbolicValue.SymAnnot> newTypeAnnots)
Returns a type mirror that is equal to this instance but has different type annotations. Note that some types ignore this method and return themselves without changing. Eg the null type cannot be annotated.- Parameters:
newTypeAnnots
- New type annotations (not null)- Returns:
- A new type, maybe this one
-
addAnnotation
default JTypeMirror addAnnotation(@NonNull SymbolicValue.SymAnnot newAnnot)
Returns a type mirror that is equal to this instance but has one more type annotation.- See Also:
withAnnotations(PSet)
-
equals
boolean equals(Object o)
Returns true if the object is a type equivalent to this one. A few kinds of types use reference identity, like captured type variables, or the null type. A few special types are represented by constants (seeTypeSystem
). Apart from those, types should always be compared using this method. orTypeOps.isSameType(JTypeMirror, JTypeMirror)
(which is null-safe).Note that types belonging to different type systems do not test equal. The type system object is global to the analysis though, so this should not ever happen in rules.
- Overrides:
equals
in classObject
- Implementation Requirements:
- This method should be implemented with
TypeOps.isSameType(JTypeMirror, JTypeMirror)
, and perform no side-effects on inference variables. - Parameters:
o
-- Returns:
-
toString
String toString()
The toString of type mirrors prints useful debug information, but shouldn't be relied on anywhere, as it may change anytime. UseTypePrettyPrint
to display types.
-
-