fixed bug where ['s were getting appended instead of prepended
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
1 package org.ibex.classgen;
2
3 import java.util.StringTokenizer;
4 import java.util.Hashtable;
5
6 public class Type {
7
8     private static Hashtable instances = new Hashtable();  // this has to appear at the top of the file
9
10     // Public API //////////////////////////////////////////////////////////////////////////////
11
12     public static final Type VOID = new Type("V", "void");
13     public static final Type INT = new Type("I", "int");
14     public static final Type LONG = new Type("J", "long");
15     public static final Type BOOLEAN = new Type("Z", "boolean");
16     public static final Type DOUBLE = new Type("D", "double");
17     public static final Type FLOAT = new Type("F", "float");
18     public static final Type BYTE = new Type("B", "byte");
19     public static final Type CHAR = new Type("C", "char");
20     public static final Type SHORT = new Type("S", "short");
21     
22     public static final Type.Class OBJECT = new Type.Class("java.lang.Object");
23     public static final Type.Class STRING = new Type.Class("java.lang.String");
24     public static final Type.Class STRINGBUFFER = new Type.Class("java.lang.StringBuffer");
25     public static final Type.Class INTEGER_OBJECT = new Type.Class("java.lang.Integer");
26     public static final Type.Class DOUBLE_OBJECT = new Type.Class("java.lang.Double");
27     public static final Type.Class FLOAT_OBJECT = new Type.Class("java.lang.Float");
28     
29     /** A zero element Type[] array (can be passed as the "args" param when a method takes no arguments */
30     public static final Type[] NO_ARGS = new Type[0];
31     
32     /** guarantee: there will only be one instance of Type for a given descriptor ==> equals() and == are interchangeable */
33     public static Type instance(String d) {
34         Type ret = (Type)instances.get(d);
35         if (ret != null) return ret;
36         if (d.startsWith("[")) return new Type.Array(instance(d.substring(1)));
37         return new Type.Class(d);
38     }
39
40     public       String  toString() { return toString; }
41     public final String  getDescriptor() { return descriptor; }
42     public       int     hashCode() { return descriptor.hashCode(); }
43     public       boolean equals(java.lang.Object o) { return this==o; }
44
45     public Type.Array  makeArray() { return (Type.Array)instance("["+descriptor); }
46
47     public Type.Ref    asRef()       { throw new RuntimeException("attempted to use "+this+" as a Type.Ref, which it is not"); }
48     public Type.Class  asClass()     { throw new RuntimeException("attempted to use "+this+" as a Type.Class, which it is not"); }
49     public Type.Array  asArray()     { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
50     public boolean     isPrimitive() { return !isRef(); }
51     public boolean     isRef()       { return false; }
52     public boolean     isClass()     { return false; }
53     public boolean     isArray()     { return false; }
54
55     // Protected/Private //////////////////////////////////////////////////////////////////////////////
56
57     protected final String descriptor;
58     protected final String toString;
59     protected Type(String descriptor) { this(descriptor, descriptor); }
60     protected Type(String descriptor, String humanReadable) {
61         this.toString = humanReadable;
62         instances.put(this.descriptor = descriptor, this);
63     }
64
65     public static class Ref extends Type {
66         protected Ref(String descriptor) { super(descriptor); }
67         protected Ref(String descriptor, String humanReadable) { super(descriptor, humanReadable); }
68         public    Type.Ref asRef() { return this; }
69         public    boolean  isRef() { return true; }
70     }
71
72     public static class Class extends Type.Ref {
73         protected Class(String s) { super(_initHelper(s), _initHelper2(s)); }
74         public Type.Class asClass() { return this; }
75         public boolean isClass() { return true; }
76         public String getShortName() { return toString.substring(toString.lastIndexOf('.')+1); }
77         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
78         private static String _initHelper(String s) {
79             if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
80             return s;
81         }
82         private static String _initHelper2(String s) {
83             if (s.startsWith("L") && s.endsWith(";")) s = s.substring(1, s.length()-1);
84             return s.replace('/', '.');
85         }
86         String[] components() {
87             StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
88             String[] a = new String[st.countTokens()];
89             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
90             return a;
91         }
92     }    
93
94     public static class Array extends Type.Ref {
95         protected Array(Type t) { super("[" + t.getDescriptor(), t.toString() + "[]"); }
96         public Type.Array asArray() { return this; }
97         public boolean isArray() { return true; }
98         public int dimension() { return getDescriptor().lastIndexOf('['); }
99     }
100
101 }