sort of working
[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 STRING = new Type.Object("java.lang.String");
9     public static final Type[] NO_ARGS = new Type[0];
10     
11     String descriptor;
12     
13     Type() { }
14     Type(String descriptor) { this.descriptor = descriptor; }
15     
16     public final String getDescriptor() { return descriptor; }
17     public int hashCode() { return descriptor.hashCode(); }
18     public boolean equals(Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
19     
20     public static Type arrayType(Type base) { return arrayType(base,1); }
21     public static Type arrayType(Type base, int dim) {
22         StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
23         for(int i=0;i<dim;i++) sb.append("[");
24         sb.append(base.descriptor);
25         return new Type(sb.toString());
26     }
27     
28     public static class Object extends Type {
29         public Object(String s) {
30             if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
31             if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
32             descriptor = s;
33         }
34         
35         public String[] components() {
36             StringTokenizer st = new StringTokenizer(descriptor.substring(1,descriptor.length()-1),"/");
37             String[] a = new String[st.countTokens()];
38             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
39             return a;
40         }
41         
42         public String internalForm() { return descriptor.substring(1,descriptor.length()-1); }
43         
44         // FEATURE: Do a proper check here (invalid chars, etc)
45         public boolean validDescriptorString(String s) {
46             return s.startsWith("L") && s.endsWith(";");
47         }
48     }    
49 }