1 package org.ibex.classgen;
3 import java.util.StringTokenizer;
6 public static final Type VOID = new Type("V");
7 public static final Type INT = new Type("I");
8 public static final Type LONG = new Type("J");
9 public static final Type BOOLEAN = new Type("Z");
10 public static final Type DOUBLE = new Type("D");
11 public static final Type FLOAT = new Type("F");
13 public static final Type.Object OBJECT = new Type.Object("java.lang.Object");
14 public static final Type.Object STRING = new Type.Object("java.lang.String");
15 public static final Type.Object STRINGBUFFER = new Type.Object("java.lang.StringBuffer");
16 public static final Type.Object INTEGER_OBJECT = new Type.Object("java.lang.Integer");
17 public static final Type.Object DOUBLE_OBJECT = new Type.Object("java.lang.Double");
18 public static final Type.Object FLOAT_OBJECT = new Type.Object("java.lang.Float");
20 public static final Type[] NO_ARGS = new Type[0];
22 final String descriptor;
24 Type(String descriptor) { this.descriptor = descriptor; }
26 public final String getDescriptor() { return descriptor; }
27 public int hashCode() { return descriptor.hashCode(); }
28 public boolean equals(java.lang.Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
30 public static Type arrayType(Type base) { return arrayType(base,1); }
31 public static Type arrayType(Type base, int dim) {
32 StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
33 for(int i=0;i<dim;i++) sb.append("[");
34 sb.append(base.descriptor);
35 return new Type(sb.toString());
38 public static class Object extends Type {
39 public Object(String s) { super(_initHelper(s)); }
41 private static String _initHelper(String s) {
42 if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
43 if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
47 public String[] components() {
48 StringTokenizer st = new StringTokenizer(descriptor.substring(1,descriptor.length()-1),"/");
49 String[] a = new String[st.countTokens()];
50 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
54 public String internalForm() { return descriptor.substring(1,descriptor.length()-1); }
56 // FEATURE: Do a proper check here (invalid chars, etc)
57 static boolean validDescriptorString(String s) {
58 return s.startsWith("L") && s.endsWith(";");