Interface JClassType
-
- All Superinterfaces:
JTypeMirror
,JTypeVisitable
public interface JClassType extends JTypeMirror
Represents class and interface types, including functional interface types. This interface can be thought of as aJClassSymbol
viewed under a given parameterization. Methods likeJTypeMirror.streamMethods(Predicate)
, return signatures that are already partially substituted. Eg the methodget(int)
for typeList<String>
has return typeString
, not the type varT
or the erasureObject
.A class type may present its symbol under several views:
- If the symbol is not generic, then it may be either
erased (where all supertypes are erased),
or not. Note that a non-erased type may have some erased supertypes,
see
getErasure()
. - If the symbol is generic, then the type could be in one of the
following configurations:
- Generic declaration:
the type arguments are the formal type parameters. All enclosing types are
either non-generic or also generic type declarations. Eg
interface List<T> { .. }
. - Parameterized: the type
has type arguments. All enclosing types are either non-generic or
also parameterised. Eg
List<String>
. - Raw: the type doesn't have type arguments,
it's considered erased, so all its supertypes are
also erased. All enclosing types are also erased. Eg
List
.
- Generic declaration:
the type arguments are the formal type parameters. All enclosing types are
either non-generic or also generic type declarations. Eg
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <T,P>
TacceptVisitor(JTypeVisitor<T,P> visitor, P p)
Accept a type visitor, dispatching on this object's runtime type to the correct method of the visitor.default @Nullable JClassType
getAsSuper(@NonNull JClassSymbol symbol)
Returns the most specific declared supertype of this type whose erasure is the same as that of the parameter.@Nullable JClassType
getDeclaredClass(String simpleName)
Return the nested class with the given name, or null if there is none.List<JClassType>
getDeclaredClasses()
Return the list of declared nested classes.@Nullable JVariableSig.FieldSig
getDeclaredField(String simpleName)
Return the field with the given name, or null if there is none.List<JVariableSig.FieldSig>
getDeclaredFields()
Return the list of declared fields.@Nullable JMethodSig
getDeclaredMethod(JExecutableSymbol sym)
Returns the typed signature for the symbol, if it is declared directly in this type, and not a supertype.@Nullable JClassType
getEnclosingType()
Returns the type immediately enclosing this type.JClassType
getErasure()
Returns the erasure of this type.List<JTypeVar>
getFormalTypeParams()
Returns the list of type variables declared by the generic type declaration.JClassType
getGenericTypeDeclaration()
If this type is generic, returns the type that represents its generic type declaration.@Nullable JClassType
getSuperClass()
Returns the generic superclass type.List<JClassType>
getSuperInterfaces()
Return the list of interface types directly implemented by this type.@NonNull JClassSymbol
getSymbol()
Returns the symbol declaring this type.List<JTypeMirror>
getTypeArgs()
A specific instantiation of the type variables ingetFormalTypeParams()
.Substitution
getTypeParamSubst()
Returns the substitution mapping the formal type parameters of all enclosing types to their type arguments.boolean
hasErasedSuperTypes()
Returns true if this type is erased.boolean
isGeneric()
Returns true if the symbol of this type declares some type parameters.boolean
isGenericTypeDeclaration()
Returns true if this represents the *declaration* of a generic class or interface and not some parameterization.boolean
isRaw()
Returns true if this type represents a raw type, ie a type whose declaration is generic, but for which no type arguments were provided.default JClassType
selectInner(JClassSymbol symbol, List<? extends JTypeMirror> targs)
Select an inner type.JClassType
selectInner(JClassSymbol symbol, List<? extends JTypeMirror> targs, org.pcollections.PSet<SymbolicValue.SymAnnot> typeAnnotations)
Select an inner type, with new type annotations.default JClassType
subst(Function<? super SubstVar,? extends @NonNull JTypeMirror> fun)
Replace the type variables occurring in the given type by their image by the given function.JClassType
withAnnotations(org.pcollections.PSet<SymbolicValue.SymAnnot> newTypeAnnots)
Returns a type mirror that is equal to this instance but has different type annotations.JClassType
withTypeArguments(List<? extends JTypeMirror> args)
Returns another class type which has the same erasure, but new type arguments.-
Methods inherited from interface net.sourceforge.pmd.lang.java.types.JTypeMirror
addAnnotation, box, equals, getConstructors, getSuperTypeSet, getTypeAnnotations, getTypeSystem, isArray, isBottom, isBoxedPrimitive, isClassOrInterface, isConvertibleTo, isFloatingPoint, isIntegral, isInterface, isNumeric, isParameterizedType, isPrimitive, isPrimitive, isReifiable, isSubtypeOf, isTop, isTypeVariable, isVoid, streamDeclaredMethods, streamMethods, toString, unbox
-
-
-
-
Method Detail
-
getSymbol
@NonNull JClassSymbol getSymbol()
Description copied from interface:JTypeMirror
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.
- Specified by:
getSymbol
in interfaceJTypeMirror
-
withAnnotations
JClassType withAnnotations(org.pcollections.PSet<SymbolicValue.SymAnnot> newTypeAnnots)
Description copied from interface:JTypeMirror
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.- Specified by:
withAnnotations
in interfaceJTypeMirror
- Parameters:
newTypeAnnots
- New type annotations (not null)- Returns:
- A new type, maybe this one
-
subst
default JClassType subst(Function<? super SubstVar,? extends @NonNull JTypeMirror> fun)
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 interfaceJTypeMirror
- Specified by:
subst
in interfaceJTypeVisitable
- Parameters:
fun
- Substitution function, eg aSubstitution
-
hasErasedSuperTypes
boolean hasErasedSuperTypes()
Returns true if this type is erased. In that case, all the generic supertypes of this type are erased, type parameters are erased in its field and method types. In particular, if this type declares type parameters, then it is a raw type.
-
isRaw
boolean isRaw()
Returns true if this type represents a raw type, ie a type whose declaration is generic, but for which no type arguments were provided. In that case the type arguments are an empty list, and all supertypes are erased.Raw types are convertible to any parameterized type of the same family via unchecked conversion.
- Specified by:
isRaw
in interfaceJTypeMirror
- See Also:
isRaw()
-
isGenericTypeDeclaration
boolean isGenericTypeDeclaration()
Description copied from interface:JTypeMirror
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
getTypeArgs()
is the same asgetFormalTypeParams()
.The generic type declaration of a generic type may be obtained with
getGenericTypeDeclaration()
.- Specified by:
isGenericTypeDeclaration
in interfaceJTypeMirror
-
getGenericTypeDeclaration
JClassType getGenericTypeDeclaration()
If this type is generic, returns the type that represents its generic type declaration. Otherwise, returns this type.- See Also:
isGenericTypeDeclaration()
-
isGeneric
boolean isGeneric()
Returns true if the symbol of this type declares some type parameters. This is true also for erased types.For example,
List
,List<T>
, andList<String>
are generic, butString
is not.- Specified by:
isGeneric
in interfaceJTypeMirror
- See Also:
isGeneric()
-
getEnclosingType
@Nullable JClassType getEnclosingType()
Returns the type immediately enclosing this type. This may be null if this is a top-level type.
-
getTypeArgs
List<JTypeMirror> getTypeArgs()
A specific instantiation of the type variables ingetFormalTypeParams()
. Note that the type arguments and formal type parameters may be mismatched in size, (only if the symbol is unresolved). In any case, no attempt is made to check that the type arguments conform to the bound on type parameters in methods likewithTypeArguments(List)
, although this is taken into account during type inference.If this type is not generic, or a raw type, returns an empty list.
If this is a generic type declaration, returns exactly the same list as
getFormalTypeParams()
.- See Also:
getFormalTypeParams()
-
getFormalTypeParams
List<JTypeVar> getFormalTypeParams()
Returns the list of type variables declared by the generic type declaration.If this type is not generic, returns an empty list. Note that if the symbol is unresolved, it is considered non-generic. But it still may have type arguments.
- See Also:
getTypeArgs()
-
getTypeParamSubst
Substitution getTypeParamSubst()
Returns the substitution mapping the formal type parameters of all enclosing types to their type arguments. If a type is raw, then its type parameters are not part of the returned mapping. Note, that this does not include type parameters of the supertypes.If this type is erased, returns a substitution erasing all type parameters.
For instance, in the type
List<String>
, this is the substitution mapping the type parameterT
ofinterface List<T>
toString
. It is suitable for use in e.g.JMethodSymbol.getReturnType(Substitution)
.
-
selectInner
default JClassType selectInner(JClassSymbol symbol, List<? extends JTypeMirror> targs)
Select an inner type. This can only be called if the given symbol represents a non-static member type of this type declaration.- Parameters:
symbol
- Symbol for the inner typetargs
- Type arguments of the inner type. If that is an empty list, and the given symbol is generic, then the inner type will be raw, or a generic type declaration, depending on whether this type is erased or not.- Returns:
- A type for the inner type
- Throws:
NullPointerException
- If one of the parameter is nullIllegalArgumentException
- If the given symbol is staticIllegalArgumentException
- If the symbol is not a member type of this type (local/anon classes don't work)IllegalArgumentException
- If the type arguments don't match the type parameters of the symbol (seewithTypeArguments(List)
)IllegalArgumentException
- If this type is raw and the inner type is not, or this type is parameterized and the inner type is not
-
selectInner
JClassType selectInner(JClassSymbol symbol, List<? extends JTypeMirror> targs, org.pcollections.PSet<SymbolicValue.SymAnnot> typeAnnotations)
Select an inner type, with new type annotations. This can only be called if the given symbol represents a non-static member type of this type declaration.- Parameters:
symbol
- Symbol for the inner typetargs
- Type arguments of the inner type. If that is an empty list, and the given symbol is generic, then the inner type will be raw, or a generic type declaration, depending on whether this type is erased or not.typeAnnotations
- Type annotations on the inner type- Returns:
- A type for the inner type
- Throws:
NullPointerException
- If one of the parameter is nullIllegalArgumentException
- If the given symbol is staticIllegalArgumentException
- If the symbol is not a member type of this type (local/anon classes don't work)IllegalArgumentException
- If the type arguments don't match the type parameters of the symbol (seewithTypeArguments(List)
)IllegalArgumentException
- If this type is raw and the inner type is not, or this type is parameterized and the inner type is not
-
getSuperClass
@Nullable JClassType getSuperClass()
Returns the generic superclass type. Returns null if this isObject
. ReturnsTypeSystem.OBJECT
if this is an interface type.
-
getAsSuper
default @Nullable JClassType getAsSuper(@NonNull JClassSymbol symbol)
Description copied from interface:JTypeMirror
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.
- Specified by:
getAsSuper
in interfaceJTypeMirror
-
getDeclaredMethod
@Nullable JMethodSig getDeclaredMethod(JExecutableSymbol sym)
Returns the typed signature for the symbol, if it is declared directly in this type, and not a supertype.- Parameters:
sym
- Method or constructor symbol
-
getDeclaredClasses
List<JClassType> getDeclaredClasses()
Return the list of declared nested classes. They are substituted with the actual type arguments of this type, if it is parameterized. They are raw if this type is raw. Does not look into supertypes.
-
getDeclaredFields
List<JVariableSig.FieldSig> getDeclaredFields()
Return the list of declared fields. They are substituted with the actual type arguments of this type, if it is parameterized. Does not look into supertypes.
-
getDeclaredField
@Nullable JVariableSig.FieldSig getDeclaredField(String simpleName)
Return the field with the given name, or null if there is none. Does not look into supertypes.
-
getDeclaredClass
@Nullable JClassType getDeclaredClass(String simpleName)
Return the nested class with the given name, or null if there is none. Does not look into supertypes.
-
getErasure
JClassType getErasure()
Description copied from interface:JTypeMirror
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
)- Specified by:
getErasure
in interfaceJTypeMirror
-
getSuperInterfaces
List<JClassType> getSuperInterfaces()
Return the list of interface types directly implemented by this type.
-
withTypeArguments
JClassType withTypeArguments(List<? extends JTypeMirror> args)
Returns another class type which has the same erasure, but new type arguments. Note that the bounds on the type arguments are not checked.- Parameters:
args
- Type arguments of the returned type. If empty, and this type is generic, returns a raw type.- Throws:
IllegalArgumentException
- If the type argument list doesn't match the type parameters of this type in length. If the symbol is unresolved, any number of type arguments is accepted.IllegalArgumentException
- If any type of the list is null, or a primitive type
-
acceptVisitor
default <T,P> T acceptVisitor(JTypeVisitor<T,P> visitor, P p)
Description copied from interface:JTypeVisitable
Accept a type visitor, dispatching on this object's runtime type to the correct method of the visitor.- Specified by:
acceptVisitor
in interfaceJTypeVisitable
- Type Parameters:
T
- Type of result of the visitorP
- Type of data of the visitor
-
-