net.sf.jode.bytecode
Class TypeSignature

java.lang.Object
  extended by net.sf.jode.bytecode.TypeSignature

public class TypeSignature
extends Object

This class contains some static methods to handle type signatures.
A type signature is a compact textual representation of a java types. It is described in the Java Virtual Machine Specification. Primitive types have a one letter type signature. Type signature of classes contains the class name. Type signatures for arrays and methods are recursively build from the type signatures of their elements.
Since java 5 there is a new class of type signatures supporting generics. These can be accessed with the getSignature methods of ClassInfo, MethodInfo and FieldInfo. Here are a few examples:

type signatureJava type
Zboolean
Bbyte
Sshort
Cchar
Iint
Ffloat
Jlong
Ddouble
Ljava/lang/Object; java.lang.Object
[[Iint[][]
(Ljava/lang/Object;I)V method with argument types Object and int and void return type.
()I method without arguments and int return type.
<E:Ljava/lang/Object;>Ljava/lang/Object;Ljava/util/Collection<TE;>;
generic class over <E extends Object> extending Object and implementing Collections<E>
<T:Ljava/lang/Object;>([TT;)[TT;
generic method over <T extends Object> taking an array of T as parameters and returning an array of T.

Author:
Jochen Hoenicke

Constructor Summary
TypeSignature()
           
 
Method Summary
static void checkMethodTypeSig(String typesig)
          Checks whether a given type signature is a valid method type signature.
static void checkTypeSig(String typesig)
          Checks whether a given type signature is a valid (not method) type signature.
static Class getClass(String typeSig)
          Generates a Class object for a type signature.
static ClassInfo getClassInfo(ClassPath classpath, String typeSig)
          Gets the ClassInfo for a class type.
static Object getDefaultValue(String typeSig)
          Gets the default value an object of the given type has.
static String getElementType(String typeSig)
          Gets the element type of an array.
static int getParameterSize(String methodTypeSig)
          Gets the number of words the parameters for the given method type signature takes.
static String[] getParameterTypes(String methodTypeSig)
          Gets the parameter type signatures of the given method signature.
static int getReturnSize(String methodTypeSig)
          Gets the size of the return type of the given method in words.
static String getReturnType(String methodTypeSig)
          Gets the return type for a method signature
static String getSignature(Class clazz)
          Generates the type signature of the given Class.
static String getSignature(Class[] paramT, Class returnT)
          Generates a method signature.
static int getTypeSize(String typeSig)
          Returns the number of words, an object of the given simple type signature takes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeSignature

public TypeSignature()
Method Detail

getSignature

public static String getSignature(Class clazz)
Generates the type signature of the given Class.

Parameters:
clazz - a java.lang.Class, this may also be a primitive or array type.
Returns:
the type signature.

getSignature

public static String getSignature(Class[] paramT,
                                  Class returnT)
Generates a method signature.

Parameters:
paramT - the java.lang.Class of the parameter types of the method.
returnT - the java.lang.Class of the return type of the method.
Returns:
the method type signature

getClass

public static Class getClass(String typeSig)
                      throws ClassNotFoundException
Generates a Class object for a type signature. This is the inverse function of getSignature.

Parameters:
typeSig - a single type signature
Returns:
the Class object representing that type.
Throws:
ClassNotFoundException

getTypeSize

public static int getTypeSize(String typeSig)
Returns the number of words, an object of the given simple type signature takes. For long and double this is two, for all other types it is one.


getElementType

public static String getElementType(String typeSig)
Gets the element type of an array.

Parameters:
typeSig - type signature of the array.
Returns:
type signature for the element type.
Throws:
IllegalArgumentException - if typeSig is not an array type signature.

getClassInfo

public static ClassInfo getClassInfo(ClassPath classpath,
                                     String typeSig)
Gets the ClassInfo for a class type.

Parameters:
classpath - the classpath in which the ClassInfo is searched.
typeSig - type signature of the class.
Returns:
the ClassInfo object for the class.
Throws:
IllegalArgumentException - if typeSig is not an class type signature.

getParameterSize

public static int getParameterSize(String methodTypeSig)
Gets the number of words the parameters for the given method type signature takes. This is the sum of getTypeSize() for each parameter type.

Parameters:
methodTypeSig - the method type signature.
Returns:
the number of words the parameters take.

getReturnSize

public static int getReturnSize(String methodTypeSig)
Gets the size of the return type of the given method in words. This is zero for void return type, two for double or long return type and one otherwise.

Parameters:
methodTypeSig - the method type signature.
Returns:
the size of the return type in words.

getParameterTypes

public static String[] getParameterTypes(String methodTypeSig)
Gets the parameter type signatures of the given method signature.

Parameters:
methodTypeSig - the method type signature.
Returns:
an array containing all parameter types in correct order.

getReturnType

public static String getReturnType(String methodTypeSig)
Gets the return type for a method signature

Parameters:
methodTypeSig - the method signature.
Returns:
the return type for a method signature, `V' for void methods.

getDefaultValue

public static Object getDefaultValue(String typeSig)
Gets the default value an object of the given type has. It is null for objects and arrays, Integer(0) for boolean and short integer types or Long(0L), Double(0.0), Float(0.0F) for long, double and float. This seems strange, but this way the type returned is the same as for FieldInfo.getConstant().

Parameters:
typeSig - the type signature.
Returns:
the default value.
Throws:
IllegalArgumentException - if this is a method type signature.

checkTypeSig

public static void checkTypeSig(String typesig)
                         throws IllegalArgumentException
Checks whether a given type signature is a valid (not method) type signature. Throws an exception otherwise.

Parameters:
typesig - the type signature.
Throws:
NullPointerException - if typeSig is null.
IllegalArgumentException - if typeSig is not a valid type signature or if it's a method type signature.

checkMethodTypeSig

public static void checkMethodTypeSig(String typesig)
                               throws IllegalArgumentException
Checks whether a given type signature is a valid method type signature. Throws an exception otherwise.

Parameters:
typesig - the type signature.
Throws:
NullPointerException - if typeSig is null.
IllegalArgumentException - if typeSig is not a valid method type signature.