Package gnu.bytecode
Class Type
- java.lang.Object
-
- gnu.bytecode.Type
-
- All Implemented Interfaces:
Type
- Direct Known Subclasses:
InlineCalls.LenientExpectedType
,ObjectType
,OccurrenceType
,PrimType
,XDataType
public abstract class Type extends Object implements Type
An abstract type as used by both gnu.bytecode and gnu.expr.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Type.NeverReturns
A marker class, used forType.neverReturnsType
.
-
Field Summary
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Object
coerceFromObject(Object obj)
Convert an object to a value of this Type.Object
coerceToObject(Object obj)
Given a raw JVM value convert it to an object of this type.abstract int
compare(Type other)
Return a numeric code showing "subtype" relationship: 1: if other is a pure subtype of this; 0: if has the same values; -1: if this is a pure subtype of other; -2: if they have values in common but neither is a subtype of the other; -3: if the types have no values in common.void
emitCoerceFromObject(CodeAttr code)
Compile code to coerce/convert from Object to this type.void
emitCoerceToObject(CodeAttr code)
Compile code to convert a object of this type on the stack to Object.void
emitConvertFromPrimitive(Type stackType, CodeAttr code)
Convert from stackType (usually PrimType) to this type.void
emitIsInstance(CodeAttr code)
String
getGenericSignature()
Type
getImplementationType()
Return Java-level implementation type.String
getMaybeGenericSignature()
String
getName()
Type
getRawType()
Return JVM-level implementation type.Type
getRealType()
If this is a type alias, get the aliased type.Class
getReflectClass()
Get the java.lang.Class object for the representation type.String
getSignature()
int
getSize()
int
getSizeInWords()
static Type
getType(String name)
Find an Type with the given name, or create a new one.int
hashCode()
int
isCompatibleWithValue(Type valueType)
If this is the target type, is a given source type compatible?static int
isCompatibleWithValue(Type targetType, Type valueType)
boolean
isExisting()
boolean
isInstance(Object obj)
boolean
isInterface()
static boolean
isMoreSpecific(Type[] t1, Type[] t2)
Return true iff t1[i].isSubtype(t2[i]) for all i.static boolean
isSame(Type t1, Type t2)
boolean
isSubtype(Type other)
Return true if this is a "subtype" of other.static boolean
isValidJavaTypeName(String name)
boolean
isVoid()
static Type
lookupType(String name)
static Type
lowestCommonSharedType(Type t1, Type t2)
static Type
lowestCommonSuperType(Type t1, Type t2)
Computes the common supertype Interfaces are not taken into account.static Type
make(Class reflectClass)
static Type
make(Class reflectClass, Type type)
Try to map java.lang.reflect.Type to gnu.bytecode.Type.static void
printSignature(String sig, int off, int len, PrintWriter out)
Type
promote()
Type
promoteIfUnsigned()
static void
registerTypeForClass(Class clas, Type type)
Register that the Type for class is type.protected void
setGenericSignature(String sig)
protected void
setName(String name)
void
setReflectClass(Class rclass)
protected void
setSignature(String sig)
static int
signatureLength(String sig)
static int
signatureLength(String sig, int pos)
Return the length of the signature starting at a given string position.static String
signatureToName(String sig)
Returns the Java-level type name from a given signature.static PrimType
signatureToPrimitive(char sig)
Returns the primitive type corresponding to a signature character.static Type
signatureToType(String sig)
Get a Type corresponding to the given signature string.static Type
signatureToType(String sig, int off, int len)
Get a Type corresponding to the given signature string.protected static int
swappedCompareResult(int code)
Change result from compare to compensate for argument swapping.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.reflect.Type
getTypeName
-
-
-
-
Field Detail
-
byteType
public static final PrimType byteType
-
shortType
public static final PrimType shortType
-
intType
public static final PrimType intType
-
longType
public static final PrimType longType
-
floatType
public static final PrimType floatType
-
doubleType
public static final PrimType doubleType
-
booleanType
public static final PrimType booleanType
-
charType
public static final PrimType charType
-
voidType
public static final PrimType voidType
-
byte_type
public static final PrimType byte_type
-
short_type
public static final PrimType short_type
-
int_type
public static final PrimType int_type
-
long_type
public static final PrimType long_type
-
float_type
public static final PrimType float_type
-
double_type
public static final PrimType double_type
-
boolean_type
public static final PrimType boolean_type
-
char_type
public static final PrimType char_type
-
void_type
public static final PrimType void_type
-
neverReturnsType
public static final Type neverReturnsType
The return type of an expression that never returns, such as a throw.
-
javalangObjectType
public static final ClassType javalangObjectType
-
objectType
public static final ClassType objectType
-
javalangBooleanType
public static final ClassType javalangBooleanType
-
javalangCharacterType
public static final ClassType javalangCharacterType
-
javalangThrowableType
public static final ClassType javalangThrowableType
-
javalangannotationAnnotationType
public static final ClassType javalangannotationAnnotationType
-
typeArray0
public static final Type[] typeArray0
-
toString_method
public static final Method toString_method
-
javalangNumberType
public static final ClassType javalangNumberType
-
clone_method
public static final Method clone_method
-
intValue_method
public static final Method intValue_method
-
longValue_method
public static final Method longValue_method
-
floatValue_method
public static final Method floatValue_method
-
doubleValue_method
public static final Method doubleValue_method
-
booleanValue_method
public static final Method booleanValue_method
-
javalangClassType
public static final ClassType javalangClassType
-
javalanginvokeMethodHandleType
public static final ClassType javalanginvokeMethodHandleType
-
nullType
public static final ObjectType nullType
The magic type of null.
-
errorType
public static final ObjectType errorType
-
javalangStringType
public static ClassType javalangStringType
-
toStringType
public static final ObjectType toStringType
-
pointer_type
@Deprecated public static final ClassType pointer_type
Deprecated.
-
string_type
@Deprecated public static final ClassType string_type
Deprecated.
-
tostring_type
@Deprecated public static final ObjectType tostring_type
Deprecated.
-
java_lang_Class_type
@Deprecated public static final ClassType java_lang_Class_type
Deprecated.
-
boolean_ctype
@Deprecated public static final ClassType boolean_ctype
Deprecated.
-
throwable_type
public static final ClassType throwable_type
-
number_type
@Deprecated public static final ClassType number_type
Deprecated.
-
reflectClass
protected Class reflectClass
-
-
Constructor Detail
-
Type
protected Type()
-
Type
public Type(Type type)
-
-
Method Detail
-
getImplementationType
public Type getImplementationType()
Return Java-level implementation type. The type used to implement types not natively understood by the JVM or the Java language. Usually, the identity function. However, a language might handle union types or template types or type expressions calculated at run time. In that case return the type used at the Java level, and known at compile time.
-
getRawType
public Type getRawType()
Return JVM-level implementation type.
-
getRealType
public Type getRealType()
If this is a type alias, get the aliased type. This is semi-deprecated.
-
isInterface
public boolean isInterface()
-
isExisting
public boolean isExisting()
-
getType
public static Type getType(String name)
Find an Type with the given name, or create a new one. Use this for "library classes", where you need the field/method types, but not one where you are about to generate code for.- Parameters:
name
- the name of the class (e..g. "java.lang.String").
-
registerTypeForClass
public static void registerTypeForClass(Class clas, Type type)
Register that the Type for class is type.
-
make
public static Type make(Class reflectClass, Type type)
Try to map java.lang.reflect.Type to gnu.bytecode.Type. If we can't handle that, resolve the Class instead.
-
getSignature
public String getSignature()
-
setSignature
protected void setSignature(String sig)
-
getGenericSignature
public String getGenericSignature()
-
setGenericSignature
protected void setGenericSignature(String sig)
-
getMaybeGenericSignature
public String getMaybeGenericSignature()
-
promote
public Type promote()
-
promoteIfUnsigned
public Type promoteIfUnsigned()
-
getSize
public final int getSize()
-
getSizeInWords
public int getSizeInWords()
-
isVoid
public final boolean isVoid()
-
signatureToPrimitive
public static PrimType signatureToPrimitive(char sig)
Returns the primitive type corresponding to a signature character.- Returns:
- a primitive type, or null if there is no such type.
-
signatureToType
public static Type signatureToType(String sig, int off, int len)
Get a Type corresponding to the given signature string.
-
signatureToType
public static Type signatureToType(String sig)
Get a Type corresponding to the given signature string.
-
printSignature
public static void printSignature(String sig, int off, int len, PrintWriter out)
-
signatureLength
public static int signatureLength(String sig, int pos)
Return the length of the signature starting at a given string position. Returns -1 for an invalid signature.
-
signatureLength
public static int signatureLength(String sig)
-
signatureToName
public static String signatureToName(String sig)
Returns the Java-level type name from a given signature. Returns null for an invalid signature.
-
getName
public String getName()
-
setName
protected void setName(String name)
-
isValidJavaTypeName
public static boolean isValidJavaTypeName(String name)
-
isInstance
public boolean isInstance(Object obj)
-
isSubtype
public final boolean isSubtype(Type other)
Return true if this is a "subtype" of other.
-
isCompatibleWithValue
public int isCompatibleWithValue(Type valueType)
If this is the target type, is a given source type compatible?- Returns:
- -1 if not compatible; 0 if need to check at run-time; 1 if compatible; 2 if compatible and no conversion or cast needed. We also return 0 for some "narrowing" conversions even if we know they will always succeed, so as to make such conversions less preferred when doing method overloading.
-
lowestCommonSuperType
public static Type lowestCommonSuperType(Type t1, Type t2)
Computes the common supertype Interfaces are not taken into account. This would be difficult, since interfaces allow multiple-inheritance. This means that there may exists multiple common supertypes to t1 and t2 that are not comparable.- Returns:
- the lowest type that is both above t1 and t2, or null if t1 and t2 have no common supertype.
-
compare
public abstract int compare(Type other)
Return a numeric code showing "subtype" relationship: 1: if other is a pure subtype of this; 0: if has the same values; -1: if this is a pure subtype of other; -2: if they have values in common but neither is a subtype of the other; -3: if the types have no values in common. "Same values" is rather loose; by "A is a subtype of B" we mean that all instance of A can be "widened" to B. More formally, A.compare(B) returns: 1: all B values can be converted to A without a coercion failure (i.e. a ClassCastException or overflow or major loss of information), but not vice versa. 0: all A values can be converted to B without a coercion failure and vice versa; -1: all A values can be converted to B without a coercion failure but not vice versa; -2: there are (potentially) some A values that can be converted to B, and some B values can be converted to A; -3: there are no A values that can be converted to B, and neither are there any B values that can be converted to A.
-
swappedCompareResult
protected static int swappedCompareResult(int code)
Change result from compare to compensate for argument swapping.
-
isMoreSpecific
public static boolean isMoreSpecific(Type[] t1, Type[] t2)
Return true iff t1[i].isSubtype(t2[i]) for all i.
-
emitIsInstance
public void emitIsInstance(CodeAttr code)
-
coerceFromObject
public abstract Object coerceFromObject(Object obj)
Convert an object to a value of this Type. The result is actually of the implementation type, boxed as appropriate, so it is suitable for standard reflective operations, like the arguments to Field#set or Method#invoke. Throw a ClassCastException when this is not possible.
-
coerceToObject
public Object coerceToObject(Object obj)
Given a raw JVM value convert it to an object of this type. I.e. the argument is an object of the type returned bygetRawType()
, boxed as needed. The result may be a language-specific (boxed) value. Generally a no-op.
-
emitConvertFromPrimitive
public void emitConvertFromPrimitive(Type stackType, CodeAttr code)
Convert from stackType (usually PrimType) to this type. However, we might only convert part-way, to some object type. If converting to this type might fail at run-time, only convert to Object (as by emitCoerceToObject); a caller can usestackType.emitConvertFromObject
to convert the rest, but that might throw an exception. (This is a bit of a kludge.)
-
emitCoerceToObject
public void emitCoerceToObject(CodeAttr code)
Compile code to convert a object of this type on the stack to Object.
-
emitCoerceFromObject
public void emitCoerceFromObject(CodeAttr code)
Compile code to coerce/convert from Object to this type.
-
getReflectClass
public Class getReflectClass()
Get the java.lang.Class object for the representation type.
-
setReflectClass
public void setReflectClass(Class rclass)
-
-