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 a JClassSymbol viewed under a given parameterization. Methods like JTypeMirror.streamMethods(Predicate), return signatures that are already partially substituted. Eg the method get(int) for type List<String> has return type String, not the type var T or the erasure Object.

    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.
    • Method Detail

      • 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 interface JTypeMirror
        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 interface JTypeMirror
        Specified by:
        subst in interface JTypeVisitable
        Parameters:
        fun - Substitution function, eg a Substitution
      • 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 interface JTypeMirror
        See Also:
        isRaw()
      • 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>, and List<String> are generic, but String is not.

        Specified by:
        isGeneric in interface JTypeMirror
      • 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 in getFormalTypeParams(). 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 like withTypeArguments(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 parameter T of interface List<T> to String. 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 type
        targs - 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 null
        IllegalArgumentException - If the given symbol is static
        IllegalArgumentException - 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 (see withTypeArguments(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 type
        targs - 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 null
        IllegalArgumentException - If the given symbol is static
        IllegalArgumentException - 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 (see withTypeArguments(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 is Object. Returns TypeSystem.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. for Enum<E>, Comparable, returns Comparable<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 interface JTypeMirror
      • 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:
        1. The erasure of a parameterized type (§4.5) G<T1,...,Tn> is |G|.
        2. The erasure of a nested type T.C is |T|.C.
        3. The erasure of an array type T[] is |T|[].
        4. The erasure of a type variable (§4.4) is the erasure of its upper bound.
        5. The erasure of an intersection type is the erasure of its leftmost component.
        6. 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 do t.getErasure().getSymbol(). The erasure procedure gets rid of types that have no symbol (except if t is a wildcard type, or the TypeSystem.NULL_TYPE)

        Specified by:
        getErasure in interface JTypeMirror
      • 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.
        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 interface JTypeVisitable
        Type Parameters:
        T - Type of result of the visitor
        P - Type of data of the visitor