FileDocCategorySizeDatePackage
Type.javaAPI DocJava SE 5 API8745Fri Aug 26 14:55:24 BST 2005com.sun.org.apache.bcel.internal.generic

Type

public abstract class Type extends Object
Abstract super class for all possible java types, namely basic types such as int, object types like String and array types, e.g. int[]
version
$Id: Type.java,v 1.1.1.1 2001/10/29 20:00:28 jvanzyl Exp $
author
M. Dahm

Fields Summary
protected byte
type
protected String
signature
public static final BasicType
VOID
Predefined constants
public static final BasicType
BOOLEAN
public static final BasicType
INT
public static final BasicType
SHORT
public static final BasicType
BYTE
public static final BasicType
LONG
public static final BasicType
DOUBLE
public static final BasicType
FLOAT
public static final BasicType
CHAR
public static final ObjectType
OBJECT
public static final ObjectType
STRING
public static final ObjectType
STRINGBUFFER
public static final ObjectType
THROWABLE
public static final Type[]
NO_ARGS
public static final ReferenceType
NULL
public static final Type
UNKNOWN
private static int
consumed_chars
Constructors Summary
protected Type(byte t, String s)


       
    type      = t;
    signature = s;
  
Methods Summary
public static com.sun.org.apache.bcel.internal.generic.Type[]getArgumentTypes(java.lang.String signature)
Convert arguments of a method (signature) to an array of Type objects.

param
signature signature string such as (Ljava/lang/String;)V
return
array of argument types

    ArrayList vec = new ArrayList();
    int       index;
    Type[]     types;

    try { // Read all declarations between for `(' and `)'
      if(signature.charAt(0) != '(")
	throw new ClassFormatError("Invalid method signature: " + signature);

      index = 1; // current string position

      while(signature.charAt(index) != ')") {
	vec.add(getType(signature.substring(index)));
	index += consumed_chars; // update position
      }
    } catch(StringIndexOutOfBoundsException e) { // Should never occur
      throw new ClassFormatError("Invalid method signature: " + signature);
    }
	
    types = new Type[vec.size()];
    vec.toArray(types);
    return types;
  
public static java.lang.StringgetMethodSignature(com.sun.org.apache.bcel.internal.generic.Type return_type, com.sun.org.apache.bcel.internal.generic.Type[] arg_types)
Convert type to Java method signature, e.g. int[] f(java.lang.String x) becomes (Ljava/lang/String;)[I

param
return_type what the method returns
param
arg_types what are the argument types
return
method signature for given type(s).

 
    StringBuffer buf = new StringBuffer("(");
    int length = (arg_types == null)? 0 : arg_types.length;

    for(int i=0; i < length; i++)
      buf.append(arg_types[i].getSignature());

    buf.append(')");
    buf.append(return_type.getSignature());

    return buf.toString();
  
public static com.sun.org.apache.bcel.internal.generic.TypegetReturnType(java.lang.String signature)
Convert return value of a method (signature) to a Type object.

param
signature signature string such as (Ljava/lang/String;)V
return
return type

    try {
      // Read return type after `)'
      int index = signature.lastIndexOf(')") + 1;
      return getType(signature.substring(index));
    } catch(StringIndexOutOfBoundsException e) { // Should never occur
      throw new ClassFormatError("Invalid method signature: " + signature);
    }
  
public java.lang.StringgetSignature()

return
signature for given type.

 return signature; 
public intgetSize()

return
stack size of this type (2 for long and double, 0 for void, 1 otherwise)

    switch(type) {
    case Constants.T_DOUBLE:
    case Constants.T_LONG: return 2;
    case Constants.T_VOID: return 0;
    default:     return 1;
    }
  
public bytegetType()

return
type as defined in Constants

 return type; 
public static final com.sun.org.apache.bcel.internal.generic.TypegetType(java.lang.String signature)
Convert signature to a Type object.

param
signature signature string such as Ljava/lang/String;
return
type object

 // Remember position in string, see getArgumentTypes

                     
       
     
  
    byte type = Utility.typeOfSignature(signature);

    if(type <= Constants.T_VOID) {
      consumed_chars = 1;
      return BasicType.getType(type);
    } else if(type == Constants.T_ARRAY) {
      int dim=0;
      do { // Count dimensions
	dim++;
      } while(signature.charAt(dim) == '[");

      // Recurse, but just once, if the signature is ok
      Type t = getType(signature.substring(dim));

      consumed_chars += dim; // update counter

      return new ArrayType(t, dim);
    } else { // type == T_REFERENCE
      int index = signature.indexOf(';"); // Look for closing `;'

      if(index < 0)
	throw new ClassFormatError("Invalid signature: " + signature);
	
      consumed_chars = index + 1; // "Lblabla;" `L' and `;' are removed

      return new ObjectType(signature.substring(1, index).replace('/", '."));
    }
  
public java.lang.StringtoString()

return
Type string, e.g. `int[]'

    return ((this.equals(Type.NULL) || (type >= Constants.T_UNKNOWN)))? signature :
      Utility.signatureToString(signature, false);