public class Type
extends java.lang.Object
This class represents primitive Java types (boolean, byte, char, double, float, int, long, and short) as well as object types (including arrays). It treats null and void (the "return value" of a void method) as null type, which is also considered as primitive.
Manipulation can also be done on those types by means of the given operations.
This class cannot be directed instantiated with "new". Users should
use createType(String)
to create a type object with a name. If the
name refers to a class, it must be a full name (including the package).
Objects of this class can also be associated with AST nodes during AST
analysis. When the analyzer resolves the type of a node representing an
expression or sub-expression in the Java program, it creates a type and
associates it with that node with setType(ASTNode, Type)
. This
information can be extracted with getType(ASTNode)
.
Modifier and Type | Field and Description |
---|---|
static int |
BOOLEAN_NUM
The integer identifier of boolean type.
|
static Type |
BOOLEAN_TYPE
The type object of boolean type.
|
static int |
BYTE_NUM
The integer identifier of byte type.
|
static Type |
BYTE_TYPE
The type object of byte type.
|
static int |
CHAR_NUM
The integer identifier of char type.
|
static Type |
CHAR_TYPE
The type object of char type.
|
static int |
DOUBLE_NUM
The integer identifier of double type.
|
static Type |
DOUBLE_TYPE
The type object of double type.
|
static int |
FLOAT_NUM
The integer identifier of float type.
|
static Type |
FLOAT_TYPE
The type object of float type.
|
static int |
INT_NUM
The integer identifier of int type.
|
static Type |
INT_TYPE
The type object of int type.
|
static int |
LONG_NUM
The integer identifier of long type.
|
static Type |
LONG_TYPE
The type object of long type.
|
static int |
NULL_NUM
The integer identifier of null type.
|
static Type |
NULL_TYPE
The type object of null type.
|
static int |
SHORT_NUM
The integer identifier of short type.
|
static Type |
SHORT_TYPE
The type object of short type.
|
Modifier and Type | Method and Description |
---|---|
Type |
addOneDimension()
Add one dimension to this type object, and return the new type object.
|
int |
compatibility(Type formalType,
java.lang.ClassLoader loader)
Compute the compatibility rating between this type and another formal
type.
|
static Type |
createType(java.lang.String fullName)
Create a type with a full name of a primitive type or a class.
|
int |
dimensions()
Count the number of dimensions of an array type.
|
static int |
dimensions(java.lang.String type)
Count the number of dimensions of an array type.
|
boolean |
equals(java.lang.Object object)
Test if this type is semantically equal to the given type.
|
static java.lang.String |
fromArrayType(java.lang.String type)
Convert the name of the Java run-time representation back to
an array type.
|
static Type |
getCommonType(Type type1,
Type type2)
Get the common type of two types when they appear in an
expression.
|
static java.lang.String |
getElementType(java.lang.String type)
Get the name of the element type of a type that may be array.
|
java.lang.String |
getName()
Get the name of this type.
|
static Type |
getOwner(org.eclipse.jdt.core.dom.ASTNode node)
Get the owner associated with an AST node, if it is resolved
as a field or method.
|
static Type |
getType(org.eclipse.jdt.core.dom.ASTNode node)
Get the type associated with an AST node.
|
int |
hashCode()
Return the hash code for the Type object.
|
boolean |
isArray()
Test if this type is an array type.
|
boolean |
isPrimitive()
Test if this type is primitive (boolean,
byte, char, double, float,
int, long, short, and
null).
|
static boolean |
isPrimitive(java.lang.String typeName)
Test if the type given by the name is primitive.
|
static void |
propagateOwner(org.eclipse.jdt.core.dom.ASTNode nTo,
org.eclipse.jdt.core.dom.ASTNode nFrom)
Copy the owner annotation from a node to another.
|
static void |
propagateType(org.eclipse.jdt.core.dom.ASTNode nTo,
org.eclipse.jdt.core.dom.ASTNode nFrom)
Copy the type annotation from a node to another.
|
static void |
removeAllTypes()
Remove all the types created in the last transformation.
|
Type |
removeOneDimension()
Remove one dimension from this type object.
|
static void |
setOwner(org.eclipse.jdt.core.dom.ASTNode node,
Type owner)
Set the owner associated with an AST node, if it is resolved
as a field or method.
|
static void |
setType(org.eclipse.jdt.core.dom.ASTNode node,
Type type)
Set the type object associated with a node.
|
static java.lang.String |
toArrayType(java.lang.String type)
Convert the name of an array type to the Java run-time
representation.
|
java.lang.Class |
toClass(java.lang.ClassLoader loader)
Convert this type to a
Class object. |
java.lang.String |
toString()
Convert this type to
String . |
public static final int BOOLEAN_NUM
public static final Type BOOLEAN_TYPE
public static final int BYTE_NUM
public static final Type BYTE_TYPE
public static final int CHAR_NUM
public static final Type CHAR_TYPE
public static final int DOUBLE_NUM
public static final Type DOUBLE_TYPE
public static final int FLOAT_NUM
public static final Type FLOAT_TYPE
public static final int INT_NUM
public static final Type INT_TYPE
public static final int LONG_NUM
public static final Type LONG_TYPE
public static final int NULL_NUM
public static final Type NULL_TYPE
public static final int SHORT_NUM
public static final Type SHORT_TYPE
public Type addOneDimension()
dimensions()
,
removeOneDimension()
public int compatibility(Type formalType, java.lang.ClassLoader loader) throws java.lang.ClassNotFoundException
The compatibility rating is an integer number. When it is -1, the two types are not compatible. When it is 0, the two types are the same. When it is a natural number, it denotes how compatible the two types are. The smaller this natural number is, the more compatible this type is with the given formal type.
formalType
- The formal type to be evaluated.loader
- The ClassLoader
object to be used when the
classes of types are resolved. This function uses this loader to
resolve both types if they are classes, and also their superclasses
and the interfaces that they implement, if necessary.java.lang.ClassNotFoundException
- If a class cannot be loaded.public static Type createType(java.lang.String fullName)
They full name of the type given can be any of the primitive types as well as null or void. When it is the name of an array type, both source representation (as "char[]") or run-time representation (as "[C") are accepted.
fullName
- The full name of the type.public int dimensions()
dimensions(String)
public static int dimensions(java.lang.String type)
type
- The array type.dimensions()
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- The type to be tested.public static java.lang.String fromArrayType(java.lang.String type)
This function does nothing if the input type is already Java run-time form.
type
- The type name to be converted.toArrayType(String)
public static Type getCommonType(Type type1, Type type2)
String
, in which case the
result type is String
, or they are the same
type.
String
. The result is also
String
.
type1
- One of the types to be evaluated.type2
- The other of the types to be evaluated.public static java.lang.String getElementType(java.lang.String type)
type
- The name of the type.public java.lang.String getName()
public static Type getOwner(org.eclipse.jdt.core.dom.ASTNode node)
node
- The node with an owner associated with it.setOwner(ASTNode, Type)
public static Type getType(org.eclipse.jdt.core.dom.ASTNode node)
node
- The node with a type associated with it.setType(ASTNode, Type)
public int hashCode()
hashCode
in class java.lang.Object
public boolean isArray()
public boolean isPrimitive()
public static boolean isPrimitive(java.lang.String typeName)
typeName
- The name of the type.public static void propagateOwner(org.eclipse.jdt.core.dom.ASTNode nTo, org.eclipse.jdt.core.dom.ASTNode nFrom)
nTo
- The node whose owner is updated.nFrom
- The node whose owner is fetched.public static void propagateType(org.eclipse.jdt.core.dom.ASTNode nTo, org.eclipse.jdt.core.dom.ASTNode nFrom)
nTo
- The node whose type is updated.nFrom
- The node whose type is fetched.public static void removeAllTypes()
public Type removeOneDimension() throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
- If this type is an
array type, but the type with one dimension less cannot be
resolved as a class.addOneDimension()
,
dimensions()
public static void setOwner(org.eclipse.jdt.core.dom.ASTNode node, Type owner)
node
- The node with an owner associated with it.owner
- The type of the owner.getOwner(ASTNode)
public static void setType(org.eclipse.jdt.core.dom.ASTNode node, Type type)
node
- The node associated with a type.type
- The type associated with the node.getType(ASTNode)
public static java.lang.String toArrayType(java.lang.String type)
This function does nothing if the input type is already Java run-time form.
type
- The type name to be converted.fromArrayType(String)
public java.lang.Class toClass(java.lang.ClassLoader loader) throws java.lang.ClassNotFoundException
Class
object.loader
- The ClassLoader
object to be used to resolve
the type.java.lang.ClassNotFoundException
- If the class with
the same name as this type cannot be loaded with the given
class loader.