Interface Type

  • All Known Subinterfaces:
    Type.ArrayType

    public interface Type
    Interface to access resolved type of an expression or a Type.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  Type.ArrayType
      Type for arrays.
      static class  Type.Primitives
      Primitive java types.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      Type erasure()
      Erasure of this type.
      String fullyQualifiedName()
      Fully qualified name of the type.
      boolean is​(String fullyQualifiedName)
      Check whether a type is the one designed by the fully qualified name.
      boolean isArray()
      Check if this type is an array.
      boolean isClass()
      Check if this type is a class, an enum, an interface or an annotation.
      boolean isNumerical()
      Check if this type is a primitive numerical type.
      boolean isParameterized()
      Check if the current type is a parameterized type or not.
      boolean isPrimitive()
      Check if this type is a primitive.
      boolean isPrimitive​(Type.Primitives primitive)
      Check if this type is the given primitive.
      boolean isSubtypeOf​(String fullyQualifiedName)
      Check whether a type is a subtype of the one designed by the fully qualified name.
      boolean isSubtypeOf​(Type superType)
      Check whether a type is a subtype of another.
      boolean isUnknown()
      Check if this type has been resolved.
      boolean isVoid()
      Check if type is Void type.
      String name()
      simple name of the type.
      Symbol.TypeSymbol symbol()
      Symbol of this type.
      List<Type> typeArguments()
      The arguments of a parameterized type, as a parameterization of a generic type.
    • Method Detail

      • is

        boolean is​(String fullyQualifiedName)
        Check whether a type is the one designed by the fully qualified name.
           Type type;
           type.is("int");
           type.is("int[]");
           type.is("java.lang.String[]");
           type.is("java.lang.Object");
        
        Parameters:
        fullyQualifiedName - fully qualified name to check. Use "[]" for arrays and the simple name for primitive type ("int", "byte"...).
        Returns:
        true if the type is the one looked for. false otherwise.
      • isSubtypeOf

        boolean isSubtypeOf​(String fullyQualifiedName)
        Check whether a type is a subtype of the one designed by the fully qualified name.

        This method will consider implemented interfaces as well as superclasses.

           Type type;
           type.isSubtypeOf("Object[]");
           type.isSubtypeOf("org.mypackage.MyClass");
           type.isSubtypeOf("org.mypackage.MyInterface");
           type.isSubtypeOf("java.lang.Object");
        
        Parameters:
        fullyQualifiedName - fully qualified name to check in the type hierarchy. Use "[]" for arrays.
        Returns:
        true if the type is the one passed in parameter or have this type in its hierarchy. false otherwise.
      • isSubtypeOf

        boolean isSubtypeOf​(Type superType)
        Check whether a type is a subtype of another.

        This method will consider implemented interfaces as well as superclasses.

           Type type, myOtherType;
           type.isSubtypeOf(myOtherType);
        
        Parameters:
        superType - instance of a potential superType.
        Returns:
        true if types are equivalent or if the one passed in parameter is in the hierarchy. false otherwise.
      • isArray

        boolean isArray()
        Check if this type is an array.
        Returns:
        true if this is an array.
      • isClass

        boolean isClass()
        Check if this type is a class, an enum, an interface or an annotation.
        Returns:
        true if this is a class, enum, interface or annotation.
      • isVoid

        boolean isVoid()
        Check if type is Void type. This is used to check type of method invocation expressions.
        Returns:
        true if the type is void.
      • isPrimitive

        boolean isPrimitive()
        Check if this type is a primitive.
        Returns:
        true if this is a primitive type.
      • isPrimitive

        boolean isPrimitive​(Type.Primitives primitive)
        Check if this type is the given primitive.
           Type type;
           type.isPrimitive(Primitives.INT);
        
        Parameters:
        primitive - primitive type to be checked with.
        Returns:
        true if this is the primitive type
      • isUnknown

        boolean isUnknown()
        Check if this type has been resolved. Type can be unknown in incomplete part of Semantic Analysis or when bytecode for a type is not provided and a method cannot be resolved.
        Returns:
        true if type has not been resolved by semantic analysis.
      • isNumerical

        boolean isNumerical()
        Check if this type is a primitive numerical type.
        Returns:
        true if type is byte, char, short, int, long, float or double.
      • fullyQualifiedName

        String fullyQualifiedName()
        Fully qualified name of the type.
        Returns:
        complete name of type.
      • name

        String name()
        simple name of the type.
        Returns:
        simple name of type.
      • symbol

        Symbol.TypeSymbol symbol()
        Symbol of this type.
        Returns:
        the symbol declaring this type.
      • erasure

        Type erasure()
        Erasure of this type.
        Returns:
        erased type.
      • isParameterized

        boolean isParameterized()
        Check if the current type is a parameterized type or not.
        Returns:
        true in case of Generic and Parameterized types
        Since:
        SonarJava 6.3
      • typeArguments

        List<Type> typeArguments()
        The arguments of a parameterized type, as a parameterization of a generic type.
        Returns:
        the ordered list of type arguments. Returns an empty lists for non-parameterized types.
        Since:
        SonarJava 6.3