tons of stuff
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
1 package org.ibex.classgen;
2
3 import java.util.StringTokenizer;
4
5 public class Type {
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");
12     
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");
19     
20     public static final Type[] NO_ARGS = new Type[0];
21     
22     String descriptor;
23     
24     Type() { }
25     Type(String descriptor) { this.descriptor = descriptor; }
26     
27     public final String getDescriptor() { return descriptor; }
28     public int hashCode() { return descriptor.hashCode(); }
29     public boolean equals(Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
30     
31     public static Type arrayType(Type base) { return arrayType(base,1); }
32     public static Type arrayType(Type base, int dim) {
33         StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
34         for(int i=0;i<dim;i++) sb.append("[");
35         sb.append(base.descriptor);
36         return new Type(sb.toString());
37     }
38     
39     public static class Object extends Type {
40         public Object(String s) {
41             if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
42             if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
43             descriptor = s;
44         }
45         
46         public String[] components() {
47             StringTokenizer st = new StringTokenizer(descriptor.substring(1,descriptor.length()-1),"/");
48             String[] a = new String[st.countTokens()];
49             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
50             return a;
51         }
52         
53         public String internalForm() { return descriptor.substring(1,descriptor.length()-1); }
54         
55         // FEATURE: Do a proper check here (invalid chars, etc)
56         public boolean validDescriptorString(String s) {
57             return s.startsWith("L") && s.endsWith(";");
58         }
59     }    
60 }