1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import com.sun.org.apache.bcel.internal.classfile.*; 59 import java.io.*; 60 import java.util.ArrayList ; 61 62 69 public abstract class Type { 70 protected byte type; 71 protected String signature; 73 75 public static final BasicType VOID = new BasicType(Constants.T_VOID); 76 public static final BasicType BOOLEAN = new BasicType(Constants.T_BOOLEAN); 77 public static final BasicType INT = new BasicType(Constants.T_INT); 78 public static final BasicType SHORT = new BasicType(Constants.T_SHORT); 79 public static final BasicType BYTE = new BasicType(Constants.T_BYTE); 80 public static final BasicType LONG = new BasicType(Constants.T_LONG); 81 public static final BasicType DOUBLE = new BasicType(Constants.T_DOUBLE); 82 public static final BasicType FLOAT = new BasicType(Constants.T_FLOAT); 83 public static final BasicType CHAR = new BasicType(Constants.T_CHAR); 84 public static final ObjectType OBJECT = new ObjectType("java.lang.Object"); 85 public static final ObjectType STRING = new ObjectType("java.lang.String"); 86 public static final ObjectType STRINGBUFFER = new ObjectType("java.lang.StringBuffer"); 87 public static final ObjectType THROWABLE = new ObjectType("java.lang.Throwable"); 88 public static final Type[] NO_ARGS = new Type[0]; 89 public static final ReferenceType NULL = new ReferenceType(); 90 public static final Type UNKNOWN = new Type(Constants.T_UNKNOWN, 91 "<unknown object>"){}; 92 93 protected Type(byte t, String s) { 94 type = t; 95 signature = s; 96 } 97 98 101 public String getSignature() { return signature; } 102 103 106 public byte getType() { return type; } 107 108 111 public int getSize() { 112 switch(type) { 113 case Constants.T_DOUBLE: 114 case Constants.T_LONG: return 2; 115 case Constants.T_VOID: return 0; 116 default: return 1; 117 } 118 } 119 120 123 public String toString() { 124 return ((this.equals(Type.NULL) || (type >= Constants.T_UNKNOWN)))? signature : 125 Utility.signatureToString(signature, false); 126 } 127 128 136 public static String getMethodSignature(Type return_type, Type[] arg_types) { 137 StringBuffer buf = new StringBuffer ("("); 138 int length = (arg_types == null)? 0 : arg_types.length; 139 140 for(int i=0; i < length; i++) 141 buf.append(arg_types[i].getSignature()); 142 143 buf.append(')'); 144 buf.append(return_type.getSignature()); 145 146 return buf.toString(); 147 } 148 149 private static int consumed_chars=0; 151 156 public static final Type getType(String signature) 157 throws StringIndexOutOfBoundsException 158 { 159 byte type = Utility.typeOfSignature(signature); 160 161 if(type <= Constants.T_VOID) { 162 consumed_chars = 1; 163 return BasicType.getType(type); 164 } else if(type == Constants.T_ARRAY) { 165 int dim=0; 166 do { dim++; 168 } while(signature.charAt(dim) == '['); 169 170 Type t = getType(signature.substring(dim)); 172 173 consumed_chars += dim; 175 return new ArrayType(t, dim); 176 } else { int index = signature.indexOf(';'); 179 if(index < 0) 180 throw new ClassFormatError ("Invalid signature: " + signature); 181 182 consumed_chars = index + 1; 184 return new ObjectType(signature.substring(1, index).replace('/', '.')); 185 } 186 } 187 188 194 public static Type getReturnType(String signature) { 195 try { 196 int index = signature.lastIndexOf(')') + 1; 198 return getType(signature.substring(index)); 199 } catch(StringIndexOutOfBoundsException e) { throw new ClassFormatError ("Invalid method signature: " + signature); 201 } 202 } 203 204 209 public static Type[] getArgumentTypes(String signature) { 210 ArrayList vec = new ArrayList (); 211 int index; 212 Type[] types; 213 214 try { if(signature.charAt(0) != '(') 216 throw new ClassFormatError ("Invalid method signature: " + signature); 217 218 index = 1; 220 while(signature.charAt(index) != ')') { 221 vec.add(getType(signature.substring(index))); 222 index += consumed_chars; } 224 } catch(StringIndexOutOfBoundsException e) { throw new ClassFormatError ("Invalid method signature: " + signature); 226 } 227 228 types = new Type[vec.size()]; 229 vec.toArray(types); 230 return types; 231 } 232 } 233 | Popular Tags |