Package jminusminus

Class Type

java.lang.Object
jminusminus.Type
Direct Known Subclasses:
ArrayTypeName, TypeName

class Type extends Object
A class for representing j-- types. All types are represented underneath (in the classRep field) by Java objects of type Class. These objects represent types in Java, so this should ease our interfacing with existing Java classes.

Class types (reference types that are represented by the identifiers introduced in class declarations) are represented using TypeName. So for now, every TypeName represents a class. In the future, TypeName could be extended to represent interfaces or enumerations.

IdentifierTypes must be "resolved" at some point, so that all Types having the same name refer to the same Type object. The resolve() method does this.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Type
    The "any" type (denotes wild expressions).
    static final Type
    The boolean type.
    static final Type
    The java.lang.Boolean type.
    static final Type
    The java.lang.Character type.
    static final Type
    The java.lang.Double type.
    static final Type
    The java.lang.Integer type.
    static final Type
    The java.lang.Long type.
    static final Type
    The char type.
    static final Type
    A type marker indicating a constructor (having no return type).
    static final Type
    The double type.
    static final Type
    The int type.
    static final Type
    The long type.
    static final Type
    The null type.
    static final Type
    The java.lang.Object type.
    static final Type
    The java.lang.String type.
    static final Type
    The void type.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    This constructor is to keep the compiler happy.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a list of this class' abstract methods.
    static String
    argTypesAsString(Type[] argTypes)
    Returns a string representation of an array of argument types.
    static boolean
    argTypesMatch(Class<?>[] argTypes1, Class<?>[] argTypes2)
    Returns true if the argument types match, and false otherwise.
    Returns a string representation for a type being appended to a StringBuffer (for the + and += operations over strings).
    static boolean
    checkAccess(int line, Class referencingType, Class type)
    Returns true if the referenced type is accessible from the referencing type, and false otherwise.
    boolean
    checkAccess(int line, Member member)
    Returns true if the member is accessible from this type, and false otherwise.
    boolean
    checkAccess(int line, Type targetType)
    Returns true if the target type is accessible from this type, and false otherwise.
    Returns the class representation for this type.
    Returns an array type's component type.
    constructorFor(Type[] argTypes)
    Finds and returns a constructor in this type having the given argument types, or null.
    boolean
    equals(Type other)
    Returns true if this type has the same descriptor as other, and false otherwise.
    Finds and returns a field in this type having the given name, or null.
    boolean
    Returns true of this type is declared abstract, and false otherwise.
    boolean
    Returns true if this is an array type, and false otherwise.
    boolean
    Returns true of this type is declared final, and false otherwise.
    boolean
    Returns true if this is an interface type, and false otherwise.
    boolean
    Returns true if this is a supertype of other, and false otherwise.
    boolean
    Returns true if this is a primitive type, and false otherwise.
    boolean
    Returns true if this is a reference type, and false otherwise.
    Returns the JVM representation of this type's name.
    boolean
    Returns true if this type matches expected, and false otherwise.
    methodFor(String name, Type[] argTypes)
    Finds and returns a method in this type having the given name and argument types, or null.
    void
    mustMatchExpected(int line, Type expectedType)
    An assertion that this type matches the specified type.
    void
    mustMatchOneOf(int line, Type... expectedTypes)
    An assertion that this type matches one of the specified types.
    Returns this type's package name.
    resolve(Context context)
    Resolves this type in the given context and returns the resolved type.
    void
    setClassRep(Class<?> classRep)
    Sets the class representation of this type to the specified partial class.
    static String
    signatureFor(String name, Type[] argTypes)
    Returns a signature for reporting unfound methods and constructors.
    Returns the simple (unqualified) name of this type.
    Returns this type's super type, or null.
    Returns the JVM descriptor of this type.
    Returns a string representation of this type.
    static Type
    typeFor(Class<?> classRep)
    Constructs and returns a representation for a type from its (Java) class representation, making sure there is a unique representation for each unique type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • INT

      public static final Type INT
      The int type.
    • CHAR

      public static final Type CHAR
      The char type.
    • BOOLEAN

      public static final Type BOOLEAN
      The boolean type.
    • LONG

      public static final Type LONG
      The long type.
    • DOUBLE

      public static final Type DOUBLE
      The double type.
    • BOXED_INT

      public static final Type BOXED_INT
      The java.lang.Integer type.
    • BOXED_CHAR

      public static final Type BOXED_CHAR
      The java.lang.Character type.
    • BOXED_BOOLEAN

      public static final Type BOXED_BOOLEAN
      The java.lang.Boolean type.
    • BOXED_LONG

      public static final Type BOXED_LONG
      The java.lang.Long type.
    • BOXED_DOUBLE

      public static final Type BOXED_DOUBLE
      The java.lang.Double type.
    • STRING

      public static final Type STRING
      The java.lang.String type.
    • OBJECT

      public static final Type OBJECT
      The java.lang.Object type.
    • VOID

      public static final Type VOID
      The void type.
    • NULLTYPE

      public static final Type NULLTYPE
      The null type.
    • ANY

      public static final Type ANY
      The "any" type (denotes wild expressions).
    • CONSTRUCTOR

      public static final Type CONSTRUCTOR
      A type marker indicating a constructor (having no return type).
  • Constructor Details

    • Type

      protected Type()
      This constructor is to keep the compiler happy.
  • Method Details

    • typeFor

      public static Type typeFor(Class<?> classRep)
      Constructs and returns a representation for a type from its (Java) class representation, making sure there is a unique representation for each unique type.
      Parameters:
      classRep - the Java class representation.
      Returns:
      a type representation of classRep.
    • classRep

      public Class<?> classRep()
      Returns the class representation for this type.
      Returns:
      the class representation for this type.
    • setClassRep

      public void setClassRep(Class<?> classRep)
      Sets the class representation of this type to the specified partial class.
      Parameters:
      classRep - the partial class.
    • equals

      public boolean equals(Type other)
      Returns true if this type has the same descriptor as other, and false otherwise.
      Parameters:
      other - the other type.
      Returns:
      true if this type has the same descriptor as other, and false otherwise.
    • isArray

      public boolean isArray()
      Returns true if this is an array type, and false otherwise.
      Returns:
      true if this is an array type, and false otherwise.
    • componentType

      public Type componentType()
      Returns an array type's component type.
      Returns:
      an array type's component type.
    • superClass

      public Type superClass()
      Returns this type's super type, or null.
      Returns:
      this type's super type, or null.
    • isPrimitive

      public boolean isPrimitive()
      Returns true if this is a primitive type, and false otherwise.
      Returns:
      true if this is a primitive type, and false otherwise.
    • isInterface

      public boolean isInterface()
      Returns true if this is an interface type, and false otherwise.
      Returns:
      true if this is an interface type, and false otherwise.
    • isReference

      public boolean isReference()
      Returns true if this is a reference type, and false otherwise.
      Returns:
      true if this is a reference type, and false otherwise.
    • isFinal

      public boolean isFinal()
      Returns true of this type is declared final, and false otherwise.
      Returns:
      true of this type is declared final, and false otherwise.
    • isAbstract

      public boolean isAbstract()
      Returns true of this type is declared abstract, and false otherwise.
      Returns:
      true of this type is declared abstract, and false otherwise.
    • isJavaAssignableFrom

      public boolean isJavaAssignableFrom(Type that)
      Returns true if this is a supertype of other, and false otherwise.
      Parameters:
      that - the candidate subtype.
      Returns:
      true if this is a supertype of other, and false otherwise.
    • abstractMethods

      public ArrayList<Method> abstractMethods()
      Returns a list of this class' abstract methods.

      It has abstract methods if:

      1. Any method declared in the class is abstract or
      2. its superclass has an abstract method which is not overridden here.
      Returns:
      a list of this class' abstract methods.
    • mustMatchOneOf

      public void mustMatchOneOf(int line, Type... expectedTypes)
      An assertion that this type matches one of the specified types. If there is no match, an error is reported.
      Parameters:
      line - the line near which the mismatch occurs.
      expectedTypes - expected types.
    • mustMatchExpected

      public void mustMatchExpected(int line, Type expectedType)
      An assertion that this type matches the specified type. If there is no match, an error is reported.
      Parameters:
      line - the line near which the mismatch occurs.
      expectedType - type with which to match.
    • matchesExpected

      public boolean matchesExpected(Type expected)
      Returns true if this type matches expected, and false otherwise.
      Parameters:
      expected - the type that this might match.
      Returns:
      true if this type matches expected, and false otherwise.
    • argTypesMatch

      public static boolean argTypesMatch(Class<?>[] argTypes1, Class<?>[] argTypes2)
      Returns true if the argument types match, and false otherwise.
      Parameters:
      argTypes1 - arguments (classReps) of one method.
      argTypes2 - arguments (classReps) of another method.
      Returns:
      true if the argument types match, and false otherwise.
    • simpleName

      public String simpleName()
      Returns the simple (unqualified) name of this type.
      Returns:
      the simple (unqualified) name of this type.
    • toString

      public String toString()
      Returns a string representation of this type.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this type.
    • toDescriptor

      public String toDescriptor()
      Returns the JVM descriptor of this type.
      Returns:
      the JVM descriptor of this type.
    • jvmName

      public String jvmName()
      Returns the JVM representation of this type's name.
      Returns:
      the JVM representation of this type's name.
    • packageName

      public String packageName()
      Returns this type's package name.
      Returns:
      this type's package name.
    • argumentTypeForAppend

      public String argumentTypeForAppend()
      Returns a string representation for a type being appended to a StringBuffer (for the + and += operations over strings).
      Returns:
      a string representation for a type being appended to a StringBuffer.
    • methodFor

      public Method methodFor(String name, Type[] argTypes)
      Finds and returns a method in this type having the given name and argument types, or null.
      Parameters:
      name - the method name.
      argTypes - the argument types.
      Returns:
      a method in this type having the given name and argument types, or null.
    • constructorFor

      public Constructor constructorFor(Type[] argTypes)
      Finds and returns a constructor in this type having the given argument types, or null.
      Parameters:
      argTypes - the argument types.
      Returns:
      a constructor in this type having the given argument types, or null.
    • fieldFor

      public Field fieldFor(String name)
      Finds and returns a field in this type having the given name, or null.
      Parameters:
      name - the name of the field we want.
      Returns:
      a field in this type having the given name, or null.
    • argTypesAsString

      public static String argTypesAsString(Type[] argTypes)
      Returns a string representation of an array of argument types.
      Parameters:
      argTypes - the array of argument types.
      Returns:
      a string representation of an array of argument types.
    • checkAccess

      public boolean checkAccess(int line, Member member)
      Returns true if the member is accessible from this type, and false otherwise.
      Parameters:
      line - the line in which the access occurs.
      member - the member being accessed.
      Returns:
      true if the member is accessible from this type, and false otherwise.
    • checkAccess

      public boolean checkAccess(int line, Type targetType)
      Returns true if the target type is accessible from this type, and false otherwise.
      Parameters:
      line - line in which the access occurs.
      targetType - the type being accessed.
      Returns:
      true if the target type is accessible from this type, and false otherwise.
    • checkAccess

      public static boolean checkAccess(int line, Class referencingType, Class type)
      Returns true if the referenced type is accessible from the referencing type, and false otherwise.
      Parameters:
      line - the line in which the access occurs.
      referencingType - the type attempting the access.
      type - the type that we want to access.
      Returns:
      true if the referenced type is accessible from the referencing type, and false otherwise.
    • resolve

      public Type resolve(Context context)
      Resolves this type in the given context and returns the resolved type.
      Parameters:
      context - context in which the names are resolved.
      Returns:
      the resolved type.
    • signatureFor

      public static String signatureFor(String name, Type[] argTypes)
      Returns a signature for reporting unfound methods and constructors.
      Parameters:
      name - the message or type name.
      argTypes - the actual argument types.
      Returns:
      a signature for reporting unfound methods and constructors.