Class JPrimitiveType
- java.lang.Object
-
- net.sourceforge.pmd.lang.java.types.JPrimitiveType
-
- All Implemented Interfaces:
JTypeMirror
,JTypeVisitable
public final class JPrimitiveType extends Object implements JTypeMirror
Mirror a primitive types. Even thoughvoid.class.isPrimitive()
returns true, we don't treat it as such and represent it withTypeSystem.NO_TYPE
instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JPrimitiveType.PrimitiveTypeKind
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <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.JClassType
box()
Returns the primitive wrapper type of this type, if this is a primitive type.boolean
equals(Object obj)
Returns true if the object is a type equivalent to this one.JTypeMirror
getErasure()
Returns the erasure of this type.JPrimitiveType.PrimitiveTypeKind
getKind()
@NonNull String
getSimpleName()
Returns the token used to represent the type in source, e.g.Set<JTypeMirror>
getSuperTypeSet()
Returns the set of (nominal) supertypes of this type.@NonNull JClassSymbol
getSymbol()
Returns the type of the primitive class, egInteger.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.int
hashCode()
boolean
isFloatingPoint()
Returns true if this type is a primitive type of a floating point type.boolean
isIntegral()
Returns true if this type is a primitive type of an integral type.boolean
isNumeric()
Returns true if this is a primitive numeric type.boolean
isPrimitive()
Returns true if this type is a primitive type.boolean
isPrimitive(JPrimitiveType.PrimitiveTypeKind kind)
Returns true if this type is the primitive type of the given kind in its type system.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.JPrimitiveType
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 class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sourceforge.pmd.lang.java.types.JTypeMirror
addAnnotation, getAsSuper, getConstructors, isArray, isBottom, isBoxedPrimitive, isClassOrInterface, isConvertibleTo, isGeneric, isGenericTypeDeclaration, isInterface, isParameterizedType, isRaw, isReifiable, isSubtypeOf, isTop, isTypeVariable, isVoid, streamDeclaredMethods, streamMethods
-
-
-
-
Method Detail
-
getTypeAnnotations
public org.pcollections.PSet<SymbolicValue.SymAnnot> getTypeAnnotations()
Description copied from interface:JTypeMirror
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
.- Specified by:
getTypeAnnotations
in interfaceJTypeMirror
-
withAnnotations
public JTypeMirror 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
-
getTypeSystem
public TypeSystem getTypeSystem()
Description copied from interface:JTypeMirror
Returns the type system that built this type.- Specified by:
getTypeSystem
in interfaceJTypeMirror
-
box
public JClassType box()
Description copied from interface:JTypeMirror
Returns the primitive wrapper type of this type, if this is a primitive type. Otherwise returns this type unchanged.- Specified by:
box
in interfaceJTypeMirror
-
unbox
public JPrimitiveType unbox()
Description copied from interface:JTypeMirror
Returns the unboxed version of this type. Returns this type unchanged if this is not a primitive wrapper type.- Specified by:
unbox
in interfaceJTypeMirror
-
getErasure
public JTypeMirror 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
-
getSymbol
public @NonNull JClassSymbol getSymbol()
Returns the type of the primitive class, egInteger.TYPE
. The returned typeClass.isPrimitive()
is true.- Specified by:
getSymbol
in interfaceJTypeMirror
-
isNumeric
public boolean isNumeric()
Description copied from interface:JTypeMirror
Returns true if this is a primitive numeric type. The only non-numeric primitive type isTypeSystem.BOOLEAN
.- Specified by:
isNumeric
in interfaceJTypeMirror
-
isPrimitive
public boolean isPrimitive(JPrimitiveType.PrimitiveTypeKind kind)
Description copied from interface:JTypeMirror
Returns true if this type is the primitive type of the given kind in its type system.- Specified by:
isPrimitive
in interfaceJTypeMirror
-
isFloatingPoint
public boolean isFloatingPoint()
Description copied from interface:JTypeMirror
Returns true if this type is a primitive type of a floating point type.- Specified by:
isFloatingPoint
in interfaceJTypeMirror
-
isIntegral
public boolean isIntegral()
Description copied from interface:JTypeMirror
Returns true if this type is a primitive type of an integral type.- Specified by:
isIntegral
in interfaceJTypeMirror
-
isPrimitive
public boolean isPrimitive()
Description copied from interface:JTypeMirror
Returns true if this type is a primitive type.- Specified by:
isPrimitive
in interfaceJTypeMirror
-
getSuperTypeSet
public Set<JTypeMirror> getSuperTypeSet()
Description copied from interface:JTypeMirror
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
.- Specified by:
getSuperTypeSet
in interfaceJTypeMirror
-
toString
public String toString()
Description copied from interface:JTypeMirror
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.- Specified by:
toString
in interfaceJTypeMirror
- Overrides:
toString
in classObject
-
equals
public boolean equals(Object obj)
Description copied from interface:JTypeMirror
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.
- Specified by:
equals
in interfaceJTypeMirror
- Overrides:
equals
in classObject
- Parameters:
obj
-- Returns:
-
getSimpleName
public @NonNull String getSimpleName()
Returns the token used to represent the type in source, e.g. "int" or "double".
-
getKind
public JPrimitiveType.PrimitiveTypeKind getKind()
-
acceptVisitor
public <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
-
subst
public 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 interfaceJTypeMirror
- Specified by:
subst
in interfaceJTypeVisitable
- Parameters:
subst
- Substitution function, eg aSubstitution
-
-